=== modified file 'django/core/files/move.py'
|
|
|
10 | 10 | |
11 | 11 | __all__ = ['file_move_safe'] |
12 | 12 | |
13 | | try: |
14 | | import shutil |
15 | | file_move = shutil.move |
16 | | except ImportError: |
17 | | file_move = os.rename |
18 | | |
19 | 13 | def file_move_safe(old_file_name, new_file_name, chunk_size = 1024*64, allow_overwrite=False): |
20 | 14 | """ |
21 | 15 | Moves a file from one location to another in the safest way possible. |
… |
… |
|
33 | 27 | if old_file_name == new_file_name: |
34 | 28 | return |
35 | 29 | |
36 | | if not allow_overwrite and os.path.exists(new_file_name): |
37 | | raise IOError("Cannot overwrite existing file '%s'." % new_file_name) |
38 | | |
39 | 30 | try: |
40 | | file_move(old_file_name, new_file_name) |
| 31 | os.rename(old_file_name, new_file_name) |
41 | 32 | return |
42 | 33 | except OSError: |
43 | 34 | # This will happen with os.rename if moving to another filesystem |
| 35 | # or when moving opened files on certain operating systems |
44 | 36 | pass |
45 | 37 | |
46 | | # If the built-in didn't work, do it the hard way. |
47 | | fd = os.open(new_file_name, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, 'O_BINARY', 0)) |
| 38 | # first open the old file, so that it won't go away |
| 39 | old_file = open(old_file_name, 'rb') |
48 | 40 | try: |
49 | | locks.lock(fd, locks.LOCK_EX) |
50 | | old_file = open(old_file_name, 'rb') |
51 | | current_chunk = None |
52 | | while current_chunk != '': |
53 | | current_chunk = old_file.read(chunk_size) |
54 | | os.write(fd, current_chunk) |
| 41 | # now open the new file, not forgetting allow_overwrite |
| 42 | fd = os.open(new_file_name, os.O_WRONLY | os.O_CREAT | getattr(os, 'O_BINARY', 0) | |
| 43 | (not allow_overwrite and os.O_EXCL or 0)) |
| 44 | try: |
| 45 | locks.lock(fd, locks.LOCK_EX) |
| 46 | current_chunk = None |
| 47 | while current_chunk != '': |
| 48 | current_chunk = old_file.read(chunk_size) |
| 49 | os.write(fd, current_chunk) |
| 50 | finally: |
| 51 | locks.unlock(fd) |
| 52 | os.close(fd) |
55 | 53 | finally: |
56 | | locks.unlock(fd) |
57 | | os.close(fd) |
58 | 54 | old_file.close() |
59 | 55 | |
60 | | os.remove(old_file_name) |
| 56 | try: |
| 57 | os.remove(old_file_name) |
| 58 | except OSError, e: |
| 59 | # Certain operating systems (Cygwin and Windows) |
| 60 | # fail when deleting opened files, ignore it |
| 61 | if getattr(e, 'winerror', 0) != 32: |
| 62 | # FIXME: should we also ignore errno 13? |
| 63 | raise |
=== modified file 'django/core/files/temp.py'
|
|
|
10 | 10 | """ |
11 | 11 | |
12 | 12 | import os |
| 13 | import sys |
13 | 14 | import tempfile |
14 | 15 | |
15 | 16 | __all__ = ('NamedTemporaryFile', 'gettempdir',) |
16 | 17 | |
17 | | if os.name == 'nt': |
| 18 | if os.name == 'nt' or sys.platform == 'cygwin': |
18 | 19 | class TemporaryFile(object): |
19 | 20 | """ |
20 | 21 | Temporary file object constructor that works in Windows and supports |
… |
… |
|
25 | 26 | fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix, |
26 | 27 | dir=dir) |
27 | 28 | self.name = name |
28 | | self._file = os.fdopen(fd, mode, bufsize) |
| 29 | self.file = os.fdopen(fd, mode, bufsize) |
| 30 | self.close_called = False |
| 31 | |
| 32 | # Because close can be called during shutdown |
| 33 | # we need to cache os.unlink and access it |
| 34 | # as self.unlink only |
| 35 | unlink = os.unlink |
| 36 | |
| 37 | def close(self): |
| 38 | if not self.close_called: |
| 39 | self.close_called = True |
| 40 | try: |
| 41 | self.file.close() |
| 42 | except (OSError, IOError): |
| 43 | pass |
| 44 | try: |
| 45 | self.unlink(self.name) |
| 46 | except (OSError): |
| 47 | pass |
29 | 48 | |
30 | 49 | def __del__(self): |
31 | | try: |
32 | | self._file.close() |
33 | | except (OSError, IOError): |
34 | | pass |
35 | | try: |
36 | | os.unlink(self.name) |
37 | | except (OSError): |
38 | | pass |
39 | | |
40 | | try: |
41 | | super(TemporaryFile, self).__del__() |
42 | | except AttributeError: |
43 | | pass |
44 | | |
45 | | |
46 | | def read(self, *args): return self._file.read(*args) |
47 | | def seek(self, offset): return self._file.seek(offset) |
48 | | def write(self, s): return self._file.write(s) |
49 | | def close(self): return self._file.close() |
50 | | def __iter__(self): return iter(self._file) |
51 | | def readlines(self, size=None): return self._file.readlines(size) |
52 | | def xreadlines(self): return self._file.xreadlines() |
| 50 | self.close() |
| 51 | |
| 52 | def read(self, *args): return self.file.read(*args) |
| 53 | def seek(self, offset): return self.file.seek(offset) |
| 54 | def write(self, s): return self.file.write(s) |
| 55 | def __iter__(self): return iter(self.file) |
| 56 | def readlines(self, size=None): return self.file.readlines(size) |
| 57 | def xreadlines(self): return self.file.xreadlines() |
53 | 58 | |
54 | 59 | NamedTemporaryFile = TemporaryFile |
55 | 60 | else: |