コード例 #1
0
        public void IsFileSameTest()
        {
            var tempFolder = GetTempFolder();
            var fs         = new FileSystemStorage();

            var path1 = fs.Combine(tempFolder, Guid.NewGuid().ToString());
            var path2 = fs.Combine(tempFolder, Guid.NewGuid().ToString());

            System.IO.File.WriteAllText(path1, "test");
            System.IO.File.WriteAllText(path2, "test");

            var result = fs.IsFileSame(path1, path2);

            if (result)
            {
                Trace.WriteLine($"Date signature: Files are the same: {path1}, {path2}");
                Assert.Fail();
            }

            var path3 = fs.Combine(tempFolder, Guid.NewGuid().ToString());

            fs.CopyFile(path1, path3);
            result = fs.IsFileSame(path1, path3);
            if (!result)
            {
                Trace.WriteLine($"Date signature: Files are not the same: {path1}, {path3}");
                Assert.Fail();
            }

            fs.DeleteFile(path1);
            fs.DeleteFile(path2);
            fs.DeleteFile(path3);
        }
コード例 #2
0
        public void DirectoryExistsTest()
        {
            var tempFolder = GetTempFolder();
            var fs         = new FileSystemStorage();

            var path   = fs.Combine(tempFolder, Guid.NewGuid().ToString());
            var result = fs.DirectoryExists(path);

            if (result)
            {
                Trace.WriteLine($"Directory {path} exists on file system");
                Assert.Fail();
            }

            path = fs.Combine(tempFolder, Guid.NewGuid().ToString());
            fs.CreateDirectory(path);

            result = fs.DirectoryExists(path);
            if (!result)
            {
                Trace.WriteLine($"Directory {path} does no exist on file system");
                Assert.Fail();
            }

            fs.DeleteDirectory(path);
        }
コード例 #3
0
        public void FileExistsTest()
        {
            var tempFolder = GetTempFolder();
            var fs         = new FileSystemStorage();

            var path   = fs.Combine(tempFolder, Guid.NewGuid().ToString());
            var result = fs.FileExists(path);

            if (result)
            {
                Trace.WriteLine($"File {path} exists on file system");
                Assert.Fail();
            }

            path = fs.Combine(tempFolder, Guid.NewGuid().ToString());
            System.IO.File.WriteAllText(path, "test");

            result = fs.FileExists(path);
            if (!result)
            {
                Trace.WriteLine($"File {path} does no exist on file system");
                Assert.Fail();
            }

            fs.DeleteFile(path);
        }
コード例 #4
0
        public static BackupSessionHistory LoadHistory(string path, string signature)
        {
            var m_IStorage = new FileSystemStorage();
            var fullPath   = m_IStorage.Combine(path, signature);

            var historyDir = m_IStorage.Combine(fullPath, HistoryDirectory);
            //m_IStorage.CreateDirectory(historyDir);

            string historyFile           = m_IStorage.Combine(historyDir, $"{signature}_history.json");
            BackupSessionHistory history = null;

            try
            {
                using (StreamReader fileStream = File.OpenText(historyFile))
                {
                    using (JsonTextReader fileReader = new JsonTextReader(fileStream))
                    {
                        Newtonsoft.Json.Linq.JToken jsonToken = null;
                        try
                        {
                            jsonToken = Newtonsoft.Json.Linq.JToken.ReadFrom(fileReader);
                        }
                        catch (Exception ex)
                        {
                            Trace.WriteLine($"Exception in IJagLogsImport: {ex.Message}");
                        }

                        int iCount     = jsonToken.Count();
                        var historyObj = jsonToken[0];

                        history = JsonConvert.DeserializeObject(historyObj.ToString(), typeof(BackupSessionHistory)) as BackupSessionHistory;

                        //foreach (Newtonsoft.Json.Linq.JObject obj in jsonToken)
                        //                    {
                        //                    history = JsonConvert.DeserializeObject(obj.ToString(), typeof(BackupSessionHistory)) as BackupSessionHistory;
                        //                  }
                    }
                }
            }
            catch (FileNotFoundException ex)
            {
                Trace.WriteLine("**Exception LoadHistory:\n" + ex.Message);
            }
            catch (DirectoryNotFoundException ex)
            {
                Trace.WriteLine("**Exception LoadHistory:\n" + ex.Message);
            }

            return(history);
        }
コード例 #5
0
        public void CreateDirectoryTest()
        {
            var tempFolder = GetTempFolder();
            var fs         = new FileSystemStorage();

            var path = fs.Combine(tempFolder, Guid.NewGuid().ToString());

            fs.CreateDirectory(path);

            var result = fs.DirectoryExists(path);

            if (!result)
            {
                Trace.WriteLine($"Failed to create directory: {path}");
                Assert.Fail();
            }

            fs.DeleteDirectory(path);

            result = fs.DirectoryExists(path);
            if (result)
            {
                Trace.WriteLine($"Failed to delete directory: {path}");
                Assert.Fail();
            }
        }
コード例 #6
0
        public void DeleteFileTest()
        {
            var tempFolder = GetTempFolder();
            var fs         = new FileSystemStorage();

            var path = fs.Combine(tempFolder, Guid.NewGuid().ToString());

            System.IO.File.WriteAllText(path, "test");

            var result = fs.FileExists(path);

            if (!result)
            {
                Trace.WriteLine($"Failed to create file: {path}");
                Assert.Fail();
            }

            fs.DeleteFile(path);
            result = fs.FileExists(path);
            if (result)
            {
                Trace.WriteLine($"Failed to delete file: {path}");
                Assert.Fail();
            }
        }
コード例 #7
0
        //public void SaveHistoryItem(string path, HistoryItem item)
        //{
        //    var fileName = m_Storage.GetFileName(path);
        //    var directory = m_Storage.GetDirectoryName(path);

        //    var historyDir = m_Storage.Combine(directory, HistoryDirectory);
        //    m_Storage.CreateDirectory(historyDir);

        //    var destPath = m_Storage.Combine(historyDir, fileName + "_history.json");

        //    string json = json = JsonConvert.SerializeObject(item, Formatting.Indented);

        //    File.WriteAllText(destPath, json);
        //}

        public void SaveHistory(BackupPerfectLogger logger = null)
        {
            var m_IStorage = new FileSystemStorage();
            var fullPath   = m_IStorage.Combine(HistoryData.TargetPath, HistoryData.SessionSignature);

            var historyDir = m_IStorage.Combine(fullPath, HistoryDirectory);

            m_IStorage.CreateDirectory(historyDir);

            string historyFile = m_IStorage.Combine(historyDir, $"{HistoryData.SessionSignature}_history.json");

            var historyData = new object[1];

            historyData[0] = this;
            string json = json = JsonConvert.SerializeObject(historyData, Formatting.Indented);

            File.WriteAllText(historyFile, json);
        }
コード例 #8
0
        public void CombineTest()
        {
            string path1 = @"c:\path1";
            string path2 = @"path2";

            var fs     = new FileSystemStorage();
            var result = fs.Combine(path1, path2);

            if (String.Compare(result, $"{path1}\\{path2}", true) != 0)
            {
                Trace.WriteLine($"path1: {path1}, path2: {path2}");
                Trace.WriteLine($"result: {result}");
                Assert.Fail();
            }
        }
コード例 #9
0
        public void GetFileAttributesTest()
        {
            var tempFolder = GetTempFolder();
            var fs         = new FileSystemStorage();

            var path = fs.Combine(tempFolder, Guid.NewGuid().ToString());

            System.IO.File.WriteAllText(path, "test");

            var result = fs.GetFileAttributes(path);

            if (result == 0 || ((int)result == -1))
            {
                Trace.WriteLine($"Failed to read file attribute from filr {path}");
                Assert.Fail();
            }

            fs.DeleteFile(path);
        }
コード例 #10
0
        public void MoveFileTest()
        {
            var tempFolder = GetTempFolder();
            var fs         = new FileSystemStorage();

            var testData = new List <Tuple <string, string> >
            {
                new Tuple <string, string>(@"file1.txt", @"folder2\file2.txt"),
            };

            foreach (var data in testData)
            {
                var path1 = fs.Combine(tempFolder, data.Item1);
                var path2 = fs.Combine(tempFolder, data.Item2);

                //Delete leftovers
                try { fs.DeleteFile(path1); } catch { }
                try { fs.DeleteFile(path2); } catch { }
                try { fs.DeleteDirectory(fs.GetDirectoryName(path2)); } catch { }

                System.IO.File.WriteAllText(path1, "test file");

                //simple move target folder no exists
                try
                {
                    fs.MoveFile(path1, path2);
                    Trace.WriteLine($"move to {path2} shuold fail to move file - path error");
                    Assert.Fail();
                }
                catch { }

                try { fs.CreateDirectory(fs.GetDirectoryName(path2)); } catch { }

                //simple move
                fs.MoveFile(path1, path2);
                if (!fs.FileExists(path2))
                {
                    Trace.WriteLine($"failed to move file {path2}");
                    Assert.Fail();
                }

                //file already exist
                fs.CopyFile(path2, path1);
                try
                {
                    fs.MoveFile(path1, path2);
                    Trace.WriteLine($"Moveto {path2} shuold fail- file exist");
                    Assert.Fail();
                }
                catch { }

                if (!fs.FileExists(path2))
                {
                    Trace.WriteLine($"File was deleted{path1}");
                    Assert.Fail();
                }

                if (!fs.FileExists(path2))
                {
                    Trace.WriteLine($"File not copied - override {path2}");
                    Assert.Fail();
                }

                fs.DeleteFile(path1);
                fs.DeleteFile(path2);

                var targetFileName      = fs.GetFileName(path2);
                var targetDirectoryName = path2.Substring(0, path2.Length - targetFileName.Length);
                fs.DeleteDirectory(targetDirectoryName);
            }
        }