#36191: FileSystemStorage with allow_overwrite=True does not truncate previous
file
--------------------------------------+------------------------------------
Reporter: Gaƫl UTARD | Owner: (none)
Type: Bug | Status: new
Component: File uploads/storage | Version: 5.1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Jacob Walls):
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted
Comment:
Thanks for the report, reproduced by adjusting this existing test case:
{{{#!diff
diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py
index c048b8f071..b1dafd7cb9 100644
--- a/tests/file_storage/tests.py
+++ b/tests/file_storage/tests.py
@@ -617,7 +617,8 @@ class OverwritingStorageTests(FileStorageTests):
name = "test.file"
self.assertFalse(self.storage.exists(name))
content_1 = b"content one"
- content_2 = b"second content"
+ content_2 = b"overwrite"
+ assert len(content_1) > len(content_2), "Ensure truncation is
tested."
f_1 = ContentFile(content_1)
f_2 = ContentFile(content_2)
stored_name_1 = self.storage.save(name, f_1)
}}}
----
{{{#!py
======================================================================
FAIL: test_save_overwrite_behavior
(file_storage.tests.OverwritingStorageTests.test_save_overwrite_behavior)
Saving to same file name twice overwrites the first file.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/.../django/tests/file_storage/tests.py", line 633, in
test_save_overwrite_behavior
self.assertEqual(fp.read(), content_2)
AssertionError: b'overwritene' != b'overwrite'
----------------------------------------------------------------------
Ran 2 tests in 0.002s
FAILED (failures=1)
}}}
Test passes with the suggested patch:
{{{#!py
diff --git a/django/core/files/storage/filesystem.py
b/django/core/files/storage/filesystem.py
index b8de9b0a58..54c31e536a 100644
--- a/django/core/files/storage/filesystem.py
+++ b/django/core/files/storage/filesystem.py
@@ -113,7 +113,7 @@ class FileSystemStorage(Storage,
StorageSettingsMixin):
| getattr(os, "O_BINARY", 0)
)
if self._allow_overwrite:
- open_flags = open_flags & ~os.O_EXCL
+ open_flags = open_flags & ~os.O_EXCL | os.O_TRUNC
fd = os.open(full_path, open_flags, 0o666)
_file = None
try:
}}}
----
Would you like to submit a PR?
--
Ticket URL: <https://code.djangoproject.com/ticket/36191#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/010701950a10fbe8-7ad1e56b-edc0-4e5a-8d48-e17301dc958e-000000%40eu-central-1.amazonses.com.