SFTP file locking mechanism

How can I make sure that a file downloaded via SFTP (on the Linux base system) remains locked during transmission, so the automatic system will not read it?

Is there an option on the client side? Or server side?

+4
locking sftp transfer
source share
2 answers

SFTP supports blocking from version 5. See specification .

You did not specify which SFTP server you are using. Therefore, I assume the most common, OpenSSH. OpenSSH only supports SFTP version 3, so it does not support blocking.

In any case, even if your server supports file locking, most SFTP clients / libraries do not support SFTP version 5. Or even if they do, they will not support the lock function. Note that the lock is explicit, the client must request it.

There are some general solutions to the problem:

  • As suggested by @ user1717259, you can download the client "made" file after the download is complete. Make the automatic system wait for the "done" file to appear.
  • You may have a dedicated โ€œdownloadโ€ folder and the client (atomically) moves the downloaded file to the โ€œdoneโ€ folder. Make an automatic system only in the "done" folder.
  • Have a file naming convention for the uploaded files (".filepart") and ask the client (atomically) to rename the file after downloading to its final name. Make the automatic system ignore the ".filepart" files.
    See (My) article Lock files when uploading / downloading to a temporary file name , for example, when implementing this approach.
  • Gross hacking is to periodically check file attributes (size and time) and consider a completed download if the attributes have not changed over a period of time.
+9
source share

A typical way to solve this problem is to upload your real file and then download the empty done.txt file.

The automated system must wait for the "done" file to appear before attempting to read the real file.

+7
source share

All Articles