]> git.cworth.org Git - tar/blob - gnu/chown.c
upstream: Fix extraction of device nodes.
[tar] / gnu / chown.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* provide consistent interface to chown for systems that don't interpret
4    an ID of -1 as meaning `don't change the corresponding ID'.
5
6    Copyright (C) 1997, 2004-2007, 2009-2010 Free Software Foundation, Inc.
7
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 /* written by Jim Meyering */
22
23 #include <config.h>
24
25 /* Specification.  */
26 #include <unistd.h>
27
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdbool.h>
31 #include <string.h>
32 #include <sys/stat.h>
33
34 #if !HAVE_CHOWN
35
36 /* Simple stub that always fails with ENOSYS, for mingw.  */
37 int
38 chown (const char *file _GL_UNUSED, uid_t uid _GL_UNUSED,
39        gid_t gid _GL_UNUSED)
40 {
41   errno = ENOSYS;
42   return -1;
43 }
44
45 #else /* HAVE_CHOWN */
46
47 /* Below we refer to the system's chown().  */
48 # undef chown
49
50 /* The results of open() in this file are not used with fchdir,
51    therefore save some unnecessary work in fchdir.c.  */
52 # undef open
53 # undef close
54
55 /* Provide a more-closely POSIX-conforming version of chown on
56    systems with one or both of the following problems:
57    - chown doesn't treat an ID of -1 as meaning
58    `don't change the corresponding ID'.
59    - chown doesn't dereference symlinks.  */
60
61 int
62 rpl_chown (const char *file, uid_t uid, gid_t gid)
63 {
64   struct stat st;
65   bool stat_valid = false;
66   int result;
67
68 # if CHOWN_CHANGE_TIME_BUG
69   if (gid != (gid_t) -1 || uid != (uid_t) -1)
70     {
71       if (stat (file, &st))
72         return -1;
73       stat_valid = true;
74     }
75 # endif
76
77 # if CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE
78   if (gid == (gid_t) -1 || uid == (uid_t) -1)
79     {
80       /* Stat file to get id(s) that should remain unchanged.  */
81       if (!stat_valid && stat (file, &st))
82         return -1;
83       if (gid == (gid_t) -1)
84         gid = st.st_gid;
85       if (uid == (uid_t) -1)
86         uid = st.st_uid;
87     }
88 # endif
89
90 # if CHOWN_MODIFIES_SYMLINK
91   {
92     /* Handle the case in which the system-supplied chown function
93        does *not* follow symlinks.  Instead, it changes permissions
94        on the symlink itself.  To work around that, we open the
95        file (but this can fail due to lack of read or write permission) and
96        use fchown on the resulting descriptor.  */
97     int open_flags = O_NONBLOCK | O_NOCTTY;
98     int fd = open (file, O_RDONLY | open_flags);
99     if (0 <= fd
100         || (errno == EACCES
101             && 0 <= (fd = open (file, O_WRONLY | open_flags))))
102       {
103         int saved_errno;
104         bool fchown_socket_failure;
105
106         result = fchown (fd, uid, gid);
107         saved_errno = errno;
108
109         /* POSIX says fchown can fail with errno == EINVAL on sockets
110            and pipes, so fall back on chown in that case.  */
111         fchown_socket_failure =
112           (result != 0 && saved_errno == EINVAL
113            && fstat (fd, &st) == 0
114            && (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode)));
115
116         close (fd);
117
118         if (! fchown_socket_failure)
119           {
120             errno = saved_errno;
121             return result;
122           }
123       }
124     else if (errno != EACCES)
125       return -1;
126   }
127 # endif
128
129 # if CHOWN_TRAILING_SLASH_BUG
130   if (!stat_valid)
131     {
132       size_t len = strlen (file);
133       if (len && file[len - 1] == '/' && stat (file, &st))
134         return -1;
135     }
136 # endif
137
138   result = chown (file, uid, gid);
139
140 # if CHOWN_CHANGE_TIME_BUG
141   if (result == 0 && stat_valid
142       && (uid == st.st_uid || uid == (uid_t) -1)
143       && (gid == st.st_gid || gid == (gid_t) -1))
144     {
145       /* No change in ownership, but at least one argument was not -1,
146          so we are required to update ctime.  Since chown succeeded,
147          we assume that chmod will do likewise.  Fortunately, on all
148          known systems where a 'no-op' chown skips the ctime update, a
149          'no-op' chmod still does the trick.  */
150       result = chmod (file, st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO
151                                           | S_ISUID | S_ISGID | S_ISVTX));
152     }
153 # endif
154
155   return result;
156 }
157
158 #endif /* HAVE_CHOWN */