annotate lib/fchownat.c @ 17298:d536543d59a6

statat: new module, split out from fstatat GNU Emacs needs the POSIX-specified fstatat, but not the gnulib-specified statat and lstat. Split the latter two into a new module 'statat'. * lib/openat.h: Depend on GNULIB_STATAT, not GNULIB_FSTATAT. * lib/openat.h, lib/statat.c (STATAT_INLINE): Rename from FSTATAT_INLINE. All uses changed. * modules/fstatat (Files): Remove lib/statat.c. (gl_MODULE_INDICATOR([fstatat])): Remove. (lib_SOURCES): Remove. (Maintainer): Add self. * modules/statat, modules/statat-tests, tests/test-statat.c: New files. * tests/test-fstatat.c (BASE): Don't define if already defined. (do_stat, do_lstat) [!TEST_STATAT]: Test fstatat instead.
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 23 Jan 2013 18:20:09 -0800
parents e542fd46ad6f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7749
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
1 /* This function serves as replacement for a missing fchownat function,
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
2 as well as a work around for the fchownat bug in glibc-2.4:
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
3 <http://lists.ubuntu.com/archives/ubuntu-users/2006-September/093218.html>
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
4 when the buggy fchownat-with-AT_SYMLINK_NOFOLLOW operates on a symlink, it
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
5 mistakenly affects the symlink referent, rather than the symlink itself.
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
6
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 17137
diff changeset
7 Copyright (C) 2006-2007, 2009-2013 Free Software Foundation, Inc.
7749
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
8
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8982
diff changeset
9 This program is free software: you can redistribute it and/or modify
7749
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
10 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8982
diff changeset
11 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8982
diff changeset
12 (at your option) any later version.
7749
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
13
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
14 This program is distributed in the hope that it will be useful,
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
17 GNU General Public License for more details.
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
18
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
19 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8982
diff changeset
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
7749
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
21
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
22 /* written by Jim Meyering */
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
23
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
24 #include <config.h>
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
25
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
26 #include <unistd.h>
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
27
12291
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
28 #include <errno.h>
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
29 #include <string.h>
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
30
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
31 #include "openat.h"
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
32
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
33 #if !HAVE_FCHOWNAT
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
34
7749
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
35 /* Replacement for Solaris' function by the same name.
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
36 Invoke chown or lchown on file, FILE, using OWNER and GROUP, in the
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
37 directory open on descriptor FD. If FLAG is AT_SYMLINK_NOFOLLOW, then
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
38 use lchown, otherwise, use chown. If possible, do it without changing
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
39 the working directory. Otherwise, resort to using save_cwd/fchdir,
11968
98e4db1661cc doc: fix comments in recent patches
Eric Blake <ebb9@byu.net>
parents: 11943
diff changeset
40 then (chown|lchown)/restore_cwd. If either the save_cwd or the
98e4db1661cc doc: fix comments in recent patches
Eric Blake <ebb9@byu.net>
parents: 11943
diff changeset
41 restore_cwd fails, then give a diagnostic and exit nonzero. */
7749
eda4a9c5e8ee Work around an fchownat bug in glibc-2.4:
Jim Meyering <jim@meyering.net>
parents:
diff changeset
42
12291
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
43 # define AT_FUNC_NAME fchownat
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
44 # define AT_FUNC_F1 lchown
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
45 # define AT_FUNC_F2 chown
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
46 # define AT_FUNC_USE_F1_COND AT_SYMLINK_NOFOLLOW
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
47 # define AT_FUNC_POST_FILE_PARAM_DECLS , uid_t owner, gid_t group, int flag
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
48 # define AT_FUNC_POST_FILE_ARGS , owner, group
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
49 # include "at-func.c"
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
50 # undef AT_FUNC_NAME
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
51 # undef AT_FUNC_F1
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
52 # undef AT_FUNC_F2
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
53 # undef AT_FUNC_USE_F1_COND
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
54 # undef AT_FUNC_POST_FILE_PARAM_DECLS
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
55 # undef AT_FUNC_POST_FILE_ARGS
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
56
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
57 #else /* HAVE_FCHOWNAT */
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
58
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
59 # undef fchownat
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
60
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
61 # if FCHOWNAT_NOFOLLOW_BUG
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
62
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
63 /* Failure to handle AT_SYMLINK_NOFOLLOW requires the /proc/self/fd or
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
64 fchdir workaround to call lchown for lchownat, but there is no need
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
65 to penalize chownat. */
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
66 static int
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
67 local_lchownat (int fd, char const *file, uid_t owner, gid_t group);
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
68
14379
2330aac2ae54 maint: adjust cpp indentation to reflect nesting depth
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
69 # define AT_FUNC_NAME local_lchownat
2330aac2ae54 maint: adjust cpp indentation to reflect nesting depth
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
70 # define AT_FUNC_F1 lchown
2330aac2ae54 maint: adjust cpp indentation to reflect nesting depth
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
71 # define AT_FUNC_POST_FILE_PARAM_DECLS , uid_t owner, gid_t group
2330aac2ae54 maint: adjust cpp indentation to reflect nesting depth
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
72 # define AT_FUNC_POST_FILE_ARGS , owner, group
2330aac2ae54 maint: adjust cpp indentation to reflect nesting depth
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
73 # include "at-func.c"
2330aac2ae54 maint: adjust cpp indentation to reflect nesting depth
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
74 # undef AT_FUNC_NAME
2330aac2ae54 maint: adjust cpp indentation to reflect nesting depth
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
75 # undef AT_FUNC_F1
2330aac2ae54 maint: adjust cpp indentation to reflect nesting depth
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
76 # undef AT_FUNC_POST_FILE_PARAM_DECLS
2330aac2ae54 maint: adjust cpp indentation to reflect nesting depth
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
77 # undef AT_FUNC_POST_FILE_ARGS
12291
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
78
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
79 # endif
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
80
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
81 /* Work around bugs with trailing slash, using the same workarounds as
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
82 chown and lchown. */
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
83
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
84 int
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
85 rpl_fchownat (int fd, char const *file, uid_t owner, gid_t group, int flag)
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
86 {
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
87 # if FCHOWNAT_NOFOLLOW_BUG
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
88 if (flag == AT_SYMLINK_NOFOLLOW)
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
89 return local_lchownat (fd, file, owner, group);
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
90 # endif
13863
4cf55d581e23 openat: Work around glibc bug with fchownat() and empty file names.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
91 # if FCHOWNAT_EMPTY_FILENAME_BUG
4cf55d581e23 openat: Work around glibc bug with fchownat() and empty file names.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
92 if (file[0] == '\0')
4cf55d581e23 openat: Work around glibc bug with fchownat() and empty file names.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
93 {
4cf55d581e23 openat: Work around glibc bug with fchownat() and empty file names.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
94 errno = ENOENT;
4cf55d581e23 openat: Work around glibc bug with fchownat() and empty file names.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
95 return -1;
4cf55d581e23 openat: Work around glibc bug with fchownat() and empty file names.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
96 }
4cf55d581e23 openat: Work around glibc bug with fchownat() and empty file names.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
97 # endif
12291
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
98 # if CHOWN_TRAILING_SLASH_BUG
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
99 {
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
100 size_t len = strlen (file);
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
101 struct stat st;
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
102 if (len && file[len - 1] == '/')
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
103 {
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
104 if (statat (fd, file, &st))
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
105 return -1;
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
106 if (flag == AT_SYMLINK_NOFOLLOW)
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
107 return fchownat (fd, file, owner, group, 0);
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
108 }
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
109 }
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
110 # endif
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
111 return fchownat (fd, file, owner, group, flag);
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
112 }
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
113
3c66159f6ce1 openat: detect Solaris fchownat bug
Eric Blake <ebb9@byu.net>
parents: 12028
diff changeset
114 #endif /* HAVE_FCHOWNAT */