When pasting a file “over” another file with the same name, is there a brief moment during which neither file exists

filesystems

This used to worry me whenever I was replacing static HTML files in a shared web hosting environment.

When pasting (or FTPing) a file "over" another file with the same name, is there a brief moment during which neither file exists?

If I have index.html on a web server, and I paste a new index.html over it, is there a chance that users requesting that file will receive a 404 error?

Best Answer

  • It really depends from two things: How the file is being replaced and what the operating system underlying the WWW server is. Here are three examples to show how this makes a difference:

    • A POSIX-conformant operating system using mv to update the file from a pre-prepared temporary file: mv is guaranteed to call (or behave like) rename(), which in turn, per the POSIX specification, guarantees that at all times the target filename, in this case index.html, will always reference some file, be it either the original or the new file. And the file contents will never be in a partially-written state.

      The same holds for an FTP/HTTP server that uploads to a temporary file and that then calls rename(), when the temporary file is completely uploaded, to move the temporary file to its specified destination.

    • Microsoft Windows using a MOVE command of some sort to update the file a pre-prepared temporary file: The usual implementation of a MOVE command (as can be seen here in the source for the ReactOS MOVE command) is to call MoveFileEx() with the MOVEFILE_REPLACE_EXISTING flag set on. This, at least on Windows NT (because the flag is passed down to the filesystem drivers which are required to support atomic renames), guarantees that a file named index.html will always exist, as with POSIX and rename().

      The same holds for an FTP/HTTP server that uploads to a temporary file and that then calls MoveFileEx(), when the temporary file is completely uploaded, to move the temporary file to its specified destination.

    • Microsoft Windows using a COPY command of some sort to update the file from a pre-prepared temporary file: The COPY command does not use MoveFileEx(). Rather, it opens the existing file, truncating it to zero length, and rewrites it in place (again, c.f. CopyFileEx() in ReactOS). Although there will never be a point where the name index.html doesn't point to a file, the file that it points to will be in a partially-written state during the copy process, and it is possible that the WWW server will serve up truncated versions of this file as a consequence.

      The same holds for an FTP/HTTP server that either (a) just uploads files in place, truncating and overwriting the originals, or (b) uploads to a temporary file but then copies the temporary file to its specified destination.

    In short: If you are uploading via an FTP/HTTP server, it depends from how your FTP/HTTP server works internally. If you are directly modifying the WWW server's file storage area, then it depends from what tools you use to do that.