Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change permission of a directory recursively using NSFileManager setAttributes

Tags:

objective-c

I'm currently using NSFileManager setAttributes to change the permission of a directory. My problem is that it doesn't appear to do so recursively. Is there any way to force it to do so?

like image 901
Pierre Avatar asked Mar 29 '10 05:03

Pierre


People also ask

How do I change the permissions on a directory recursively?

To modify the permission flags on existing files and directories, use the chmod command ("change mode"). It can be used for individual files or it can be run recursively with the -R option to change permissions for all of the subdirectories and files within a directory.

How do I change the permissions on a Linux directory recursively?

You can change permissions of files using numeric or symbolic mode with the chmod command. Use the chmod command with the R (recursive) option to work on all directories and files under a given directory. The permissions of a file can be changed only with the user with sudo priviledges, or the file owner.

How do you recursively chmod?

It is common to use the basic chmod command to change the permission of a single file. However, you may need to modify the permission recursively for all files within a directory. In such cases, the chmod recursive option ( -R or --recursive ) sets the permission for a directory (and the files it contains).

Which chmod option changes file permission in the current directory and all subdirectories?

One of the options to change multiple files is to run chmod recursive with the -R (recursive, and not the capital) option. The recursive option will change the permissions for all the files, including those under sub-directories, inside a given path. 1. Consider the following command, chmod -R a=r,u=rwx my_dir .


1 Answers

I don't think there's a built-in method to do this, but it shouldn't be hard to do something like:

NSString *path = @"/The/root/directory";
NSDictionary *attributes;   // Assume that this is already setup


NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *subPaths = [fileManager subpathsAtPath:path];
for (NSString *aPath in subPaths) {
    BOOL isDirectory;
    [fileManager fileExistsAtPath:aPath isDirectory:&isDirectory];
    if (isDirectory) {
        // Change the permissions on the directory here
        NSError *error = nil;
        [fileManager setAttributes:attributes ofItemAtPath:aPath error:&error];
        if (error) {
            // Handle the error
        }
    }
}

This is untested, but should give you a starting point.

like image 110
Nick Forge Avatar answered Oct 07 '22 13:10

Nick Forge