Use get_pathinfo instead of separate os.path.{is*,exists} calls.

Signed-off-by: Daira Hopwood <daira@jacaranda.org>
This commit is contained in:
Daira Hopwood 2015-08-11 17:02:53 +01:00
parent 4f7c02c651
commit 6eb272856e
2 changed files with 18 additions and 19 deletions

View File

@ -12,7 +12,7 @@ from twisted.application import service
from allmydata.util import fileutil from allmydata.util import fileutil
from allmydata.interfaces import IDirectoryNode from allmydata.interfaces import IDirectoryNode
from allmydata.util import log from allmydata.util import log
from allmydata.util.fileutil import precondition_abspath from allmydata.util.fileutil import precondition_abspath, get_pathinfo
from allmydata.util.assertutil import precondition from allmydata.util.assertutil import precondition
from allmydata.util.encodingutil import listdir_unicode, to_filepath, \ from allmydata.util.encodingutil import listdir_unicode, to_filepath, \
@ -240,21 +240,18 @@ class Uploader(QueueMixin):
def _process_child(self, path_u): def _process_child(self, path_u):
precondition(isinstance(path_u, unicode), path_u) precondition(isinstance(path_u, unicode), path_u)
# note: symlinks to directories are both islink() and isdir() pathinfo = get_pathinfo(path_u)
isdir = os.path.isdir(path_u)
isfile = os.path.isfile(path_u)
islink = os.path.islink(path_u)
if islink: if pathinfo.islink:
self.warn("WARNING: cannot backup symlink %s" % quote_local_unicode_path(path_u)) self.warn("WARNING: cannot backup symlink %s" % quote_local_unicode_path(path_u))
return None return None
elif isdir: elif pathinfo.isdir:
# process directories unconditionally # process directories unconditionally
self._append_to_deque(path_u) self._append_to_deque(path_u)
# recurse on the child directory # recurse on the child directory
return self._scan(path_u) return self._scan(path_u)
elif isfile: elif pathinfo.isfile:
file_version = self._db.get_local_file_version(path_u) file_version = self._db.get_local_file_version(path_u)
if file_version is None: if file_version is None:
# XXX upload if we didn't record our version in magicfolder db? # XXX upload if we didn't record our version in magicfolder db?
@ -285,13 +282,17 @@ class Uploader(QueueMixin):
def _process(self, path_u): def _process(self, path_u):
precondition(isinstance(path_u, unicode), path_u) precondition(isinstance(path_u, unicode), path_u)
d = defer.succeed(None) d = defer.succeed(None)
def _maybe_upload(val): def _maybe_upload(val):
pathinfo = get_pathinfo(path_u)
self._pending.remove(path_u) # FIXME make _upload_pending hold relative paths self._pending.remove(path_u) # FIXME make _upload_pending hold relative paths
relpath_u = os.path.relpath(path_u, self._local_path_u) relpath_u = os.path.relpath(path_u, self._local_path_u)
encoded_name_u = magicpath.path2magic(relpath_u) encoded_name_u = magicpath.path2magic(relpath_u)
if not os.path.exists(path_u):
if not pathinfo.exists:
self._log("drop-upload: notified object %r disappeared " self._log("drop-upload: notified object %r disappeared "
"(this is normal for temporary objects)" % (path_u,)) "(this is normal for temporary objects)" % (path_u,))
self._count('objects_disappeared') self._count('objects_disappeared')
@ -319,12 +320,12 @@ class Uploader(QueueMixin):
d2.addCallback(lambda x: self._get_filenode(encoded_name_u)) d2.addCallback(lambda x: self._get_filenode(encoded_name_u))
d2.addCallback(add_db_entry) d2.addCallback(add_db_entry)
d2.addCallback(lambda x: Exception("file does not exist")) d2.addCallback(lambda x: Exception("file does not exist")) # FIXME wrong
return d2 return d2
elif os.path.islink(path_u): elif pathinfo.islink:
self.warn("WARNING: cannot upload symlink %s" % quote_local_unicode_path(path_u)) self.warn("WARNING: cannot upload symlink %s" % quote_local_unicode_path(path_u))
return None return None
elif os.path.isdir(path_u): elif pathinfo.isdir:
self._notifier.watch(to_filepath(path_u), mask=self.mask, callbacks=[self._notify], recursive=True) self._notifier.watch(to_filepath(path_u), mask=self.mask, callbacks=[self._notify], recursive=True)
uploadable = Data("", self._client.convergence) uploadable = Data("", self._client.convergence)
encoded_name_u += u"@_" encoded_name_u += u"@_"
@ -338,7 +339,7 @@ class Uploader(QueueMixin):
upload_d.addCallbacks(_succeeded, _failed) upload_d.addCallbacks(_succeeded, _failed)
upload_d.addCallback(lambda ign: self._scan(path_u)) upload_d.addCallback(lambda ign: self._scan(path_u))
return upload_d return upload_d
elif os.path.isfile(path_u): elif pathinfo.isfile:
version = self._db.get_local_file_version(relpath_u) version = self._db.get_local_file_version(relpath_u)
if version is None: if version is None:
version = 0 version = 0
@ -349,11 +350,9 @@ class Uploader(QueueMixin):
d2 = self._upload_dirnode.add_file(encoded_name_u, uploadable, metadata={"version":version}, overwrite=True) d2 = self._upload_dirnode.add_file(encoded_name_u, uploadable, metadata={"version":version}, overwrite=True)
def add_db_entry(filenode): def add_db_entry(filenode):
filecap = filenode.get_uri() filecap = filenode.get_uri()
s = os.stat(path_u) # XXX maybe just pass pathinfo
size = s[stat.ST_SIZE] self._db.did_upload_file(filecap, relpath_u, version,
ctime = s[stat.ST_CTIME] pathinfo.mtime, pathinfo.ctime, pathinfo.size)
mtime = s[stat.ST_MTIME]
self._db.did_upload_file(filecap, relpath_u, version, mtime, ctime, size)
self._count('files_uploaded') self._count('files_uploaded')
d2.addCallback(add_db_entry) d2.addCallback(add_db_entry)
return d2 return d2

View File

@ -641,7 +641,7 @@ def get_pathinfo(path_u):
) )
except OSError as e: except OSError as e:
if e.errno == ENOENT: if e.errno == ENOENT:
return PathInfo(isdir=False, return PathInfo(isdir =False,
isfile=False, isfile=False,
islink=False, islink=False,
exists=False, exists=False,