Revision a9d68e40 lib/utils/io.py
b/lib/utils/io.py | ||
---|---|---|
117 | 117 |
if backup and not dry_run and os.path.isfile(file_name): |
118 | 118 |
CreateBackup(file_name) |
119 | 119 |
|
120 |
dir_name, base_name = os.path.split(file_name) |
|
121 |
fd, new_name = tempfile.mkstemp('.new', base_name, dir_name) |
|
120 |
# Whether temporary file needs to be removed (e.g. if any error occurs) |
|
122 | 121 |
do_remove = True |
123 |
# here we need to make sure we remove the temp file, if any error |
|
124 |
# leaves it in place |
|
122 |
|
|
123 |
# Function result |
|
124 |
result = None |
|
125 |
|
|
126 |
(dir_name, base_name) = os.path.split(file_name) |
|
127 |
(fd, new_name) = tempfile.mkstemp(suffix=".new", prefix=base_name, |
|
128 |
dir=dir_name) |
|
125 | 129 |
try: |
126 |
if uid != -1 or gid != -1: |
|
127 |
os.chown(new_name, uid, gid) |
|
128 |
if mode: |
|
129 |
os.chmod(new_name, mode) |
|
130 |
if callable(prewrite): |
|
131 |
prewrite(fd) |
|
132 |
if data is not None: |
|
133 |
os.write(fd, data) |
|
134 |
else: |
|
135 |
fn(fd) |
|
136 |
if callable(postwrite): |
|
137 |
postwrite(fd) |
|
138 |
os.fsync(fd) |
|
139 |
if atime is not None and mtime is not None: |
|
140 |
os.utime(new_name, (atime, mtime)) |
|
130 |
try: |
|
131 |
if uid != -1 or gid != -1: |
|
132 |
os.chown(new_name, uid, gid) |
|
133 |
if mode: |
|
134 |
os.chmod(new_name, mode) |
|
135 |
if callable(prewrite): |
|
136 |
prewrite(fd) |
|
137 |
if data is not None: |
|
138 |
os.write(fd, data) |
|
139 |
else: |
|
140 |
fn(fd) |
|
141 |
if callable(postwrite): |
|
142 |
postwrite(fd) |
|
143 |
os.fsync(fd) |
|
144 |
if atime is not None and mtime is not None: |
|
145 |
os.utime(new_name, (atime, mtime)) |
|
146 |
finally: |
|
147 |
# Close file unless the file descriptor should be returned |
|
148 |
if close: |
|
149 |
os.close(fd) |
|
150 |
else: |
|
151 |
result = fd |
|
152 |
|
|
153 |
# Rename file to destination name |
|
141 | 154 |
if not dry_run: |
142 | 155 |
os.rename(new_name, file_name) |
156 |
# Successful, no need to remove anymore |
|
143 | 157 |
do_remove = False |
144 | 158 |
finally: |
145 |
if close: |
|
146 |
os.close(fd) |
|
147 |
result = None |
|
148 |
else: |
|
149 |
result = fd |
|
150 | 159 |
if do_remove: |
151 | 160 |
RemoveFile(new_name) |
152 | 161 |
|
Also available in: Unified diff