def _sftp_setstat(sftp_session_int, file_path, entry_attributes): result = c_sftp_setstat(sftp_session_int, c_char_p(bytify(file_path)), entry_attributes.raw) if result < 0: type_ = sftp_get_error(sftp_session_int) if type_ >= 0: raise SftpError("Set-stat on [%s] failed: %s" % (file_path, sftp_get_error_string(type_))) else: raise SftpError("Set-stat on [%s] failed. There was an " "unspecified error." % (file_path))
def _sftp_chmod(sftp_session_int, file_path, mode): result = c_sftp_chmod(sftp_session_int, c_char_p(bytify(file_path)), c_int(mode)) if result < 0: type_ = sftp_get_error(sftp_session_int) if type_ >= 0: raise SftpError("CHMOD of [%s] for mode [%o] failed: %s" % (file_path, mode, sftp_get_error_string(type_))) else: raise SftpError("CHMOD of [%s] for mode [%o] failed. There was " % "an unspecified error." % (file_path, mode))
def _sftp_stat(sftp_session_int, file_path): attr = c_sftp_stat(sftp_session_int, c_char_p(bytify(file_path))) if attr is None: type_ = sftp_get_error(sftp_session_int) if type_ >= 0: raise SftpError("Could not acquire attributes for STAT of [%s]: " "%s" % (file_path, sftp_get_error_string(type_))) else: raise SftpError("Could not acquire attributes for STAT of [%s]. " "There was an unspecified error." % (file_path)) return EntryAttributes(attr)
def _sftp_init(sftp_session_int): logging.debug("Initializing SFTP session: %d" % (sftp_session_int)) result = c_sftp_init(sftp_session_int) if result < 0: type_ = sftp_get_error(sftp_session_int) if type_ >= 0: raise SftpError("Could not create SFTP session: %s" % (sftp_get_error_string(type_))) else: raise SftpError("Could not create SFTP session. There was an " "unspecified error.")
def _sftp_symlink(sftp_session_int, to, from_): result = c_sftp_symlink(sftp_session_int, c_char_p(bytify(to)), c_char_p(bytify(from_))) if result < 0: type_ = sftp_get_error(sftp_session_int) if type_ >= 0: raise SftpError("Symlink of [%s] to target [%s] failed: %s" % (from_, to, sftp_get_error_string(type_))) else: raise SftpError("Symlink of [%s] to target [%s] failed. There was " "an unspecified error." % (from_, to))
def _sftp_readlink(sftp_session_int, file_path): target = c_sftp_readlink(sftp_session_int, c_char_p(bytify(file_path))) if target is None: type_ = sftp_get_error(sftp_session_int) if type_ >= 0: raise SftpError("Read of link [%s] failed: %s" % (file_path, sftp_get_error_string(type_))) else: raise SftpError("Read of link [%s] failed. There was an " "unspecified error." % (file_path)) return target
def _sftp_unlink(sftp_session_int, file_path): logging.debug("Deleting file: %s" % (file_path)) result = c_sftp_unlink(sftp_session_int, c_char_p(bytify(file_path))) # TODO: This seems to be a large integer. What is it? if result < 0: type_ = sftp_get_error(sftp_session_int) if type_ >= 0: raise SftpError("Unlink of [%s] failed: %s" % (file_path, sftp_get_error_string(type_))) else: raise SftpError("Unlink of [%s] failed. There was an unspecified " "error." % (file_path))
def _sftp_lstat(sftp_session_int, file_path): attr = c_sftp_lstat(sftp_session_int, c_char_p(bytify(file_path))) if attr is None: type_ = sftp_get_error(sftp_session_int) if type_ >= 0: raise SftpError("LSTAT of [%s] failed: %s" % (file_path, sftp_get_error_string(type_))) else: raise SftpError("LSTAT of [%s] failed. There was an unspecified " "error." % (file_path)) return EntryAttributes(attr)
def _sftp_rmdir(sftp_session_int, path): logging.debug("Deleting directory: %s" % (path)) result = c_sftp_rmdir(sftp_session_int, c_char_p(bytify(path))) if result < 0: type_ = sftp_get_error(sftp_session_int) if type_ >= 0: raise SftpError("RMDIR of [%s] failed: %s" % (path, sftp_get_error_string(type_))) else: raise SftpError("RMDIR of [%s] failed. There was an unspecified " "error." % (path))
def _sftp_chown(sftp_session_int, file_path, uid, gid): result = c_sftp_chown(sftp_session_int, c_char_p(bytify(file_path)), c_int(uid), c_int(gid)) if result < 0: type_ = sftp_get_error(sftp_session_int) if type_ >= 0: raise SftpError( "CHOWN of [%s] for UID (%d) and GID (%d) failed: " "%s" % (file_path, uid, gid, sftp_get_error_string(type_))) else: raise SftpError("CHOWN of [%s] for UID (%d) and GID (%d) failed. " "There was an unspecified error." % (file_path, mode))
def _sftp_rename(sftp_session_int, filepath_old, filepath_new): result = c_sftp_rename(sftp_session_int, c_char_p(bytify(filepath_old)), c_char_p(bytify(filepath_new))) if result < 0: type_ = sftp_get_error(sftp_session_int) if type_ >= 0: raise SftpError( "Rename of [%s] to [%s] failed: %s" % (filepath_old, filepath_new, sftp_get_error_string(type_))) else: raise SftpError("Rename of [%s] to [%s] failed. There was an " "unspecified error." % (filepath_old, filespace_new))
def _sftp_opendir(sftp_session_int, path): logging.debug("Opening directory: %s" % (path)) sd = c_sftp_opendir(sftp_session_int, bytify(path)) if sd is None: type_ = sftp_get_error(sftp_session_int) if type_ >= 0: raise SftpError("Could not open directory [%s]: %s" % (path, sftp_get_error_string(type_))) else: raise SftpError("Could not open directory [%s]. There was an " "unspecified error." % (path)) logging.debug("Directory resource ID is (%d)." % (sd)) return sd
def _sftp_open(sftp_session_int, filepath, access_type, mode): logging.debug("Opening file: %s" % (filepath)) sf = c_sftp_open(sftp_session_int, bytify(filepath), access_type, mode) if sf is None: type_ = sftp_get_error(sftp_session_int) if type_ >= 0: raise SftpError("Could not open file [%s]: %s" % (filepath, sftp_get_error_string(type_))) else: raise SftpError("Could not open file [%s]. There was an " "unspecified error." % (filepath)) logging.debug("File [%s] opened as [%s]." % (filepath, sf)) return sf
def _sftp_read(sf, count): buffer_ = create_string_buffer(count) received_bytes = c_sftp_read(sf, cast(buffer_, c_void_p), c_size_t(count)) if received_bytes < 0: raise SftpError("Read failed.") return buffer_.raw[0:received_bytes]
def remove_directory(node_path, depth=0): if depth > MAX_REMOTE_RECURSION_DEPTH: raise SftpError("Remote rmtree recursed too deeply. Either " "the directories run very deep, or we've " "encountered a cycle (probably via hard-" "links).") if node_path in parents: while parents[node_path]: remove_directory(parents[node_path][0], depth + 1) del parents[node_path][0] self.__log.debug("REMOTE: Removing directory: %s" % (node_path)) self.rmdir(node_path) # All children subdirectories have been deleted. Delete our parent # record. try: del parents[node_path] except KeyError: pass # Delete the mapping from us to our parent. del path_relations[node_path]
def _sftp_new(ssh_session_int): logging.debug("Creating SFTP session.") session = c_sftp_new(ssh_session_int) if session is None: raise SftpError("Could not create SFTP session.") logging.debug("New SFTP session: %s" % (session)) return session
def _sftp_mkdir(sftp_session_int, path, mode, check_exists_on_fail=True): logging.debug("Creating directory: %s" % (path)) result = c_sftp_mkdir(sftp_session_int, c_char_p(bytify(path)), c_int(mode)) if result < 0: if check_exists_on_fail is not False: if _sftp_exists(sftp_session_int, path) is True: raise SftpAlreadyExistsError("Path already exists: %s" % (path)) type_ = sftp_get_error(sftp_session_int) if type_ >= 0: raise SftpError("MKDIR of [%s] for mode [%o] failed: %s" % (path, mode, sftp_get_error_string(type_))) else: raise SftpError("MKDIR of [%s] for mode [%o] failed. There was " % "an unspecified error." % (path, mode))
def _sftp_utimes(sftp_session_int, file_path, atime_epoch, mtime_epoch): atime = CTimeval() mtime = CTimeval() atime.tv_sec = int(atime_epoch) atime.tv_usec = 0 mtime.tv_sec = int(mtime_epoch) mtime.tv_usec = 0 times = (CTimeval * 2)(atime, mtime) result = c_sftp_utimes(sftp_session_int, c_char_p(bytify(file_path)), byref(times)) if result < 0: raise SftpError("Times updated of [%s] failed." % (file_path))
def _sftp_listdir(sftp_session_int, path, get_directories=True, get_files=True): logging.debug("Listing directory: %s" % (path)) with SftpDirectory(sftp_session_int, bytify(path)) as sd_: while 1: attributes = _sftp_readdir(sftp_session_int, sd_) if attributes is None: break if attributes.is_directory is True and get_directories is True or \ attributes.is_directory is False and get_files is True: yield attributes if not _sftp_dir_eof(sd_): raise SftpError("We're done iterating the directory, but it's not " "at EOF.")
def _sftp_closedir(sd): logging.debug("Closing directory: %d" % (sd)) result = c_sftp_closedir(sd) if result != SSH_NO_ERROR: raise SftpError("Could not close directory.")
def _sftp_close(sf): logging.debug("Closing file: %s" % (sf)) result = c_sftp_close(sf) if result != SSH_NO_ERROR: raise SftpError("Close failed with code (%d)." % (result))
def _sftp_write(sf, buffer_): buffer_raw = create_string_buffer(buffer_) result = c_sftp_write(sf, cast(buffer_raw, c_void_p), len(buffer_)) if result < 0: raise SftpError("Could not write to file.")
def _sftp_tell(sf): position = c_sftp_tell(sf) if position < 0: raise SftpError("Could not read current position in file.") return position
def _sftp_seek(sf, position): if c_sftp_seek(sf, position) < 0: raise SftpError("Could not seek to the position (%d)." % (position))
def _sftp_fstat(sf): attr = c_sftp_fstat(sf) if attr is None: raise SftpError("Could not acquire attributes for FSTAT.") return EntryAttributes(attr)