wxString clSFTP::Read(const wxString& remotePath) throw (clException) { if ( !m_sftp ) { throw clException("SFTP is not initialized"); } sftp_file file = sftp_open(m_sftp, remotePath.mb_str(wxConvISO8859_1).data(), O_RDONLY, 0); if (file == NULL) { throw clException(wxString() << _("Failed to open remote file: ") << remotePath << ". " << ssh_get_error(m_ssh->GetSession()), sftp_get_error(m_sftp)); } wxString content; char buffer[1024]; int nbytes = 0; memset(buffer, 0, sizeof(buffer)); nbytes = sftp_read(file, buffer, sizeof(buffer)); while (nbytes > 0) { content << wxString(buffer, nbytes); memset(buffer, 0, sizeof(buffer)); nbytes = sftp_read(file, buffer, sizeof(buffer)); } if ( nbytes < 0 ) { sftp_close(file); throw clException(wxString() << _("Failed to read remote file: ") << remotePath << ". " << ssh_get_error(m_ssh->GetSession()), sftp_get_error(m_sftp)); } sftp_close( file ); return content; }
static void torture_sftp_read_blocking(void **state) { struct torture_state *s = *state; struct torture_sftp *t = s->ssh.tsftp; char libssh_tmp_file[] = "/tmp/libssh_sftp_test_XXXXXX"; char buf[MAX_XFER_BUF_SIZE]; ssize_t bytesread; ssize_t byteswritten; int fd; sftp_file file; mode_t mask; file = sftp_open(t->sftp, "/usr/bin/ssh", O_RDONLY, 0); assert_non_null(file); mask = umask(S_IRWXO | S_IRWXG); fd = mkstemp(libssh_tmp_file); umask(mask); unlink(libssh_tmp_file); for (;;) { bytesread = sftp_read(file, buf, MAX_XFER_BUF_SIZE); if (bytesread == 0) { break; /* EOF */ } assert_false(bytesread < 0); byteswritten = write(fd, buf, bytesread); assert_int_equal(byteswritten, bytesread); } close(fd); sftp_close(file); }
static int arsenal_read (const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { int amount_read; (void) path; (void) offset; pthread_mutex_lock (&mutex); amount_read = sftp_read ((struct sftp_fd *) fi->fh, buf, size, offset); if (amount_read <= 0) { print_error ("sftp_read"); if (errno == EOF) { print_error ("sftp_read: EOF"); pthread_mutex_unlock (&mutex); return -EOF; } pthread_mutex_unlock (&mutex); return -ENOENT; } pthread_mutex_unlock (&mutex); return amount_read; }
sf::Int64 SFTPInputStream::read(void *data, sf::Int64 size) { uint8_t buffer[size]; sf::Int64 nbytes = sftp_read(this->file, buffer, sizeof(buffer)); memcpy(data, buffer, nbytes); return nbytes; this->index += nbytes; }
static ssize_t _sftp_read(csync_vio_method_handle_t *fhandle, void *buf, size_t count) { int rc = -1; rc = sftp_read(fhandle, buf, count); if (rc < 0) { errno = _sftp_portable_to_errno(sftp_get_error(_sftp_session)); } return rc; }
uint8_t* sn_sftp_load_file(ssh_session ssh, sftp_session sftp, std::string path) { uint8_t *data; int access_type, fd, nbytes, index, rc; char buffer[SFTP_MAX_XFER_BUFFER]; index = 0; sftp_file file; access_type = O_RDONLY; file = sftp_open(sftp, path.c_str(), access_type, 0); if (file == NULL) { std::cerr << "Can't open file for reading: " << strerror(errno) << std::endl; return NULL; } sftp_attributes attr = sftp_fstat(file); data = new uint8_t[attr->size + 1]; for(;;) { nbytes = sftp_read(file, buffer, sizeof(buffer)); if (nbytes == 0) { break; } else if (nbytes < 0) { std::cerr << "Error while reading file: " << ssh_get_error(ssh) << std::endl; sftp_close(file); delete[] data; return NULL; } memcpy(data + index, buffer, nbytes); index += nbytes; } rc = sftp_close(file); assert(rc == SSH_OK); data[attr->size] = '\0'; sftp_attributes_free(attr); return data; }
void clSFTP::Read(const wxString& remotePath, wxMemoryBuffer& buffer) throw(clException) { if(!m_sftp) { throw clException("SFTP is not initialized"); } sftp_file file = sftp_open(m_sftp, remotePath.mb_str(wxConvUTF8).data(), O_RDONLY, 0); if(file == NULL) { throw clException(wxString() << _("Failed to open remote file: ") << remotePath << ". " << ssh_get_error(m_ssh->GetSession()), sftp_get_error(m_sftp)); } SFTPAttribute::Ptr_t fileAttr = Stat(remotePath); if(!fileAttr) { throw clException(wxString() << _("Could not stat file:") << remotePath << ". " << ssh_get_error(m_ssh->GetSession()), sftp_get_error(m_sftp)); } wxInt64 fileSize = fileAttr->GetSize(); if(fileSize == 0) return; // Allocate buffer for the file content char pBuffer[65536]; // buffer // Read the entire file content wxInt64 bytesLeft = fileSize; wxInt64 bytesRead = 0; while(bytesLeft > 0) { wxInt64 nbytes = sftp_read(file, pBuffer, sizeof(pBuffer)); bytesRead += nbytes; bytesLeft -= nbytes; buffer.AppendData(pBuffer, nbytes); } if(bytesRead != fileSize) { sftp_close(file); buffer.Clear(); throw clException(wxString() << _("Could not read file:") << remotePath << ". " << ssh_get_error(m_ssh->GetSession()), sftp_get_error(m_sftp)); } sftp_close(file); }
int main() { // INIT ssh_session sshsession = ssh_new(); ssh_options_set(sshsession, SSH_OPTIONS_HOST, "192.168.1.117"); if (ssh_connect(sshsession) == 0) { if (ssh_userauth_password(sshsession, "root", "password") == 0) { sftp_session sftpsession = sftp_new(sshsession); if (sftp_init(sftpsession) == 0) { // DIR READ sftp_dir dir = sftp_opendir(sftpsession, "/root/"); sftp_attributes file; while((file = sftp_readdir(sftpsession, dir))) { if (file->type == 1) // 1 == file; 2 == dir { // todo.. } } sftp_closedir(dir); // FILE READ sftp_file l_file = sftp_open(sftpsession, "/root/test.txt", O_RDONLY, 0); sftp_attributes attr = sftp_fstat(l_file); QByteArray l_ba; l_ba.resize(attr->size); sftp_read(l_file, l_ba.data(), attr->size); // Returns readed sftp_close(l_file); } } ssh_disconnect(sshsession); } return 0; }
static int ggnfs_open(const char *fileName, struct fuse_file_info *fi) { fprintf(stderr, "inside sftp_read_sync\n"); int access_type; sftp_file file; char buffer[MAX_XFER_BUF_SIZE]; int nbytes, nwritten, rc; int fd; access_type = O_RDONLY; char filePath[PATH_MAX]; strcpy(filePath, remotePath); //strcat(filePath,"/"); strcat(filePath,fileName); fprintf(stderr, "filePath : %s\n", filePath); file = sftp_open(ggnfs_data.sftp, filePath, access_type, 0); if (file == NULL) { fprintf(stderr, "Can't open file for reading: %s\n", ssh_get_error(ggnfs_data.session)); return SSH_ERROR; } char localFilePath[PATH_MAX] ; strcpy(localFilePath, tmp_dir); strcat(localFilePath, fileName); fprintf(stderr, "local file path NOW NOW-> : %s\n", localFilePath); fd = open(localFilePath, O_CREAT | O_RDWR,0644); if (fd < 0) { fprintf(stderr, "Can't open file for writing: %s\n", strerror(errno)); return SSH_ERROR; } fprintf(stderr, "before for loop to read -> : %s\n", localFilePath); for (;;) { nbytes = sftp_read(file, buffer, sizeof(buffer)); if (nbytes == 0) { break; // EOF } else if (nbytes < 0) { fprintf(stderr, "Error while reading file: %s\n", ssh_get_error(ggnfs_data.session)); sftp_close(file); return SSH_ERROR; } nwritten = write(fd, buffer, nbytes); if (nwritten != nbytes) { fprintf(stderr, "Error writing: %s\n", strerror(errno)); sftp_close(file); return SSH_ERROR; } } fprintf(stderr, "AFTER for loop to read -> : %s\n", localFilePath); rc = sftp_close(file); if (rc != SSH_OK) { fprintf(stderr, "Can't close the read file: %s\n", ssh_get_error(ggnfs_data.session)); return rc; } fi->fh = fd; return EXIT_SUCCESS; }
/* it's just a proof of concept code for sftp, till i write a real documentation about it */ void do_sftp(SSH_SESSION *session){ SFTP_SESSION *sftp=sftp_new(session); SFTP_DIR *dir; SFTP_ATTRIBUTES *file; SFTP_FILE *fichier; SFTP_FILE *to; int len=1; int i; char data[8000]; if(!sftp){ ssh_say(0,"sftp error initialising channel : %s\n",ssh_get_error(session)); return; } if(sftp_init(sftp)){ ssh_say(0,"error initialising sftp : %s\n",ssh_get_error(session)); return; } /* the connection is made */ /* opening a directory */ dir=sftp_opendir(sftp,"./"); if(!dir) { ssh_say(0,"Directory not opened(%s)\n",ssh_get_error(session)); return ; } /* reading the whole directory, file by file */ while((file=sftp_readdir(sftp,dir))){ ssh_say(0,"%30s(%.8lo) : %.5d.%.5d : %.10lld bytes\n",file->name,file->permissions,file->uid,file->gid,file->size); sftp_attributes_free(file); } /* when file=NULL, an error has occured OR the directory listing is end of file */ if(!sftp_dir_eof(dir)){ ssh_say(0,"error : %s\n",ssh_get_error(session)); return; } if(sftp_dir_close(dir)){ ssh_say(0,"Error : %s\n",ssh_get_error(session)); return; } /* this will open a file and copy it into your /home directory */ /* the small buffer size was intended to stress the library. of course, you can use a buffer till 20kbytes without problem */ fichier=sftp_open(sftp,"/usr/bin/ssh",O_RDONLY,NULL); if(!fichier){ ssh_say(0,"Error opening /usr/bin/ssh : %s\n",ssh_get_error(session)); return; } /* open a file for writing... */ to=sftp_open(sftp,"ssh-copy",O_WRONLY | O_CREAT,NULL); if(!to){ ssh_say(0,"Error opening ssh-copy for writing : %s\n",ssh_get_error(session)); return; } while((len=sftp_read(fichier,data,4096)) > 0){ if(sftp_write(to,data,len)!=len){ ssh_say(0,"error writing %d bytes : %s\n",len,ssh_get_error(session)); return; } } printf("finished\n"); if(len<0) ssh_say(0,"Error reading file : %s\n",ssh_get_error(session)); sftp_file_close(fichier); sftp_file_close(to); printf("fichiers ferm�\n"); to=sftp_open(sftp,"/tmp/grosfichier",O_WRONLY|O_CREAT,NULL); for(i=0;i<1000;++i){ len=sftp_write(to,data,8000); printf("wrote %d bytes\n",len); if(len != 8000){ printf("chunk %d : %d (%s)\n",i,len,ssh_get_error(session)); } } sftp_file_close(to); /* close the sftp session */ sftp_free(sftp); printf("session sftp termin�\n"); }
static int do_read(const char* infile, FILE* fp, getmode_t mode, ftp_transfer_func hookf, uint64_t offset) { if (gvSSHTrySCP && !offset) { char* escaped = bash_backslash_quote(infile); /* try to set up a scp connection */ ssh_scp scp = ssh_scp_new(ftp->session, SSH_SCP_READ, escaped); free(escaped); if (scp != NULL) { int rc = ssh_scp_init(scp); if (rc == SSH_OK) return do_scp_read(scp, infile, fp, mode, hookf); ssh_scp_free(scp); } } time_t then = time(NULL) - 1; ftp_set_close_handler(); if (hookf) hookf(&ftp->ti); ftp->ti.begin = false; /* check if remote file is not a directory */ sftp_attributes attrib = sftp_stat(ftp->sftp_session, infile); if (!attrib) return -1; if (S_ISDIR(attrib->permissions)) { ftp_err(_("Cannot download a directory: %s\n"), infile); sftp_attributes_free(attrib); return -1; } sftp_attributes_free(attrib); /* open remote file */ sftp_file file = sftp_open(ftp->sftp_session, infile, O_RDONLY, 0); if (!file) { ftp_err(_("Cannot open file for reading: %s\n"), ssh_get_error(ftp->session)); return -1; } /* seek to offset */ int r = sftp_seek64(file, offset); if (r != SSH_OK) { ftp_err(_("Failed to seek: %s\n"), ssh_get_error(ftp->session)); sftp_close(file); return -1; } /* read file */ char buffer[SSH_BUFSIZ]; ssize_t nbytes = 0; while ((nbytes = sftp_read(file, buffer, sizeof(buffer))) > 0) { if (ftp_sigints() > 0) { ftp_trace("break due to sigint\n"); break; } errno = 0; if (fwrite(buffer, nbytes, 1, fp) != 1) { ftp_err(_("Error while writing to file: %s\n"), strerror(errno)); ftp->ti.ioerror = true; sftp_close(file); return -1; } ftp->ti.size += nbytes; if (hookf) { time_t now = time(NULL); if (now > then) { hookf(&ftp->ti); then = now; } } } if (nbytes < 0) { ftp_err(_("Error while reading from file: %s\n"), ssh_get_error(ftp->session)); r = -1; } sftp_close(file); return r; }
static void do_sftp(ssh_session session){ sftp_session sftp=sftp_new(session); sftp_dir dir; sftp_attributes file; sftp_statvfs_t sftpstatvfs; struct statvfs sysstatvfs; sftp_file fichier; sftp_file to; int len=1; unsigned int i; char data[DATALEN]={0}; char *lnk; unsigned int count; if(!sftp){ fprintf(stderr, "sftp error initialising channel: %s\n", ssh_get_error(session)); return; } if(sftp_init(sftp)){ fprintf(stderr, "error initialising sftp: %s\n", ssh_get_error(session)); return; } printf("Additional SFTP extensions provided by the server:\n"); count = sftp_extensions_get_count(sftp); for (i = 0; i < count; i++) { printf("\t%s, version: %s\n", sftp_extensions_get_name(sftp, i), sftp_extensions_get_data(sftp, i)); } /* test symlink and readlink */ if (sftp_symlink(sftp, "/tmp/this_is_the_link", "/tmp/sftp_symlink_test") < 0) { fprintf(stderr, "Could not create link (%s)\n", ssh_get_error(session)); return; } lnk = sftp_readlink(sftp, "/tmp/sftp_symlink_test"); if (lnk == NULL) { fprintf(stderr, "Could not read link (%s)\n", ssh_get_error(session)); return; } printf("readlink /tmp/sftp_symlink_test: %s\n", lnk); sftp_unlink(sftp, "/tmp/sftp_symlink_test"); if (sftp_extension_supported(sftp, "*****@*****.**", "2")) { sftpstatvfs = sftp_statvfs(sftp, "/tmp"); if (sftpstatvfs == NULL) { fprintf(stderr, "statvfs failed (%s)\n", ssh_get_error(session)); return; } printf("sftp statvfs:\n" "\tfile system block size: %llu\n" "\tfundamental fs block size: %llu\n" "\tnumber of blocks (unit f_frsize): %llu\n" "\tfree blocks in file system: %llu\n" "\tfree blocks for non-root: %llu\n" "\ttotal file inodes: %llu\n" "\tfree file inodes: %llu\n" "\tfree file inodes for to non-root: %llu\n" "\tfile system id: %llu\n" "\tbit mask of f_flag values: %llu\n" "\tmaximum filename length: %llu\n", (unsigned long long) sftpstatvfs->f_bsize, (unsigned long long) sftpstatvfs->f_frsize, (unsigned long long) sftpstatvfs->f_blocks, (unsigned long long) sftpstatvfs->f_bfree, (unsigned long long) sftpstatvfs->f_bavail, (unsigned long long) sftpstatvfs->f_files, (unsigned long long) sftpstatvfs->f_ffree, (unsigned long long) sftpstatvfs->f_favail, (unsigned long long) sftpstatvfs->f_fsid, (unsigned long long) sftpstatvfs->f_flag, (unsigned long long) sftpstatvfs->f_namemax); sftp_statvfs_free(sftpstatvfs); if (statvfs("/tmp", &sysstatvfs) < 0) { fprintf(stderr, "statvfs failed (%s)\n", strerror(errno)); return; } printf("sys statvfs:\n" "\tfile system block size: %llu\n" "\tfundamental fs block size: %llu\n" "\tnumber of blocks (unit f_frsize): %llu\n" "\tfree blocks in file system: %llu\n" "\tfree blocks for non-root: %llu\n" "\ttotal file inodes: %llu\n" "\tfree file inodes: %llu\n" "\tfree file inodes for to non-root: %llu\n" "\tfile system id: %llu\n" "\tbit mask of f_flag values: %llu\n" "\tmaximum filename length: %llu\n", (unsigned long long) sysstatvfs.f_bsize, (unsigned long long) sysstatvfs.f_frsize, (unsigned long long) sysstatvfs.f_blocks, (unsigned long long) sysstatvfs.f_bfree, (unsigned long long) sysstatvfs.f_bavail, (unsigned long long) sysstatvfs.f_files, (unsigned long long) sysstatvfs.f_ffree, (unsigned long long) sysstatvfs.f_favail, (unsigned long long) sysstatvfs.f_fsid, (unsigned long long) sysstatvfs.f_flag, (unsigned long long) sysstatvfs.f_namemax); } /* the connection is made */ /* opening a directory */ dir=sftp_opendir(sftp,"./"); if(!dir) { fprintf(stderr, "Directory not opened(%s)\n", ssh_get_error(session)); return ; } /* reading the whole directory, file by file */ while((file=sftp_readdir(sftp,dir))){ fprintf(stderr, "%30s(%.8o) : %s(%.5d) %s(%.5d) : %.10llu bytes\n", file->name, file->permissions, file->owner, file->uid, file->group, file->gid, (long long unsigned int) file->size); sftp_attributes_free(file); } /* when file=NULL, an error has occured OR the directory listing is end of file */ if(!sftp_dir_eof(dir)){ fprintf(stderr, "Error: %s\n", ssh_get_error(session)); return; } if(sftp_closedir(dir)){ fprintf(stderr, "Error: %s\n", ssh_get_error(session)); return; } /* this will open a file and copy it into your /home directory */ /* the small buffer size was intended to stress the library. of course, you can use a buffer till 20kbytes without problem */ fichier=sftp_open(sftp,"/usr/bin/ssh",O_RDONLY, 0); if(!fichier){ fprintf(stderr, "Error opening /usr/bin/ssh: %s\n", ssh_get_error(session)); return; } /* open a file for writing... */ to=sftp_open(sftp,"ssh-copy",O_WRONLY | O_CREAT, 0700); if(!to){ fprintf(stderr, "Error opening ssh-copy for writing: %s\n", ssh_get_error(session)); return; } while((len=sftp_read(fichier,data,4096)) > 0){ if(sftp_write(to,data,len)!=len){ fprintf(stderr, "Error writing %d bytes: %s\n", len, ssh_get_error(session)); return; } } printf("finished\n"); if(len<0) fprintf(stderr, "Error reading file: %s\n", ssh_get_error(session)); sftp_close(fichier); sftp_close(to); printf("fichiers ferm\n"); to=sftp_open(sftp,"/tmp/grosfichier",O_WRONLY|O_CREAT, 0644); for(i=0;i<1000;++i){ len=sftp_write(to,data,DATALEN); printf("wrote %d bytes\n",len); if(len != DATALEN){ printf("chunk %d : %d (%s)\n",i,len,ssh_get_error(session)); } } sftp_close(to); /* close the sftp session */ sftp_free(sftp); printf("sftp session terminated\n"); }
int FSSftp::Read ( int fd, void* buf, int size, int* err, FSCInfo* info ) { MutexLock lock( &mutex ); int ret = CheckSession( err, info ); //??? if ( ret ) { return ret; } if ( fd < 0 || fd >= MAX_FILES || !fileTable[fd] ) { if ( err ) { *err = EINVAL; } return -1; } /* на всякий случай как в Write, а то хз */ int bytes = 0; char* s = ( char* )buf; while ( size > 0 ) { int n = size; if ( n > 0x4000 ) { n = 0x4000; } int ret = sftp_read( fileTable[fd], s, n ); if ( ret < 0 ) { if ( err ) { *err = sftp_get_error( sftpSession ); } return -1; } if ( !ret ) { break; } s += ret; size -= ret; bytes += ret; } return bytes; /* int bytes = sftp_read(fileTable[fd], buf, size); if (bytes<0) { if (err) *err = sftp_get_error(sftpSession); return -1; } return bytes; */ }