From d660831c5a8598fd718731a59107f12b387af289 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Wed, 31 Jul 1996 04:39:25 +0000 Subject: (DO_CHOWN): New macro -- derived from code in copy. (copy): Use DO_CHOWN to assure that any newly-created symbolic links have proper owner and group. --- src/cp.c | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/cp.c b/src/cp.c index fba54affc..4f04553d7 100644 --- a/src/cp.c +++ b/src/cp.c @@ -37,9 +37,17 @@ uid_t geteuid (); #endif #ifdef HAVE_LCHOWN -# define chown(PATH, OWNER, GROUP) lchown(PATH, OWNER, GROUP) +# define LINK_CHOWN(FILE, OWNER, GROUP) lchown(FILE, OWNER, GROUP) +#else +# define LINK_CHOWN(FILE, OWNER, GROUP) chown(FILE, OWNER, GROUP) #endif +#define DO_CHOWN(FILE, NEW_UID, NEW_GID) \ + (LINK_CHOWN ((FILE), (myeuid == 0 ? (NEW_UID) : myeuid), (NEW_GID)) \ + /* If non-root uses -p, it's ok if we can't preserve ownership. \ + But root probably wants to know, e.g. if NFS disallows it. */ \ + && (errno != EPERM || myeuid == 0)) + /* Used by do_copy, make_path_private, and re_protect to keep a list of leading directories whose protections need to be fixed after copying. */ @@ -817,6 +825,18 @@ copy (const char *src_path, const char *dst_path, int new_dst, dev_t device, error (0, errno, "%s", dst_path); goto un_backup; } + + /* Change the owner and group of the just-created symbolic link + if this system has the lchown function. */ +#ifdef HAVE_LCHOWN + if (flag_preserve + && DO_CHOWN (dst_path, src_sb.st_uid, src_sb.st_gid)) + { + error (0, errno, "%s", dst_path); + goto un_backup; + } +#endif + return 0; } else @@ -892,6 +912,18 @@ copy (const char *src_path, const char *dst_path, int new_dst, dev_t device, error (0, errno, _("cannot create symbolic link `%s'"), dst_path); goto un_backup; } + + /* Change the owner and group of the just-created symbolic link + if this system has the lchown function. */ +#ifdef HAVE_LCHOWN + if (flag_preserve + && DO_CHOWN (dst_path, src_sb.st_uid, src_sb.st_gid)) + { + error (0, errno, "%s", dst_path); + goto un_backup; + } +#endif + return 0; } else @@ -918,12 +950,7 @@ copy (const char *src_path, const char *dst_path, int new_dst, dev_t device, return 1; } - /* If non-root uses -p, it's ok if we can't preserve ownership. - But root probably wants to know, e.g. if NFS disallows it. */ - if (chown (dst_path, - (myeuid == 0 ? src_sb.st_uid : myeuid), - src_sb.st_gid) - && (errno != EPERM || myeuid == 0)) + if (DO_CHOWN (dst_path, src_sb.st_uid, src_sb.st_gid)) { error (0, errno, "%s", dst_path); return 1; -- cgit v1.2.3-70-g09d2