all: prefer https: URLs
[gnulib.git] / lib / fchownat.c
blob9e08eb77d84f74e251a72f2a2ad639ce66a8dd86
1 /* This function serves as replacement for a missing fchownat function,
2 as well as a work around for the fchownat bug in glibc-2.4:
3 <http://lists.ubuntu.com/archives/ubuntu-users/2006-September/093218.html>
4 when the buggy fchownat-with-AT_SYMLINK_NOFOLLOW operates on a symlink, it
5 mistakenly affects the symlink referent, rather than the symlink itself.
7 Copyright (C) 2006-2007, 2009-2017 Free Software Foundation, Inc.
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <https://www.gnu.org/licenses/>. */
22 /* written by Jim Meyering */
24 #include <config.h>
26 #include <unistd.h>
28 #include <errno.h>
29 #include <string.h>
31 #include "openat.h"
33 #if !HAVE_FCHOWNAT
35 /* Replacement for Solaris' function by the same name.
36 Invoke chown or lchown on file, FILE, using OWNER and GROUP, in the
37 directory open on descriptor FD. If FLAG is AT_SYMLINK_NOFOLLOW, then
38 use lchown, otherwise, use chown. If possible, do it without changing
39 the working directory. Otherwise, resort to using save_cwd/fchdir,
40 then (chown|lchown)/restore_cwd. If either the save_cwd or the
41 restore_cwd fails, then give a diagnostic and exit nonzero. */
43 # define AT_FUNC_NAME fchownat
44 # define AT_FUNC_F1 lchown
45 # define AT_FUNC_F2 chown
46 # define AT_FUNC_USE_F1_COND AT_SYMLINK_NOFOLLOW
47 # define AT_FUNC_POST_FILE_PARAM_DECLS , uid_t owner, gid_t group, int flag
48 # define AT_FUNC_POST_FILE_ARGS , owner, group
49 # include "at-func.c"
50 # undef AT_FUNC_NAME
51 # undef AT_FUNC_F1
52 # undef AT_FUNC_F2
53 # undef AT_FUNC_USE_F1_COND
54 # undef AT_FUNC_POST_FILE_PARAM_DECLS
55 # undef AT_FUNC_POST_FILE_ARGS
57 #else /* HAVE_FCHOWNAT */
59 # undef fchownat
61 # if FCHOWNAT_NOFOLLOW_BUG
63 /* Failure to handle AT_SYMLINK_NOFOLLOW requires the /proc/self/fd or
64 fchdir workaround to call lchown for lchownat, but there is no need
65 to penalize chownat. */
66 static int
67 local_lchownat (int fd, char const *file, uid_t owner, gid_t group);
69 # define AT_FUNC_NAME local_lchownat
70 # define AT_FUNC_F1 lchown
71 # define AT_FUNC_POST_FILE_PARAM_DECLS , uid_t owner, gid_t group
72 # define AT_FUNC_POST_FILE_ARGS , owner, group
73 # include "at-func.c"
74 # undef AT_FUNC_NAME
75 # undef AT_FUNC_F1
76 # undef AT_FUNC_POST_FILE_PARAM_DECLS
77 # undef AT_FUNC_POST_FILE_ARGS
79 # endif
81 /* Work around bugs with trailing slash, using the same workarounds as
82 chown and lchown. */
84 int
85 rpl_fchownat (int fd, char const *file, uid_t owner, gid_t group, int flag)
87 # if FCHOWNAT_NOFOLLOW_BUG
88 if (flag == AT_SYMLINK_NOFOLLOW)
89 return local_lchownat (fd, file, owner, group);
90 # endif
91 # if FCHOWNAT_EMPTY_FILENAME_BUG
92 if (file[0] == '\0')
94 errno = ENOENT;
95 return -1;
97 # endif
98 # if CHOWN_TRAILING_SLASH_BUG
100 size_t len = strlen (file);
101 struct stat st;
102 if (len && file[len - 1] == '/')
104 if (statat (fd, file, &st))
105 return -1;
106 if (flag == AT_SYMLINK_NOFOLLOW)
107 return fchownat (fd, file, owner, group, 0);
110 # endif
111 return fchownat (fd, file, owner, group, flag);
114 #endif /* HAVE_FCHOWNAT */