Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#20660 closed Bug (fixed)

Filefield.delete() on empty field delete MEDIA_ROOT

Reported by: stanislas.guerra@… Owned by: Claude Paroz
Component: File uploads/storage Version: dev
Severity: Normal Keywords: FileField delete
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Maybe this is because I use a symlink in my Apache configuration to serve the media :

$ ls -l /var/www/mysite.domain.com/documents/
-rw-r--r-- 1 www-data www-data  474  1 févr. 15:27 maintenance.html
lrwxrwxrwx 1 root     root       20 26 juin  15:02 media -> /data/media_mysite
drwxr-xr-x 6 myuser www-data 4096 16 mai   12:01 static



$ ls -l /data/media_mysite
total 8
drwxrwsr-x 3 myuser www-data 4096 13 févr. 10:13 stuffs
drwxrwsr-x 3 myuser www-data 4096 13 févr. 10:00 other_stuffs

But when I call delete() on an ImageField, if that very field is empty, the symlink is wipped out!

$ python manage.py shell
In [1]: from myproject.myapp.models import MyModel
In [2]: obj = MyModel.objects.get(euid="21439011")
In [3]: obj.my_file
Out[3]: <FieldFile: None>
In [4]: obj.my_file.delete()


$ ls -l /var/www/mysite.domain.com/documents/
-rw-r--r-- 1 www-data www-data  474  1 févr. 15:27 maintenance.html
drwxr-xr-x 6 myproject www-data 4096 16 mai   12:01 static

Should not an exception be raised here instead ?

Change History (9)

comment:1 by stanislas.guerra@…, 12 years ago

I should have mention that MEDIA_ROOT is not deleted (only the symlink is.)

comment:2 by Claude Paroz, 12 years ago

Triage Stage: UnreviewedAccepted
Version: 1.4master

I could reproduce this. And yes, the deletion succeeds only when MEDIA_ROOT is a symlink (when it's a real directory, it will fail with OSError: [Errno 21] Is a directory: ... at least on Linux).

I tend to think there are two bugs:

  • FieldFile should not call storage.delete when self._file is None
  • FileSystemStorage should not try to remove a path when its delete method receives '' as argument.

comment:3 by Claude Paroz, 12 years ago

Owner: changed from nobody to Claude Paroz
Status: newassigned

comment:5 by Baptiste Mispelon, 12 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Claude Paroz <claude@…>, 12 years ago

Resolution: fixed
Status: assignedclosed

In ea3fe78a9d742904f6902cdc353a11d795418105:

Fixed #20660 -- Do not try to delete an unset FieldFile

Thanks stanislas.guerra at gmail.com for the report and
Baptiste Mispelon for the review.

comment:7 by Claude Paroz <claude@…>, 12 years ago

In 7fbab3ebaf8b60bbe847b772f895df47067a60d3:

Do not allow FileSystemStorage.delete to receive an empty name

Refs #20660.

comment:8 by Claude Paroz <claude@…>, 12 years ago

In b6aed803b20cc7898a82fd65845e97676276f3fa:

[1.6.x] Fixed #20660 -- Do not try to delete an unset FieldFile

Thanks stanislas.guerra at gmail.com for the report and
Baptiste Mispelon for the review.
Backport of ea3fe78a9d from master.

comment:9 by Claude Paroz <claude@…>, 12 years ago

In a9b5a1e506a9e8492407399b8bec1c2a8420be60:

[1.6.x] Do not allow FileSystemStorage.delete to receive an empty name

Refs #20660.
Backport of 7fbab3eba from master.

Note: See TracTickets for help on using tickets.
Back to Top