Skip to content

Commit fe05867

Browse files
committed
changed from dictionary to object
1 parent f7b7874 commit fe05867

2 files changed

Lines changed: 109 additions & 48 deletions

File tree

FolderSync/FileProps.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Security.Cryptography;
2+
3+
namespace FolderSync
4+
{
5+
public class FileProps
6+
{
7+
private readonly string _fileName;
8+
private readonly string _absoluteFilePath;
9+
private readonly string _absolutePath;
10+
private readonly string _relativeFilePath;
11+
private readonly string _relativePath;
12+
private readonly string _md5Code;
13+
14+
public FileProps(string file, string sourceDir)
15+
{
16+
_fileName = Path.GetFileName(file);
17+
_absoluteFilePath = file;
18+
_relativeFilePath = file.Replace(sourceDir, "");
19+
_absolutePath = Path.GetDirectoryName(file);
20+
_relativePath = _relativeFilePath.Replace(_fileName, "");
21+
using (var md5 = MD5.Create())
22+
{
23+
using (var stream = File.OpenRead(_absoluteFilePath))
24+
{
25+
var hash = md5.ComputeHash(stream);
26+
_md5Code = BitConverter.ToString(hash).Replace("-", "").ToLower();
27+
}
28+
}
29+
}
30+
31+
public string FileName => _fileName;
32+
public string AbsoluteFilePath => _absoluteFilePath;
33+
public string AbsolutePath => _absolutePath;
34+
public string RelativeFilePath => _relativeFilePath;
35+
public string RelativePath => _relativePath;
36+
public string md5Code => _md5Code;
37+
}
38+
}

FolderSync/Program.cs

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.IO;
33
using Microsoft.Extensions.Configuration;
4-
using System.Security.Cryptography;
4+
55
using System.Runtime.InteropServices;
66

77
namespace FolderSync
@@ -49,11 +49,11 @@ private static void ParseArgs(IConfiguration config)
4949
break;
5050
case "log":
5151
if (Directory.Exists(kvp.Value))
52-
logFilePath = Path.Combine(kvp.Value, $"SyncLog_{DateTime.Now.ToString("dd-MM-yyyy HH:mm")}.log");
52+
logFilePath = Path.Combine(kvp.Value, $"SyncLog_{DateTime.Now.ToString("dd-MM-yyyy")}.log");
5353
else
5454
{
5555
Console.WriteLine($"The directory {kvp.Value} does not exist, defaulting to {Path.Combine(Directory.GetCurrentDirectory(), "Log.log")}");
56-
logFilePath = Path.Combine(Directory.GetCurrentDirectory(), $"SyncLog_{DateTime.Now.ToString("dd-MM-yyyy HH:mm")}.log");
56+
logFilePath = Path.Combine(Directory.GetCurrentDirectory(), $"SyncLog_{DateTime.Now.ToString("dd-MM-yyyy")}.log");
5757
}
5858
break;
5959
default:
@@ -70,17 +70,55 @@ private static void InvalidPath(string value)
7070

7171
private static void Sync()
7272
{
73-
string[] sourceFiles = RemoveRoot(Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories), ref sourceFolder);
74-
string[] sourceDirs = RemoveRoot(Directory.GetDirectories(sourceFolder, "*", SearchOption.AllDirectories), ref sourceFolder);
75-
string[] destFiles = RemoveRoot(Directory.GetFiles(destinationFolder, "*", SearchOption.AllDirectories), ref destinationFolder);
76-
string[] destDirs = RemoveRoot(Directory.GetDirectories(destinationFolder, "*", SearchOption.AllDirectories), ref destinationFolder);
77-
78-
73+
List<FileProps> sourceFileList = new List<FileProps>();
74+
List<FileProps> backupFileList = new List<FileProps>();
75+
FileProps sourceProps;
76+
FileProps backupProps;
77+
string[] sourceFiles = Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories);
78+
foreach (string file in sourceFiles)
79+
{
80+
sourceProps = new FileProps(file, sourceFolder);
81+
sourceFileList.Add(sourceProps);
82+
}
7983

80-
// if (!AreEqual(sourceDirs, destDirs))
81-
// {
84+
string[] destFiles = Directory.GetFiles(destinationFolder, "*", SearchOption.AllDirectories);
85+
foreach (string file in destFiles)
86+
{
87+
backupProps = new FileProps(file, destinationFolder);
88+
backupFileList.Add(backupProps);
89+
}
8290

83-
// }
91+
foreach(var file in sourceFileList)
92+
{
93+
var matchingBackupByFile = backupFileList.FindAll(backup => backup.RelativeFilePath == file.RelativeFilePath); // the problem here is if there are multiple occurrences of the same file
94+
var matchingBackupByMD5 = backupFileList.FindAll(backup => backup.md5Code == file.md5Code); // the problem here is if there are multiple occurrences of the same md5
95+
96+
// if (backupDict.ContainsKey(kvp.Key) && IsSameMD5(backupDict[kvp.Key], sourceDict[kvp.Key])) // nothing to do here, both files are the same
97+
// continue;
98+
// else if (!backupDict.ContainsKey(kvp.Key) && !backupDict.ContainsValue(kvp.Value)) // the original file has no backup, it will be created here
99+
// {
100+
// string newFile = Path.Combine(destinationFolder, kvp.Key);
101+
// string newDirectory = Path.GetDirectoryName(newFile);
102+
// if (!Directory.Exists(newDirectory))
103+
// Directory.CreateDirectory(newDirectory);
104+
// File.Copy(Path.Combine(sourceFolder, kvp.Key), newFile);
105+
// Log(Path.GetFileName(newFile), newDirectory, Actions.created);
106+
// }
107+
// else if (backupDict.ContainsKey(kvp.Key) && !IsSameMD5(backupDict[kvp.Key], sourceDict[kvp.Key])) // the original file was modified
108+
// {
109+
// string file = Path.Combine(destinationFolder, kvp.Key);
110+
// File.Delete(file);
111+
// File.Copy(Path.Combine(sourceFolder, kvp.Key), file);
112+
// Log(Path.GetFileName(file), Path.GetDirectoryName(file), Actions.edited);
113+
// }
114+
// else if (!backupDict.ContainsKey(kvp.Key) && backupDict.ContainsValue(kvp.Value)) // the original file was moved or renamed
115+
// {
116+
// // check if it was renamed
117+
// string originalPath = RemoveRoot(Path.GetDirectoryName(Path.Combine(sourceFolder, kvp.Key)), ref sourceFolder); // why am i not getting back the stripped path?
118+
119+
// // check if it was moved
120+
// }
121+
}
84122
}
85123

86124
private static string[] RemoveRoot(string[] fsCollection, ref string root)
@@ -92,44 +130,25 @@ private static string[] RemoveRoot(string[] fsCollection, ref string root)
92130
return fsCollection.OrderBy(s=>s).ToArray();
93131
}
94132

95-
private static bool AreEqual(string[] source, string[] destination)
133+
private static string RemoveRoot(string path, ref string root)
96134
{
97-
if (source.Length != destination.Length)
98-
return false;
99-
100-
for (int i = 0; i < source.Length; i++)
101-
{
102-
if (source[i] != destination[i])
103-
return false;
104-
}
105-
106-
return true;
107-
}
108-
109-
private static string CalculateMD5(string file)
110-
{
111-
using (var md5 = MD5.Create())
112-
{
113-
using (var stream = File.OpenRead(file))
114-
{
115-
var hash = md5.ComputeHash(stream);
116-
return BitConverter.ToString(hash).Replace("-", "").ToLower();
117-
}
118-
}
135+
path = path.Replace(root, "");
136+
return path;
119137
}
120138

121-
private static bool CompareMd5(string origin, string backup)
139+
private static bool IsSameMD5(string origin, string backup)
122140
{
123-
return String.Equals(CalculateMD5(origin), CalculateMD5(backup), StringComparison.OrdinalIgnoreCase);
141+
return String.Equals(origin, backup, StringComparison.OrdinalIgnoreCase);
124142
}
125143

126144
enum Actions
127145
{
128-
Created,
129-
Deleted,
130-
Copied,
131-
Renamed,
132-
Moved
146+
created,
147+
deleted,
148+
copied,
149+
renamed,
150+
moved,
151+
edited
133152
}
134153

135154
private static void Log(string filename, string fullDestinationName, Actions action)
@@ -138,15 +157,16 @@ private static void Log(string filename, string fullDestinationName, Actions act
138157
string grammar = "";
139158
switch (action)
140159
{
141-
case Actions.Created:
160+
case Actions.created:
161+
case Actions.edited:
142162
grammar = "in";
143163
break;
144-
case Actions.Deleted:
164+
case Actions.deleted:
145165
grammar = "from";
146166
break;
147-
case Actions.Copied:
148-
case Actions.Renamed:
149-
case Actions.Moved:
167+
case Actions.copied:
168+
case Actions.renamed:
169+
case Actions.moved:
150170
grammar = "to";
151171
break;
152172
default:
@@ -155,8 +175,11 @@ private static void Log(string filename, string fullDestinationName, Actions act
155175
string logTemplate = $"{dt} - {filename} was {action} {grammar} {fullDestinationName}";
156176
Console.WriteLine(logTemplate);
157177

178+
if (!Directory.Exists(Path.GetDirectoryName(logFilePath)))
179+
Directory.CreateDirectory(Path.GetDirectoryName(logFilePath));
180+
158181
if (!File.Exists(logFilePath))
159-
File.Create(logFilePath);
182+
File.Create(logFilePath).Dispose();
160183

161184
using (var logFile = File.AppendText(logFilePath))
162185
{

0 commit comments

Comments
 (0)