Protect lock check section

This commit is contained in:
1024jp 2015-02-21 23:19:16 +09:00
parent 83e76f5c41
commit a72afc0de6
2 changed files with 26 additions and 10 deletions

View File

@ -8,6 +8,7 @@ Change Log
### Fixes
- Fix an issue where octal file permisson in the document inspector was wrong.
- Improve general stability.

View File

@ -1484,25 +1484,40 @@ NSString *const CEIncompatibleConvertedCharKey = @"convertedChar";
- (BOOL)canUnlockFileAtURL:(NSURL *)url isLocked:(BOOL *)isLocked lockAgain:(BOOL)lockAgain
// ------------------------------------------------------
{
__block BOOL isFinderLocked = NO;
BOOL success = NO;
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isFinderLocked = [[fileManager attributesOfItemAtPath:[url path] error:nil] fileIsImmutable];
if (isFinderLocked) {
// Finder Lock
BOOL success = [fileManager setAttributes:@{NSFileImmutable:@NO} ofItemAtPath:[url path] error:nil];
if (success) {
if (lockAgain) {
//
[fileManager setAttributes:@{NSFileImmutable:@YES} ofItemAtPath:[url path] error:nil];
@synchronized(self) {
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self];
[coordinator coordinateReadingItemAtURL:url options:NSFileCoordinatorReadingWithoutChanges
error:&error
byAccessor:^(NSURL *newURL)
{
isFinderLocked = [[fileManager attributesOfItemAtPath:[newURL path] error:nil] fileIsImmutable];
}];
if (isFinderLocked) {
// unlock file once
success = [fileManager setAttributes:@{NSFileImmutable:@NO} ofItemAtPath:[url path] error:nil];
if (success) {
// lock file again if needed
if (lockAgain) {
[fileManager setAttributes:@{NSFileImmutable:@YES} ofItemAtPath:[url path] error:nil];
}
}
} else {
return NO;
// no-lock file is always treated as success
success = YES;
}
}
if (isLocked) {
*isLocked = isFinderLocked;
}
return YES;
return success;
}