Beispiel #1
0
        /// <summary>
        /// Create filemap from json string.
        /// </summary>
        /// <param name="json">The json string.</param>
        /// <returns>The built filemap.</returns>
        public static Filemap FromJson(string json)
        {
            // deserialize object from `json` string.
            var filemap = new Filemap
            {
                _files = new List <File>(JsonConvert.DeserializeObject <File[]>(json))
            };

            return(filemap);
        }
Beispiel #2
0
        /// <summary>
        /// Calculate diff between this filemap and given filemap.
        /// </summary>
        /// <param name="filemap">The filemap which is the 'latest' filemap.</param>
        /// <returns>The file diff map.</returns>
        public FileDiff[] GetDiff(Filemap filemap)
        {
            var diff = new List <FileDiff>();

            // find new files of modified
            Parallel.ForEach(filemap._files, (file) =>
            {
                if (!IsPresent(file.FileName))
                {
                    // this file has been created
                    // add to diff map
                    diff.Add(new FileDiff
                    {
                        FileName = file.FileName,
                        DiffType = FileDiff.Type.Created,
                        Version  = file.Version
                    });
                    return;
                }

                if (IsChanged(file))
                {
                    // this file is changed
                    // add to diff map
                    diff.Add(new FileDiff
                    {
                        FileName = file.FileName,
                        DiffType = FileDiff.Type.Changed,
                        Version  = file.Version
                    });
                }
            });

            // find deleted files
            Parallel.ForEach(_files, (file) =>
            {
                if (!filemap.IsPresent(file.FileName))
                {
                    // this file is deleted
                    // add to diff map
                    diff.Add(new FileDiff
                    {
                        FileName = file.FileName,
                        DiffType = FileDiff.Type.Delete
                    });
                }
            });

            return(diff.ToArray());
        }
Beispiel #3
0
        /// <summary>
        /// Build filemap for `root` directory.
        /// </summary>
        /// <param name="root">The root directory.</param>
        /// <param name="ignores">The ignore files.</param>
        /// <returns>The built filemap.</returns>
        public static Filemap Build(string root, string[] ignores)
        {
            var filemap = new Filemap();

            if (!Directory.Exists(root))
            {
                throw new DirectoryNotFoundException();
            }

            // find all files
            var files = Directory.GetFiles(root, "*.*", SearchOption.AllDirectories);

            // build file version map
            foreach (var file in files)
            {
                // build relative file name
                var filename = file.Replace("\\", "/").Remove(0, root.Length);

                // check if file is excluded
                if (FileUtils.IsExcluded(filename, ignores))
                {
                    continue;
                }

                if (filename.Contains(".mysync"))
                {
                    continue;
                }

                // check exclusions
                if (ignores != null)
                {
                }

                // check file info
                var fileinfo = new FileInfo(file);
                filemap._files.Add(new File
                {
                    FileName = filename,
                    Version  = fileinfo.LastWriteTime.ToBinary()
                });
            }

            return(filemap);
        }