diff options
author | Tomeu Vizoso <tomeu@tomeuvizoso.net> | 2007-11-06 16:25:49 (GMT) |
---|---|---|
committer | Tomeu Vizoso <tomeu@tomeuvizoso.net> | 2007-11-06 16:25:49 (GMT) |
commit | 74cbc138780f125b5752d150125cd0871efce72d (patch) | |
tree | 46817eb0ece1a21b258d3fc57feae8cc4fd70d23 | |
parent | d7ed2f9622c74ab9de11cedbdbdebd7da53486ca (diff) |
#4654 Hard link files on check out instead of copying if possible.
-rw-r--r-- | NEWS | 2 | ||||
-rw-r--r-- | src/olpc/datastore/backingstore.py | 25 | ||||
-rw-r--r-- | src/olpc/datastore/model.py | 7 |
3 files changed, 23 insertions, 11 deletions
@@ -1,3 +1,5 @@ +* #4654 Hard link files on check out instead of copying if possible. (tomeu) + Snapshot 2b5596554c * #4234 Fix copying a download to an usb stick. (marco) diff --git a/src/olpc/datastore/backingstore.py b/src/olpc/datastore/backingstore.py index e0e420b..a841f73 100644 --- a/src/olpc/datastore/backingstore.py +++ b/src/olpc/datastore/backingstore.py @@ -19,6 +19,8 @@ import sha import subprocess import time import threading +import errno +import shutil import dbus import xapian @@ -343,11 +345,12 @@ class FileBackingStore(BackingStore): return os.path.join(self.base, str(uid)) def _targetFile(self, uid, target=None, ext=None, env=None): + logging.debug('FileBackingStore._targetFile: %r %r %r %r' % (uid, target, ext, env)) # paths out of the datastore, working copy targets path = self._translatePath(uid) if not os.path.exists(path): return None - + if target: targetpath = target else: targetpath = uid.replace('/', '_').replace('.', '__') @@ -355,8 +358,12 @@ class FileBackingStore(BackingStore): if not ext.startswith('.'): ext = ".%s" % ext targetpath = "%s%s" % (targetpath, ext) - base = '/tmp' - if env: base = env.get('cwd', base) + # TODO: When rainbow can tell us, we'll save the file to a dir inside the + # activity file space. + profile = os.environ.get('SUGAR_PROFILE', 'default') + base = os.path.join(os.path.expanduser('~'), '.sugar', profile, 'data') + if not os.path.exists(base): + os.makedirs(base) targetpath = os.path.join(base, targetpath) attempt = 0 @@ -375,10 +382,16 @@ class FileBackingStore(BackingStore): break targetpath = "%s(%s)%s" % (targetpath, attempt, ext) + + try: + os.link(path, targetpath) + except OSError, e: + if e.errno == errno.EXDEV: + shutil.copy(path, targetpath) + else: + raise - if subprocess.call(['cp', path, targetpath]): - raise OSError("unable to create working copy") - return open(targetpath, 'rw') + return open(targetpath, 'r') def _mapContent(self, uid, fp, path, env=None): """map a content object and the file in the repository to a diff --git a/src/olpc/datastore/model.py b/src/olpc/datastore/model.py index 9b00e2c..4558a10 100644 --- a/src/olpc/datastore/model.py +++ b/src/olpc/datastore/model.py @@ -260,11 +260,8 @@ class Content(object): if not hasattr(self, "_file") or not self._file or \ self._file.closed is True: target, ext = self.suggestName() - try: - targetfile = self.backingstore._targetFile(self.id, target, ext) - self._file = targetfile - except OSError: - self._file = None + targetfile = self.backingstore._targetFile(self.id, target, ext) + self._file = targetfile return self._file def set_file(self, fileobj): |