summaryrefslogtreecommitdiff
path: root/lib/openat.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2006-01-11 13:32:03 +0000
committerJim Meyering <jim@meyering.net>2006-01-11 13:32:03 +0000
commit9cbd6c03665e0d531215e957710becb71e5e6a7b (patch)
treee2d43217d9cbd94f076da98a048a095adac2d32f /lib/openat.c
parentc6749127cae6a7debf135dacfff51a9a09e56d74 (diff)
downloadcoreutils-9cbd6c03665e0d531215e957710becb71e5e6a7b.tar.xz
(fchownat): New function.
Diffstat (limited to 'lib/openat.c')
-rw-r--r--lib/openat.c64
1 files changed, 59 insertions, 5 deletions
diff --git a/lib/openat.c b/lib/openat.c
index 69d4c23f9..273c5b6ec 100644
--- a/lib/openat.c
+++ b/lib/openat.c
@@ -1,5 +1,5 @@
/* provide a replacement openat function
- Copyright (C) 2004, 2005 Free Software Foundation, Inc.
+ Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -23,16 +23,16 @@
#include "openat.h"
+#include <stdarg.h>
+#include <stddef.h>
+#include <errno.h>
+
#include "dirname.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */
#include "fcntl--.h"
#include "openat-priv.h"
#include "save-cwd.h"
#include "unistd--.h"
-#include <stdarg.h>
-#include <stddef.h>
-#include <errno.h>
-
/* Replacement for Solaris' openat function.
<http://www.google.com/search?q=openat+site:docs.sun.com>
Simulate it by doing save_cwd/fchdir/open/restore_cwd.
@@ -286,3 +286,57 @@ unlinkat (int fd, char const *file, int flag)
errno = saved_errno;
return err;
}
+
+/* Replacement for Solaris' function by the same name.
+ Invoke chown or lchown on file, FILE, using OWNER and GROUP, in the
+ directory open on descriptor FD. If FLAG is AT_SYMLINK_NOFOLLOW, then
+ use lchown, otherwise, use chown. If possible, do it without changing
+ the working directory. Otherwise, resort to using save_cwd/fchdir,
+ then mkdir/restore_cwd. If either the save_cwd or the restore_cwd
+ fails, then give a diagnostic and exit nonzero. */
+int
+fchownat (int fd, char const *file, uid_t owner, gid_t group, int flag)
+{
+ struct saved_cwd saved_cwd;
+ int saved_errno;
+ int err;
+
+ if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file))
+ return (flag == AT_SYMLINK_NOFOLLOW
+ ? lchown (file, owner, group)
+ : chown (file, owner, group));
+
+ {
+ char *proc_file;
+ BUILD_PROC_NAME (proc_file, fd, file);
+ err = (flag == AT_SYMLINK_NOFOLLOW
+ ? lchown (proc_file, owner, group)
+ : chown (proc_file, owner, group));
+ /* If the syscall succeeds, or if it fails with an unexpected
+ errno value, then return right away. Otherwise, fall through
+ and resort to using save_cwd/restore_cwd. */
+ if (0 <= err || ! EXPECTED_ERRNO (errno))
+ return err;
+ }
+
+ if (save_cwd (&saved_cwd) != 0)
+ openat_save_fail (errno);
+
+ err = fchdir (fd);
+ saved_errno = errno;
+
+ if (! err)
+ {
+ err = (flag == AT_SYMLINK_NOFOLLOW
+ ? lchown (file, owner, group)
+ : chown (file, owner, group));
+ saved_errno = errno;
+
+ if (restore_cwd (&saved_cwd) != 0)
+ openat_restore_fail (errno);
+ }
+
+ free_cwd (&saved_cwd);
+ errno = saved_errno;
+ return err;
+}