| OLD | NEW |
| 1 # Copyright 2013 The LUCI Authors. All rights reserved. | 1 # Copyright 2013 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """Provides functions: get_native_path_case(), isabs() and safe_join(). | 5 """Provides functions: get_native_path_case(), isabs() and safe_join(). |
| 6 | 6 |
| 7 This module assumes that filesystem is not changing while current process | 7 This module assumes that filesystem is not changing while current process |
| 8 is running and thus it caches results of functions that depend on FS state. | 8 is running and thus it caches results of functions that depend on FS state. |
| 9 """ | 9 """ |
| 10 | 10 |
| (...skipping 780 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 791 up. | 791 up. |
| 792 """ | 792 """ |
| 793 assert path and path[-1] != os.sep, path | 793 assert path and path[-1] != os.sep, path |
| 794 path = os.path.abspath(path) | 794 path = os.path.abspath(path) |
| 795 dir_name, base_name = os.path.split(path) | 795 dir_name, base_name = os.path.split(path) |
| 796 | 796 |
| 797 fd, tmp_name = tempfile.mkstemp(dir=dir_name, prefix=base_name+'_') | 797 fd, tmp_name = tempfile.mkstemp(dir=dir_name, prefix=base_name+'_') |
| 798 try: | 798 try: |
| 799 with os.fdopen(fd, 'wb') as f: | 799 with os.fdopen(fd, 'wb') as f: |
| 800 f.write(body) | 800 f.write(body) |
| 801 f.flush() |
| 802 os.fsync(fd) |
| 801 if sys.platform != 'win32': | 803 if sys.platform != 'win32': |
| 802 os.rename(tmp_name, path) | 804 os.rename(tmp_name, path) |
| 803 else: | 805 else: |
| 804 # Flags are MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH. | 806 # Flags are MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH. |
| 805 MoveFileEx(unicode(tmp_name), unicode(path), 0x1|0x8) | 807 MoveFileEx(unicode(tmp_name), unicode(path), 0x1|0x8) |
| 806 tmp_name = None # no need to remove it in 'finally' block anymore | 808 tmp_name = None # no need to remove it in 'finally' block anymore |
| 807 finally: | 809 finally: |
| 808 if tmp_name: | 810 if tmp_name: |
| 809 try: | 811 try: |
| 810 os.remove(tmp_name) | 812 os.remove(tmp_name) |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1025 errors = [] | 1027 errors = [] |
| 1026 fs.rmtree(root, onerror=lambda *args: errors.append(args)) | 1028 fs.rmtree(root, onerror=lambda *args: errors.append(args)) |
| 1027 if errors: | 1029 if errors: |
| 1028 # There's no hope. | 1030 # There's no hope. |
| 1029 sys.stderr.write( | 1031 sys.stderr.write( |
| 1030 'Failed to delete %s. The following files remain:\n' % root) | 1032 'Failed to delete %s. The following files remain:\n' % root) |
| 1031 for _, path, _ in errors: | 1033 for _, path, _ in errors: |
| 1032 sys.stderr.write('- %s\n' % path) | 1034 sys.stderr.write('- %s\n' % path) |
| 1033 raise errors[0][2][0], errors[0][2][1], errors[0][2][2] | 1035 raise errors[0][2][0], errors[0][2][1], errors[0][2][2] |
| 1034 return False | 1036 return False |
| OLD | NEW |