0 votes
by (290 points)

On deleting a non existing directory on server the SFTP exception's ProtocolCode property is null even though in the rebex logs I can see that server returned error code 2 (No such file).

I am using traversal mode recursive to delete files inside the directory as well.

Below is the rebex logs:

2019-04-29 14:21:43.987 INFO FileTransferClient(1)[1] Info: Home directory is '/root'.
2019-04-29 14:21:43.997 INFO FileTransferClient(1)[1] Command: SSHFXPREALPATH (2, '/srv/ftp/testfolder/')
2019-04-29 14:21:44.309 INFO FileTransferClient(1)[1] Response: SSHFXPNAME (2, 1 item)
2019-04-29 14:21:44.312 INFO FileTransferClient(1)[1] Command: SSHFXPSTAT (3, '/srv/ftp/testfolder')
2019-04-29 14:21:44.624 INFO FileTransferClient(1)[1] Response: SSHFXPATTRS (3)
2019-04-29 14:21:48.393 INFO FileTransferClient(1)[1] Batch: Calling Delete(string = 'test', TraversalMode = 'Recursive').
2019-04-29 14:21:48.421 DEBUG FileTransferClient(1)[1] Batch: Executing multi-file operation: Deleting, source = 'test', target = '', TransferMethod.Copy, MoveMode.All, LinkProcessingMode.FollowLinks, ActionOnExistingFiles.ThrowException.
2019-04-29 14:21:48.423 DEBUG FileTransferClient(1)[1] Batch: Normalizing source path ('test').
2019-04-29 14:21:48.424 DEBUG FileTransferClient(1)[1] Batch: Checking source path ('/srv/ftp/testfolder/test').
2019-04-29 14:21:48.426 INFO FileTransferClient(1)[1] Command: SSHFXPLSTAT (4, '/srv/ftp/testfolder/test')
2019-04-29 14:21:48.847 INFO FileTransferClient(1)[1] Response: SSHFXPSTATUS (4, 2, 'No such file')

Applies to: Rebex SFTP

1 Answer

0 votes
by (144k points)

It looks like you are calling FileTransferClient object's Delete method to delete the non-existent directory. The Delete method, along with Upload, Download or GetItems, is a multi-file method that does not directly correspond to a single SFTP protocol request - it can involve multiple files and directories.

As part of its operation, the method first tried to determine whether the specified directory exists. When it determined that it doesn't (by issuing SSH_FXP_LSTAT request and receiving the response indicating the item does not exist), it failed with an SftpException with a Status of OperationFailure, indicating that the requested operation (deleting the specified directory) could not be completed - but that's not considered a SFTP-protocol-level error. Therefore, the lack of ProtocolCode in this case is by design.

On the other hand, if you used a single-file methods such as RemoveDirectory or DeleteFile to delete a directory or a file, this would result in the SFTP client issuing a single SSH_FXP_RMDIR or SSH_FXP_REMOVE request. And if the server rejected that request, an SftpException with Status of ProtocolError would be raised. THose exceptions represent SFTP-protocol-level errors and ProtocolCode is available for those.

by (290 points)
Thanks for suggesting the solution. I got the expected SFTP error code by using RemoveDirectory. But this method only deletes the empty directory.

How can I delete a directory and its contents and also receive the SFTP error code (2 NoSuchFile;NoSuchDirectory) if the directory does not exists?
by (70.2k points)
We think that the easiest solution for your case is to check directory existence before calling Delete() method. So, you can delete directories like this:

    if (client.DirectoryExists("test"))
         client.Delete("test", TraversalMode.Recursive);

Is this acceptable solution for you?
by (290 points)
Yes, this will work.
Can you please tell which property I can check in case of ProtocolCode being null to get the some useful information. So that I can handle the error based on the value.

Thanks
by (70.2k points)
The exception's Message is the most informative.
For general information you can check exception's Status and Code.
...