Re: copyfile(3) copies the posix "owner" for *some* files only
Re: copyfile(3) copies the posix "owner" for *some* files only
- Subject: Re: copyfile(3) copies the posix "owner" for *some* files only
- From: Jerry Krinock <email@hidden>
- Date: Wed, 28 Jul 2010 21:28:26 -0700
Just to give the resolution on this thread, wrapping chown with fts_xxx() was not difficult. This seems to work…
/*!
@brief Wraps chown(2) with fts(3) so that it operates recursively,
like chown(8) with -R option.
@details Parameters are same as chown(2).
@result Returns 0 if successful.
Returns -1 if chown fails for the given path or any descendant,
which usually happens if you don't have the elevated privileges
required to chown things. To provide the reason for the failure,
chown(2) will set errno.
Returns -2 if fts_open() could not be initialized. I don't know how
-2 could ever happen though, because fts_open()
still returns 0 even if you give it a nonexistent path.
*/
int chown_recursive(char * const path, uid_t uid, uid_t gid) ;
#include <fts.h>
#include <unistd.h>
int chown_recursive(char * const path, uid_t uid, uid_t gid) {
// See man fts(3) for these. Your mileage may vary.
int fts_options = FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV | FTS_SEEDOT ;
// fts_open requires a null-terminated array of paths.
// We only have one path.
char* paths[2] ;
paths[0] = path ;
paths[1] = NULL ;
FTS* ftsp = fts_open(paths, fts_options, NULL) ;
if (ftsp == NULL) {
return -2 ;
}
FTSENT* ftsPointer = 0 ;
while ((ftsPointer = fts_read(ftsp)) != NULL) {
// This will execute once for each item in the tree.
int result ;
switch (ftsPointer->fts_info) {
// List here the file types you want to be chown-ed.
// I hope I didn't miss any.
case FTS_F: // regular file
case FTS_D: // directory
case FTS_DOT: // system dot file, i.e. '.' or '..'
case FTS_SL: // symbolic link
// Do the actual work
result = chown(ftsPointer->fts_path, uid, gid) ;
if (result != 0) {
// chown only returns either 0 or -1.
// Must be -1 here.
return result ;
}
break ;
default:
break;
}
}
fts_close(ftsp) ;
return 0 ;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden