-2

I have encountered a problem that confused me for some time. In Linux, I logged in as a root user. Then I wrote a Java program that used RandomAccessFile to write data to disk first, then I deleted the file.

But the program continued to run without throwing any exception! But I couldn't find the written data even in the Linux PageCache! The following is the code snippet:

RandomAccessFile file =  new RandomAccessFile("/opt/temp.log", "rw");
     byte[] content = "Hello!!!\n".getBytes();
     for (int i = 0; i < 2; i++) {
         file.write(content, 0, content.length);
         Thread.sleep(3000);//just for lefting time to delete the "temp.log" file! 
     }

But if I change the file.write() method to file.writeBytes(), it regenerated the new file and wrote the data to the file. The following is the code snippet:

RandomAccessFile file =  new RandomAccessFile("/opt/temp.log", "rw");          
     for (int i = 0; i < 2; i++) {                                                                               
         file.writeBytes("Hello!!!\n");                                             
         Thread.sleep(3000);//just for lefting time to delete the "temp.log" file!                                                    
     }

What happened? Is there a bug in RandomAccessFile?

user207421
  • 305,947
  • 44
  • 307
  • 483
ronhunlam
  • 3
  • 5
  • 1
    *"But the program continued to run"* Of course it did, it's going to run for 1000 * 30s = **8 hours 20 minutes**. --- *"if I change the file.write method to file.writeBytes"* You can't just change the method name, you must have changed something else too, so please **edit** the question and show us the alternate code you wrote. – Andreas Jan 01 '20 at 03:34
  • Also: what is a PageCache and how did you "inspect" such a thing for your file data. – markspace Jan 01 '20 at 03:35
  • 1
    This is more about deleted files in Unix generally. – chrylis -cautiouslyoptimistic- Jan 01 '20 at 03:36
  • 1
    Also: what are you expecting to happen? The file data in memory and the file on disk is not perfectly synchronized. They're two different things and you can change one without affecting the other at all. So everything you said is perfectly reasonable, even though you seem surprised by it. So please tell what you were expecting to happen, why you expected and why it matters (what problem are you trying to solve?), otherwise I'm not sure anyone will understand what you are driving at. – markspace Jan 01 '20 at 03:37
  • My problem is why the RandomAccessFile doesn't write data to file if I deleted the file using `file.write(content, 0, content.length)` method but `file.writeBytes()` method does? – ronhunlam Jan 01 '20 at 03:53
  • @markspace the **PageCache** is the **Linux PageCache**, I used **vmtouch** to inspect the PageCache occupied by the `/opt/temp.log` file. – ronhunlam Jan 01 '20 at 03:59
  • 3
    The file handle is still open until you close it; when you delete a file that has a file handle open to it, it is [**not** actually deleted until the last open handle is closed](https://stackoverflow.com/a/2031100/2970947). – Elliott Frisch Jan 01 '20 at 04:21
  • 2
    ... but the directory entry is deleted, so you can't find it. However the program can keep writing to it. In Windows the delete would have failed as the file is open. – user207421 Jan 01 '20 at 05:30

1 Answers1

1

This is expected in POSIX/Unix systems and systems whose designs are heavily influenced by POSIX/Unix like Linux. A file in a Unix system will remain readable and writable as long as there's there's something referencing that file. A file can have multiple names (hard links) in the filesystem, but a process can also have an open file handle or a mmap on the file. The file won't really be freed by the system as long as at least one of those is still active.

Yes this means that it is still possible for a process to continue read and write to a file after you deleted it from the filesystem. For all intents and purposes, a file without a name is still a perfectly functional file and will still take space on the disk.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144