【发布时间】:2014-05-15 08:21:42
【问题描述】:
我尝试通过 libssh2 sftp 子系统将多个文件上传到远程服务器。我的问题是我必须在传输一个文件后始终关闭 sftp 句柄和 sftp 会话,否则函数 libssh2_sftp_open() 将失败,“FXP_STATUS 4,无法打开/创建文件”。由于会话和文件的重新初始化,我失去了宝贵的时间。
有没有办法在不重新初始化会话和/或句柄的情况下循环上传多个文件?我希望这段代码 sn-p 能告诉你我的问题, 这里在一个循环通过后重新初始化,但需要 4 秒来上传 4 kbytes:
// Transfer at most MAXTRANSFER files
for (i=0;(i < ((MAXTRANSFER<nfiles)?MAXTRANSFER:nfiles)) && (iRet == SSHH_OK); i++) {
localfile = malloc(sizeof(char) * (strlen(directory) + strlen(fnames[i]) + 2));
strcpy(localfile, directory);
strcat(localfile, "/");
strcat(localfile, fnames[i]);
local = fopen(localfile, "rb");
if (!local) {
dbgPrintFormat(DEBUG_WARNING, "dir_sftp: Can't open local file %s", localfile);
iRet = SSHH_ERR_SETTINGS;
}
// Init SFTP
if (iRet == SSHH_OK) {
sftp_session = libssh2_sftp_init(session);
if (!sftp_session) {
dbgPrintFormat(DEBUG_ERROR, "dir_sftp: Unable to init SFTP session (%d)", sftp_session);
iRet = SSHH_ERR_SETTINGS;
}
}
if (iRet == SSHH_OK) {
stat(localfile, &fileinfo);
char *dstfile = malloc(sizeof(char) * (strlen(destdir) + strlen(fnames[i]) + 2));
strcpy(dstfile, destdir);
strcat(dstfile, "/");
strcat(dstfile, fnames[i]);
dbgPrintFormat(DEBUG_FINE, "dir_sftp: Start sending file %s", localfile);
// Request a file via SFTP
sftp_handle = libssh2_sftp_open(sftp_session, dstfile,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRWXU|LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IXGRP);
if (!sftp_handle) {
dbgPrintFormat(DEBUG_ERROR, "dir_sftp: Unable to open file with SFTP");
iRet = SSHH_ERR_SETTINGS;
}
if (iRet == SSHH_OK) {
dbgPrintFormat(DEBUG_FINE, "dir_sftp: SFTP session waiting to send file %s",localfile);
do {
nread = fread(mem, 1, sizeof(mem), local);
if (nread <= 0) {
/* end of file */
break;
}
ptr = mem;
do {
/* write the same data over and over, until EOF */
rc = libssh2_sftp_write(sftp_handle, ptr, nread);
// EOF
if(rc < 0)
break;
ptr += rc;
nread -= rc;
} while (nread);
} while (rc > 0);
}
ulSftpTxTryCount[iSftpTxTryCountIdx]++;
if (iRet == SSHH_OK) {
// Remove localfile *** if no error
if (remove(localfile) == -1) {
dbgPrintFormat(DEBUG_WARNING,"dir_sftp: Error removing file: %s", localfile);
}
if( successCnt ) {
(*successCnt)++;
}
dbgPrintFormat(DEBUG_FINE, "dir_sftp: File sent after %lu tries", ulSftpTxTryCount[iSftpTxTryCountIdx]);
iSftpTxTryCountIdx = (iSftpTxTryCountIdx + 1) % SCP_TRYCNT_NOF;
ulSftpTxTryCount[iSftpTxTryCountIdx] = 0;
}
free (dstfile);
}
if (local)
fclose(local);
free (localfile);
// Close file sftp handle
if (libssh2_sftp_close(sftp_handle) < 0) {
dbgPrintFormat(DEBUG_WARNING, "dir_sftp: Error closing SFTP handle ");
}
// Close sftp session
libssh2_sftp_shutdown(sftp_session);
}
【问题讨论】:
-
它本来可以工作的,听起来你遇到了一个错误......
-
我不知道它是如何工作的,因为我在网络或 libssh2 的手册中找不到示例,他们正在上传/打开多个文件...但是我认为我必须关闭 sftp_session 才能打开另一个文件是没有意义的?