Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if file is being copied under macOS

Tags:

file

macos

fuser

Users on our network copy files on the server in a directory called "DropBox" with AFP connection, simply dragging them with the Finder.

A script running on the server checks periodically for the presence of new files inside "DropBox" and then moves them with mv into other directories.

How can the script check if a file is being copied (and wait for the process to complete before moving it away)?

I've tried with fuser filename with no success. If the file copy is issued by a remote machine fuser reports that no process is using the file.

like image 750
Paolo Avatar asked Nov 03 '22 18:11

Paolo


1 Answers

There are two approaches to this problem:

1. flag the filename to tell the file is partial:

When a file is written (moved/copied) into the DropBox .part extension is added to the name. When the writing is over the .part extension is removed. Other processes operate on files only if the .part extension is not present.

2. check the file size; if it's constant then the write operation is ended (maybe)

This is less accurate and more complicated that (1) but offer a good guess if option 1 is not viable for whatever reason. For each file in the dropbox the time the size isn't changed is recorded. When a file increases in size the time value is reset to zero. When a file is not being written the time values remains constant. If a file isn't grown for at least (30 seconds, 1 minute, the more time you can afford the better it is of course) then write operation are over and the file can be managed. This approach fails when a transfer in interrupted.

like image 83
Paolo Avatar answered Nov 09 '22 08:11

Paolo