c - Proper way to create path with specific permissions -
developing in c language, write function create path specific permissions using function int mkdir(const char *pathname, mode_t mode). i'm not interested in solution works (that, know how do), i'm interested in proper solution, solution of practice.
my questions following :
how handle umask , mode in order have permissions requested ?
i thought of 2 solutions :
int create_path(char *pathname, mode_t mode, mode_t umask_wanted) { mode_t old_mask = umask(umask_wanted); mkdir(pathname, mode); umask(old_mask); } or
int create_path(char *pathname, mode_t mode) { mode_t old_mask = umask(0); mkdir(pathname, mode); umask(old_mask); } or creating different function different masks in order ensure user of function aware of does.
or else ?
how handle creation of full path ?
here piece of code shows solution might quit naive.
int create_path(char *pathname, mode_t mode) { int i; char* path = malloc(strlen(pathname) + 1); if (path==0) strcpy(path, pathname); for(i=0; path[i] != '\0'; i++) { if (path[i] == '/') { path[i] = '\0'; mkdir(path, mode); path[i] = '/'; } } free(path); } is solution of practice? or better idea?
how handle permissions when part of path exists?
should change permissions of existing folders in order fit mode specified?
or should change if gives more permission?
note : pieces of code examples illustrate questions. solution, manage errors of mkdir , mix of in 1 function.
note2 : assume user of function knows , exact permissions needs ;-)
Comments
Post a Comment