From 74cbc138780f125b5752d150125cd0871efce72d Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Tue, 06 Nov 2007 16:25:49 +0000 Subject: #4654 Hard link files on check out instead of copying if possible. --- diff --git a/NEWS b/NEWS index 197596d..81437b3 100644 --- a/NEWS +++ b/NEWS @@ -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): -- cgit v0.9.1