2

I am writing an application in objective c, one of the functions of the application is that it monitors a local folder for new files. The local folder is shared for other users, so anyone is able to put (usually files around 5gb) on there. Im looking for a way to interact with the new files that are put on the shared folder. These files have to be done copying before the applications tries to access them.

I've tried to monitor the size of the new file, the problem is that these files are also copied over wireless, so in case of a hickup the size stays the same and the application will already start interacting with the file.

The client side that puts files on the share is not running any copy applications, just finder or windows explorer. So there is no way to work with lock files or anything like that.

How is for instance Dropbox doing this?

  • How are you monitoring the directory for new files? – mttrb May 04 '14 at 14:19
  • The usual way to deal with this is to rename the files after they're completely written. I believe there's are some UNIX features that facilitate this, though I don't know if they map into Objective-C well. – Hot Licks May 04 '14 at 14:36
  • have a look to this answer, it might help you. – BoilingLime May 04 '14 at 14:48
  • Maybe [`NSFileManager`](https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/nsfilemanager_class/reference/reference.html#//apple_ref/doc/constant_group/File_Attribute_Keys)'s attribute key `NSString * const NSFileBusy` may be of use for you? – HAS May 05 '14 at 06:59

1 Answers1

0

Apparently you can use the shell command:

lsof -Fc path/to/thefile

and that will tell you which processes have the file open. See this thread for more info: Detect file in use by other process

Community
  • 1
  • 1
Elias
  • 1,367
  • 11
  • 25
  • Thanks Elias, that seems to work! At the moment im running a second thread to constantly check if there are new files in the directory. After that add it to the array and for each file in the array it will give the output of lsof. Still looking for a more efficient way to do this. – Alvin Rotteveel May 04 '14 at 18:09
  • It looks like lsof needs to run as sudo. I don't think there is a way to run the application without user interaction (to allow a task to run as sudo). – Alvin Rotteveel May 04 '14 at 19:40