4

I am using WinSCP .NET Assembly in PowerShell for file synchronization with a SFTP server. I am using the following code for synchronization:
http://winscp.net/eng/docs/library_session_synchronizedirectories#powershell

Problem is, when this script sync files through upload to SFTP it generates error, although it actually uploads the file. The script gets terminated immediately with the following error:

Upload of C:\FileSync\files\test2.txt succeeded

Permissions of /Reports/test2.txt kept with their defaults

Setting timestamp of /Reports/test2.txt failed:
WinSCP.SessionRemoteException: Upload of file 'test2.txt' was successful, but error occurred while setting the permissions and/or timestamp.
If the problem persists, turn off setting permissions or preserving timestamp. Alternatively you can turn on 'Ignore permission errors' option.
---> WinSCP.SessionRemoteException: The server does not support the operation.
Error code: 8
Error message from server: SSHServerAPI.SFTP.fxp_attrs
--- End of inner exception stack trace ---


I am not finding any way how to "ignore permission error" as it's suggested in the errors.

The script does not complain when doing the syncchroniation through downloading files from SFTP.

Any help please?

Community
  • 1
  • 1
cloudify
  • 73
  • 1
  • 7

1 Answers1

1

The error is documented here:
https://winscp.net/eng/docs/message_preserve_time_perm

  • I assume you did not enable setting permissions (it's off by default). If you did, turn it off by setting the TransferOptions.FilePermissions (see below).

  • Your server probably does not support updating timestamps of remote file. That makes it complicated to allow local-to-remote synchronization of files against such server as the timestamps are primary criteria to compare the files.

  • In general it does not make sense to turn off updating timestamp with synchronization as the update is basically an integral part of the synchronization.

  • The hint to "Ignore permission errors" is there for basic file transfers. As mentioned already, it does not make sense for synchronization.

So all you can to is to:

It's questionable though how such synchronization is meaningful.

$transferOptions = New-Object WinSCP.TransferOptions
...
$transferOptions.FilePermissions = $Null # This is default
$transferOptions.PreserveTimestamp = $False

$synchronizationResult = $session.SynchronizeDirectories(
    [WinSCP.SynchronizationMode]::Remote, "d:\www", "/home/martin/public_html",
    $False, $False, [WinSCP.SynchronizationCriteria]::Size)
Martin Prikryl
  • 147,050
  • 42
  • 335
  • 704
  • Martin, many thanks. Your code update worked for me. A question though, why you think such synchronization may not be meaningful? – cloudify Apr 06 '16 at 12:56
  • Well, synchronizing based on file size is not very reliable. For example, if you change just one byte/letter in the file, the size does not change. – Martin Prikryl Apr 06 '16 at 14:09
  • Martin, how can I instead fix this issue on the server-side, so that the user in question actually _does_ have permission to change file permissions/timestamps? – Bjonnfesk Jan 15 '19 at 21:24
  • That's pretty broad question. It depends on what server you are using. As the [article](https://winscp.net/eng/docs/message_preserve_time_perm) linked at the very beginning on my answer says: *On some systems (e.g. Linux), you need to be an owner of the file (write permissions are not enough) to modify its permissions or timestamp.* - If you need more details, please ask a proper question (on [su] - as this is not a programming question) – Martin Prikryl Jan 16 '19 at 06:42