Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fixed #18233: Raise IOError if destination file already exists, in django.core.files.move.file_move_safe. #18

Closed
wants to merge 2 commits into from

4 participants

Aviral Dasgupta Gabriel Falcão Anssi Kääriäinen Jannis Leidel
Aviral Dasgupta

On certain platforms like Linux, the existing code will simply ignore allow_overwrite and go ahead and overwrite the destination file anyway. This fixes it.

ps. This is also my first contribution to Django :)

Gabriel Falcão

Great work with the individual commit with tests !
:+1:

Jannis Leidel jezdez commented on the diff May 03, 2012
django/core/files/move.py
@@ -51,6 +51,8 @@ def file_move_safe(old_file_name, new_file_name, chunk_size = 1024*64, allow_ove
51 51
         return
52 52
 
53 53
     try:
  54
+        if not allow_overwrite and os.path.exists(new_file_name):
  55
+            raise IOError('Destination file already exists: \'{0}\''.format(new_file_name))
1
Jannis Leidel Owner
jezdez added a note May 03, 2012

It's best practice to use common string substitution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Anssi Kääriäinen
Owner

I am closing this pull request as the ticket discussions indicate this isn't 100% ready for commit.

Anssi Kääriäinen akaariai closed this July 16, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

May 01, 2012
Aviral Dasgupta Fixed #18233: Raise IOError if destination file already exists, in
django.core.files.move.file_move_safe.
68ee929
Aviral Dasgupta Added regression test for #18233. 872b976
This page is out of date. Refresh to see the latest.
1  AUTHORS
@@ -563,6 +563,7 @@ answer newbie questions, and generally made Django that much better:
563 563
     Gasper Zejn <zejn@kiberpipa.org>
564 564
     Jarek Zgoda <jarek.zgoda@gmail.com>
565 565
     Cheng Zhang
  566
+    Aviral Dasgupta <aviraldg@gmail.com>
566 567
 
567 568
 A big THANK YOU goes to:
568 569
 
2  django/core/files/move.py
@@ -51,6 +51,8 @@ def file_move_safe(old_file_name, new_file_name, chunk_size = 1024*64, allow_ove
51 51
         return
52 52
 
53 53
     try:
  54
+        if not allow_overwrite and os.path.exists(new_file_name):
  55
+            raise IOError('Destination file already exists: \'{0}\''.format(new_file_name))
54 56
         os.rename(old_file_name, new_file_name)
55 57
         return
56 58
     except OSError:
22  tests/regressiontests/file_storage/tests.py
@@ -23,6 +23,7 @@
23 23
 from django.core.files.images import get_image_dimensions
24 24
 from django.core.files.storage import FileSystemStorage, get_storage_class
25 25
 from django.core.files.uploadedfile import UploadedFile
  26
+from django.core.files.move import file_move_safe
26 27
 from django.test import SimpleTestCase
27 28
 from django.utils import unittest
28 29
 from ..servers.tests import LiveServerBase
@@ -586,3 +587,24 @@ def test_urllib2_urlopen(self):
586 587
         remote_file = self.urlopen('/example_view/')
587 588
 
588 589
         self.assertEqual(stored_file.read(), remote_file.read())
  590
+
  591
+class MoveTestCase(unittest.TestCase):
  592
+    """
  593
+    Test django.core.files.move.
  594
+    """
  595
+    def setUp(self):
  596
+        self.file_a = tempfile.mkstemp()
  597
+        self.file_b = tempfile.mkstemp()
  598
+
  599
+    def tearDown(self):
  600
+        os.close(self.file_a[0])
  601
+        os.close(self.file_b[0])
  602
+        os.remove(self.file_a[1])
  603
+        os.remove(self.file_b[1])
  604
+
  605
+    def test_overwrite_existing_file(self):
  606
+        """
  607
+        Test that file_move_safe raises an IOError if you try to overwrite an
  608
+        existing file without passing in allow_overwrite=True.
  609
+        """
  610
+        self.assertRaises(IOError, file_move_safe, self.file_a[1], self.file_b[1])
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.