PDA

View Full Version : "Dynamic" folder?




grimreaper1377
Feb 15, 2009, 04:46 PM
Hi,

For my application, I want to make it where if the user drops a file into the folder, it will automatically do something with that file. I've found a way to detect a change in the filesystem, but not for a specific folder. I guess this is similar to what dropbox does. I think they use python - would I need to resort to that?

Thanks!



HiRez
Feb 15, 2009, 05:46 PM
Is it a specific folder, or you don't necessarily know in advance which folder? Yes, there is a big shortcoming in the filesystem utilities (and in trying to use launchd for the same purpose) where, as you say, you can detect a change, but there's no way to tell *what* changed, or *where*, making it kind of pointless.

If it's a specific folder, I guess you can just have your application keep a list of the contents of that folder and periodically poll it (inefficient) or use the filesystem changed notification as a trigger of when to compare the folder contents.

Another way to go is to use AppleScript and Folder Actions. In theory these work wonderfully for dropbox type purposes, but I have found them to be extremely flakey and unreliable, and that unreliability dates back to the earliest days of OS X, so I would not hold my breath for that to improve soon.

HiRez
Feb 15, 2009, 05:54 PM
One more note, whatever method you use there is a "gotcha" you need to be aware of. When a file starts copying into your folder, that's generally when you'll get the notification or folder action triggered. If your files might be at all large (more than a few K), this can cause problems as your application or script may try to begin processing on files before they are fully copied. So you may need to start a loop when you detect a new file and then keep checking (polling) for the size increase of each new file, comparing it to the previous value, until the size stops increasing. Then you know your files are complete and can go to work on them, although if you have a bad or intermittent network connection that the files are coming in on, even this can be a little unreliable, and it also causes a delay in processing while you wait and check, which can be a problem if you need to do processing immediately.

robbieduncan
Feb 15, 2009, 05:55 PM
You could use UKKQueue (http://www.zathras.de/programming/sourcecode.htm#UKKQueue) which will give you a nice notification based mechanism to watch for changes at a specific path...

grimreaper1377
Feb 17, 2009, 09:52 AM
Thanks for your ideas guys. I wonder how dropbox does it:rolleyes:

lee1210
Feb 17, 2009, 09:56 AM
Thanks for your ideas guys. I wonder how dropbox does it:rolleyes:

per this thread:
http://forums.macrumors.com/showthread.php?t=575658

It sounds like "secret dropbox sauce". =)

It would certainly be possible to poll a particular folder, even if that seems costlier than the OS notifying you of a change. It might be possible to figure out if you got dropbox installed and took a look at "what's going on".

-Lee

iSee
Feb 17, 2009, 10:28 AM
If you're looking to do some C programming, I was reading about a low-level Mac OS API called FSEvent that will allow you to register for file system events on a particular path.

This is a new API for Leopard, though the underpinnings were in place for Tiger.

Here's the aricle: http://arstechnica.com/apple/reviews/2007/10/mac-os-x-10-5.ars/7

Here's the Apple API documentation:

http://developer.apple.com/documentation/Darwin/Conceptual/FSEvents_ProgGuide/UsingtheFSEventsFramework/chapter_4_section_1.html#//apple_ref/doc/uid/TP40005289-CH4-SW4

For a higher-level solution, you could look at folder actions.

robbieduncan
Feb 17, 2009, 10:33 AM
If you're looking to do some C programming, I was reading about a low-level Mac OS API called FSEvent that will allow you to register for file system events on a particular path.

The link I posted above is a nice, pre-written, tested Cocoa level wrapper round FSEvent. No C programming required :D

HiRez
Feb 18, 2009, 02:20 AM
You could use UKKQueue (http://www.zathras.de/programming/sourcecode.htm#UKKQueue) which will give you a nice notification based mechanism to watch for changes at a specific path...Thanks for that link, that is very cool and could come in handy.