Exemple #1
0
Fichier : Ui.cs Projet : ralfw/sync
 public void LogConflict(RepoFile repoFile)
 {
     var fgc = Console.ForegroundColor;
     Console.ForegroundColor = ConsoleColor.Red;
     Console.Write(" *** Conflict detected! *** ");
     Console.ForegroundColor = fgc;
 }
Exemple #2
0
        public void FilterUnchangedById(RepoFile repoFile, Action<RepoFile> onChangedRepoFile)
        {
            if (_syncTable.Any(e => e.RelativeFilename == repoFile.RelativeFileName &&
                                    e.Id == repoFile.Id)) return;

            onChangedRepoFile(repoFile);
        }
Exemple #3
0
 public void FilterExistingFiles(RepoFile repoFile, Action<RepoFile> onNonExistingFile)
 {
     var filename = GetAbsoluteFilename(repoFile);
     if (!File.Exists(filename)) {
         onNonExistingFile(repoFile);
     }
 }
Exemple #4
0
        public void FilterUnchangedByTimeStamp(RepoFile repoFile, Action<RepoFile> onChangedRepoFile)
        {
            if (_syncTable.Any(e => e.RelativeFilename == repoFile.RelativeFileName &&
                                    e.TimeStamp.ToString("s") == repoFile.TimeStamp.ToString("s"))) return;

            onChangedRepoFile(repoFile);
        }
Exemple #5
0
 public void DetectUpdateConflct(RepoFile fromLocalSyncTable, RepoFile fromLocalFileSystem, RepoFile changedRemoteFile,
                                 Action<RepoFile> onNoConflict, Action<RepoFile> onConflict)
 {
     if (No_local_file(fromLocalSyncTable, fromLocalFileSystem)) { onNoConflict(changedRemoteFile); return; }
     if (Local_file_is_new(fromLocalSyncTable, fromLocalFileSystem)) { onConflict(changedRemoteFile); return; }
     if (Local_file_was_updated(fromLocalSyncTable, fromLocalFileSystem)) { onConflict(changedRemoteFile); return; }
     onNoConflict(changedRemoteFile);
 }
Exemple #6
0
        public RepoFile GetTimeStamp(RepoFile repoFile)
        {
            var entry = _syncTable.Find(e => e.RelativeFilename == repoFile.RelativeFileName);
            if (entry == null) return null;

            repoFile.TimeStamp = entry.TimeStamp;
            return new RepoFile {Id=repoFile.Id, User=repoFile.User,RelativeFileName = repoFile.RelativeFileName, RepoRoot = repoFile.RepoRoot, TimeStamp = entry.TimeStamp};
        }
Exemple #7
0
        public RepoFile GetTimeStamp(RepoFile repoFile)
        {
            var filename = GetAbsoluteFilename(repoFile);
            if (!File.Exists(filename)) return null;

            var timeStamp = File.GetLastWriteTime(filename);
            return new RepoFile{Id=repoFile.Id, RelativeFileName = repoFile.RelativeFileName, RepoRoot = repoFile.RepoRoot, User = repoFile.User, TimeStamp = timeStamp};
        }
Exemple #8
0
        public void AddOrUpdateEntry(RepoFile repoFile)
        {
            DeleteEntry(repoFile);

            _syncTable.Add(SyncTableEntry.CreateFrom(repoFile));

            SyncTablePersistor.Save(_repoPath, _syncTable);
        }
Exemple #9
0
 public RepoFile DeleteEntry(RepoFile repoFile)
 {
     Dictionary<string, object> item;
     if (_parseObjects.TryFindByFieldvalue(_repoName, "relativeFilename", HttpUtility.UrlEncode(repoFile.RelativeFileName), out item))
     {
         _parseObjects.Delete(_repoName, item["objectId"].ToString());
     }
     return repoFile;
 }
Exemple #10
0
        public RepoFile DeleteEntry(RepoFile repoFile)
        {
            var entry = _syncTable.Find(e => e.RelativeFilename == repoFile.RelativeFileName);
            if (entry != null) _syncTable.Remove(entry);

            SyncTablePersistor.Save(_repoPath, _syncTable);

            return repoFile;
        }
Exemple #11
0
        public void Dont_ignore_files_with_dotSync_extension(string relativeFilename)
        {
            var sut = new IgnoreFilter(REPO_PATH, new string[]{});

            var rf = new RepoFile {RelativeFileName = relativeFilename};
            RepoFile result = null;
            sut.Filter(rf, _ => result = _);

            Equalidator.AreEqual(result, rf);
        }
Exemple #12
0
 public static SyncTableEntry CreateFrom(RepoFile repoFile)
 {
     return new SyncTableEntry
         {
             Id=repoFile.Id,
             RelativeFilename = repoFile.RelativeFileName,
             TimeStamp = repoFile.TimeStamp,
             User = repoFile.User
         };
 }
Exemple #13
0
        public void No_conflict_with_nonexistent_local_file()
        {
            var sut = new ConflictMediator();

            RepoFile result = null;
            var remote = new RepoFile { TimeStamp = new DateTime(2000, 12, 31, 12, 13, 14, 789) };
            sut.DetectUpdateConflct(null, null, remote, _ => result = _, null);

            Equalidator.AreEqual(remote, result);
        }
Exemple #14
0
        public void Conflict_with_new_file()
        {
            var sut = new ConflictMediator();

            RepoFile result = null;
            var current = new RepoFile { TimeStamp = new DateTime(2000, 7, 28, 9, 10, 11, 456) };
            var remote = new RepoFile { TimeStamp = new DateTime(2000, 12, 31, 12, 13, 14, 789) };
            sut.DetectUpdateConflct(null, current, remote, null, _ => result = _);

            Equalidator.AreEqual(remote, result);
        }
Exemple #15
0
        public void Delete_entry()
        {
            var syncTable = new List<SyncTableEntry> {new SyncTableEntry{RelativeFilename = "myfn"}};
            var sut = new RemoteSyncTable(REPO_PATH, syncTable);

            var rf = new RepoFile {RelativeFileName = "myfn"};
            var result = sut.DeleteEntry(rf);

            Equalidator.AreEqual(result, rf);
            Assert.AreEqual(0, syncTable.Count);
        }
Exemple #16
0
        public void Delete()
        {
            var rf = new RepoFile { RelativeFileName = "myfile.txt" };
            var ms = new MemoryStream(Encoding.ASCII.GetBytes("hello"));
            rf = _sut.Upload(rf, ms);

            var result = _sut.Delete(rf);

            Equalidator.AreEqual(result, rf);
            Assert.IsFalse(File.Exists(@"testrepo\" + rf.Id));
        }
Exemple #17
0
        public void No_conflict()
        {
            var sut = new ConflictMediator();

            RepoFile result = null;
            var local = new RepoFile {TimeStamp = new DateTime(2000,5,12,10,11,12, 123)};
            var current = new RepoFile {TimeStamp = new DateTime(2000,5,12,10,11,12,456)};
            var remote = new RepoFile {TimeStamp = new DateTime(2000, 12, 31, 12, 13, 14, 789)};
            sut.DetectUpdateConflct(local, current, remote, _ => result = _, null);

            Equalidator.AreEqual(remote, result);
        }
Exemple #18
0
        public void Delete_entry()
        {
            var sut = new LocalSyncTable(REPO_PATH, new List<SyncTableEntry>
                {
                    new SyncTableEntry { RelativeFilename = "myfn", Id = "myid" },
                    new SyncTableEntry { RelativeFilename = "myfn2", Id = "myid2" }
                });

            var rf = new RepoFile {RelativeFileName = "myfn2"};
            var result = sut.DeleteEntry(rf);

            Equalidator.AreEqual(result, rf);
        }
Exemple #19
0
        public void CollectRepoFiles(string repoRoot, Action<RepoFile> continueWith)
        {
            var absoluteRepoRoot = Path.GetFullPath(repoRoot);

            var files = Directory.GetFiles(absoluteRepoRoot, "*.*", SearchOption.AllDirectories);
            foreach (var file in files) {
                var repoFile = new RepoFile {
                    RepoRoot = absoluteRepoRoot,
                    RelativeFileName = file.Substring(absoluteRepoRoot.Length + 1)
                };
                continueWith(repoFile);
            }
        }
Exemple #20
0
 public RepoFile Upload(RepoFile repoFile, Stream stream)
 {
     repoFile.Id = Guid.NewGuid() + ".txt";
     var repoFilename = Build_repoFilename(repoFile);
     using(var fs = new FileStream(repoFilename, FileMode.Create, FileAccess.Write))
     {
         var buffer = new byte[16*1024];
         int n;
         while ((n = stream.Read(buffer, 0, buffer.Length)) > 0)
             fs.Write(buffer, 0, n);
     }
     return repoFile;
 }
Exemple #21
0
        public void Filter(RepoFile repoFile, Action<RepoFile> onNotIgnored)
        {
            var normalizedFilename = repoFile.RelativeFileName.Replace(@"\", "/");

            if (normalizedFilename == ".sync" || normalizedFilename == ".syncconfig")
                return;

            if (_ignorePatterns.Where(pattern => !string.IsNullOrWhiteSpace(pattern))
                               .Any(pattern => Regex.Match(normalizedFilename, pattern, RegexOptions.IgnoreCase).Success))
                return;

            onNotIgnored(repoFile);
        }
Exemple #22
0
        public void Delete_entry()
        {
            var repoFile = new RepoFile {
                                            Id = "myid" + DateTime.Now.ToString("s"),
                                            RelativeFileName = "myfntobedeleted",
                                            RepoRoot = "myroot",
                                            TimeStamp = DateTime.Now,
                                            User = "******"
                                        };
            _sut.AddEntry(repoFile);

            var result = _sut.DeleteEntry(repoFile);

            Assert.AreEqual(repoFile.Id, result.Id);
        }
Exemple #23
0
        private RepoFile ResolveConflicts(RepoFile changedRemoteFile)
        {
            ILocalSyncTable localSyncTable = new LocalSyncTable(".");
            ILocalFileSystem localFileSystem = new LocalFileSystem();
            IConflictMediator conflictMediator = new ConflictMediator();

            var fromLocalSyncTable = localSyncTable.GetTimeStamp(changedRemoteFile);
            var fromLocalFileSystem = localFileSystem.GetTimeStamp(changedRemoteFile);
            conflictMediator.DetectUpdateConflct(fromLocalSyncTable, fromLocalFileSystem, changedRemoteFile,
                                                 _ => { }, localFile => {
                                                                            localFileSystem.Rename(localFile);
                                                                            _ui.LogConflict(localFile);
                                                                        });

            return changedRemoteFile;
        }
Exemple #24
0
        public RepoFile Upload(RepoFile repoFile, Stream stream)
        {
            var uniqueFilename = "fn_" + Guid.NewGuid().ToString();
            var uploadInfo = _parseFiles.Upload(stream, uniqueFilename);
            stream.Dispose();

            var pfi = new ParseFileInfo {Url = uploadInfo.Url, Name = uploadInfo.Name};

            return new RepoFile {
                                    Id = pfi.ToString(),
                                    RelativeFileName = repoFile.RelativeFileName,
                                    RepoRoot = repoFile.RepoRoot,
                                    User = repoFile.User,
                                    TimeStamp = repoFile.TimeStamp
                                };
        }
Exemple #25
0
        public void UpdateEntry(RepoFile repoFile, Action<RepoFile> onEntryUpdated, Action<RepoFile> onNoEntry)
        {
            var entry = _syncTable.Find(_ => _.RelativeFilename == repoFile.RelativeFileName);
            if (entry != null)
            {
                var oldRepoFile = entry.ToRepoFile();

                entry.Id = repoFile.Id;
                entry.User = repoFile.User;
                entry.TimeStamp = repoFile.TimeStamp;

                SyncTablePersistor.Save(_repoPath, _syncTable);

                onEntryUpdated(oldRepoFile);
            }
            else
                onNoEntry(repoFile);
        }
Exemple #26
0
        public void Download()
        {
            var rf = new RepoFile {RelativeFileName = "myfile.txt"};
            var ms = new MemoryStream(Encoding.ASCII.GetBytes("hello"));
            rf = _sut.Upload(rf, ms);

            var result = _sut.Download(rf);

            Assert.AreEqual(rf.RelativeFileName, result.Item1.RelativeFileName);

            ms = new MemoryStream();
            using (var fs = result.Item2)
            {
                var buffer = new byte[1024];
                int n;
                while((n = fs.Read(buffer, 0, buffer.Length)) > 0)
                    ms.Write(buffer, 0, n);
            }

            ms.Seek(0, SeekOrigin.Begin);
            var sr = new StreamReader(ms);
            Assert.AreEqual("hello", sr.ReadLine());
        }
Exemple #27
0
        public void TimeStamp_from_local_file_system_is_read()
        {
            using (File.Create("new-file")) {
                ;
            }
            File.SetLastWriteTime("new-file", new DateTime(2010, 2, 4, 11, 12, 13));

            var rf = new RepoFile {RepoRoot = ".", RelativeFileName = "new-file", TimeStamp = new DateTime(2013, 1, 3)};
            var result = sut.GetTimeStamp(rf);

            Assert.AreNotSame(result, rf);
            Assert.That(result.TimeStamp, Is.EqualTo(new DateTime(2010, 2, 4, 11, 12, 13)));

            File.Delete("new-file");
        }
Exemple #28
0
        public void No_file_to_ignore()
        {
            var sut = new IgnoreFilter(REPO_PATH, new string[]{});

            RepoFile result = null;
            var rf = new RepoFile {RelativeFileName = "hello.txt"};
            sut.Filter(rf, _ => result = _);

            Equalidator.AreEqual(result, rf);
        }
Exemple #29
0
        public void Load_sync_table_upon_creation()
        {
            var sut = new RemoteSyncTable(REPO_PATH, new List<SyncTableEntry>());
            var rf = new RepoFile { Id = "myid", User = "******", TimeStamp = new DateTime(2000, 5, 12), RelativeFileName = "myfn" };
            sut.AddEntry(rf);

            sut = new RemoteSyncTable(REPO_PATH);

            RepoFile result = null;
            sut.CollectRepoFiles(_ => result = _);
            Equalidator.AreEqual(rf, result);

            sut.UpdateEntry(new RepoFile { Id = "myid2", User = "******", TimeStamp = new DateTime(1999, 12, 31), RelativeFileName = "myfn" }, _ => { }, null);
            Assert.AreEqual("myfn\tmyid2\tmyuser2\t1999-12-31T00:00:00\r\n", File.ReadAllText(REPO_PATH + @"\.sync"));
        }
Exemple #30
0
        public void Update_nonexisting_entry()
        {
            var syncTable = new List<SyncTableEntry> {new SyncTableEntry {Id = "myid", RelativeFilename = "myfn"}};
            var sut = new RemoteSyncTable(REPO_PATH, syncTable);

            var rf = new RepoFile { Id = "my new id", RelativeFileName = "my non existing fn" };
            RepoFile result = null;
            sut.UpdateEntry(rf, null, _ => result = _);

            Equalidator.AreEqual(result, rf);
        }