def _sftp_open_exclusive(self, abspath, mode=None): """Open a remote path exclusively. SFTP supports O_EXCL (SFTP_FLAG_EXCL), which fails if the file already exists. However it does not expose this at the higher level of SFTPClient.open(), so we have to sneak away with it. WARNING: This breaks the SFTPClient abstraction, so it could easily break against an updated version of paramiko. :param abspath: The remote absolute path where the file should be opened :param mode: The mode permissions bits for the new file """ # TODO: jam 20060816 Paramiko >= 1.6.2 (probably earlier) supports # using the 'x' flag to indicate SFTP_FLAG_EXCL. # However, there is no way to set the permission mode at open # time using the sftp_client.file() functionality. path = self._get_sftp()._adjust_cwd(abspath) # mutter('sftp abspath %s => %s', abspath, path) attr = SFTPAttributes() if mode is not None: attr.st_mode = mode omode = (SFTP_FLAG_WRITE | SFTP_FLAG_CREATE | SFTP_FLAG_TRUNC | SFTP_FLAG_EXCL) try: t, msg = self._get_sftp()._request(CMD_OPEN, path, omode, attr) if t != CMD_HANDLE: raise TransportError('Expected an SFTP handle') handle = msg.get_string() return SFTPFile(self._get_sftp(), handle, 'wb', -1) except (paramiko.SSHException, IOError), e: self._translate_io_exception(e, abspath, ': unable to open', failure_exc=FileExists)
def chmod(self, mode): """ Change the mode (permissions) of this file. The permissions are unix-style and identical to those used by Python's `os.chmod` function. :param int mode: new permissions """ self.sftp._log(DEBUG, 'chmod(%s, %r)' % (hexlify(self.handle), mode)) attr = SFTPAttributes() attr.st_mode = mode self.sftp._request(CMD_FSETSTAT, self.handle, attr)
def chmod(self, mode): """ Change the mode (permissions) of this file. The permissions are unix-style and identical to those used by Python's `os.chmod` function. :param int mode: new permissions """ self.sftp._log(DEBUG, 'chmod(%s, %r)' % (hexlify(self.handle), mode)) attr = SFTPAttributes() attr.st_mode = mode self.sftp._request(CMD_FSETSTAT, self.handle, attr)
def chmod(self, path, mode): """ Change the mode (permissions) of a file. The permissions are unix-style and identical to those used by Python's `os.chmod` function. :param str path: path of the file to change the permissions of :param int mode: new permissions """ path = self._adjust_cwd(path) self._log(DEBUG, 'chmod(%r, %r)' % (path, mode)) attr = SFTPAttributes() attr.st_mode = mode self._request(CMD_SETSTAT, path, attr)
def mkdir(self, path, mode=o777): """ Create a folder (directory) named ``path`` with numeric mode ``mode``. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out. :param str path: name of the folder to create :param int mode: permissions (posix-style) for the newly-created folder """ path = self._adjust_cwd(path) self._log(DEBUG, 'mkdir(%r, %r)' % (path, mode)) attr = SFTPAttributes() attr.st_mode = mode self._request(CMD_MKDIR, path, attr)
def chmod(self, path, mode): """ Change the mode (permissions) of a file. The permissions are unix-style and identical to those used by Python's `os.chmod` function. :param str path: path of the file to change the permissions of :param int mode: new permissions """ path = self._adjust_cwd(path) self._log(DEBUG, 'chmod(%r, %r)' % (path, mode)) attr = SFTPAttributes() attr.st_mode = mode self._request(CMD_SETSTAT, path, attr)
def mkdir(self, path, mode=o777): """ Create a folder (directory) named ``path`` with numeric mode ``mode``. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out. :param str path: name of the folder to create :param int mode: permissions (posix-style) for the newly-created folder """ path = self._adjust_cwd(path) self._log(DEBUG, 'mkdir(%r, %r)' % (path, mode)) attr = SFTPAttributes() attr.st_mode = mode self._request(CMD_MKDIR, path, attr)
def chmod(self, path, mode): """ Change the mode (permissions) of a file. The permissions are unix-style and identical to those used by python's C{os.chmod} function. @param path: path of the file to change the permissions of. @type path: string @param mode: new permissions. @type mode: int """ path = self._adjust_cwd(path) attr = SFTPAttributes() attr.st_mode = mode self._request(CMD_SETSTAT, path, attr)
def mkdir(self, path, mode=0777): """ Create a folder (directory) named C{path} with numeric mode C{mode}. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out. @param path: name of the folder to create. @type path: string @param mode: permissions (posix-style) for the newly-created folder. @type mode: int """ path = self._adjust_cwd(path) attr = SFTPAttributes() attr.st_mode = mode self._request(CMD_MKDIR, path, attr)
def chmod(self, path, mode): """ Change the mode (permissions) of a file. The permissions are unix-style and identical to those used by python's C{os.chmod} function. @param path: path of the file to change the permissions of. @type path: string @param mode: new permissions. @type mode: int """ path = self._adjust_cwd(path) attr = SFTPAttributes() attr.st_mode = mode self._request(CMD_SETSTAT, path, attr)
def mkdir(self, path, mode=0777): """ Create a folder (directory) named C{path} with numeric mode C{mode}. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out. @param path: name of the folder to create. @type path: string @param mode: permissions (posix-style) for the newly-created folder. @type mode: int """ path = self._adjust_cwd(path) attr = SFTPAttributes() attr.st_mode = mode self._request(CMD_MKDIR, path, attr)
def stat(self): if self.content_provider.get(self.path) is None: return SFTP_NO_SUCH_FILE mtime = calendar.timegm(datetime.now().timetuple()) sftp_attrs = SFTPAttributes() sftp_attrs.st_size = self.content_provider.get_size(self.path) sftp_attrs.st_uid = 0 sftp_attrs.st_gid = 0 sftp_attrs.st_mode = (stat.S_IRWXO | stat.S_IRWXG | stat.S_IRWXU | (stat.S_IFDIR if self.content_provider.is_dir( self.path) else stat.S_IFREG)) sftp_attrs.st_atime = mtime sftp_attrs.st_mtime = mtime sftp_attrs.filename = os.path.basename(self.path) return sftp_attrs
def _sftp_open_exclusive(self, abspath, mode=None): """Open a remote path exclusively. SFTP supports O_EXCL (SFTP_FLAG_EXCL), which fails if the file already exists. However it does not expose this at the higher level of SFTPClient.open(), so we have to sneak away with it. WARNING: This breaks the SFTPClient abstraction, so it could easily break against an updated version of paramiko. :param abspath: The remote absolute path where the file should be opened :param mode: The mode permissions bits for the new file """ # TODO: jam 20060816 Paramiko >= 1.6.2 (probably earlier) supports # using the 'x' flag to indicate SFTP_FLAG_EXCL. # However, there is no way to set the permission mode at open # time using the sftp_client.file() functionality. path = self._get_sftp()._adjust_cwd(abspath) # mutter('sftp abspath %s => %s', abspath, path) attr = SFTPAttributes() if mode is not None: attr.st_mode = mode omode = (SFTP_FLAG_WRITE | SFTP_FLAG_CREATE | SFTP_FLAG_TRUNC | SFTP_FLAG_EXCL) try: t, msg = self._get_sftp()._request(CMD_OPEN, path, omode, attr) if t != CMD_HANDLE: raise TransportError('Expected an SFTP handle') handle = msg.get_string() return SFTPFile(self._get_sftp(), handle, 'wb', -1) except (paramiko.SSHException, IOError), e: self._translate_io_exception(e, abspath, ': unable to open', failure_exc=FileExists)
def stat(self): if self.content_provider.get(self.path) is None: return SFTP_NO_SUCH_FILE mtime = calendar.timegm(datetime.now().timetuple()) sftp_attrs = SFTPAttributes() sftp_attrs.st_size = self.content_provider.get_size(self.path) sftp_attrs.st_uid = 0 sftp_attrs.st_gid = 0 sftp_attrs.st_mode = ( stat.S_IRWXO | stat.S_IRWXG | stat.S_IRWXU | ( stat.S_IFDIR if self.content_provider.is_dir(self.path) else stat.S_IFREG ) ) sftp_attrs.st_atime = mtime sftp_attrs.st_mtime = mtime sftp_attrs.filename = os.path.basename(self.path) return sftp_attrs