Is there a better way to find out if a file is being written?

We have a FreeBSD server with samba, in which employees copy image files, which are then uploaded to our web servers (thus, they do not need to contact ftp). Sometimes, if a script is loaded at the same time as copying files, it may download an incomplete file.

We fixed this by getting a list of files along with file sizes, then waiting 5 seconds and double-checking file sizes. If the sizes match, then saving them for loading, if they do not match, is checked again after another 5 seconds.

This seems like a weird way to check if files are being written. Is there a better, easier way to do this?

+6
source share
3 answers

Use the flock function http://php.net/flock - when recording a file, get an exclusive lock flock($handle, LOCK_EX) after it is released lock flock($handle, LOCK_UN) .

Downloading the script may also try to get an exceptional write lock, if it succeeds, it is ok to move the file, otherwise not.

EDIT: Sorry, I forgot that users copy files to the server via samba ... Thus, there is no place to use flock when copying ... But loading the script can use flock($handle, LOCK_EX) to see if successful or not.

+2
source

I recommend shell_exec() smbstatus (1) for example. smbstatus -LB to check for locked files

+2
source

Write a script to copy the files to a temporary folder on the Samba server, and then, when they are completely copied and reset, move (for example, disconnect / move, and not copy again) them to the download folder.

0
source

Source: https://habr.com/ru/post/914907/


All Articles