From: Sascha Silbe <sascha@silbe.org>
Subject: [PATCH] fix file descriptor leak in filestore.retrieve() and use only mkstemp()
Fix file descriptor leak in filestore.retrieve() and use only mkstemp().
Signed-off-by: Sascha Silbe <sascha@silbe.org>
---
src/carquinyol/filestore.py | 19 ++++---------------
1 files changed, 4 insertions(+), 15 deletions(-)
diff --git a/src/carquinyol/filestore.py b/src/carquinyol/filestore.py
index 5a90a8e..1e2949b 100644
a
|
b
|
class FileStore(object): |
110 | 110 | elif extension: |
111 | 111 | extension = '.' + extension |
112 | 112 | |
113 | | destination_path = os.path.join(destination_dir, uid + extension) |
114 | | |
115 | | attempt = 1 |
116 | | while os.path.exists(destination_path): |
117 | | if attempt > 10: |
118 | | fd_, destination_path = tempfile.mkstemp(prefix=uid, |
119 | | suffix=extension, |
120 | | dir=destination_dir) |
121 | | del fd_ |
122 | | os.unlink(destination_path) |
123 | | break |
124 | | else: |
125 | | file_name = '%s_%s%s' % (uid, attempt, extension) |
126 | | destination_path = os.path.join(destination_dir, file_name) |
127 | | attempt += 1 |
| 113 | fd, destination_path = tempfile.mkstemp(prefix=uid, suffix=extension, |
| 114 | dir=destination_dir) |
| 115 | os.close(fd) |
| 116 | os.unlink(destination_path) |
128 | 117 | |
129 | 118 | # Try to hard link from the original file to the targetpath. This can |
130 | 119 | # fail if the file is in a different filesystem. Do a symlink instead. |