This source file includes following definitions.
- chown
- rpl_chown
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include <config.h>
22
23
24 #include <unistd.h>
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdbool.h>
29 #include <string.h>
30 #include <sys/stat.h>
31
32 #if !HAVE_CHOWN
33
34
35 int
36 chown (_GL_UNUSED const char *file, _GL_UNUSED uid_t uid,
37 _GL_UNUSED gid_t gid)
38 {
39 errno = ENOSYS;
40 return -1;
41 }
42
43 #else
44
45
46 # undef chown
47
48
49
50
51
52
53
54 int
55 rpl_chown (const char *file, uid_t uid, gid_t gid)
56 {
57 struct stat st;
58 bool stat_valid = false;
59 int result;
60
61 # if CHOWN_CHANGE_TIME_BUG
62 if (gid != (gid_t) -1 || uid != (uid_t) -1)
63 {
64 if (stat (file, &st))
65 return -1;
66 stat_valid = true;
67 }
68 # endif
69
70 # if CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE
71 if (gid == (gid_t) -1 || uid == (uid_t) -1)
72 {
73
74 if (!stat_valid && stat (file, &st))
75 return -1;
76 if (gid == (gid_t) -1)
77 gid = st.st_gid;
78 if (uid == (uid_t) -1)
79 uid = st.st_uid;
80 }
81 # endif
82
83 # if CHOWN_MODIFIES_SYMLINK
84 {
85
86
87
88
89
90 int open_flags = O_NONBLOCK | O_NOCTTY | O_CLOEXEC;
91 int fd = open (file, O_RDONLY | open_flags);
92 if (0 <= fd
93 || (errno == EACCES
94 && 0 <= (fd = open (file, O_WRONLY | open_flags))))
95 {
96 int saved_errno;
97 bool fchown_socket_failure;
98
99 result = fchown (fd, uid, gid);
100 saved_errno = errno;
101
102
103
104 fchown_socket_failure =
105 (result != 0 && saved_errno == EINVAL
106 && fstat (fd, &st) == 0
107 && (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode)));
108
109 close (fd);
110
111 if (! fchown_socket_failure)
112 {
113 errno = saved_errno;
114 return result;
115 }
116 }
117 else if (errno != EACCES)
118 return -1;
119 }
120 # endif
121
122 # if CHOWN_TRAILING_SLASH_BUG
123 if (!stat_valid)
124 {
125 size_t len = strlen (file);
126 if (len && file[len - 1] == '/' && stat (file, &st))
127 return -1;
128 }
129 # endif
130
131 result = chown (file, uid, gid);
132
133 # if CHOWN_CHANGE_TIME_BUG
134 if (result == 0 && stat_valid
135 && (uid == st.st_uid || uid == (uid_t) -1)
136 && (gid == st.st_gid || gid == (gid_t) -1))
137 {
138
139
140
141
142
143 result = chmod (file, st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO
144 | S_ISUID | S_ISGID | S_ISVTX));
145 }
146 # endif
147
148 return result;
149 }
150
151 #endif