How does sftp replace files

sftpupload

For example I have some http static server. I'm working with .js files via sftp. Keeping them sync.
I'm wondering if .js file is replaced instantly or if the stfp server will write into that file as a stream?

So for example:

  • client started uploading file
  • read stream opened
  • server accept that request
  • open write stream into file location
  • chunks sending to the server
  • chunks accepted and piping into write stream
  • http static server is trying to access file which is not yet uploaded completely and pick partially uploaded file?
  • the result = js errors on the web page

or

  • sftp load the whole file into the memory
  • then dump whole file from memory into the file

This is the best scenario for me.

Haven't time to check how it's working. Any explanation would be good.

Best Answer

scp and sftp will replace the file in-place, so yes, theoretically an HTTP client could try to fetch a partially-uploaded file.

You can mitigate this by using rsync over SSH instead of sftp as this will write to a temporary file first and then replace the file. rsync can also be configured to backup any files it replaces.