Locked file after transfer

Advertisement

cosb
Joined:
Posts:
3
Location:
Canada

Locked file after transfer

Hello,
I use the winscp .net library 5.15.4 to send files to a server.
At the end of a transfer I attempt to archive the respective transferred file (i.e. put it in zip file), however I started to get occasional "System.IO.IOException: The process cannot access the file" exceptions.

My code is very simple:

protected override void ExecuteTransfer(IFileInfo fi)
{
  TransferOptions transferOptions = new TransferOptions
  {
    TransferMode = TransferMode.Binary, 
    OverwriteMode = OverwriteMode.Overwrite,
    FilePermissions = null, // This is default
    PreserveTimestamp = false
  };

  string destinationFile = $"{_ts._transferPipe.RemoteFolder}/{fi.Name}";
  _ts._session.PutFiles(@fi.FullName, destinationFile, false, transferOptions).Check();
  // If it was successful - move the file to processed

  // Archive won't throw an exception
  Archive(fi.FullName);
}

private void Archive(string fileFullName)
{

  FileInfo fileInfo = new FileInfo(fileFullName);

  try
  {
    string zipFileName = $"{_ts._transferPipe.ProcessedFolder}\\archive_{DateTime.Now:yyyyMM}.zip";

    using (var zipArchive = ZipFile.Open(zipFileName, ZipArchiveMode.Update))
    {                   
      zipArchive.CreateEntryFromFile(fileInfo.FullName,  fileInfo.Name);          
    }

    if (fileInfo.Exists)
      fileInfo.Delete();

  }
  catch (Exception ex)
  {
    _log.Error($"An error occurred while trying to archive or delete the file {fileFullName}. I am going to attempt to move it.", ex);

    string destFileName = $@"{_ts._transferPipe.ProcessedFolder}\{fileInfo.Name}";
    try
    {
      if (fileInfo.Exists)
        fileInfo.MoveTo(destFileName);
    }
    catch (Exception ex2)
    {
      _log.Error($"Moving the file {fileFullName} to {destFileName} failed as well! I will leave it here!", ex2);
      
    }
    
  }
}


The archive method fails, then moving the file fails as well. Could it be that WinScp still locks the file after the transfer? The other possibility would be that the antivirus software locks the file (Trend Micro) but the more I think about it I think it is WinScp. I investigated with Resource Monitor to see what process locked the file, however, it was too late, the lock was released.

I also started to look at the ResumeSupport property. I am not clear how this works. As far as I am concerned if a transfer fails my app is going to retry to send it. Should I set the ResumeSupport to Off?

Thanks!

Reply with quote

Advertisement

cosb
Joined:
Posts:
3
Location:
Canada

Thank you for your answer.

Could you please confirm that after the call to Check the files transferred in that session are unlocked?

I actually read that page and these pages as well:
https://winscp.net/eng/docs/library_transferresumesupport
https://winscp.net/eng/docs/script_locking_files_while_uploading

I must say the documentation is very dry. For instance, it is not clear from the documentation what is the default value for ResumeSupport.State. Is it TransferResumeSupportState.Default? Again, reading the docs on the first page:

Sets what files will be transferred with resume support/to temporary filename. Use TransferResumeSupportState.Default for built-in default (currently all files above 100 KB), TransferResumeSupportState.On for all files, TransferResumeSupportState.Off for no file (turn off) or TransferResumeSupportState.Smart for all files above threshold (see Threshold).

I have no idea what the following phrase is talking about. Where can I find the information about the "built-in default"?
Use TransferResumeSupportState.Default for built-in default (currently all files above 100 KB)

The second article made sense.

I use WinScp in a service that wakes up every minute and transfers files that haven't been transferred yet (uploads & downloads). The files are transferred one by one. Not the most efficient thing but for my requirements is good enough. If a file fails to transfer, the application will attempt to resend or get it next time. Do I need to be worried about the resume support? I didn't think so but I might be wrong. The files are usually small, a few KBs.

Thanks

Reply with quote

cosb
Joined:
Posts:
3
Location:
Canada

Just adding more to the puzzle - the files appeared to be have been transferred successfully, however, I downloaded them back and their sizes were bigger and they contained only zeros. Any idea? I checked the Trend Micro Office Scan and there is no log entry. I checked the windows event logs, and again nothing. These are ascii pgp files with encrypted content. I think it must be the antivirus, however, I would have thought that I find some kind of mention in their log.

Update: I re-run the transfer - the same files and it was successful this time.

So, I don't think it's WinScp at fault here (I should probably now head to the Trend Micro forums), but I would appreciate if you can let me know if you've seen this behaviour before, i.e. antivirus software interfering with the transfers and changing the files.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,253
Location:
Prague, Czechia

The files are unlocked as soon as they are transferred (at the time PutFiles finishes the latest).

"Resume support" has unlikely anything to do with your problem.

It's not common for an antivirus to interfere, but it happens.

Reply with quote

Advertisement

loulou296
Guest

WINSCP locks our files after transfer

We are experiencing the same problem and the file system says WINSCP has it locked.

Is there anything we can do to unlock - a parm to set ??

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,253
Location:
Prague, Czechia

Re: WINSCP locks our files after transfer

loulou296 wrote:

We are experiencing the same problem and the file system says WINSCP has it locked.

Is there anything we can do to unlock - a parm to set ??
We need to know a way more information. How are you using WinSCP? Some log files. What is WinSCP doing at the time the file is locked? Etc.

Reply with quote

Advertisement

You can post new topics in this forum