summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGaël Utard <gael.utard@eedf.fr>2025-02-15 15:55:33 +0100
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-02-17 14:01:00 +0100
commit0d1dd6bba0c18b7feb6caa5cbd8df80fbac54afd (patch)
tree2105176ab2903fe8a7dfc5cef82d8340f2f96c89
parent2fa1f99ab49ae5b6bc0a15ed9398d6fa2996f02a (diff)
Fixed #36191 -- Truncated the overwritten file content in FileSystemStorage.
-rw-r--r--AUTHORS1
-rw-r--r--django/core/files/storage/filesystem.py2
-rw-r--r--docs/releases/5.1.7.txt4
-rw-r--r--tests/file_storage/tests.py12
4 files changed, 18 insertions, 1 deletions
diff --git a/AUTHORS b/AUTHORS
index e3389edbf5..b816c305e5 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -369,6 +369,7 @@ answer newbie questions, and generally made Django that much better:
Fraser Nevett <mail@nevett.org>
Gabriel Grant <g@briel.ca>
Gabriel Hurley <gabriel@strikeawe.com>
+ Gaël Utard
gandalf@owca.info
Garry Lawrence
Garry Polley <garrympolley@gmail.com>
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:
diff --git a/docs/releases/5.1.7.txt b/docs/releases/5.1.7.txt
index e184da6aca..deda4f2f92 100644
--- a/docs/releases/5.1.7.txt
+++ b/docs/releases/5.1.7.txt
@@ -12,3 +12,7 @@ Bugfixes
* Fixed a bug in Django 5.1 where the ``{% querystring %}`` template tag
returned an empty string rather than ``"?"`` when all parameters had been
removed from the query string (:ticket:`36182`).
+
+* Fixed a bug in Django 5.1 where ``FileSystemStorage``, with
+ ``allow_overwrite`` set to ``True``, did not truncate the overwritten file
+ content (:ticket:`36191`).
diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py
index c048b8f071..70c7a4f15f 100644
--- a/tests/file_storage/tests.py
+++ b/tests/file_storage/tests.py
@@ -634,6 +634,18 @@ class OverwritingStorageTests(FileStorageTests):
finally:
self.storage.delete(name)
+ def test_save_overwrite_behavior_truncate(self):
+ name = "test.file"
+ original_content = b"content extra extra extra"
+ new_smaller_content = b"content"
+ self.storage.save(name, ContentFile(original_content))
+ try:
+ self.storage.save(name, ContentFile(new_smaller_content))
+ with self.storage.open(name) as fp:
+ self.assertEqual(fp.read(), new_smaller_content)
+ finally:
+ self.storage.delete(name)
+
def test_save_overwrite_behavior_temp_file(self):
"""Saving to same file name twice overwrites the first file."""
name = "test.file"