Hi,
sorry to jump into this old thread, but I've a question about the following sentence "By default the data protection value is inherited from the parent directory when you create an item." because as far as I have tested this, it seems to be a wrong assumption.
For example I have the following code:
objc
BOOL success = YES;
NSError *error = nil;
NSURL *tmpDir = [NSFileManager.defaultManager.temporaryDirectory URLByAppendingPathComponent:@"tmpDir"];
success = [NSFileManager.defaultManager createDirectoryAtURL:tmpDir withIntermediateDirectories:YES attributes:@{ NSFileProtectionKey: NSFileProtectionComplete } error:&error];
NSFileProtectionType tmpDirFileProtectionType = [NSFileManager.defaultManager attributesOfItemAtPath:tmpDir.path error:nil][NSFileProtectionKey];
// tmpDirFileProtectionType == NSFileProtectionComplete - this is OK and expected
NSURL *subDir = [tmpDir URLByAppendingPathComponent:@"subDir"];
success = [NSFileManager.defaultManager createDirectoryAtURL:subDir withIntermediateDirectories:YES attributes:nil error:&error];
NSFileProtectionType subDirFileProtectionType = [NSFileManager.defaultManager attributesOfItemAtPath:subDir.path error:nil][NSFileProtectionKey];
// subDirFileProtectionType == NSFileProtectionComplete - this is OK and expected
NSString *someStr = @"someStr";
NSURL *someStrFile1 = [tmpDir URLByAppendingPathComponent:@"someStr1.txt"];
NSURL *someStrFile2 = [subDir URLByAppendingPathComponent:@"someStr2.txt"];
success = [someStr writeToURL:someStrFile1 atomically:YES encoding:NSUTF8StringEncoding error:&error];
success = [someStr writeToURL:someStrFile2 atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSFileProtectionType someStrFile1FileProtectionType = [NSFileManager.defaultManager attributesOfItemAtPath:someStrFile1.path error:nil][NSFileProtectionKey];
NSFileProtectionType someStrFile2FileProtectionType = [NSFileManager.defaultManager attributesOfItemAtPath:someStrFile2.path error:nil][NSFileProtectionKey];
// someStrFile1FileProtectionType == NSFileProtectionCompleteUntilFirstUserAuthentication - this is NOT OK and NOT expected
// someStrFile2FileProtectionType == NSFileProtectionCompleteUntilFirstUserAuthentication - this is NOT OK and NOT expected
I've tested this on an iPhone X with iOS 14.5 and the app does not use the file protection entitlement.
Am I doing something wrong?
Post
Replies
Boosts
Views
Activity
If I pass NO to the atomically parameter the file inherits the NSFileProtectionType.
Is it expected/documented that the atomically parameter prevents the inheritance of NSFileProtectionType?
Furthermore I have observed a similar behavior with -[NSString copyItemAtURL:toURL:error:]. The copied item does not inherit the NSFileProtectionType of its new parent directory. Do I have to manually change the attribute after the item has been copied?
Thanks a lot for your quick response and the detailed explanation!
The more I think about files and its metadata, the observed behavior makes sense to me :D
So for creating new files I will definitely change my code to use -writeToURL:options:error:.
How big is this file?
The files in my app are typically documents like TXT, PDF, MS Word, etc. Therefor the size varies between a few KBs and many MBs.
I guess storing such files in NSData would exceed the memory limits pretty fast.
Unless you want to spend more time with some research about this topic, I am going to use NSFileManagers -copyItemAtURL:toURL:error: in combination with -setAttributes:ofItemAtPath:error:.