예제 #1
0
        public override void DeleteNote(INote inote)
        {
            var note = (FilesystemNote)inote;

            if (note.IsConflictNote)
            {
                return;
            }

            if (File.Exists(note.PathRemote))
            {
                ANFileSystemUtil.DeleteFileAndFolderIfEmpty(FilesystemPlugin.Name, _logger, _config.Folder, note.PathRemote);
            }
        }
예제 #2
0
        private FilesystemNote ReadNoteFromPath(string path)
        {
            var info = new FileInfo(path);

            var note = new FilesystemNote(Guid.NewGuid(), _config);

            using (note.SuppressDirtyChanges())
            {
                note.Title            = Path.GetFileNameWithoutExtension(info.FullName);
                note.Path             = ANFileSystemUtil.GetDirectoryPath(_config.Folder, info.DirectoryName);
                note.Text             = File.ReadAllText(info.FullName, _config.Encoding);
                note.CreationDate     = info.CreationTime;
                note.ModificationDate = info.LastWriteTime;
                note.PathRemote       = info.FullName;
                note.IsLocked         = info.IsReadOnly;
            }

            return(note);
        }
예제 #3
0
        public override RemoteUploadResult UploadNoteToRemote(ref INote inote, out INote conflict, ConflictResolutionStrategy strategy)
        {
            FilesystemNote note = (FilesystemNote)inote;

            var path = note.GetPath(_config);

            if (File.Exists(note.PathRemote) && path != note.PathRemote && !File.Exists(path))
            {
                _logger.Debug(FilesystemPlugin.Name, "Upload note to changed remote path");                 // path changed and new path does not exist

                WriteNoteToPath(note, path);
                conflict = null;
                ANFileSystemUtil.DeleteFileAndFolderIfEmpty(FilesystemPlugin.Name, _logger, _config.Folder, note.PathRemote);
                note.PathRemote = path;
                return(RemoteUploadResult.Uploaded);
            }
            else if (File.Exists(note.PathRemote) && path != note.PathRemote && File.Exists(path))
            {
                _logger.Debug(FilesystemPlugin.Name, "Upload note to changed remote path");                 // path changed and new path does exist

                var conf = ReadNoteFromPath(note.PathRemote);
                if (conf.ModificationDate > note.ModificationDate)
                {
                    conflict = conf;
                    if (strategy == ConflictResolutionStrategy.UseClientCreateConflictFile || strategy == ConflictResolutionStrategy.UseClientVersion || strategy == ConflictResolutionStrategy.ManualMerge)
                    {
                        WriteNoteToPath(note, path);
                        ANFileSystemUtil.DeleteFileAndFolderIfEmpty(FilesystemPlugin.Name, _logger, _config.Folder, note.PathRemote);
                        note.PathRemote = path;
                        return(RemoteUploadResult.Conflict);
                    }
                    else
                    {
                        note.PathRemote = path;
                        return(RemoteUploadResult.Conflict);
                    }
                }
                else
                {
                    WriteNoteToPath(note, path);
                    conflict = null;
                    ANFileSystemUtil.DeleteFileAndFolderIfEmpty(FilesystemPlugin.Name, _logger, _config.Folder, note.PathRemote);
                    note.PathRemote = path;
                    return(RemoteUploadResult.Uploaded);
                }
            }
            else if (File.Exists(path))             // normal update
            {
                var conf = ReadNoteFromPath(path);
                if (conf.ModificationDate > note.ModificationDate)
                {
                    conflict = conf;
                    if (strategy == ConflictResolutionStrategy.UseClientCreateConflictFile || strategy == ConflictResolutionStrategy.UseClientVersion)
                    {
                        WriteNoteToPath(note, path);
                        if (note.PathRemote != "")
                        {
                            ANFileSystemUtil.DeleteFileAndFolderIfEmpty(FilesystemPlugin.Name, _logger, _config.Folder, note.PathRemote);
                        }
                        note.PathRemote = path;
                        return(RemoteUploadResult.Conflict);
                    }
                    else
                    {
                        note.PathRemote = path;
                        return(RemoteUploadResult.Conflict);
                    }
                }
                else
                {
                    WriteNoteToPath(note, path);
                    conflict        = null;
                    note.PathRemote = path;
                    return(RemoteUploadResult.Uploaded);
                }
            }
            else             // new file
            {
                WriteNoteToPath(note, path);
                conflict        = null;
                note.PathRemote = path;
                return(RemoteUploadResult.Uploaded);
            }
        }
예제 #4
0
        private static bool SyncNotesToFolder(List <AugmentedNote> reponotes, string targetFolder)
        {
            var dataRepo   = reponotes.ToList();
            var dataSystem = new List <Tuple <string, string> >();          // <rel_path, content>

            foreach (var file in FileSystemUtil.EnumerateFilesDeep(targetFolder, 16, new[] { ".git" }))
            {
                if (!(file.ToLower().EndsWith(".txt") || file.ToLower().EndsWith(".md")))
                {
                    continue;
                }

                var rpath = FileSystemUtil.MakePathRelative(file, targetFolder);
                var txt   = File.ReadAllText(file, Encoding.UTF8);

                dataSystem.Add(Tuple.Create(rpath, txt));
            }

            var files_nochange = new List <string>();

            for (int i = dataRepo.Count - 1; i >= 0; i--)
            {
                var match = dataSystem.FirstOrDefault(ds => ds.Item1 == dataRepo[i].RelativePath && ds.Item2 == dataRepo[i].Content);
                if (match == null)
                {
                    continue;
                }

                // Note exists in repo and filesystem with same content - everything is ok

                files_nochange.Add(match.Item1);

                dataSystem.Remove(match);
                dataRepo.RemoveAt(i);
            }

            if (dataSystem.Count == 0 && dataRepo.Count == 0)
            {
                LoggerSingleton.Inst.Debug("LocalGitMirror", "SyncNotesToFolder found 0 differences", string.Join("\n", files_nochange));
                return(false);
            }

            var files_deleted = new List <string>();

            for (int i = dataSystem.Count - 1; i >= 0; i--)
            {
                if (dataRepo.Any(ds => ds.RelativePath == dataSystem[i].Item1))
                {
                    continue;
                }

                // Note exists in filesystem but not in repo - delete it

                //var noteRepo = null;
                var noteFSys = dataSystem[i];

                files_deleted.Add(noteFSys.Item1);

                dataSystem.RemoveAt(i);
                var fpath = Path.Combine(targetFolder, noteFSys.Item1);
                File.Delete(fpath);

                LoggerSingleton.Inst.Info("LocalGitMirror", $"File deleted: '{noteFSys.Item1}'", fpath);
            }

            var files_modified = new List <string>();

            for (int i = dataSystem.Count - 1; i >= 0; i--)
            {
                var match = dataRepo.FirstOrDefault(ds => ds.RelativePath == dataSystem[i].Item1 && ds.Content == dataSystem[i].Item2);
                if (match == null)
                {
                    continue;
                }

                // Note exists in filesystem and in repo but with different content - modify it

                var noteRepo = match;
                var noteFSys = dataSystem[i];

                files_modified.Add(noteFSys.Item1);
                dataSystem.RemoveAt(i);
                dataRepo.Remove(noteRepo);

                var fpath = Path.Combine(targetFolder, noteRepo.RelativePath);
                File.WriteAllText(fpath, noteRepo.Content, new UTF8Encoding(false));

                LoggerSingleton.Inst.Info("LocalGitMirror", $"File modified: '{noteRepo.RelativePath}'", fpath);
            }

            var files_created = new List <string>();

            for (int i = dataRepo.Count - 1; i >= 0; i--)
            {
                // Note exists in repo but not in filesystem - create it

                var noteRepo = dataRepo[i];
                //var noteFSys = null;

                files_created.Add(noteRepo.RelativePath);
                dataRepo.RemoveAt(i);

                var fpath = Path.Combine(targetFolder, noteRepo.RelativePath);
                Directory.CreateDirectory(Path.GetDirectoryName(fpath));
                File.WriteAllText(fpath, noteRepo.Content, new UTF8Encoding(false));

                LoggerSingleton.Inst.Info("LocalGitMirror", $"File created: '{noteRepo.RelativePath}'", fpath);
            }

            var dir_deleted = new List <string>();

            foreach (var dir in FileSystemUtil.EnumerateEmptyDirectories(targetFolder, 16).ToList())
            {
                dir_deleted.Add(dir);
                ANFileSystemUtil.DeleteDirectoryWithRetry(LoggerSingleton.Inst, dir);

                var rpath = FileSystemUtil.MakePathRelative(dir, targetFolder);
                LoggerSingleton.Inst.Info("LocalGitMirror", $"Directory dleted: '{rpath}'", dir);
            }

            LoggerSingleton.Inst.Debug("LocalGitMirror", "SyncNotesToFolder found multiple differences",
                                       "Unchanged:\n{\n" + string.Join("\n", files_nochange.Select(p => "    " + p)) + "\n}\n\n" +
                                       "Deleted:\n{\n" + string.Join("\n", files_deleted.Select(p => "    " + p)) + "\n}\n\n" +
                                       "Created:\n{\n" + string.Join("\n", files_created.Select(p => "    " + p)) + "\n}\n\n" +
                                       "Modified:\n{\n" + string.Join("\n", files_modified.Select(p => "    " + p)) + "\n}\n\n" +
                                       "Empty-Directories:\n{\n" + string.Join("\n", dir_deleted.Select(p => "    " + p)) + "\n}\n\n");

            return(true);
        }