From: Bruno Haible Date: Thu, 14 Aug 2025 19:59:20 +0000 (+0200) Subject: lchown: Use issymlink. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f478e006d3a4d43dbed9651a0eba41d5f5732cfa;p=thirdparty%2Fgnulib.git lchown: Use issymlink. * lib/lchown.c (lchown): Use issymlink instead of readlink. * modules/lchown (Depends-on): Remove readlink. Add issymlink. --- diff --git a/ChangeLog b/ChangeLog index 4e387893cb..b072f949d7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2025-08-14 Bruno Haible + + lchown: Use issymlink. + * lib/lchown.c (lchown): Use issymlink instead of readlink. + * modules/lchown (Depends-on): Remove readlink. Add issymlink. + 2025-08-14 Bruno Haible chown: Use issymlink. diff --git a/lib/lchown.c b/lib/lchown.c index ce7d31730a..e5e277101c 100644 --- a/lib/lchown.c +++ b/lib/lchown.c @@ -44,9 +44,7 @@ lchown (const char *file, uid_t uid, gid_t gid) { # if HAVE_CHOWN # if ! CHOWN_MODIFIES_SYMLINK - char readlink_buf[1]; - - if (0 <= readlink (file, readlink_buf, sizeof readlink_buf)) + if (issymlink (file) > 0) { errno = EOPNOTSUPP; return -1; @@ -79,19 +77,22 @@ rpl_lchown (const char *file, uid_t uid, gid_t gid) { /* Prefer readlink to lstat+S_ISLNK, to avoid EOVERFLOW issues in the common case where FILE is a non-symlink. */ - char linkbuf[1]; - int r = readlink (file, linkbuf, 1); - if (r < 0) - return errno == EINVAL ? chown (file, uid, gid) : r; + int ret = issymlink (file); + if (ret < 0) + return -1; + if (ret == 0) + /* FILE is not a symlink. */ + return chown (file, uid, gid); /* Later code can use the status, so get it if possible. */ - r = lstat (file, &st); - if (r < 0) - return r; - stat_valid = true; + ret = lstat (file, &st); + if (ret < 0) + return -1; /* An easy check: did FILE change from a symlink to a non-symlink? */ if (!S_ISLNK (st.st_mode)) return chown (file, uid, gid); + + stat_valid = true; } # endif diff --git a/modules/lchown b/modules/lchown index 45f16d91aa..94ba7a9cdd 100644 --- a/modules/lchown +++ b/modules/lchown @@ -7,7 +7,7 @@ m4/lchown.m4 Depends-on: unistd-h -readlink [test $HAVE_LCHOWN = 0 || test $REPLACE_LCHOWN = 1] +issymlink [test $HAVE_LCHOWN = 0 || test $REPLACE_LCHOWN = 1] chown [test $HAVE_LCHOWN = 0 || test $REPLACE_LCHOWN = 1] errno-h [test $HAVE_LCHOWN = 0 || test $REPLACE_LCHOWN = 1] bool [test $HAVE_LCHOWN = 0 || test $REPLACE_LCHOWN = 1]