int FSSftp::SetFileTime ( FSPath& path, FSTime aTime, FSTime mTime, int* err, FSCInfo* info ) { MutexLock lock( &mutex ); int ret = CheckSession( err, info ); if ( ret ) { return ret; } LIBSSH2_SFTP_ATTRIBUTES attr; attr.flags = LIBSSH2_SFTP_ATTR_ACMODTIME; attr.atime = aTime; attr.mtime = mTime; try { int ret; WHILE_EAGAIN_( ret, libssh2_sftp_setstat( sftpSession, ( char* )path.GetString( _operParam.charset, '/' ), &attr ) ); CheckSFTP( ret ); } catch ( int e ) { if ( err ) { *err = e; } return ( e == -2 ) ? -2 : -1; } return 0; }
int SyncTransferThread::do_touch_sftp_file_with_time(QString fileName, QDateTime time) { // sftp_open, 可以得到当前的文件属性 // sftp_close, // 修改LIBSSH2_SFTP_ATTRIBUTE中的最后修改日期 // sftp_set_stat, 修改文件的最后修改日期。 LIBSSH2_SFTP_ATTRIBUTES attr; int ret; ret = libssh2_sftp_stat(this->ssh2_sftp, GlobalOption::instance()->remote_codec->fromUnicode(fileName), &attr); if (ret != 0) { //int libssh2_session_last_error(LIBSSH2_SESSION *session, char **errmsg, int *errmsg_len, int want_buf char errmsg[200] = {0}; int emlen = 0; libssh2_session_last_error(this->ssh2_sess, (char **)&errmsg, &emlen, 0); q_debug()<<"sftp set stat error: "<<errmsg; } attr.flags = LIBSSH2_SFTP_ATTR_ACMODTIME; attr.mtime = time.toTime_t(); ret = libssh2_sftp_setstat(this->ssh2_sftp, GlobalOption::instance()->remote_codec->fromUnicode(fileName), &attr); if (ret != 0) { char errmsg[200] = {0}; int emlen = 0; libssh2_session_last_error(this->ssh2_sess, (char **)&errmsg, &emlen, 0); q_debug()<<"sftp set stat error: "<<errmsg; } return 0; }
/* sftp设置文件属性 */ static int sftp_set_stat(protocol_data_t *protocol, char *filename, file_t *fileinfo) { LIBSSH2_SFTP_ATTRIBUTES attrs; if (protocol == NULL || protocol->protocol_data == NULL || filename == NULL || fileinfo == NULL) { return -1; } sftp_data_t *data = (sftp_data_t *)protocol->protocol_data; attrs.flags = LIBSSH2_SFTP_ATTR_SIZE | LIBSSH2_SFTP_ATTR_UIDGID | LIBSSH2_SFTP_ATTR_PERMISSIONS | LIBSSH2_SFTP_ATTR_ACMODTIME; attrs.filesize = fileinfo->filesize; attrs.permissions = fileinfo->permissions; attrs.uid = fileinfo->uid; attrs.gid = fileinfo->gid; attrs.mtime = fileinfo->mtime; attrs.atime = fileinfo->atime; return libssh2_sftp_setstat(data->sftp_session, filename, &attrs); }