Skip to content

Instantly share code, notes, and snippets.

@mikeabdullah
Last active January 6, 2024 07:31
Show Gist options
  • Select an option

  • Save mikeabdullah/4740463 to your computer and use it in GitHub Desktop.

Select an option

Save mikeabdullah/4740463 to your computer and use it in GitHub Desktop.

Revisions

  1. mikeabdullah revised this gist Feb 9, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ - (BOOL)atomicCopyItemAtURL:(NSURL *)sourceURL

    // Clean up
    NSError *error;
    if (![manager removeItemAtURL:tempDir error:NULL])
    if (![manager removeItemAtURL:tempDir error:&error])
    {
    NSLog(@"Failed to remove temp directory after atomic copy: %@", error);
    }
  2. mikeabdullah revised this gist Feb 8, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@ - (BOOL)atomicCopyItemAtURL:(NSURL *)sourceURL
    {
    NSFileManager *manager = [NSFileManager defaultManager];

    // First copy into a temporary location where failure doesn't matter
    NSURL *tempDir = [manager URLForDirectory:NSItemReplacementDirectory
    inDomain:NSUserDomainMask
    appropriateForURL:destinationURL
    @@ -17,6 +18,7 @@ - (BOOL)atomicCopyItemAtURL:(NSURL *)sourceURL
    BOOL result = [manager copyItemAtURL:sourceURL toURL:tempURL error:outError];
    if (result)
    {
    // Move the complete item into place, replacing any existing item in the process
    NSURL *resultingURL;
    result = [manager replaceItemAtURL:destinationURL
    withItemAtURL:tempURL
  3. mikeabdullah revised this gist Feb 8, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ - (BOOL)atomicCopyItemAtURL:(NSURL *)sourceURL
    NSError *error;
    if (![manager removeItemAtURL:tempDir error:NULL])
    {
    NSLog(@"Failed to remove temp directory after atomic copywriting: %@", error);
    NSLog(@"Failed to remove temp directory after atomic copy: %@", error);
    }

    return result;
  4. mikeabdullah revised this gist Feb 8, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ - (BOOL)atomicCopyItemAtURL:(NSURL *)sourceURL
    NSError *error;
    if (![manager removeItemAtURL:tempDir error:NULL])
    {
    NSLog(@"Failed to remove temp directory after atomic media record writing: %@", error);
    NSLog(@"Failed to remove temp directory after atomic copywriting: %@", error);
    }

    return result;
  5. mikeabdullah revised this gist Feb 8, 2013. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,10 @@ - (BOOL)atomicCopyItemAtURL:(NSURL *)sourceURL

    if (result)
    {
    OBASSERT([[resultingURL absoluteString] isEqualToString:[destinationURL absoluteString]]);
    NSAssert([resultingURL.absoluteString isEqualToString:destinationURL.absoluteString],
    @"URL unexpectedly changed during replacement from:\n%@\nto:\n%@",
    destinationURL,
    resultingURL);
    }
    }

  6. mikeabdullah revised this gist Feb 8, 2013. 1 changed file with 18 additions and 14 deletions.
    32 changes: 18 additions & 14 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,29 @@
    - (BOOL)atomicCopyItemAtURL:(NSURL *)sourceURL toURL:(NSURL *)destinationURL error:(NSError **)outError
    - (BOOL)atomicCopyItemAtURL:(NSURL *)sourceURL
    toURL:(NSURL *)destinationURL
    error:(NSError **)outError
    {
    NSURL *tempDir = [[NSFileManager defaultManager] URLForDirectory:NSItemReplacementDirectory
    inDomain:NSUserDomainMask
    appropriateForURL:destinationURL
    create:YES
    error:outError];
    NSFileManager *manager = [NSFileManager defaultManager];

    NSURL *tempDir = [manager URLForDirectory:NSItemReplacementDirectory
    inDomain:NSUserDomainMask
    appropriateForURL:destinationURL
    create:YES
    error:outError];

    if (!tempDir) return NO;

    NSURL *tempURL = [tempDir URLByAppendingPathComponent:[destinationURL lastPathComponent]];

    BOOL result = [[NSFileManager defaultManager] copyItemAtURL:sourceURL toURL:tempURL error:outError];
    BOOL result = [manager copyItemAtURL:sourceURL toURL:tempURL error:outError];
    if (result)
    {
    NSURL *resultingURL;
    result = [[NSFileManager defaultManager] replaceItemAtURL:destinationURL
    withItemAtURL:tempURL
    backupItemName:nil
    options:NSFileManagerItemReplacementUsingNewMetadataOnly
    resultingItemURL:&resultingURL
    error:outError];
    result = [manager replaceItemAtURL:destinationURL
    withItemAtURL:tempURL
    backupItemName:nil
    options:NSFileManagerItemReplacementUsingNewMetadataOnly
    resultingItemURL:&resultingURL
    error:outError];

    if (result)
    {
    @@ -29,7 +33,7 @@ - (BOOL)atomicCopyItemAtURL:(NSURL *)sourceURL toURL:(NSURL *)destinationURL err

    // Clean up
    NSError *error;
    if (![[NSFileManager defaultManager] removeItemAtURL:tempDir error:NULL])
    if (![manager removeItemAtURL:tempDir error:NULL])
    {
    NSLog(@"Failed to remove temp directory after atomic media record writing: %@", error);
    }
  7. mikeabdullah created this gist Feb 8, 2013.
    38 changes: 38 additions & 0 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    - (BOOL)atomicCopyItemAtURL:(NSURL *)sourceURL toURL:(NSURL *)destinationURL error:(NSError **)outError
    {
    NSURL *tempDir = [[NSFileManager defaultManager] URLForDirectory:NSItemReplacementDirectory
    inDomain:NSUserDomainMask
    appropriateForURL:destinationURL
    create:YES
    error:outError];

    if (!tempDir) return NO;

    NSURL *tempURL = [tempDir URLByAppendingPathComponent:[destinationURL lastPathComponent]];

    BOOL result = [[NSFileManager defaultManager] copyItemAtURL:sourceURL toURL:tempURL error:outError];
    if (result)
    {
    NSURL *resultingURL;
    result = [[NSFileManager defaultManager] replaceItemAtURL:destinationURL
    withItemAtURL:tempURL
    backupItemName:nil
    options:NSFileManagerItemReplacementUsingNewMetadataOnly
    resultingItemURL:&resultingURL
    error:outError];

    if (result)
    {
    OBASSERT([[resultingURL absoluteString] isEqualToString:[destinationURL absoluteString]]);
    }
    }

    // Clean up
    NSError *error;
    if (![[NSFileManager defaultManager] removeItemAtURL:tempDir error:NULL])
    {
    NSLog(@"Failed to remove temp directory after atomic media record writing: %@", error);
    }

    return result;
    }