Securely copy files to NAS Filer

We have a NetApp NAS file manager that seems to fail from time to time, not sure if it depends on network problems, heavy load or Filer itself; the fact is that the usual System.IO.File.Copy(...) command sometimes works unexpectedly while it worked a minute earlier, and works again a minute after ... filer works with the CIFS file system.

I see an exception in my Log4Net log files:

System.IO.IOException: The specified network name is no longer available. in System.IO .__ Error.WinIOError (Int32 errorCode, String possibly FullPath) ...

the network command does not know what is happening and why, now I think if I can implement a simple try / retry system to copy the file and repeat the copy in case of failure, it may be System.IO.File. Copying is not intended for CIFS storages, but for regular NTFS disks or stable network storage.

Are there common .NET templates or classes suitable for this copy and retry, or should I just use the approach like in the following pseudocode?

 while(!copied && count <5) { count++; try { //here copy the file ... //if no exception copy was ok copied = true; } catch { if(count >= 5) { // Log that retry limit has been reached... } else { // make thread to wait for some time, // waiting time can be in function of count or fixed... } } } 
+6
source share
2 answers

after weeks and weeks of research, tests and pain, I finally seemed to find a working solution, decided to replace the System.IO.File.Copy method with a Microsoft Robocopy call, which is available in Win Server 2008 R2 and seems to work on the first try. It makes me comfortable that I am not reinventing the wheel, but using proven technology designed specifically for my needs. Thank you all for your answers and comments anyway.

+1
source

It also helps me. I have an old NAS server and from time to time Windows shows an error telling me that the disk is no longer available .
To control the copy process, perhaps you could use CopyFileEx (from the Windows API ) instead, as shown in the following example:

 public class SecureFileCopy { public static void CopyFile(FileInfo source, FileInfo destination, CopyFileOptions options, CopyFileCallback callback, object state) { if (source == null) throw new ArgumentNullException("source"); if (destination == null) throw new ArgumentNullException("destination"); if ((options & ~CopyFileOptions.All) != 0) throw new ArgumentOutOfRangeException("options"); new FileIOPermission( FileIOPermissionAccess.Read, source.FullName).Demand(); new FileIOPermission( FileIOPermissionAccess.Write, destination.FullName).Demand(); CopyProgressRoutine cpr = callback == null ? null : new CopyProgressRoutine(new CopyProgressData( source, destination, callback, state).CallbackHandler); bool cancel = false; if (!CopyFileEx(source.FullName, destination.FullName, cpr, IntPtr.Zero, ref cancel, (int)options)) { throw new IOException(new Win32Exception().Message); } } private class CopyProgressData { private FileInfo _source = null; private FileInfo _destination = null; private CopyFileCallback _callback = null; private object _state = null; public CopyProgressData(FileInfo source, FileInfo destination, CopyFileCallback callback, object state) { _source = source; _destination = destination; _callback = callback; _state = state; } public int CallbackHandler( long totalFileSize, long totalBytesTransferred, long streamSize, long streamBytesTransferred, int streamNumber, int callbackReason, IntPtr sourceFile, IntPtr destinationFile, IntPtr data) { return (int)_callback(_source, _destination, _state, totalFileSize, totalBytesTransferred); } } private delegate int CopyProgressRoutine( long totalFileSize, long TotalBytesTransferred, long streamSize, long streamBytesTransferred, int streamNumber, int callbackReason, IntPtr sourceFile, IntPtr destinationFile, IntPtr data); [SuppressUnmanagedCodeSecurity] [DllImport("Kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)] private static extern bool CopyFileEx( string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref bool pbCancel, int dwCopyFlags); } public delegate CopyFileCallbackAction CopyFileCallback( FileInfo source, FileInfo destination, object state, long totalFileSize, long totalBytesTransferred); public enum CopyFileCallbackAction { Continue = 0, Cancel = 1, Stop = 2, Quiet = 3 } [Flags] public enum CopyFileOptions { None = 0x0, FailIfDestinationExists = 0x1, Restartable = 0x2, AllowDecryptedDestination = 0x8, All = FailIfDestinationExists | Restartable | AllowDecryptedDestination } 

There is a more detailed description in the MSDN journal .

+2
source

Source: https://habr.com/ru/post/922411/


All Articles