lchmod for osx (was: Symlinks in OS X (10.4.1))

Wesley W. Terpstra wesley at terpstra.ca
Tue Oct 16 11:44:35 GMT 2007


Amazing no one else has written this... Someone with an intel mac  
should test this to check the uint32_t works there too (I have a  
powerpc).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/attr.h> /* !!! */
#include <sys/stat.h>
#include <unistd.h>

int my_lchmod(const char *path, mode_t mode) {
         struct attrlist attrList;
         uint32_t m;

         /* manpage is wrong! not mode_t, but uint32_t is required */
         memset(&attrList, 0, sizeof(attrList));
         attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
         attrList.commonattr = ATTR_CMN_ACCESSMASK;
         m = mode;
         return setattrlist(path, &attrList, &m, sizeof(m),  
FSOPT_NOFOLLOW);
}

int main(int argc, const char **argv) {
         struct stat st;
         const char *file;
         int perms;

         file = argv[1];
         perms = strtol(argv[2], 0, 0);

         if (lstat(file, &st) != 0) {
                 perror("stat");
                 return 1;
         }

         st.st_mode = (st.st_mode & ~0777) | perms;

         if (my_lchmod(file, st.st_mode) != 0) {
                 perror("lchmod");
                 return 1;
         }

         return 0;
}




More information about the rsync mailing list