Skip to content

Commit f9ca00b

Browse files
Merge pull request #19 from andreaskueffel/development
remove rememberremotestate and fix maxage handling, implement deleting for abstract provider
2 parents 36afe68 + a241cf3 commit f9ca00b

11 files changed

Lines changed: 101 additions & 40 deletions

File tree

FileSyncApp/Program.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class Program
2525
public static Dictionary<string, IFileJob> Jobs = new Dictionary<string, IFileJob>();
2626
public static void Main(string[] args)
2727
{
28-
ConfigureLogger(args.FirstOrDefault());
28+
ConfigureLogger(args?.FirstOrDefault());
2929
log = LoggerFactory.CreateLogger("FileSyncAppMain");
3030
if (null != args && args.Length > 0)
3131
{
@@ -50,6 +50,7 @@ static void RunProgram()
5050
ContractResolver = new IgnorePropertyResolver(new string[] { "Logger" }),
5151
TypeNameHandling = TypeNameHandling.Auto,
5252
};
53+
5354
if (!File.Exists("config.json"))
5455
{
5556
log.LogInformation("Config file {A} not found, creating a new one", "config.json");
@@ -96,20 +97,32 @@ static void RunProgram()
9697
var json = JsonConvert.SerializeObject(jobOptions, Formatting.Indented, jsonSettings);
9798
File.WriteAllText("config.json", json);
9899
}
100+
99101
log.LogInformation("reading config file {A}", "config.json");
100-
var readJobOptions = JsonConvert.DeserializeObject<Dictionary<string, IFileJobOptions>>(File.ReadAllText("config.json"), jsonSettings);
101-
//List<IFileJob> Jobs = new List<IFileJob>();
102+
Dictionary<string, IFileJobOptions> readJobOptions = new Dictionary<string, IFileJobOptions>();
103+
try
104+
{
105+
readJobOptions = JsonConvert.DeserializeObject<Dictionary<string, IFileJobOptions>>(File.ReadAllText("config.json"), jsonSettings);
106+
}
107+
catch (Exception exc)
108+
{
109+
log.LogCritical(exc, "exception reading config file {A}", "config.json");
110+
return;
111+
}
102112

103113
foreach (var jobOption in readJobOptions)
104114
{
105-
106115
jobOption.Value.Logger = LoggerFactory.CreateLogger(jobOption.Key);
107116
Jobs.Add(jobOption.Key, FileSyncJob.CreateJob(jobOption.Value));
108117
}
109118
JobsReady?.Invoke(null, EventArgs.Empty);
119+
Dictionary<IFileJob, bool> jobsDone = new Dictionary<IFileJob, bool>();
110120
foreach (var job in Jobs)
111121
{
122+
jobsDone.Add(job.Value, false);
123+
job.Value.JobFinished += (s, e) => { jobsDone[(IFileJob)s] = true; if (jobsDone.All(x => x.Value)) if (!File.Exists("fullsync.done")) File.Create("fullsync.done"); };
112124
job.Value.StartJob();
125+
113126
}
114127
log.LogInformation("Press Ctrl+C to exit");
115128
while (keepRunning)

FileSyncAppWin/MainForm.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ public partial class MainForm : Form
44
{
55
Thread consoleThread;
66

7-
public MainForm()
7+
public MainForm(string[] args)
88
{
99
InitializeComponent();
1010
this.FormClosing += (s, e) => { FileSyncApp.Program.keepRunning = false; consoleThread?.Join(10_000); };
1111
consoleThread = new Thread(() =>
1212
{
13-
FileSyncApp.Program.Main(null);
13+
FileSyncApp.Program.Main(args);
1414
});
1515
FileSyncApp.Program.JobsReady += (s, e) =>
1616
{

FileSyncAppWin/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static void Main(string[] args)
5252
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
5353
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
5454
ApplicationConfiguration.Initialize();
55-
mainForm = new MainForm();
55+
mainForm = new MainForm(args);
5656
Application.Run(mainForm);
5757
}
5858
catch (Exception exception)

FileSyncLibNet/AccessProviders/FileIoAccessProvider.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public FileIoAccessProvider(ILogger logger, string stateFilename)
1717
this.logger = logger;
1818
if (!string.IsNullOrEmpty(stateFilename))
1919
remoteState = new RemoteState(stateFilename);
20-
20+
2121
}
2222
public void UpdateAccessPath(string accessPath)
2323
{
@@ -31,7 +31,7 @@ public void CreateDirectory(string path)
3131

3232
public FileInfo2 GetFileInfo(string path)
3333
{
34-
if(null!=remoteState)
34+
if (null != remoteState)
3535
return remoteState.GetFileInfo(Path.Combine(AccessPath, path));
3636

3737
var fi = new FileInfo(Path.Combine(AccessPath, path));
@@ -43,7 +43,7 @@ public FileInfo2 GetFileInfo(string path)
4343
};
4444
}
4545

46-
public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, string path = null, bool recursive = false, List<string> subfolders = null)
46+
public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, string path = null, bool recursive = false, List<string> subfolders = null, bool olderFiles = false)
4747
{
4848
DirectoryInfo _di = new DirectoryInfo(AccessPath);
4949
List<FileInfo2> ret_val = new List<FileInfo2>();
@@ -57,10 +57,12 @@ public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, s
5757
searchPattern: pattern,
5858
searchOption: recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
5959

60-
61-
ret_val.AddRange(_fi.Select(x => new FileInfo2(x.FullName.Substring(AccessPath.Length + 1), x.Exists) { Length = x.Length, LastWriteTime = x.LastWriteTime }).ToList());
60+
ret_val.AddRange(_fi.Where(x => (olderFiles ? x.LastWriteTime <= minimumLastWriteTime : x.LastWriteTime >= minimumLastWriteTime))
61+
.Select(x => new FileInfo2(x.FullName.Substring(AccessPath.Length + 1), x.Exists) { Length = x.Length, LastWriteTime = x.LastWriteTime })
62+
.ToList());
6263
}
63-
catch (Exception exc) {
64+
catch (Exception exc)
65+
{
6466
logger?.LogError(exc, "exception in GetFiles for path {A}, dir {B}", path, dir);
6567
}
6668
}

FileSyncLibNet/AccessProviders/IAccessProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal interface IAccessProvider
99
{
1010
void CreateDirectory(string path);
1111
void UpdateAccessPath(string path);
12-
List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, string path = null, bool recursive = false, List<string> subfolders = null);
12+
List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, string path = null, bool recursive = false, List<string> subfolders = null, bool olderFiles=false);
1313
FileInfo2 GetFileInfo(string path);
1414
void Delete(FileInfo2 fileInfo);
1515
Stream GetStream(FileInfo2 file);

FileSyncLibNet/AccessProviders/ScpAccessProvider.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal class ScpAccessProvider : IAccessProvider
1717
SftpClient ftpClient;
1818
private readonly RemoteState remoteState;
1919
public string AccessPath { get; private set; }
20-
20+
2121
private readonly ILogger logger;
2222
public ScpAccessProvider(NetworkCredential credentials, ILogger logger, string stateFilename)
2323
{
@@ -137,7 +137,7 @@ public bool MatchesPattern(string fileName, string pattern)
137137
return Regex.IsMatch(fileName, regexPattern, RegexOptions.IgnoreCase);
138138
}
139139

140-
public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, string path = null, bool recursive = false, List<string> subfolders = null)
140+
public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, string path = null, bool recursive = false, List<string> subfolders = null, bool olderFiles = false)
141141
{
142142
//add try catch for non existent folders
143143
EnsureConnected();
@@ -156,7 +156,7 @@ public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, s
156156
try
157157
{
158158
var files = ftpClient.ListDirectory(basePath);
159-
var sepChar="/";
159+
var sepChar = "/";
160160
if (recursive)
161161
{
162162
foreach (var folder in files.Where(x => x.IsDirectory && !(x.Name == ".") && !(x.Name == "..")))
@@ -165,8 +165,8 @@ public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, s
165165
ret_val.AddRange(GetFiles(minimumLastWriteTime, pattern, subPath, recursive));
166166
}
167167
}
168-
ret_val.AddRange(files.Where(x => MatchesPattern(x.Name, pattern)).Where(x => x.LastWriteTime >= minimumLastWriteTime && !x.IsDirectory).Select(x =>
169-
new FileInfo2($"{(AccessPath.Length + 1<basePath.Length?(basePath.Substring(AccessPath.Length+1))+sepChar:string.Empty)}{x.Name}", exists: true) { LastWriteTime = x.LastWriteTime, Length = x.Length }).ToList());
168+
ret_val.AddRange(files.Where(x => MatchesPattern(x.Name, pattern)).Where(x => (olderFiles ? x.LastWriteTime <= minimumLastWriteTime : x.LastWriteTime >= minimumLastWriteTime) && !x.IsDirectory).Select(x =>
169+
new FileInfo2($"{(AccessPath.Length + 1 < basePath.Length ? (basePath.Substring(AccessPath.Length + 1)) + sepChar : string.Empty)}{x.Name}", exists: true) { LastWriteTime = x.LastWriteTime, Length = x.Length }).ToList());
170170
}
171171
catch (Exception exc)
172172
{

FileSyncLibNet/FileSyncJob/FileSyncJob.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class FileSyncJob : IFileJob
1818
private readonly Timer timer;
1919
private readonly ISyncProvider syncProvider;
2020
private volatile bool v_jobRunning = false;
21+
public static bool InitialFullSync { get; set; } = false;
2122

2223
private FileSyncJob(IFileJobOptions fileSyncJobOptions)
2324
{

FileSyncLibNet/FileSyncJob/FileSyncJobOptions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class FileSyncJobOptions : FileJobOptionsBase, IFileSyncJobOptions
1414
public bool SyncDeleted { get; set; } = false;
1515
public bool DeleteSourceAfterBackup { get; set; } = false;
1616
public bool RememberLastSync { get; set; } = true;
17-
public bool RememberRemoteState { get; set; } = false;
1817
public TimeSpan MaxAge { get; set; }
1918

2019
public FileSyncJobOptions()

FileSyncLibNet/FileSyncJob/FileSyncJobOptionsBuilder.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ public IFileSyncJobOptionsBuilderSetProperties RememberLastSync(bool rememberLas
5858
jobOptions.RememberLastSync = rememberLastSync;
5959
return this;
6060
}
61-
public IFileSyncJobOptionsBuilderSetProperties RememberRemoteState(bool rememberRemoteState)
62-
{
63-
jobOptions.RememberRemoteState = rememberRemoteState;
64-
return this;
65-
}
61+
6662
public IFileSyncJobOptionsBuilderSetProperties WithMaxAge(TimeSpan maxAge)
6763
{
6864
jobOptions.MaxAge = maxAge;

FileSyncLibNet/FileSyncJob/IFileSyncJobOptions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public interface IFileSyncJobOptions : IFileJobOptions
99
bool DeleteSourceAfterBackup { get; set; }
1010
bool SyncDeleted { get; set; }
1111
bool RememberLastSync { get; set; }
12-
bool RememberRemoteState { get; set; }
1312
TimeSpan MaxAge { get; set; }
1413
}
1514
}

0 commit comments

Comments
 (0)