Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomeu Vizoso <tomeu@tomeuvizoso.net>2008-10-09 08:33:46 (GMT)
committer Tomeu Vizoso <tomeu@tomeuvizoso.net>2008-10-09 08:33:46 (GMT)
commita94cb43e28fbb1d29dd761a1aea75aa9896c11cf (patch)
tree6098b16ce3aa95e84d2188a33f7ec73583f2ea8a
parent7656508515bb5a4569bac0cfc6e689fabaa640fd (diff)
When handing a file reference to a caller of get_file_name(), try a bit harder to get an unique name
-rw-r--r--src/olpc/datastore/filestore.py24
1 files changed, 17 insertions, 7 deletions
diff --git a/src/olpc/datastore/filestore.py b/src/olpc/datastore/filestore.py
index 9662885..e19b579 100644
--- a/src/olpc/datastore/filestore.py
+++ b/src/olpc/datastore/filestore.py
@@ -96,13 +96,15 @@ class FileStore(object):
destination_path = os.path.join(destination_dir, uid)
- # Try to make the original file readable. This can fail if the file is
- # in a FAT filesystem.
- try:
- os.chmod(file_path, 0604)
- except OSError, e:
- if e.errno != errno.EPERM:
- raise
+ attempt = 1
+ while os.path.exists(destination_path):
+ if attempt > 10:
+ destination_path = tempfile.mkstemp(prefix=uid,
+ dir=destination_dir)
+ else:
+ file_name = '%s_%s' % (uid, attempt)
+ destination_path = os.path.join(destination_dir, file_name)
+ attempt += 1
# Try to hard link from the original file to the targetpath. This can
# fail if the file is in a different filesystem. Do a symlink instead.
@@ -114,6 +116,14 @@ class FileStore(object):
else:
raise
+ # Try to make the original file readable. This can fail if the file is
+ # in a FAT filesystem.
+ try:
+ os.chmod(file_path, 0604)
+ except OSError, e:
+ if e.errno != errno.EPERM:
+ raise
+
return destination_path
def get_file_path(self, uid):