11using FileSyncLibNet . FileSyncJob ;
22using Microsoft . Extensions . Logging ;
33using System ;
4+ using System . Collections . Generic ;
5+ using System . ComponentModel ;
46using System . Diagnostics ;
57using System . IO ;
8+ using System . Linq ;
69using System . Net ;
10+ using System . Threading ;
711
812namespace FileSyncLibNet . SyncProviders
913{
@@ -22,38 +26,47 @@ public override void SyncSourceToDest()
2226 int copied = 0 ;
2327 int skipped = 0 ;
2428 DirectoryInfo _di = new DirectoryInfo ( JobOptions . SourcePath ) ;
25- var _fi = _di . EnumerateFiles (
26- searchPattern : JobOptions . SearchPattern ,
27- searchOption : JobOptions . Recursive ? SearchOption . AllDirectories : SearchOption . TopDirectoryOnly ) ;
2829 //Dateien ins Backup kopieren
2930 if ( JobOptions . Credentials != null )
3031 {
31-
32- }
3332
34- foreach ( FileInfo f in _fi )
33+ }
34+
35+
36+ foreach ( var dir in JobOptions . Subfolders . Count > 0 ? _di . GetDirectories ( ) : new [ ] { _di } )
3537 {
36- var relativeFilename = f . FullName . Substring ( Path . GetFullPath ( JobOptions . SourcePath ) . Length ) . TrimStart ( '\\ ' ) ;
37- var remotefile = new FileInfo ( Path . Combine ( JobOptions . DestinationPath , relativeFilename ) ) ;
38- bool copy = ! remotefile . Exists || remotefile . Length != f . Length || remotefile . LastWriteTime != f . LastWriteTime ;
39- if ( copy )
38+ if ( JobOptions . Subfolders . Count > 0 && ! JobOptions . Subfolders . Select ( x => x . ToLower ( ) ) . Contains ( dir . Name . ToLower ( ) ) )
39+ continue ;
40+ var _fi = dir . EnumerateFiles (
41+ searchPattern : JobOptions . SearchPattern ,
42+ searchOption : JobOptions . Recursive ? SearchOption . AllDirectories : SearchOption . TopDirectoryOnly ) ;
43+
44+
45+ foreach ( FileInfo f in _fi )
4046 {
41- try
47+
48+ var relativeFilename = f . FullName . Substring ( Path . GetFullPath ( JobOptions . SourcePath ) . Length ) . TrimStart ( '\\ ' ) ;
49+ var remotefile = new FileInfo ( Path . Combine ( JobOptions . DestinationPath , relativeFilename ) ) ;
50+ bool copy = ! remotefile . Exists || remotefile . Length != f . Length || remotefile . LastWriteTime != f . LastWriteTime ;
51+ if ( copy )
4252 {
43- logger . LogDebug ( "Copy {A}" , relativeFilename ) ;
44- File . Copy ( f . FullName , remotefile . FullName , true ) ;
45- copied ++ ;
53+ try
54+ {
55+ logger . LogDebug ( "Copy {A}" , relativeFilename ) ;
56+ File . Copy ( f . FullName , remotefile . FullName , true ) ;
57+ copied ++ ;
58+ }
59+ catch ( Exception exc )
60+ {
61+ logger . LogError ( exc , "Exception copying {A}" , relativeFilename ) ;
62+ }
4663 }
47- catch ( Exception exc )
64+ else
4865 {
49- logger . LogError ( exc , "Exception copying {A}" , relativeFilename ) ;
66+ skipped ++ ;
67+ logger . LogTrace ( "Skip {A}" , relativeFilename ) ;
5068 }
5169 }
52- else
53- {
54- skipped ++ ;
55- logger . LogTrace ( "Skip {A}" , relativeFilename ) ;
56- }
5770 }
5871 sw . Stop ( ) ;
5972 logger . LogInformation ( "{A} files copied, {B} files skipped in {C}s" , copied , skipped , sw . ElapsedMilliseconds / 1000.0 ) ;
@@ -74,7 +87,142 @@ void CopyFileWithBuffer(FileInfo source, FileInfo destination)
7487 //destination.Attributes = source.Attributes;
7588
7689 }
77-
90+ void EnsureAccess ( )
91+ {
92+ //#region Backup
93+ //if (!string.IsNullOrWhiteSpace(einst.BasicSettings["BackupPath"]))
94+ //{
95+ // //Dateien sichern:
96+ // bool useCredentials = ((string)einst.BasicSettings["BackupPath"]).StartsWith(@"\\");
97+ // string backupTarget = einst.BasicSettings["BackupPath"];
98+
99+ // if (backupTarget.StartsWith(@"\\")) //Netzwerk
100+ // {
101+ // try
102+ // {
103+ // //Probe network access:
104+ // bool accesible = false;
105+ // bool writeable = false;
106+ // try
107+ // {
108+ // DirectoryInfo di = new DirectoryInfo(backupTarget);
109+ // while (di != null && !di.Exists)
110+ // {
111+ // di = di.Parent;
112+ // }
113+ // if (di != null && di.Exists)
114+ // accesible = true;
115+ // if (accesible)
116+ // {
117+ // Directory.CreateDirectory(backupTarget);
118+ // File.WriteAllText(backupTarget + "\\hri_write_probing", "probing write access");
119+ // try
120+ // {
121+ // File.Delete(backupTarget + "\\hri_write_probing");
122+ // }
123+ // catch (Exception exc)
124+ // {
125+ // log.LogWarning(exc, "Exception when deleting 'hri_write_probing' file, assuming writable");
126+ // }
127+ // writeable = true;
128+ // }
129+ // }
130+ // catch { }
131+ // if (!accesible || !writeable)
132+ // {
133+ // DirectoryInfo di = new DirectoryInfo(backupTarget);
134+ // string netpath = di.Root.FullName;
135+
136+ // if (accesible) //Change credentials..
137+ // {
138+ // NetworkShare.CancelExistingConnection(netpath);
139+ // }
140+
141+ // string username = ((string)Einstellungen.BasicSettings["NetworkCredentials"]).Split(':')[0];
142+ // string password = ((string)Einstellungen.BasicSettings["NetworkCredentials"]).Split(':')[1];
143+ // NetworkCredential networkCredential = new NetworkCredential(username, password);
144+ // NetworkShare net = null;
145+ // try { net = new NetworkShare(netpath, networkCredential, true); }
146+ // catch (Win32Exception exception) { log.LogInformation("Backup: Fehler beim herstellen einer Netzwerkverbindung zu " + netpath + ":\r\n" + exception.ToString()); }
147+ // }
148+ // }
149+ // catch (Exception e)
150+ // {
151+ // log.LogError("backuptimer_tick", e);
152+ // log.LogInformation("Backup: Fehler beim Zugriff auf Netzlaufwerk:\r\n" + e.ToString());
153+ // }
154+
155+ // }
156+ // //else //Filesystem
157+ // {
158+ // try
159+ // {
160+ // //Beim Netzlaufwerk versuchen es erneut zu verbinden
161+ // string drive = backupTarget.Split(':')[0];
162+ // //if (!Map(drive + ":"))
163+ // // log.LogInformation("Fehler beim mappen des Laufwerkes " + drive + ": - Bitte Einstellungen überprüfen");
164+ // bool canWrite = true;
165+ // if (!Directory.Exists(backupTarget))
166+ // {
167+ // try
168+ // {
169+ // Directory.CreateDirectory(backupTarget);
170+ // }
171+ // catch { log.LogInformation($"Konnte nicht auf Backuppfad {backupTarget} schreiben"); canWrite = false; }
172+ // }
173+ // if (canWrite)
174+ // foreach (string folder in new string[] { "HriFFTLog", "HriShockLog", "HriLog", "HriDebugLog", "HRI" })
175+ // {
176+ // string _path = Path.Combine(ProductionDataPath, folder);
177+ // if (!Directory.Exists(_path))
178+ // Directory.CreateDirectory(_path);
179+ // string destPath = Path.Combine(backupTarget, folder);
180+ // if (Directory.Exists(backupTarget))
181+ // if (!Directory.Exists(destPath))
182+ // Directory.CreateDirectory(destPath);
183+ // DirectoryInfo _di = new DirectoryInfo(_path);
184+ // FileInfo[] _fi = _di.GetFiles();
185+ // //Dateien ins Backup kopieren
186+ // foreach (FileInfo f in _fi)
187+ // {
188+ // bool copy = false;
189+ // string remotefilename = Path.Combine(destPath, f.Name);
190+ // if (!File.Exists(remotefilename))
191+ // {
192+ // copy = true;
193+ // }
194+ // else
195+ // {
196+ // FileInfo remoteFile = new FileInfo(remotefilename);
197+ // if (remoteFile.Length != f.Length)
198+ // copy = true;
199+ // }
200+ // if (copy)
201+ // {
202+ // File.Copy(f.FullName, remotefilename, true);
203+ // Thread.Sleep(15);
204+ // }
205+ // else
206+ // {
207+ // Thread.Sleep(5);
208+ // }
209+ // }
210+ // }
211+
212+
213+ // }
214+ // catch (Exception e)
215+ // {
216+ // log.LogError("backuptimer_tick", e);
217+ // log.LogInformation("Fehler beim erstellen des Backups!\r\n" + e.ToString());
218+ // }
219+ // }
220+ //}
221+ //#endregion
222+
223+
224+ }
225+
78226
79227
80228
0 commit comments