FileWatcher icon indicating copy to clipboard operation
FileWatcher copied to clipboard

Deleting files show as "renamed" instead of "removed"

Open njmullen opened this issue 6 years ago • 16 comments

After importing the project and getting it setup, I tested deleting files from a directory. The notifications fire properly but they state that the file was renamed instead of removed. Additionally, adding a file will say that it was modified instead of created.

I tried this on an iCloud and a non-iCloud Drive folder.

This is the output of printing from extension FileWatcherEvent

Swift.print(self.removed) Swift.print(self.renamed) Swift.print(result.description)

false true The file /Users/nick/Downloads/IMG_1739.jpeg was renamed

Any ideas? Love the library btw, thank you!

njmullen avatar Aug 24 '19 22:08 njmullen

Hey there. Love that you tried the framework.

Have your tried:

filewatcher.callback = { event in
  print(event.created) // both file and folder
  print(event.fileCreated)
  print(event.folderCreated)
  print("Something happened here: " + event.path)
}

can be found here: https://github.com/eonist/FileWatcher/blob/e9e1e140cacd39abf92ef35fc20d17ccc2cde96b/FileWatcherEvent.swift#L27

and

https://github.com/eonist/FileWatcher/blob/e9e1e140cacd39abf92ef35fc20d17ccc2cde96b/FileWatcherEvent.swift#L40

eonist avatar Aug 25 '19 11:08 eonist

Here is my AppDelegate.swift didFinishLaunching method

` func applicationDidFinishLaunching(_ aNotification: Notification) { print("Initializing application") let filewatcher = FileWatcher([NSString(string: "~/Downloads").expandingTildeInPath])

    filewatcher.callback = { event in
        print("Something happened here: " + event.path)
        print(event.fileRemoved)
        print(event.dirRemoved)
        print(event.fileCreated)
        print(event.dirCreated)
        print(event.flags)
        print(event)
        print(event.description)
    }
    
    filewatcher.start() // start monitoring
    filewatcher.queue = DispatchQueue.global()

`

and here is my output for deleting then un-deleting a file

Something happened here: /Users/nick/Downloads/IMG_1738.jpeg false false false false 67584 filelock.FileWatcherEvent The file /Users/nick/Downloads/IMG_1738.jpeg was renamed Something happened here: /Users/nick/Downloads/.DS_Store false false false false 70656 filelock.FileWatcherEvent The file /Users/nick/Downloads/.DS_Store was modified Something happened here: /Users/nick/Downloads/IMG_1738.jpeg false false false false 67584 filelock.FileWatcherEvent The file /Users/nick/Downloads/IMG_1738.jpeg was renamed Something happened here: /Users/nick/Downloads/.DS_Store false false false false 70656 filelock.FileWatcherEvent The file /Users/nick/Downloads/.DS_Store was modified

njmullen avatar Aug 25 '19 15:08 njmullen

Apple changes how their os works from time to time. If you delete a file and it goes to the trash its now recorded as a rename. If you skip the trash bin (aka immediate deletion), its recorded as delete. I update the framework to differentiate the two. Its not perfect but, a temp solution. I think its possible to fix by working a bit on the FSEvent flags. You can do additional testing with a file assertion method. With something like: renamed && !FileManager.assertExistence(filePath)

eonist avatar Aug 25 '19 17:08 eonist

Hey, with the latest update you sent, I am sending a new version 0.2.1 to the Cocoa Pods. Sounds good? @eonist

alioguzhan avatar Aug 26 '19 21:08 alioguzhan

@alioguzhan do I need to push git tags?

eonist avatar Aug 27 '19 05:08 eonist

@eonist Oh, It would be great. we need 0.2.1 tag

alioguzhan avatar Aug 27 '19 06:08 alioguzhan

@alioguzhan I'm at work so I just did it with github, should work the same as using gif in terminal right?: https://github.com/eonist/FileWatcher/releases/tag/0.2.1

eonist avatar Aug 27 '19 07:08 eonist

@eonist Yes it is correct. I can take it from here. I will release a new version to CocoaPods. Thanks

alioguzhan avatar Aug 27 '19 08:08 alioguzhan

It's done : https://cocoapods.org/pods/FileWatcher @njmullen @eonist

alioguzhan avatar Aug 27 '19 08:08 alioguzhan

Awesome, thank you so much!

Has this change been pushed to the repo yet? I tried replacing FIleWatcherEvent.swift with the one from the latest commit 2 days ago, but I'm still having the same issue. I'm using it just with the files rather than through Cocoapods.

njmullen avatar Aug 27 '19 22:08 njmullen

Did you try doing alt + cmd + del on a file? Does this produce a delete flag?

eonist avatar Aug 28 '19 12:08 eonist

@eonist I tried your cool solution and i'm able a) to monitor multiple directories b) Used background thread and NSOperations. Works fine c) Changes in subdirectories are also detected. Huge.

Fantastic work. But when i add or removed a file - the status shows is 'Rename'

In below code - fileCreated and fileCreated is never 'true' fileRenamed is true in both cases - adding and removing

Any way to distinguish between addition and removal?

if event.fileCreated == true { Swift.print("New File is created here... \(event.path)") } if event.fileRemoved == true { Swift.print("New File is removed from here... \(event.path)") } if event.fileRenamed == true { Swift.print("New File is renamed from here... \(event.path)") }

kamleshgk avatar Oct 21 '20 15:10 kamleshgk

In apples doc it says:

/*
 * A file system object was removed at the specific path supplied in this event.
 * (This flag is only ever set if you specified the FileEvents flag when creating the stream.)
 */
@available(OSX 10.7, *)
public var kFSEventStreamEventFlagItemRemoved: Int { get }

This flag is only ever set if you specified the FileEvents flag when creating the stream I don't understand what that means and 10min research on google doesn't bring immediate clarification. But if you want to differentiate removal and renaming. you can simply do: if !FileManager().fileExists(atPath: event.path) { Swift.print("was deleted") }

I added this in the latest commit.

eonist avatar Oct 22 '20 22:10 eonist

Hey @eonist I totally appreciate the reply. I just do a file exists as you suggested and if it does not exist, its removed.

Cheers!

kamleshgk avatar Oct 24 '20 04:10 kamleshgk

Ran into this issue as well but it was difficult to check if an item was in the trash with .fileExists since it's a system directory and needed full disk permission for some reason, at least in my case. If anybody runs into this, I was able to check if the file is in the trash with getRelationship. Here's an extension, can be adapted to other directories too:

extension FileManager {
    public func isInTrash(_ file: URL) -> Bool {
        var relationship: URLRelationship = .other
        try? getRelationship(&relationship, of: .trashDirectory, in: .userDomainMask, toItemAt: file)
        return relationship == .contains
    }
}

Usage:

let fileExistsInTrash = FileManager.default.isInTrash(file)

Also @eonist, much appreciation for the package!

alienator88 avatar Nov 11 '23 20:11 alienator88

Haa! Nice method! And nice way of solving something adhock! FileObservability is definitely a bit of dark magic to cover all needs.

eonist avatar Nov 13 '23 09:11 eonist