Beispiel #1
0
        /// <summary>
        /// Распаковать архив конфигурации
        /// </summary>
        public bool UnpackConfig(string srcFileName, ConfigOptions configOptions)
        {
            try
            {
                // удаление существующей конфигурации
                List <RelPath> configPaths = GetConfigPaths(configOptions.ConfigParts);
                PathDict       pathDict    = PrepareIgnoredPaths(configOptions.IgnoredPaths);

                foreach (RelPath relPath in configPaths)
                {
                    ClearDir(relPath, pathDict);
                }

                // определение допустимых директорий для распаковки
                ConfigParts   configParts    = configOptions.ConfigParts;
                List <string> allowedEntries = new List <string>(AllConfigParts.Length);

                foreach (ConfigParts configPart in AllConfigParts)
                {
                    if (configParts.HasFlag(configPart))
                    {
                        allowedEntries.Add(DirectoryBuilder.GetDirectory(configPart, '/'));
                    }
                }

                // распаковка новой конфигурации
                using (FileStream fileStream =
                           new FileStream(srcFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (ZipArchive zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read))
                    {
                        string instanceDir = Settings.Directory;

                        foreach (ZipArchiveEntry entry in zipArchive.Entries)
                        {
                            if (StartsWith(entry.FullName, allowedEntries, StringComparison.Ordinal))
                            {
                                string relPath      = entry.FullName.Replace('/', Path.DirectorySeparatorChar);
                                string destFileName = instanceDir + relPath;
                                Directory.CreateDirectory(Path.GetDirectoryName(destFileName));
                                entry.ExtractToFile(destFileName, true);
                            }
                        }

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                log.WriteException(ex, Localization.UseRussian ?
                                   "Ошибка при распаковке конфигурации из архива" :
                                   "Error unpacking configuration from archive");
                return(false);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Очистить директорию
        /// </summary>
        private void ClearDir(RelPath relPath, PathDict ignoredPathDict)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(GetAbsPath(relPath));

            if (dirInfo.Exists)
            {
                ClearDir(dirInfo, ignoredPathDict.GetOrAdd(relPath.ConfigPart, relPath.AppFolder),
                         out bool dirEmpty);
            }
        }
        public override TValue Get(string subIndex)
        {
            string key        = subIndex;
            string finalIndex = subIndex;

            if (PathDict.ContainsKey(key))
            {
                finalIndex = PathDict[key];
            }
            return(bufferIndexer.Get(key, finalIndex));
        }
Beispiel #4
0
        /// <summary>
        /// Подготовить исключаемые пути: разделить по группам, применить поиск файлов по маске
        /// </summary>
        private PathDict PrepareExcludedPaths(ICollection <RelPath> relPaths)
        {
            PathDict pathDict = new PathDict();

            if (relPaths != null)
            {
                foreach (RelPath relPath in relPaths)
                {
                    PathList pathList   = pathDict.GetOrAdd(relPath.ConfigPart, relPath.AppFolder);
                    bool     pathIsMask = relPath.IsMask;
                    string[] absPathArr;

                    if (relPath.IsMask)
                    {
                        string dir = GetAbsPath(relPath.ConfigPart, relPath.AppFolder, "");
                        absPathArr = Directory.Exists(dir) ?
                                     Directory.GetFiles(dir, relPath.Path) : new string[0];
                    }
                    else
                    {
                        absPathArr = new string[] { GetAbsPath(relPath) };
                    }

                    foreach (string absPath in absPathArr)
                    {
                        char lastSym = absPath[absPath.Length - 1];

                        if (lastSym == Path.DirectorySeparatorChar || lastSym == Path.AltDirectorySeparatorChar)
                        {
                            pathList.Dirs.Add(absPath);
                        }
                        else
                        {
                            pathList.Files.Add(absPath);
                        }
                    }
                }
            }

            return(pathDict);
        }
Beispiel #5
0
        /// <summary>
        /// Распаковать архив конфигурации
        /// </summary>
        public bool UnpackConfig(string srcFileName, ConfigOptions configOptions)
        {
            try
            {
                // delete the existing configuration
                List <RelPath> configPaths = GetConfigPaths(configOptions.ConfigParts);
                PathDict       pathDict    = PrepareIgnoredPaths(configOptions.IgnoredPaths);

                foreach (RelPath relPath in configPaths)
                {
                    ClearDir(relPath, pathDict);
                }

                // delete a project information file
                string instanceDir         = Settings.Directory;
                string projectInfoFileName = Path.Combine(instanceDir, ProjectInfoEntryName);
                File.Delete(projectInfoFileName);

                // define allowed directories to unpack
                ConfigParts   configParts    = configOptions.ConfigParts;
                List <string> allowedEntries = new List <string> {
                    ProjectInfoEntryName
                };

                foreach (ConfigParts configPart in AllConfigParts)
                {
                    if (configParts.HasFlag(configPart))
                    {
                        allowedEntries.Add(DirectoryBuilder.GetDirectory(configPart, '/'));
                    }
                }

                // unpack the new configuration
                using (FileStream fileStream =
                           new FileStream(srcFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (ZipArchive zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read))
                    {
                        foreach (ZipArchiveEntry entry in zipArchive.Entries)
                        {
                            if (StartsWith(entry.FullName, allowedEntries, StringComparison.Ordinal))
                            {
                                string relPath      = entry.FullName.Replace('/', Path.DirectorySeparatorChar);
                                string destFileName = instanceDir + relPath;
                                Directory.CreateDirectory(Path.GetDirectoryName(destFileName));
                                entry.ExtractToFile(destFileName, true);
                            }
                        }

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                log.WriteException(ex, Localization.UseRussian ?
                                   "Ошибка при распаковке конфигурации из архива" :
                                   "Error unpacking configuration from archive");
                return(false);
            }
        }