/// <summary>
        ///
        /// </summary>
        /// <param name="settings"></param>
        protected void DeployFonts(AppBaseStorageDeployerSettings settings)
        {
            // Nada que desplegar
            if (settings.fonts?.Any() != true)
            {
                return;
            }

            // TODO: Newer windows versions allow for per-user font registration, we should implement that
            // because with this approach fonts are installed first come first-served, and cannot be updated
            // if necessary. Plus we don't know who owns a font, se we can't delete them on cleanup

            var utilsFont = new UtilsFont();

            foreach (var font in settings.fonts.AsIterable())
            {
                // Check that the provided path is a zip with the font
                var fontSourceZip = Path.Combine(this.Deployment.appPath, font.Path);

                if (Path.GetExtension(fontSourceZip) != ".zip")
                {
                    throw new Exception($"Unable to install the font '{font.Path}' because fonts have to be in a .zip file.");
                }

                if (!File.Exists(fontSourceZip))
                {
                    throw new Exception($"Font file not found '{font.Path}'.");
                }

                // We use a know directory
                var fontTempPath          = UtilsSystem.GetTempPath("font-" + Guid.NewGuid());
                var fontPersitentTempPath = UtilsSystem.GetTempPath("font-chef");

                try
                {
                    ZipFile.ExtractToDirectory(fontSourceZip, fontTempPath);
                    UtilsSystem.CopyFilesRecursively(new DirectoryInfo(fontTempPath), new DirectoryInfo(fontPersitentTempPath), true, true);
                    utilsFont.InstallFont(fontPersitentTempPath);
                }
                finally
                {
                    UtilsSystem.DeleteDirectory(fontTempPath, this.Logger, 8);

                    // This will some fonts get locked by the native API without explanation
                    try
                    {
                        UtilsSystem.DeleteDirectory(fontPersitentTempPath, this.Logger, 4);
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void TestSymlinksAreNotRemoved()
        {
            var logger = new TestLogsLogger(this, nameof(this.TestSymlinksAreNotRemoved));

            string testPath = UtilsSystem.GetTempPath("symlink_test" + Guid.NewGuid());

            var pathWebsite            = Path.Combine(testPath, "website");
            var pathContentsPersistent = Path.Combine(testPath, "content_store_persistent");

            Directory.CreateDirectory(pathWebsite);
            Directory.CreateDirectory(pathContentsPersistent);
            Directory.CreateDirectory(Path.Combine(pathContentsPersistent, "empty_directory"));

            string linkedDir  = Path.Combine(pathWebsite, "contents");
            string linkedDir2 = Path.Combine(pathWebsite, "contents2", "contents");

            UtilsSystem.EnsureDirectoryExists(linkedDir);
            UtilsSystem.EnsureDirectoryExists(linkedDir2);

            UtilsJunction.EnsureLink(linkedDir, pathContentsPersistent, logger, true, linkType: UtilsJunction.LinkTypeRequest.Junction);
            UtilsJunction.EnsureLink(linkedDir2, pathContentsPersistent, logger, true, linkType: UtilsJunction.LinkTypeRequest.Symlink);

            Assert.True(UtilsJunction.IsJunctionOrSymlink(linkedDir));
            Assert.True(UtilsJunction.IsJunctionOrSymlink(linkedDir2));

            string fileInContentsPeristent = Path.Combine(pathContentsPersistent, "test.txt");
            string fileInSymlinkDir        = Path.Combine(linkedDir, "test.txt");

            Assert.Equal(fileInContentsPeristent, UtilsJunction.ResolvePath(fileInSymlinkDir));

            string fileInContentsPeristent2 = Path.Combine(pathContentsPersistent, "test2.txt");
            string fileInSymlinkDir2        = Path.Combine(linkedDir2, "test2.txt");

            Assert.Equal(fileInContentsPeristent2, UtilsJunction.ResolvePath(fileInSymlinkDir2));

            File.WriteAllText(fileInSymlinkDir, "testfile");
            File.WriteAllText(fileInSymlinkDir2, "testfile");

            Assert.True(File.Exists(fileInSymlinkDir), $"File exists {fileInSymlinkDir}");
            Assert.True(File.Exists(fileInSymlinkDir2), $"File exists {fileInSymlinkDir2}");
            Assert.True(File.Exists(fileInContentsPeristent), $"File exists {fileInContentsPeristent}");
            Assert.True(File.Exists(fileInContentsPeristent2), $"File exists {fileInContentsPeristent2}");

            // If we delete the directory containing the symlink, the file still exists
            UtilsSystem.DeleteDirectory(pathWebsite, logger);
            Assert.False(Directory.Exists(pathWebsite), "Directory exists " + pathWebsite);

            Assert.False(File.Exists(fileInSymlinkDir), $"File exists {fileInSymlinkDir}");
            Assert.True(File.Exists(fileInContentsPeristent), $"File exists {fileInContentsPeristent}");
            Assert.False(File.Exists(fileInSymlinkDir2), $"File exists {fileInSymlinkDir2}");
            Assert.True(File.Exists(fileInContentsPeristent2), $"File exists {fileInContentsPeristent2}");

            Directory.Delete(testPath, true);
        }
Ejemplo n.º 3
0
        public void TestResolveJunctionPath()
        {
            var logger = new TestLogsLogger(this, nameof(this.TestResolveJunctionPath));

            string testPath = UtilsSystem.GetTempPath("symlink_test" + Guid.NewGuid());

            // Probar resolución de nivel 1

            string test1OriginalPath = UtilsSystem.EnsureDirectoryExists(Path.Combine(testPath, "test1"), true);
            string test1LinkPath     = Path.Combine(testPath, "test1_link");
            string test1JunctionPath = Path.Combine(testPath, "test1_junction");

            UtilsJunction.EnsureLink(test1LinkPath, test1OriginalPath, logger, true, linkType: UtilsJunction.LinkTypeRequest.Symlink);
            UtilsJunction.EnsureLink(test1JunctionPath, test1OriginalPath, logger, true, linkType: UtilsJunction.LinkTypeRequest.Junction);

            Assert.Equal(test1OriginalPath, UtilsJunction.ResolvePath(test1LinkPath));
            Assert.Equal(test1OriginalPath, UtilsJunction.ResolvePath(test1JunctionPath));

            // Probar resolución de subdirectorio existente y no existente

            string test2OriginalPath = UtilsSystem.EnsureDirectoryExists(Path.Combine(testPath, "test2"), true);
            string test2LinkPath     = Path.Combine(testPath, "test2_link");
            string test2JunctionPath = Path.Combine(testPath, "test2_junction");

            UtilsJunction.EnsureLink(test2LinkPath, test2OriginalPath, logger, true, linkType: UtilsJunction.LinkTypeRequest.Symlink);
            UtilsJunction.EnsureLink(test2JunctionPath, test2OriginalPath, logger, true, linkType: UtilsJunction.LinkTypeRequest.Junction);

            string test2LinkSubDir     = UtilsSystem.EnsureDirectoryExists(Path.Combine(test2LinkPath, "sub1", "sub2"), true);
            string test2JunctionSubDir = UtilsSystem.EnsureDirectoryExists(Path.Combine(test2JunctionPath, "sub3", "sub4"), true);

            Assert.Equal(Path.Combine(test2OriginalPath, "sub1", "sub2"), UtilsJunction.ResolvePath(test2LinkSubDir));
            Assert.Equal(Path.Combine(test2OriginalPath, "sub3", "sub4"), UtilsJunction.ResolvePath(test2JunctionSubDir));

            // Ahora subdirectorios que no existen
            Assert.Equal(Path.Combine(test2OriginalPath, "sub4", "sub5"), UtilsJunction.ResolvePath(Path.Combine(test2LinkPath, "sub4", "sub5")));
            Assert.Equal(Path.Combine(test2OriginalPath, "sub6", "sub7"), UtilsJunction.ResolvePath(Path.Combine(test2JunctionPath, "sub6", "sub7")));

            // Ahora una cadena de enlaces dentro de otro enlace...
            string test3LinkSubDir = Path.Combine(test2LinkPath, "sub8");

            UtilsSystem.EnsureDirectoryExists(Path.Combine(test2LinkPath, "test3"), true);
            UtilsJunction.EnsureLink(test3LinkSubDir, Path.Combine(test2LinkPath, "test3"), logger, true, linkType: UtilsJunction.LinkTypeRequest.Symlink);

            Assert.Equal(Path.Combine(test2OriginalPath, "test3"), UtilsJunction.ResolvePath(test3LinkSubDir));

            UtilsSystem.DeleteDirectory(testPath, logger, 2);

            // Non existent and malformed network uri get reconstructed as-is
            string testNetworkUri = "\\\\147.83.73.25\\a\\b\\c\\\\d";

            Assert.Equal(testNetworkUri, UtilsJunction.ResolvePath(testNetworkUri));
        }
Ejemplo n.º 4
0
        public void TestSimpleStore()
        {
            var simpleStore = new SimpleStore(UtilsSystem.GetTempPath("samplestore"));

            simpleStore.Clear();

            Assert.False(simpleStore.Get <string>("itemA", out _));

            simpleStore.Set("itemA", "testdataA", 50);

            Assert.True(simpleStore.Get <string>("itemA", out var itemA));
            Assert.Equal("testdataA", itemA.Data);

            Assert.False(simpleStore.Get <string>("itemB", out _));

            simpleStore.Set("itemB", "testdataB", 50);

            Assert.True(simpleStore.Get <string>("itemA", out itemA));
            Assert.Equal("testdataA", itemA.Data);

            Assert.True(simpleStore.Get <string>("itemB", out var itemB));
            Assert.Equal("testdataB", itemB.Data);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Execute the opreation...
        /// </summary>
        /// <param name="destination"></param>
        /// <param name="forceDownload"></param>
        protected void DoExecute(
            string destination,
            bool forceDownload = false)
        {
            var uri  = this.Config.uri;
            var maps = this.Config.maps;

            var filename = Path.GetFileName(uri);

            var tmpDir  = UtilsSystem.GetTempPath("iischef_cache", UtilsEncryption.GetMD5(uri));
            var tmpFile = UtilsSystem.CombinePaths(UtilsSystem.GetTempPath(), UtilsEncryption.GetMD5(uri) + "_" + filename);

            if (forceDownload && Directory.Exists(tmpDir))
            {
                Directory.Delete(tmpDir, true);
            }

            if (Directory.Exists(tmpDir))
            {
                var difo = new DirectoryInfo(tmpDir);
                if (!difo.EnumerateFiles("*", SearchOption.AllDirectories).Any())
                {
                    Directory.Delete(tmpDir, true);
                }
            }

            if (!Directory.Exists(tmpDir))
            {
                var parsedUri = new Uri(uri);
                if (parsedUri.Scheme.Equals("file", StringComparison.CurrentCultureIgnoreCase))
                {
                    var path = Path.Combine(this.LocalArtifactPath, parsedUri.LocalPath.TrimStart("\\".ToCharArray()));
                    File.Copy(path, tmpFile);
                }
                else
                {
                    using (var wc = new WebClient())
                    {
                        try
                        {
                            wc.Headers.Add(
                                "User-Agent",
                                "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.33 Safari/537.36");
                            wc.DownloadFile(uri, tmpFile);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Could not download file: " + uri, ex);
                        }
                    }
                }

                UtilsSystem.EnsureDirectoryExists(tmpDir, true);

                if (tmpFile.EndsWith(".zip"))
                {
                    ZipFile.ExtractToDirectory(tmpFile, tmpDir);
                }
                else
                {
                    File.Move(tmpFile, UtilsSystem.CombinePaths(tmpDir, filename));
                }

                File.Delete(tmpFile);
            }

            // Move the files according to the maps
            foreach (var map in maps)
            {
                var files = (new DirectoryInfo(tmpDir)).GetFiles(map.Key, SearchOption.AllDirectories);

                if (!files.Any())
                {
                    throw new Exception(
                              string.Format(
                                  "No matching files found for pattern: {0} in package {1} ['{2}']",
                                  map.Key,
                                  uri,
                                  tmpDir));
                }

                if (files.Count() == 1)
                {
                    var dest = UtilsSystem.CombinePaths(destination, map.Value);
                    UtilsSystem.EnsureDirectoryExists(dest);
                    File.Copy(files.First().FullName, dest);
                }
                else
                {
                    foreach (var f in files)
                    {
                        var subpath = f.FullName.Replace((new DirectoryInfo(tmpDir)).FullName, string.Empty);
                        var dest    = UtilsSystem.CombinePaths(destination, map.Value, subpath);
                        UtilsSystem.EnsureDirectoryExists(dest);

                        try
                        {
                            File.Copy(f.FullName, dest);
                        }
                        catch (Exception e)
                        {
                            throw new Exception($"Error copying file '{f.FullName}' to '{dest}'");
                        }
                    }
                }
            }
        }