开发者

LibCurl SFTP Rename file

开发者 https://www.devze.com 2023-02-06 04:48 出处:网络
SOLVED ... After more shifting around, by using the \"rename\" as quoted command, it required the full path inclusive of the original name and full path inclusive of rename-to destination.I didn\'t t

SOLVED ...

After more shifting around, by using the "rename" as quoted command, it required the full path inclusive of the original name and full path inclusive of rename-to destination. I didn't try with the path in my other frustrated attempts.


Hi all, I've tried all different combinations, keep failing and don't know what I'm missing. I'm trying to do a simple file rename on a SFTP site using Libcurl. I've looked all over for answers, but keep coming up short.

BTW... For this testing, there is only one file name in the folder, so no issue about existing file overwrites... And I'm the login user who originally uploaded the file for any possible "permissions" issues.

My first test was to get the what would be expected as simple syntax correct, but resulted otherwise. I start开发者_如何学Ced with a NON-SFTP site first... just regular FTP.

// ex: fpt://mysite.com/subpathNeeded/
curl_easy_setopt(MyCurl, CURLOPT_URL, RemotePath );  


// need a "QUOTE" command before rename will occur in postQuote
strcpy_s( NewCmd, _countof(NewCmd), "PWD \0" );  
quotelist = curl_slist_append(quotelist, NewCmd ); 

// NOW, we can issue the rename from and rename to commands
strcpy_s( RenameFrom, _countof(RenameFrom), "RNFR " );
strcat_s( RenameFrom, _countof(RenameFrom), RemoteCurrentFileName );
postquotelist = curl_slist_append( postquotelist, RenameFrom );

strcpy_s( RenameTo, _countof(RenameTo), "RNTO " );
strcat_s( RenameTo, _countof(RenameTo), RemoteRenameToName );
postquotelist = curl_slist_append( postquotelist, RenameTo );

curl_easy_setopt(MyCurl, CURLOPT_QUOTE, quotelist ); 
curl_easy_setopt(MyCurl, CURLOPT_POSTQUOTE, postquotelist ); 

// NOW, perform the print working directory, then rename...
MyCurlResult = curl_easy_perform(MyCurl); 

This works no problem. So now, I switch over to SFTP and it fails... By researching, SFTP doesn't like "PWD", but does allow "pwd" (case issue), no problem. Then, it doesn't like the RNFR and RNTO but does accept "mv" (move). So, if I'm in SFTP mode, I change to

// lower case "pwd" print working directory
strcpy_s( NewCmd, _countof(NewCmd), "pwd\0" );
quotelist = curl_slist_append(quotelist, NewCmd ); 

// "mv" = move "originalfile" "newfile"
strcpy_s( RenameFrom, _countof(RenameFrom), "mv \"\0" );
strcat_s( RenameFrom, _countof(RenameFrom), RemoteCurrentFileName );
strcat_s( RenameFrom, _countof(RenameFrom), "\" \"\0" );
strcat_s( RenameFrom, _countof(RenameFrom), RemoteRenameToName );
strcat_s( RenameFrom, _countof(RenameFrom), "\"\0" );
postquotelist = curl_slist_append( postquotelist, RenameFrom );

I then get a CURL ERROR when I perform this...

However, if I don't try the "mv" command and only send the QUOTE command of "pwd", it goes through fine and it does properly list the expected folder/subdirectory I'm trying to rename the file in. So I know its in the proper directory. The values I have in the "RemoteCurrentFileName" and "RemoteRenameToName" are just the stems of the file, no full path to them respectively. I've also tried including the full path as well and both versions fail. Ex:

RemoteCurrentFileName = "FileIWantToRename.txt"
    or 
RemoteCurrentFileName = "/subpathNeeded/FileIWantToRename.txt"

Additionally, I've looked at the documentation and noted "rename" is a valid "QUOTE" command and tried that too..

rename "original file" "new file"

and that failed too

This should not be this difficult to do. What simple/stupid am I missing... This is totally frustrating.


SOLVED ...

After more shifting around, by using the "rename" as quoted command, it required the full path inclusive of the original name and full path inclusive of rename-to destination. I didn't try with the path in my other frustrated attempts

strcpy_s( RenameFrom, _countof(RenameFrom), "rename \"\0" );
strcat_s( RenameFrom, _countof(RenameFrom), RemoteCurrentFileNameWithFullPath );
strcat_s( RenameFrom, _countof(RenameFrom), "\" \"\0" );
strcat_s( RenameFrom, _countof(RenameFrom), RemoteRenameToNameWithFullPath );
strcat_s( RenameFrom, _countof(RenameFrom), "\"\0" );
quotelist = curl_slist_append( quotelist, RenameFrom );

MyCurlResult = curl_easy_perform(MyCurl); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号