Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

My web-service running on nginx and php deliver's files using X-Accel-Redirect header to the client, if I use rm /my/folder/*.* how will it handle files currently being accessed/downloaded by clients?

share|improve this question

1 Answer 1

up vote 10 down vote accepted

When a process starts reading from a file, it creates a filehandle attached to the inode where the file lives.

When you delete a file, you delete the link from the directory to the inode. That link will be removed immediately, so that no other process will be able to find the file.

However, as long as there is an active filehandle to the inode, the contents will still be accessible from the process using that filehandle. Once all active processes with filehandles to that inode have closed their filehandles, the space where the file has resided will be marked as empty.

You can think of a filename as a kind of symlink.

  1. You have a file named /inodes/1234, and you symlink that to /myapp/myfile.txt
  2. You open /myapp/myfile.txt. What really happens is that you open /inodes/1234 because that is the actual file.
  3. You delete the symlink /myapp/myfile.txt. The original file /inodes/1234 still exist, and the process that opened it in step 2 is still reading/accessing it.
  4. Another process asks for /myapp/myfile.txt. It doesn't exist because you deleted the symlink. (The original /inodes/1234 still exists, of course!)
  5. The second process wants to create /myapp/myfile.txt. The operating system creates the file /inodes/4711 and creates a symlink to /myapp/myfile.txt

Of course it's slightly more complex than that, but it's a useful model in this instance.

Incidentally, this is also an answer to the question "why is my disk full even though I deleted this huge log file and there's nothing else taking up the space" - if a process was still reading from or writing to the file, you just made it invisible, you didn't actually clear it.

share|improve this answer
    
Say one file is being downloaded by client1 and I delete the file-link, what will happen if my php script checks for file existence while the other client is still downloading the file? Also, will I be able to re-create the file with same name and location while client1 download is active but the file-link is delted via rm? –  iTech Feb 7 at 8:42
    
I'll add that to my answer. –  Jenny D Feb 7 at 8:44
2  
@iTech: Why don't you test this yourself - it's easy to do. –  Iain Feb 7 at 8:46

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.