Exemplo n.º 1
0
        public void Store_Directory()
        {
            var fileSystem = new MockFileSystem();

            fileSystem.RegisterDirectory(@"C:\dir");

            var dateTime = new MockDateTime(new DateTime(2010, 10, 1, 0, 0, 0));

            var serviceContainer = new ManualServiceContainer();

            serviceContainer.Register <IFileSystem>(fileSystem);
            serviceContainer.Register <IDateTimeService>(dateTime);

            var executionContext = new ExecutionContext(serviceContainer);

            var directory = new DirectoryObject(@"C:\dir", fileSystem);

            var storage         = new DirectoryStorage(@"C:\backup");
            var storageInstance = storage.CreateInstance(executionContext);

            var backupContext = new BackupContext(null, executionContext);
            var result        = storageInstance.Store(directory, backupContext).ToArray();

            var expected = new IOperation[] {
                new CreateDirectoryOperation(new DirectoryObject(@"C:\backup\2010-10-01 00-00-00\C\dir", fileSystem), fileSystem, executionContext)
            };

            CollectionAssert.AreEqual(expected, result);
        }
Exemplo n.º 2
0
        public void TestExists()
        {
            var testDir = new DirectoryInfo(Path.Combine(GetPath(), "TestDir"));

            testDir.Create();
            testDir.Refresh();

            try
            {
                Assert.IsTrue(testDir.Exists);

                var storage = new DirectoryStorage(GetDirectory());
                Assert.IsTrue(storage.Exists("TestDir"));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                testDir.Refresh();
                if (testDir.Exists)
                {
                    testDir.Delete(true);
                }
            }
        }
Exemplo n.º 3
0
        public void TestGet()
        {
            // Preparation
            var directories = new List <DirectoryInfo>()
            {
                new DirectoryInfo(Path.Combine(GetPath(), "Directory0")),
                new DirectoryInfo(Path.Combine(GetPath(), "Directory1"))
            };

            directories.ForEach(d =>
            {
                d.Create();
                d.Refresh();
            });

            try
            {
                var storage = new DirectoryStorage(GetDirectory());
                int index   = 0;
                directories.ForEach(d =>
                {
                    Assert.IsTrue(d.Exists);

                    var dir = storage.Get($"Directory{index}");
                    Assert.IsTrue(dir.Exists);
                    Assert.AreEqual(d.FullName, dir.FullName);

                    index++;
                });

                index = 0;
                foreach (var d in storage.GetAll())
                {
                    Assert.IsTrue(d.Exists);
                    Assert.AreEqual(directories[index].FullName, d.FullName);

                    index++;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                directories.ForEach(d =>
                {
                    d.Refresh();
                    if (d.Exists)
                    {
                        d.Delete(true);
                    }
                });
            }
        }
Exemplo n.º 4
0
        private async Task DontCreateFileWhenReading()
        {
            await Create("@ace", "some_other_file").DisposeAsync();

            var storage = new DirectoryStorage(_tmpDir.FullName);
            var mods    = await storage.GetMods(CancellationToken.None);

            await mods.Values.Single().OpenRead("/mod.cpp", CancellationToken.None);

            Assert.False(File.Exists(Path.Join(_tmpDir.FullName, "@ace", "mod.cpp")));
        }
Exemplo n.º 5
0
        private async Task GetMods()
        {
            await using var file = Create("@ace", "mod.cpp");
            await file.WriteLineAsync("Ey yo");

            var storage = new DirectoryStorage(_tmpDir.FullName);

            var mods = await storage.GetMods(CancellationToken.None);

            Assert.Single(mods);
            Assert.Contains("@ace", (IDictionary <string, IStorageMod>)mods);
        }
Exemplo n.º 6
0
        LocaleFileStorage GetFileList()
        {
            LocaleFileStorage files       = new LocaleFileStorage();
            DirectoryStorage  directories = ReceiveIncludedDirectories();

            foreach (var entry in directories)
            {
                FillFileListRecursively(entry.DirectoryPath, files, entry.State, 1);
            }

            return(files);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Overloaded. Initializes a new instance of ContentManager.
        /// </summary>
        /// <param name="game"></param>
        public ContentManager(Game game, string contentDirectory = "Content")
        {
            this.Game = game;

            this.contentDirectory = contentDirectory;

            content = new Dictionary <string, Item>();
            loaders = ContentLoader.GatherContentLoaders()
                      .Select(clt => (ContentLoader)Activator.CreateInstance(clt))
                      .ToList();

            vtStorage = new DirectoryStorage(Path.Combine(contentDirectory, ".vtstorage"));
        }
Exemplo n.º 8
0
        private async Task CreateNestedFile()
        {
            await using var file = Create("@ace", "mod.cpp");
            await file.WriteLineAsync("Ey yo");

            var storage = new DirectoryStorage(_tmpDir.FullName);
            var mods    = await storage.GetMods(CancellationToken.None);

            await using var newFile = await(mods.Values.Single().OpenWrite("/addons/addon2.pbo", CancellationToken.None));
            await newFile.WriteAsync(new byte[] { 1, 2, 3 });

            newFile.Close();
            // TODO: check file contents, file still in use
        }
Exemplo n.º 9
0
        public void TestMove()
        {
            // Preparation
            DirectoryInfo to = new DirectoryInfo(Path.Combine(GetPath(), "MoveTo"));

            if (to.Exists)
            {
                to.Delete(true);
            }
            DirectoryInfo from = new DirectoryInfo(Path.Combine(GetPath(), "MoveFrom"));

            from.Create();

            from.Refresh();
            to.Refresh();

            try
            {
                Assert.IsTrue(from.Exists);
                Assert.IsFalse(to.Exists);

                var storage = new DirectoryStorage(GetDirectory());
                storage.Move("MoveTo", from);

                from.Refresh();
                to.Refresh();
                Assert.IsFalse(from.Exists);
                Assert.IsTrue(to.Exists);
            }
            catch (Exception e)
            {
                Debug.Log(e.StackTrace);
                throw e;
            }
            finally
            {
                from.Refresh();
                if (from.Exists)
                {
                    from.Delete(true);
                }
                to.Refresh();
                if (to.Exists)
                {
                    to.Delete(true);
                }
            }
        }
Exemplo n.º 10
0
        DirectoryStorage ReceiveIncludedDirectories()
        {
            DirectoryStorage directories = new DirectoryStorage();

            var result = _retrieve("SELECT `path`, `state` FROM `updates_include`");

            if (result == null)
            {
                return(directories);
            }

            do
            {
                var field = result.Fetch();

                var path = field[0].GetString();
                if (path.Substring(0, 1) == "$")
                {
                    path = path.Substring(1);
                }
                if (path.Substring(0, 1) == "/")
                {
                    path = path.Substring(1);
                }

                path = Path.Combine(_sourceDirectory, path);
                if (!Directory.Exists(path))
                {
                    FEL_LOG_WARN("sql.updates", "DBUpdater: Given update include directory \"{0}\" does not exist, skipped!", path);
                    continue;
                }

                directories.Add(new DirectoryEntry(path, field[1].GetString().ToEnum <State>()));

                FEL_LOG_TRACE("sql.updates", "Added applied file \"{0}\" from remote.", Path.GetFileName(path));
            } while (result.NextRow());

            result.Dispose();

            return(directories);
        }
Exemplo n.º 11
0
        public void TestRestoreBackup()
        {
            var testBackup = new DirectoryInfo(Path.Combine(GetPath(), "test_backup"));

            testBackup.Create();
            testBackup.Refresh();

            var test = new DirectoryInfo(Path.Combine(GetPath(), "test"));

            test.Refresh();

            try
            {
                Assert.IsTrue(testBackup.Exists);

                var storage  = new DirectoryStorage(GetDirectory());
                var restored = storage.RestoreBackup();
                Assert.AreEqual(1, restored.Count);
                Assert.AreEqual("test", restored[0]);
                Assert.IsTrue(storage.Exists("test"));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                testBackup.Refresh();
                test.Refresh();
                if (testBackup.Exists)
                {
                    testBackup.Delete(true);
                }
                if (test.Exists)
                {
                    test.Delete(true);
                }
            }
        }
Exemplo n.º 12
0
        public void Store_Drive()
        {
            var fileSystem = new MockFileSystem();

            fileSystem.RegisterDrive(@"C:");

            var dateTime = new MockDateTime(new DateTime(2010, 10, 1, 0, 0, 0));

            var serviceContainer = new ManualServiceContainer();

            serviceContainer.Register <IFileSystem>(fileSystem);
            serviceContainer.Register <IDateTimeService>(dateTime);

            var executionContext = new ExecutionContext(serviceContainer);

            var drive = new DriveObject(@"C:", fileSystem);

            var storage         = new DirectoryStorage(@"C:\backup");
            var storageInstance = storage.CreateInstance(executionContext);

            var backupContext = new BackupContext(null, executionContext);
            var result        = storageInstance.Store(drive, backupContext).ToArray();
        }
Exemplo n.º 13
0
        public void BackupDirectoryPath()
        {
            var fileSystem = new MockFileSystem();

            fileSystem.RegisterFile(@"C:\file");

            var dateTime = new MockDateTime(new DateTime(2010, 10, 1, 0, 0, 0));

            var serviceContainer = new ManualServiceContainer();

            serviceContainer.Register <IFileSystem>(fileSystem);
            serviceContainer.Register <IDateTimeService>(dateTime);

            var executionContext = new ExecutionContext(serviceContainer);

            var file = new FileObject(@"C:\file", fileSystem);

            var storage         = new DirectoryStorage(@"C:\backup");
            var storageInstance = (DirectoryStorageInstance)storage.CreateInstance(executionContext);

            var expected = Path.Parse(@"C:\backup").Combine(dateTime.GetCurrentDateTime().ToString("yyyy-MM-dd HH-mm-ss"));

            Assert.AreEqual(expected, storageInstance.BackupDirectoryPath);
        }
Exemplo n.º 14
0
        public void TestCopy()
        {
            // Preparation
            DirectoryInfo to     = new DirectoryInfo(Path.Combine(GetPath(), "CopyTo"));
            FileInfo      toText = new FileInfo(Path.Combine(to.FullName, "text"));

            if (to.Exists)
            {
                to.Delete(true);
            }

            DirectoryInfo from     = new DirectoryInfo(Path.Combine(GetPath(), "CopyFrom"));
            FileInfo      fromText = new FileInfo(Path.Combine(from.FullName, "text"));

            from.Create();
            File.WriteAllText(fromText.FullName, "test");

            Action refreshState = () =>
            {
                from.Refresh();
                fromText.Refresh();
                to.Refresh();
                toText.Refresh();
            };

            refreshState();

            try
            {
                Assert.IsTrue(from.Exists);
                Assert.IsTrue(fromText.Exists);
                Assert.AreEqual("test", File.ReadAllText(fromText.FullName));
                Assert.IsFalse(to.Exists);
                Assert.IsFalse(toText.Exists);

                var storage = new DirectoryStorage(GetDirectory());
                storage.Copy("CopyTo", from);

                refreshState();
                Assert.IsTrue(from.Exists);
                Assert.IsTrue(fromText.Exists);
                Assert.AreEqual("test", File.ReadAllText(fromText.FullName));
                Assert.IsTrue(to.Exists);
                Assert.IsTrue(toText.Exists);
                Assert.AreEqual("test", File.ReadAllText(toText.FullName));

                File.WriteAllText(fromText.FullName, "test2");
                storage.Copy("CopyTo", from);

                refreshState();
                Assert.IsTrue(from.Exists);
                Assert.IsTrue(fromText.Exists);
                Assert.AreEqual("test2", File.ReadAllText(fromText.FullName));
                Assert.IsTrue(to.Exists);
                Assert.IsTrue(toText.Exists);
                Assert.AreEqual("test2", File.ReadAllText(toText.FullName));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                to.Refresh();
                from.Refresh();
                if (to.Exists)
                {
                    to.Delete(true);
                }
                if (from.Exists)
                {
                    from.Delete(true);
                }
            }
        }
Exemplo n.º 15
0
        public void TestDelete()
        {
            var directories = new List <DirectoryInfo>()
            {
                new DirectoryInfo(Path.Combine(GetPath(), "Dir0")),
                new DirectoryInfo(Path.Combine(GetPath(), "Dir1")),
                new DirectoryInfo(Path.Combine(GetPath(), "Dir2")),
                new DirectoryInfo(Path.Combine(GetPath(), "Dir3")),
            };

            directories.ForEach(d =>
            {
                d.Create();
                d.Refresh();
            });

            try
            {
                directories.ForEach(d =>
                {
                    Assert.IsTrue(d.Exists);
                });

                var storage = new DirectoryStorage(GetDirectory());
                for (int i = 0; i < directories.Count; i++)
                {
                    Assert.IsTrue(storage.Exists($"Dir{i}"));
                }
                storage.Delete("Dir3");
                for (int i = 0; i < directories.Count; i++)
                {
                    directories[i].Refresh();
                    if (i == directories.Count - 1)
                    {
                        Assert.IsFalse(directories[i].Exists);
                        Assert.IsFalse(storage.Exists($"Dir{i}"));
                    }
                    else
                    {
                        Assert.IsTrue(directories[i].Exists);
                        Assert.IsTrue(storage.Exists($"Dir{i}"));
                    }
                }

                storage.DeleteAll();
                for (int i = 0; i < directories.Count; i++)
                {
                    directories[i].Refresh();
                    Assert.IsFalse(directories[i].Exists);
                    Assert.IsFalse(storage.Exists($"Dir{i}"));
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                directories.ForEach(d =>
                {
                    d.Refresh();
                    if (d.Exists)
                    {
                        d.Delete(true);
                    }
                });
            }
        }