]> git.cworth.org Git - tar/blob - lib/chown.c
Imported Upstream version 1.20
[tar] / lib / chown.c
1 /* provide consistent interface to chown for systems that don't interpret
2    an ID of -1 as meaning `don't change the corresponding ID'.
3
4    Copyright (C) 1997, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* written by Jim Meyering */
20
21 #include <config.h>
22
23 /* Specification.  */
24 #include <unistd.h>
25
26 #include <stdbool.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <errno.h>
31
32 /* Below we refer to the system's chown().  */
33 #undef chown
34
35 /* The results of open() in this file are not used with fchdir,
36    therefore save some unnecessary work in fchdir.c.  */
37 #undef open
38 #undef close
39
40 /* Provide a more-closely POSIX-conforming version of chown on
41    systems with one or both of the following problems:
42    - chown doesn't treat an ID of -1 as meaning
43    `don't change the corresponding ID'.
44    - chown doesn't dereference symlinks.  */
45
46 int
47 rpl_chown (const char *file, uid_t uid, gid_t gid)
48 {
49 #if CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE
50   if (gid == (gid_t) -1 || uid == (uid_t) -1)
51     {
52       struct stat file_stats;
53
54       /* Stat file to get id(s) that should remain unchanged.  */
55       if (stat (file, &file_stats))
56         return -1;
57
58       if (gid == (gid_t) -1)
59         gid = file_stats.st_gid;
60
61       if (uid == (uid_t) -1)
62         uid = file_stats.st_uid;
63     }
64 #endif
65
66 #if CHOWN_MODIFIES_SYMLINK
67   {
68     /* Handle the case in which the system-supplied chown function
69        does *not* follow symlinks.  Instead, it changes permissions
70        on the symlink itself.  To work around that, we open the
71        file (but this can fail due to lack of read or write permission) and
72        use fchown on the resulting descriptor.  */
73     int open_flags = O_NONBLOCK | O_NOCTTY;
74     int fd = open (file, O_RDONLY | open_flags);
75     if (0 <= fd
76         || (errno == EACCES
77             && 0 <= (fd = open (file, O_WRONLY | open_flags))))
78       {
79         int result = fchown (fd, uid, gid);
80         int saved_errno = errno;
81
82         /* POSIX says fchown can fail with errno == EINVAL on sockets,
83            so fall back on chown in that case.  */
84         struct stat sb;
85         bool fchown_socket_failure =
86           (result != 0 && saved_errno == EINVAL
87            && fstat (fd, &sb) == 0 && S_ISFIFO (sb.st_mode));
88
89         close (fd);
90
91         if (! fchown_socket_failure)
92           {
93             errno = saved_errno;
94             return result;
95           }
96       }
97     else if (errno != EACCES)
98       return -1;
99   }
100 #endif
101
102   return chown (file, uid, gid);
103 }