Ejemplo n.º 1
0
        public Tree LoadTreeFromDom(string treeId, XDocument document)
        {
            string xslFilename = Path.Combine(TreeDefinitionsFolder, XslFilename);

            var fileInfo = new C1FileInfo(xslFilename);

            // The default xslt transformation does no changes and it's xsl file has size of 358 bytes, skipping this file saves up to 0.5 second on site startup
            if (fileInfo.Exists && fileInfo.Length != 358)
            {
                try
                {
                    var newDocument = new XDocument();
                    using (XmlWriter xmlWriter = newDocument.CreateWriter())
                    {
                        var xslTransform = new XslCompiledTransform();

                        xslTransform.LoadFromPath(xslFilename);

                        xslTransform.Transform(document.CreateReader(), xmlWriter);
                    }

                    document = newDocument;
                }
                catch (Exception ex)
                {
                    Log.LogCritical("TreeFacade", "Failed to apply xslt on the tree {0} with the following exception", treeId);
                    Log.LogCritical("TreeFacade", ex);
                }
            }

            return(Load(treeId, document));
        }
        private static XDocument TryLoad(C1FileInfo candidateFile)
        {
            XDocument dataDocument = null;

            try
            {
                XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
                xmlReaderSettings.CheckCharacters = false;

                using (XmlReader xmlReader = XmlReaderUtils.Create(candidateFile.FullName, xmlReaderSettings))
                {
                    dataDocument = XDocument.Load(xmlReader);
                }
            }
            catch (Exception)
            {
                // broken file - should not stop us...
            }

            return(dataDocument);
        }
        public IEnumerable <SiteUpdateInformation> GetUpdateSummaries(Guid installationId)
        {
            var basePath = PathUtil.Resolve(_location);
            var folder   = Path.Combine(basePath, installationId.ToString());

            var list = new List <SiteUpdateInformation>();

            if (!Directory.Exists(folder))
            {
                return(list);
            }

            var files = C1Directory.GetFiles(folder, "*.zip");

            foreach (var f in files)
            {
                try
                {
                    var fi = new C1FileInfo(f);
                    var packageInformation = PackageSystemServices.GetPackageInformationFromZipfile(f);
                    var changelog          = ZipFileHelper.GetContentFromFile(f, "changelog.txt");

                    list.Add(new SiteUpdateInformation
                    {
                        Id             = packageInformation.Id,
                        InstallationId = installationId,
                        FileName       = fi.Name,
                        Name           = packageInformation.Name,
                        Version        = packageInformation.Version,
                        ReleasedDate   = fi.CreationTime,
                        ChangeLog      = changelog
                    });
                }
                catch { }
            }

            return(list);
        }
Ejemplo n.º 4
0
        private static XDocument TryLoad(C1FileInfo candidateFile, ref string errorCause)
        {
            XDocument dataDocument = null;

            try
            {
                var xmlReaderSettings = new XmlReaderSettings {
                    CheckCharacters = false
                };

                using (XmlReader xmlReader = XmlReaderUtils.Create(candidateFile.FullName, xmlReaderSettings))
                {
                    dataDocument = XDocument.Load(xmlReader);
                }
            }
            catch (Exception ex)
            {
                errorCause = ex.Message;
                // broken file - should not stop us...
            }

            return(dataDocument);
        }
        /// <summary>
        /// Will pull up most recent from disk - if no good file is found an empty store will be constructed anyway
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="elementName"></param>
        /// <param name="keyGetter"></param>
        /// <returns></returns>
        private static FileRecord LoadFileRecordFromDisk(string filePath, string elementName, Func <XElement, IDataId> keyGetter)
        {
            XDocument  dataDocument = null;
            C1FileInfo usedFile     = null;

            IList <C1FileInfo> candidateFiles = GetCandidateFiles(filePath);

            foreach (C1FileInfo candidateFile in candidateFiles)
            {
                bool tryLoad = true;

                while (tryLoad && dataDocument == null)
                {
                    dataDocument = TryLoad(candidateFile);

                    if (dataDocument == null)
                    {
                        if ((DateTime.Now - candidateFile.LastWriteTime).TotalSeconds > 30)
                        {
                            tryLoad = false;
                        }
                        else
                        {
                            Thread.Sleep(250); // other processes/servers may be writing to this file at the moment. Patience young padawan!
                        }
                    }
                }

                if (dataDocument != null)
                {
                    usedFile = candidateFile;
                    break;
                }
            }

            if (dataDocument == null)
            {
                dataDocument = new XDocument(new XElement("fallback"));
                Log.LogWarning(LogTitle, "Did not find a healthy XML document for '{0}' - creating an empty store.", filePath);
            }

            List <XElement> elements = ExtractElements(dataDocument);

            var index = new Hashtable <IDataId, XElement>(elements.Count);

            foreach (var element in elements)
            {
                IDataId id = keyGetter(element);
                if (!index.ContainsKey(id))
                {
                    index.Add(id, element);
                }
                else
                {
                    Log.LogWarning(LogTitle, "Found multiple elements in '{0}' sharing same key - duplicates ignored.", filePath);
                }
            }

            if (usedFile != null)
            {
                // clean up old and unused files
                foreach (C1FileInfo file in candidateFiles.Where(f => f.LastWriteTime < usedFile.LastWriteTime))
                {
                    try
                    {
                        C1File.Move(file.FullName, file.FullName + ".ghost");
                    }
                    catch (Exception)
                    {
                        Log.LogWarning(LogTitle, "Failed to clean up ghost file '{0}'.", filePath);
                    }
                }
            }

            DateTime lastModifiedFileDate = usedFile != null ? usedFile.LastWriteTime : DateTime.Now;

            return(new FileRecord
            {
                FilePath = filePath,
                ElementName = elementName,
                RecordSet = new RecordSet {
                    Index = index
                },
                ReadOnlyElementsList = new List <XElement>(elements),
                LastModified = DateTime.Now,
                FileModificationDate = lastModifiedFileDate
            });
        }
Ejemplo n.º 6
0
    protected string CreateBackup()
    {
        var    Url         = Request.Url;
        string zipFilename = Url.Scheme + "_" + Url.Host + (Url.IsDefaultPort ? "" : (" " + Url.Port)) + "_" +
                             DateTime.Now.ToString("yyyy_MM.dd_HHmm") + ".zip";
        string zipFilePath = PathCombine(BackupDirectory, zipFilename);

        string backupDirectory = Path.GetDirectoryName(zipFilePath);

        if (!C1Directory.Exists(backupDirectory))
        {
            C1Directory.CreateDirectory(backupDirectory);
        }


        var filenames   = GetFilesRecursive(BaseDirectory);
        var directories = GetDirectoriesRecursive(BaseDirectory);

        lock (_lock)
        {
            using (var archiveFileStream = FileCreate(zipFilePath))
                using (var archive = new ZipArchive(archiveFileStream, ZipArchiveMode.Create))
                {
                    int oldScriptTimeOut = Server.ScriptTimeout;
                    Server.ScriptTimeout = ScriptTimeoutInSeconds;

                    foreach (string directory in directories)
                    {
                        if (directory.Contains(BackupDirectoryRelativePath))
                        {
                            continue;
                        }

                        string directoryEntryName = directory.Replace(BaseDirectory, "").Replace("\\", "/") + "/";

                        var dirEntry = archive.CreateEntry(directoryEntryName);
                        dirEntry.LastWriteTime = Directory.GetLastWriteTime(directory);
                    }

                    foreach (string file in filenames)
                    {
                        if (file.Contains(BackupDirectoryRelativePath) || IsTemporaryDirectory(file))
                        {
                            continue;
                        }

                        string fileEntryName = file.Replace(BaseDirectory, "").Replace("\\", "/");

                        var entry = archive.CreateEntry(fileEntryName, BackupCompressionLevel);

                        try
                        {
                            var fi = new C1FileInfo(file);
                            entry.LastWriteTime = fi.LastWriteTime;

                            using (var fileStream = C1File.OpenRead(file))
                                using (var entryStream = entry.Open())
                                {
                                    fileStream.CopyTo(entryStream);
                                }
                        }
                        catch (Exception e)
                        {
                            if (!file.Contains("App_Data\\Composite\\LogFiles"))
                            {
                                this.Validators.Add(new ErrorSummary(e.Message));
                            }
                        }
                    }

                    Server.ScriptTimeout = oldScriptTimeOut;
                }
        }

        return(zipFilename);
    }