0

I have the following situation here. My OS shows that django TemporaryUploadedFile which I got via the POST request does not exist anymore but somehow this uploaded file can be read. Here is the code

text_file = request.FILES['text_file']

print(text_file.temporary_file_path())
os.system('ls -l ' + text_file.temporary_file_path())

fs = FileSystemStorage()
file_new =fs.save(text_file.name, text_file)

print(text_file.temporary_file_path())
os.system('ls -l ' + text_file.temporary_file_path())

fs.delete(file_new)

for chunk in text_file.chunks():
    text += chunk.decode(encoding)

print('Got text OK.')

This gives the following output:

/tmp/tmp0tngal9t.upload foo.txt
-rw------- 1 mine machine 3072889 oct 18 19:29 /tmp/tmp0tngal9t.upload

/tmp/tmp0tngal9t.upload foo.txt
ls: cannot access '/tmp/tmp0tngal9t.upload': No such file or directory

Got text OK.

So TemporaryUploadedFile is disappeared after it was saved to file_new which later is also deleted. Anyway text_file is successfully read by chunks and I get all the text from uploaded foo.txt file. How it is possible? From where text_file.chunks() gets the data if text_file does not exist anymore?

I use:

  • python 3.5.2
  • django 1.10.2
  • ubuntu 16.04.1
Mikhail Geyer
  • 881
  • 2
  • 9
  • 27

1 Answers1

0

I found out that this problem still remains for bare python, so it is not particularly related to django as in this example I just read text_file which were open in request.FILES['text_file'].

I re-asked the similar question here focusing on python only. It turned out that the problem is not so related with python either, but with Linux/Unix system file management. I quote here the answer of Jean-François Fabre:

Nothing to do with Python. In C, Fortran, or Visual Cobol you'd have the same behaviour as long as the code gets its handle from open system call.

On Linux/Unix systems, once a process has a handle on a file, it can read it, even if the file is deleted. For more details check that question (I wasn't sure if it was OK to do that, it seems to be)

On Windows you just wouldn't be able to delete the file as long as it's locked by a process.

Community
  • 1
  • 1
Mikhail Geyer
  • 881
  • 2
  • 9
  • 27