Sftp RenameFTPFile problem [Solved: Needs help doc updating]

Just starting out? Need help? Post your questions and find answers here.
User avatar
parsec
New User
New User
Posts: 8
Joined: Sat Dec 14, 2024 9:33 am

Sftp RenameFTPFile problem [Solved: Needs help doc updating]

Post by parsec »

Hello there, PB noob entering the chat...

I'm currently evaluating PureBasic and its SFTP feature in particular. I have problems with the code below -- RenameFTPFile fails (as does DeleteFTPFile) every single time. Opening connection, setting remote directory and uploading files works fine. So... am I doing something wrong here or is there an issue with said procedures?

The target machine is an Ubuntu Linux 20.04 while the PB app is running on Win11.

Code: Select all

; Set EXE format to Console in Compiler options
Procedure SFTP_Test()
  ; Set server, paths and credentials to real values before testing
  Protected server$ = "sftp://server.com", usr$ = "user123", pwd$ = "password123"
  Protected rmtDir$ = "/home/user/Downloads" 
  Protected srcFile$ = "c:\temp\image.png", tempFile$ = "_tmp.image.png", destFile$ = "image.png"
  
  If OpenFTP(0, server$, usr$, pwd$, #True, 22)
    If SetFTPDirectory(0, rmtDir$)
      PrintN("Remote directory is " + GetFTPDirectory(0))
      
      SendFTPFile(0, srcFile$, tempFile$)
      
      If RenameFTPFile(0, tempFile$, destFile$) = 0
        PrintN("Error renaming '" + tempFile$ +"'")  ;<=== Always fails
      EndIf
    Else
      PrintN("Unable To set remote directory: "+ rmtDir$)
    EndIf
  
  CloseFTP(0)
Else
  PrintN("Failed to connect to: " + server$)
EndIf 

EndProcedure

OpenConsole()
SFTP_Test()
CloseConsole()
Last edited by parsec on Sun Dec 15, 2024 11:59 am, edited 1 time in total.
User avatar
Kiffi
Addict
Addict
Posts: 1499
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Sftp

Post by Kiffi »

untested, but does this also happen if you use a Delay() between SendFTPFile() and RenameFTPFile()?

Code: Select all

      SendFTPFile(0, srcFile$, tempFile$)
      
      Delay(1000) ; wait a little bit...
      
      If RenameFTPFile(0, tempFile$, destFile$) = 0
        PrintN("Error renaming '" + tempFile$ +"'")  ;<=== Always fails
      EndIf
Hygge
infratec
Always Here
Always Here
Posts: 7616
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Sftp

Post by infratec »

I can only test ftps, but ftp uses RNFR and RNTO
sftp uses RENAME.

But in my libcurl zip file is also an example for sftp rename.
Maybe you can test this.

viewtopic.php?t=80118
User avatar
parsec
New User
New User
Posts: 8
Joined: Sat Dec 14, 2024 9:33 am

Re: Sftp

Post by parsec »

Kiffi wrote: Sat Dec 14, 2024 2:35 pm untested, but does this also happen if you use a Delay() between SendFTPFile() and RenameFTPFile()?
@Kiffi: Thanks for your suggestion. There is no difference when a Delay is inserted.

@infratec: Unfortunately I can't test your library atm because of running into the PB Demo line count limit. It does seem very useful though, will surely revisit it once I buy a PB license.

I did figure out what the problem was by examining the auth.log on the Linux server and comparing PB and WinSCP sftp sessions.

There are two main gotcha's/issues with RenameFTPFile() when connecting over sftp:

* Contrary to the help docs, RenameFTPFile must be provided with full paths to the files that are to be renamed. SetFTPDirectory followed by a RenameFTPFile call using file names only will always fail.

* RenameFTPFile will (incorrectly?) return 0 even when renaming was successful. Not sure if this applies to only SFTP or FTP as well (haven't tested the latter)


(DeleteFTPFile() has the same problem -- works with full paths but always returns 0)

So once the initial problem was sorted out I had a red herring error message as a secondary problem due to the RenameFTPFile proc return value. Once I examined the logs and files on the server I could see that it actually worked if full paths are used.
infratec
Always Here
Always Here
Posts: 7616
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Sftp RenameFTPFile problem [Solved: Needs help doc updating]

Post by infratec »

Hm ...

maybe change directory is the problem that you need the full path.
User avatar
parsec
New User
New User
Posts: 8
Joined: Sat Dec 14, 2024 9:33 am

Re: Sftp RenameFTPFile problem [Solved: Needs help doc updating]

Post by parsec »

infratec wrote: Sun Dec 15, 2024 12:02 pm Hm ...

maybe change directory is the problem that you need the full path.
SetFTPDirectory is called with full path already. And no errors are reported in the auth.log for this operation.


EDIT: And I also verify this with GetFTPDirectory before uploading.
Fred
Administrator
Administrator
Posts: 18204
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Sftp RenameFTPFile problem [Solved: Needs help doc updating]

Post by Fred »

Thanks, SFTP is quite new in PB so it will probably needs some more testing to be bullet proof. I will take a closer look to your findings
Post Reply