<commit_before>﻿// MySync © 2016-2017 Damian 'Erdroy' Korczowski

using System;
using System.IO;
using System.Linq;
using MySync.Client.Core.Projects;
using MySync.Client.Utilities;

namespace MySync.Client.Core
{
    public class FileSystem
    {
        private FileSystemWatcher _fileSystemWatcher;
        
        internal FileSystem() { }

        public void Open(SFtpClient client)
        {
            Client = client;
        }

        public void Close()
        {
            Client.Close();
        }

        public void BuildFilemap()
        {
            // create file mapping
            // run file system watcher

            var rootDir = Project.LocalDirectory + "\\data\\";

            if (Mapping != null)
            {
                Changed = false;
                Mapping.Update(Project, rootDir);
                return;
            }

            Mapping = FileMapping.CreateFileMapping(Project, rootDir);

            _fileSystemWatcher?.Dispose();
            _fileSystemWatcher = new FileSystemWatcher(rootDir)
            {
                EnableRaisingEvents = true,
                IncludeSubdirectories = true,

                Filter = "*.*",

                NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.CreationTime | 
                NotifyFilters.FileName | NotifyFilters.DirectoryName
            };

            _fileSystemWatcher.Created += FileSystemWatcher_Created;
            _fileSystemWatcher.Changed += FileSystemWatcher_Changed;
            _fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
            _fileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
        }

        public void DeleteEmptyDirs(string dir)
        {
            try
            {
                foreach (var d in Directory.EnumerateDirectories(dir))
                {
                    DeleteEmptyDirs(d);
                }

                var entries = Directory.EnumerateFileSystemEntries(dir);

                if (!entries.Any())
                {
                    try
                    {
                        Directory.Delete(dir);
                    }
                    catch (UnauthorizedAccessException) { }
                    catch (DirectoryNotFoundException) { }
                }
            }
            catch (UnauthorizedAccessException) { }
        }

        public FileMapping GetRemoteMapping(int commitId = -1)
        {
            // download from the server
            // load from json
            // return

            if(commitId == -1)
            {
                commitId = Project.GetCurrentCommit();
            }

            try
            {
                var json = Client.DownloadFile(Project.RemoteDirectory + "/filemaps/filemap_" + commitId + ".json");
                return FileMapping.FromJson(json);
            }
            catch
            {<commit_msg>fixed crash when push or pull is being finalized<commit_after>﻿// MySync © 2016-2017 Damian 'Erdroy' Korczowski

using System;
using System.IO;
using System.Linq;
using MySync.Client.Core.Projects;
using MySync.Client.Utilities;

namespace MySync.Client.Core
{
    public class FileSystem
    {
        private FileSystemWatcher _fileSystemWatcher;
        
        internal FileSystem() { }

        public void Open(SFtpClient client)
        {
            Client = client;
        }

        public void Close()
        {
            Client.Close();
        }

        public void BuildFilemap()
        {
            // create file mapping
            // run file system watcher

            var rootDir = Project.LocalDirectory + "\\data\\";

            if (Mapping != null)
            {
                Changed = false;
                Mapping.Update(Project, rootDir);
                return;
            }

            Mapping = FileMapping.CreateFileMapping(Project, rootDir);

            _fileSystemWatcher?.Dispose();
            _fileSystemWatcher = new FileSystemWatcher(rootDir)
            {
                EnableRaisingEvents = true,
                IncludeSubdirectories = true,

                Filter = "*.*",

                NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.CreationTime | 
                NotifyFilters.FileName | NotifyFilters.DirectoryName
            };

            _fileSystemWatcher.Created += FileSystemWatcher_Created;
            _fileSystemWatcher.Changed += FileSystemWatcher_Changed;
            _fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
            _fileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
        }

        public void DeleteEmptyDirs(string dir)
        {
            try
            {
                foreach (var d in Directory.EnumerateDirectories(dir))
                {
                    DeleteEmptyDirs(d);
                }

                var entries = Directory.EnumerateFileSystemEntries(dir);

                if (!entries.Any())
                {
                    try
                    {
                        Directory.Delete(dir);
                    }
                    catch (UnauthorizedAccessException) { }
                    catch (DirectoryNotFoundException) { }
                }
            }
            catch (UnauthorizedAccessException) { }
            catch (DirectoryNotFoundException) { }
        }

        public FileMapping GetRemoteMapping(int commitId = -1)
        {
            // download from the server
            // load from json
            // return

            if(commitId == -1)
            {
                commitId = Project.GetCurrentCommit();
            }

            try
            {
                var json = Client.DownloadFile(Project.RemoteDirectory + "/filemaps/filemap_" + commitId + ".json");
                return FileMapping.FromJson(json);
            }
            catch
            {
{'fraction_non_alphanumeric': 0.07676490747087046, 'fraction_numerical': 0.003427004797806717, 'mean_word_length': 1.3394789579158317, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4211763', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1351, 'n_words': 280}