def get_info(self, ref, raise_if_missing=True): if isinstance(ref, str): ref = unicode(ref) os_path = self._abspath(ref) if not os.path.exists(os_path): if raise_if_missing: raise NotFound("Could not found file '%s' under '%s'" % (ref, self.base_folder)) else: return None folderish = os.path.isdir(os_path) stat_info = os.stat(os_path) if folderish: size = 0 else: size = stat_info.st_size mtime = datetime.utcfromtimestamp(stat_info.st_mtime) # TODO Do we need to load it everytime ? remote_ref = self.get_remote_id(ref) # On unix we could use the inode for file move detection but that won't # work on Windows. To reduce complexity of the code and the possibility # to have Windows specific bugs, let's not use the unix inode at all. # uid = str(stat_info.st_ino) return FileInfo(self.base_folder, ref, folderish, mtime, digest_func=self._digest_func, check_suspended=self.check_suspended, remote_ref=remote_ref, size=size)
def get_info(self, fs_item_id, raise_if_missing=True): fs_item = self.get_fs_item(fs_item_id) if fs_item is None: if raise_if_missing: raise NotFound("Could not find '%s' on '%s'" % (fs_item_id, self.server_url)) return None return self.file_to_info(fs_item)
def fetch(self, ref, **kwargs): try: return self.execute("Document.Fetch", value=ref, **kwargs) except urllib2.HTTPError as e: if e.code == 404: raise NotFound("Failed to fetch document %r on server %r" % (ref, self.server_url)) raise e
def bind(self, binder): check_credential = True if hasattr(binder, 'no_check') and binder.no_check: check_credential = False check_fs = self._manager.is_checkfs() if hasattr(binder, 'no_fscheck') and binder.no_fscheck: check_fs = False self._server_url = self._normalize_url(binder.url) self._remote_user = binder.username self._remote_password = binder.password self._remote_token = binder.token self._web_authentication = self._remote_token is not None if check_fs: created_folder = False try: if not os.path.exists(os.path.dirname(self._local_folder)): raise NotFound() if not os.path.exists(self._local_folder): os.mkdir(self._local_folder) created_folder = True self._check_fs(self._local_folder) except Exception as e: try: if created_folder: os.rmdir(self._local_folder) except: pass raise e nxclient = None if check_credential: nxclient = self.remote_doc_client_factory( self._server_url, self._remote_user, self._manager.device_id, self._manager.client_version, proxies=self._manager.proxies, proxy_exceptions=self._manager.proxy_exceptions, password=self._remote_password, token=self._remote_token, timeout=self._handshake_timeout) if self._remote_token is None: self._remote_token = nxclient.request_token() if self._remote_token is not None: # The server supports token based identification: do not store the # password in the DB self._remote_password = None # Save the configuration self._dao.update_config("web_authentication", self._web_authentication) self._dao.update_config("server_url", self._server_url) self._dao.update_config("remote_user", self._remote_user) self._dao.update_config("remote_password", self._remote_password) self._dao.update_config("remote_token", self._remote_token) if nxclient: self.get_update_infos(nxclient) # Check for the root # If the top level state for the server binding doesn't exist, # create the local folder and the top level state. self._check_root()
def get_info(self, ref, raise_if_missing=True, fetch_parent_uid=True, use_trash=True, include_versions=False): if not self.exists(ref, use_trash=use_trash, include_versions=include_versions): if raise_if_missing: raise NotFound("Could not find '%s' on '%s'" % ( self._check_ref(ref), self.server_url)) return None return self._doc_to_info(self.fetch(self._check_ref(ref)), fetch_parent_uid=fetch_parent_uid)
def _extract_edit_info(self, ref): dir_path = os.path.dirname(ref) uid = self._local_client.get_remote_id(dir_path) server_url = self._local_client.get_remote_id(dir_path, "nxdirectedit") user = self._local_client.get_remote_id(dir_path, "nxdirectedituser") engine = self._get_engine(server_url, user=user) if engine is None: raise NotFound() remote_client = engine.get_remote_doc_client() remote_client.check_suspended = self.stop_client digest_algorithm = self._local_client.get_remote_id( dir_path, "nxdirecteditdigestalgorithm") digest = self._local_client.get_remote_id(dir_path, "nxdirecteditdigest") return uid, engine, remote_client, digest_algorithm, digest
def set_remote_id(self, ref, remote_id, name='ndrive'): # type: (Text, Text, Text) -> None if not isinstance(remote_id, bytes): remote_id = unicodedata.normalize('NFC', remote_id).encode('utf-8') path = self.abspath(ref) log.trace('Setting xattr %s with value %r on %r', name, remote_id, path) locker = self.unlock_path(path, False) if sys.platform == 'win32': path_alt = path + ':' + name try: if not os.path.exists(path): raise NotFound() stat_ = os.stat(path) with open(path_alt, 'w') as f: f.write(remote_id) # Avoid time modified change os.utime(path, (stat_.st_atime, stat_.st_mtime)) except IOError as e: # Should not happen if e.errno == os.errno.EACCES: self.unset_path_readonly(path) with open(path_alt, 'w') as f: f.write(remote_id) self.set_path_readonly(path) else: raise e finally: self.lock_path(path, locker) return if sys.platform == 'linux2': name = 'user.' + name try: stat_ = os.stat(path) xattr.setxattr(path, name, remote_id) os.utime(path, (stat_.st_atime, stat_.st_mtime)) finally: self.lock_path(path, locker)
def set_remote_id(self, ref, remote_id, name='ndrive'): if type(remote_id).__name__ == "unicode": remote_id = unicodedata.normalize('NFC', remote_id).encode('utf-8') # Can be move to another class path = self._abspath(ref) log.trace('Setting xattr %s with value %r on %r', name, remote_id, path) locker = self.unlock_path(path, False) if AbstractOSIntegration.is_windows(): pathAlt = path + ":" + name try: if not os.path.exists(path): raise NotFound() stat = os.stat(path) with open(pathAlt, "w") as f: f.write(remote_id) # Avoid time modified change os.utime(path, (stat.st_atime, stat.st_mtime)) except IOError as e: # Should not happen if e.errno == os.errno.EACCES: self.unset_path_readonly(path) with open(pathAlt, "w") as f: f.write(remote_id) self.set_path_readonly(path) else: raise e finally: self.lock_path(path, locker) else: try: import xattr stat = os.stat(path) if AbstractOSIntegration.is_mac(): xattr.setxattr(path, name, remote_id) else: xattr.setxattr(path, 'user.' + name, remote_id) os.utime(path, (stat.st_atime, stat.st_mtime)) finally: self.lock_path(path, locker)
def get_info(self, ref, raise_if_missing=True): if isinstance(ref, str): ref = unicode(ref) os_path = self._abspath(ref) if not os.path.exists(os_path): if raise_if_missing: raise NotFound("Could not found file '%s' under '%s'" % (ref, self.base_folder)) else: return None folderish = os.path.isdir(os_path) stat_info = os.stat(os_path) if folderish: size = 0 else: size = stat_info.st_size try: mtime = datetime.utcfromtimestamp(stat_info.st_mtime) except ValueError, e: log.error( str(e) + "file path: %s. st_mtime value: %s" % (str(os_path), str(stat_info.st_mtime))) mtime = datetime.utcfromtimestamp(0)
def get_info(self, ref, raise_if_missing=True): os_path = self._abspath(ref) if not os.path.exists(os_path): if raise_if_missing: raise NotFound("Could not found file '%s' under '%s'" % (ref, self.base_folder)) else: return None folderish = os.path.isdir(os_path) stat_info = os.stat(os_path) mtime = datetime.fromtimestamp(stat_info.st_mtime) path = u'/' + os_path[len(safe_long_path(self.base_folder)) + 1:] path = path.replace(os.path.sep, u'/') # unix style path # On unix we could use the inode for file move detection but that won't # work on Windows. To reduce complexity of the code and the possibility # to have Windows specific bugs, let's not use the unix inode at all. # uid = str(stat_info.st_ino) return FileInfo(self.base_folder, path, folderish, mtime, digest_func=self._digest_func, check_suspended=self.check_suspended)
def get_info(self, ref, raise_if_missing=True): # type: (Text, bool) -> Union[FileInfo, None] if isinstance(ref, bytes): ref = unicode(ref) os_path = self.abspath(ref) if not os.path.exists(os_path): if raise_if_missing: err = 'Could not find file into {!r}: ref={!r}, os_path={!r}' raise NotFound(err.format(self.base_folder, ref, os_path)) return None folderish = os.path.isdir(os_path) stat_info = os.stat(os_path) size = 0 if folderish else stat_info.st_size try: mtime = datetime.datetime.utcfromtimestamp(stat_info.st_mtime) except ValueError, e: log.error( str(e) + "file path: %s. st_mtime value: %s" % (str(os_path), str(stat_info.st_mtime))) mtime = datetime.datetime.utcfromtimestamp(0)