1 /* Provide a stub lchown function for systems that lack it.
3 Copyright (C) 1998-1999, 2002, 2004, 2006-2007, 2009-2020 Free Software
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.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 /* written by Jim Meyering */
32 /* If the system chown does not follow symlinks, we don't want it
33 replaced by gnulib's chown, which does follow symlinks. */
34 # if CHOWN_MODIFIES_SYMLINK
38 /* Work just like chown, except when FILE is a symbolic link.
39 In that case, set errno to EOPNOTSUPP and return -1.
40 But if autoconf tests determined that chown modifies
41 symlinks, then just call chown. */
44 lchown (const char *file
, uid_t uid
, gid_t gid
)
47 # if ! CHOWN_MODIFIES_SYMLINK
50 if (lstat (file
, &stats
) == 0 && S_ISLNK (stats
.st_mode
))
57 return chown (file
, uid
, gid
);
59 # else /* !HAVE_CHOWN */
65 #else /* HAVE_LCHOWN */
69 /* Work around trailing slash bugs in lchown. */
71 rpl_lchown (const char *file
, uid_t uid
, gid_t gid
)
73 bool stat_valid
= false;
76 # if CHOWN_CHANGE_TIME_BUG
79 if (gid
!= (gid_t
) -1 || uid
!= (uid_t
) -1)
81 if (lstat (file
, &st
))
84 if (!S_ISLNK (st
.st_mode
))
85 return chown (file
, uid
, gid
);
89 # if CHOWN_TRAILING_SLASH_BUG
92 size_t len
= strlen (file
);
93 if (len
&& file
[len
- 1] == '/')
94 return chown (file
, uid
, gid
);
98 result
= lchown (file
, uid
, gid
);
100 # if CHOWN_CHANGE_TIME_BUG && HAVE_LCHMOD
101 if (result
== 0 && stat_valid
102 && (uid
== st
.st_uid
|| uid
== (uid_t
) -1)
103 && (gid
== st
.st_gid
|| gid
== (gid_t
) -1))
105 /* No change in ownership, but at least one argument was not -1,
106 so we are required to update ctime. Since lchown succeeded,
107 we assume that lchmod will do likewise. But if the system
108 lacks lchmod and lutimes, we are out of luck. Oh well. */
109 result
= lchmod (file
, st
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
110 | S_ISUID
| S_ISGID
| S_ISVTX
));
117 #endif /* HAVE_LCHOWN */