Skip to content

Instantly share code, notes, and snippets.

@krzyzanowskim
Last active October 22, 2024 23:18
Show Gist options
  • Save krzyzanowskim/61fc00e64875b1f1c81c9ef5255fad4a to your computer and use it in GitHub Desktop.
Save krzyzanowskim/61fc00e64875b1f1c81c9ef5255fad4a to your computer and use it in GitHub Desktop.
Filewrapper does not reread (refresh) its content
import Foundation
let dirURL = URL(filePath: "/Users/marcinkrzyzanowski/Downloads/filewrappertest.test")
// cleanup
try? FileManager.default.removeItem(at: URL(filePath: "/Users/marcinkrzyzanowski/Downloads/filewrappertest.test/COPY.png"))
let fw = try FileWrapper(url: dirURL)
// fw.fileWrappers // ["IMG_0736.png": <NSFileWrapper: 0x6000023a8420>]
// add item (copy new item into directory
try FileManager.default.copyItem(at: URL(filePath: "/Users/marcinkrzyzanowski/Downloads/filewrappertest.test/IMG_0736.png"), to: URL(filePath: "/Users/marcinkrzyzanowski/Downloads/filewrappertest.test/COPY.png"))
// Force update parent directory attribute fixes the matchesContents(of:)
// try FileManager().setAttributes([.modificationDate: Date.now.addingTimeInterval(2)], ofItemAtPath: dirURL.path())
fw.fileWrappers! // ["IMG_0736.png": <NSFileWrapper: 0x6000023a8420>]
fw.matchesContents(of: dirURL) // true (!!!!!!)
// read, like in "refresh"
// Recursively rereads the entire contents of a file wrapper from the specified location on disk.
try fw.read(from: dirURL)
// https://developer.apple.com/documentation/foundation/filewrapper/1408360-matchescontents
// Indicates whether the contents of a file wrapper matches a directory, regular file, or symbolic link on disk.
// For a directory, children are compared against the files in the directory, recursively.
fw.matchesContents(of: dirURL) // true! but the content is different
fw.fileWrappers! // ["COPY.png": <NSFileWrapper: 0x600003454a20>, "IMG_0736.png": <NSFileWrapper: 0x6000023a8420>]
@krzyzanowskim
Copy link
Author

krzyzanowskim commented Oct 22, 2024

// remove file from the directory does not update directory attributes (?) that FileWrapper cares about
// the modificationDate is in fact updated, but I need to set it again to make the differente (!)
unlink("/Users/marcinkrzyzanowski/Downloads/filewrappertest.test/COPY.png")
// therefore manually update modificationDate attribute
try FileManager().setAttributes([.modificationDate: Date.now.addingTimeInterval(1)], ofItemAtPath: dirURL.path())

@krzyzanowskim
Copy link
Author

the other time, copyItem does not update parent directory

// add item (copy new item into directory `modificationDate`
try FileManager.default.copyItem(at: URL(filePath: "/Users/marcinkrzyzanowski/Downloads/filewrappertest.test/IMG_0736.png"), to: URL(filePath: "/Users/marcinkrzyzanowski/Downloads/filewrappertest.test/COPY.png"))

all of all it is about make sure attributes change or matchesContents(of:) not going to recognize any change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment