Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
Amazing. Thank you! –  Closure Cowboy Mar 20 '11 at 0:45

The file will be there still, but if they request it at the wrong time it may be in an intermediate state (empty or truncated).

If the program that handles the file upload is smart enough, it may upload to a new name and then replace the old file. If so, there will indeed be a very brief interval where the name index.html doesn't exist but the temporary file name or a backup of the previous version of the file exists. (The former may be in a temporary directory and therefore not visible from the outside.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.