15

From what I see, network mapped drives appear as subfolders of the /Volumes folder.

What is the proper way to get file changes updates (delete/create/update) from this folder?

Would /dev/fsevents work for that?

How does Finder know about the changes?

jww
  • 97,681
  • 90
  • 411
  • 885
Jamie
  • 657
  • 7
  • 18
  • Unless the file sharing protocol includes a way for the server to notify the client of changes (and I don't know of any protocols that do this), it won't be possible. – Gordon Davisson Apr 09 '15 at 05:43
  • That makes sense, but still: how does Finder do it? – Jamie Apr 09 '15 at 08:55
  • @Gordon - NTFS and SMB allow client notifications of changes (even mapped network drives if its a Windows system running NTFS). But the Linux machines I have interop'd with running SAMBA don't fire the event. So its kind of like only Microsoft provides the feature even though any provider should be able to offer it. – jww Apr 15 '15 at 11:15
  • @GordonDavisson also the OS X native AFP sends notifications, just check ed with fseventer and my os x client/server setup. – mahal tertin Apr 16 '15 at 12:20
  • @Jamie Are you looking for a way to implement this in an Cocoa-Application? Swift or ObjC? – mahal tertin Apr 16 '15 at 12:32
  • @mahaltertin I was looking to implement this using C/C++ as standalone app and use some form of interprocess communication to send the data to another app. – Jamie Apr 16 '15 at 14:50
  • @Jamie great, so have a look at my answer which points you to the documentation of two APIs which you can use in your C/C++ app. – mahal tertin Apr 16 '15 at 16:18
  • @Jamie I'd love to win that bounty. What else would you need in my answer? – mahal tertin Apr 20 '15 at 15:07

2 Answers2

3

You can use fswatch, which I find easest to install via homebrew. And, yes it does use FSEvents. Then you just do:

fswatch /Volumes/MUSIC

where MUSIC is a Samba-based music server on my network.

Here is how it looks in action... first I show the mounted volumes (and that MUSIC is Samba based) in the top window, then I start fswatch in the bottom left window, then I make modifications in the filesystem in the top window and you can see them happen in the Finder and also see in the bottom left window that fswatch tracks all the events.

enter image description here

You can also use it to interact with another program whenever events are detected, like this (extracted from the fswatch manpage):

Probably the simplest way to pipe fswatch to another program in order to respond to an event is using xargs:

   $ fswatch -0 [opts] [paths] | xargs -0 -n 1 -I {} [command]
  • fswatch -0 will split records using the NUL character.

  • xargs -0 will split records using the NUL character. This is required to correctly match impedance with fswatch.

  • xargs -n 1 will invoke command every record. If you want to do it every x records, then use xargs -n x.

  • xargs -I {} will substitute occurrences of {} in command with the parsed argument. If the command you are running does not need the event path name, just delete this option. If you prefer using another replacement string, substitute {} with yours.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thank you for the detailed fswatch usage sample! I was asking this from a developer's point of view but from a user perspective your answer is a great one. – Jamie May 04 '15 at 15:25
  • @Mark can `fswatch` work when changing files on a remote computer that has a mapped volume to macos? I have a windows network map and `fswatch` works when I change a file to that directory from inside the mac comp, but it does not display anything when I change a file using the windows machine on the physical path. – Christos Lytras Dec 01 '17 at 22:53
  • @ChristosLytras Sorry, I don't know - I generally try and avoid Windows. – Mark Setchell Dec 02 '17 at 08:21
3

You're correct, OS X mounts the network drives in /Volumes

The way to get file change updates is to use File System Events API. It is a C-based API where you would watch for all changes in specific directories (or even /).

You would create the stream with FSEventStreamCreate and starting it with FSEventStreamScheduleWithRunLoop

Be prepared to dig into the header-file as there is more documentation on it as in the Reference documentation

From what I can tell, Finder probably uses some internal API or the kernel queues which are more complex to setup than the higher-level API of FSEvents.h

There is a nice GUI to helping you see how the whole events come in. It's called fseventer by fernlightning (not yet Yosemite ready)

mahal tertin
  • 3,239
  • 24
  • 41