From dce9c18d56db8c9d07f08526aaca36789eda816b Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Thu, 20 Sep 2007 10:21:11 +0000 Subject: Add a suggested_filename property which the journal can use to suggest a filename when copying on an USB stick. Using the filelike is not good because it would require a file copy. And the filename property conflicts with the import code. Maybe we can figure out a better way to do it after trial-3. This is all internal so it shouldn't cause any format compatibility issue. --- diff --git a/NEWS b/NEWS index eed9446..09766c3 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,5 @@ +* #3469 Human readable names on USB. (marco) + Snapshot 11013dc3ca * #3599 Fix the import logic on USB sticks. (marco) diff --git a/src/olpc/datastore/backingstore.py b/src/olpc/datastore/backingstore.py index bf0c94d..dc6b2ef 100644 --- a/src/olpc/datastore/backingstore.py +++ b/src/olpc/datastore/backingstore.py @@ -640,8 +640,6 @@ class InplaceFileBackingStore(FileBackingStore): # blacklist all the hidden directories if '/.' in dirpath: continue - logging.info([ dirpath, dirname, filenames ]) - for fn in filenames: # give the thread a chance to exit if not self._runWalker: break @@ -713,6 +711,29 @@ class InplaceFileBackingStore(FileBackingStore): except Exception, exc: completion(exc) + def _get_unique_filename(self, suggested_filename): + filename = suggested_filename.replace('/', '_') + + # FAT limit is 255, leave some space for uniqueness + max_len = 250 + if len(filename) > max_len: + name, extension = os.path.splitext(filename) + filename = name[0:max_len - extension] + extension + + if os.path.exists(os.path.join(self.uri, filename)): + i = 1 + while len(filename) <= max_len: + name, extension = os.path.splitext(filename) + filename = name + '_' + str(i) + extension + if not os.path.exists(os.path.join(self.uri, filename)): + break + i += 1 + + if len(filename) > max_len: + filename = None + + return filename + def create(self, props, filelike, can_move=False): # the file would have already been changed inplace # don't touch it @@ -728,6 +749,10 @@ class InplaceFileBackingStore(FileBackingStore): # place the file proposed_name = props.get('filename', None) if not proposed_name: + suggested = props.get('suggested_filename', None) + if suggested: + proposed_name = self._get_unique_filename(suggested) + if not proposed_name: proposed_name = os.path.split(filelike.name)[1] # record the name before qualifying it to the store props['filename'] = proposed_name -- cgit v0.9.1