Page 1 of 1

Sftp RenameFTPFile problem [Solved: Needs help doc updating]

Posted: Sat Dec 14, 2024 2:05 pm
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()

Re: Sftp

Posted: Sat Dec 14, 2024 2:35 pm
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

Re: Sftp

Posted: Sat Dec 14, 2024 3:34 pm
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

Re: Sftp

Posted: Sun Dec 15, 2024 11:56 am
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.

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

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

maybe change directory is the problem that you need the full path.

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

Posted: Sun Dec 15, 2024 12:05 pm
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.

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

Posted: Sun Dec 15, 2024 8:24 pm
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