예제 #1
0
        /// <summary>
        /// Stores the results of a given URL in the cache.
        /// Description is appended to the file hash when saving. If not present, the filename will be used.
        /// If `move` is true, then the file will be moved; otherwise, it will be copied.
        ///
        /// Returns a path to the newly cached file.
        ///
        /// This method is filesystem transaction aware.
        /// </summary>
        public string Store(Uri url, string path, string description = null, bool move = false)
        {
            log.DebugFormat("Storing {0}", url);

            // Make sure we clear our cache entry first.
            this.Remove(url);

            string hash = CreateURLHash(url);

            description = description ?? Path.GetFileName(path);

            string fullName   = String.Format("{0}-{1}", hash, Path.GetFileName(description));
            string targetPath = Path.Combine(cachePath, fullName);

            log.DebugFormat("Storing {0} in {1}", path, targetPath);

            if (move)
            {
                tx_file.Move(path, targetPath);
            }
            else
            {
                tx_file.Copy(path, targetPath, overwrite: true);
            }

            return(targetPath);
        }
예제 #2
0
        /// <summary>
        /// Stores the results of a given URL in the cache.
        /// Description is adjusted to be filesystem-safe and then appended to the file hash when saving.
        /// If not present, the filename will be used.
        /// If `move` is true, then the file will be moved; otherwise, it will be copied.
        ///
        /// Returns a path to the newly cached file.
        ///
        /// This method is filesystem transaction aware.
        /// </summary>
        public string Store(Uri url, string path, string description = null, bool move = false)
        {
            log.DebugFormat("Storing {0}", url);

            // Make sure we clear our cache entry first.
            Remove(url);

            string hash = CreateURLHash(url);

            description = description ?? Path.GetFileName(path);

            Debug.Assert(
                Regex.IsMatch(description, "^[A-Za-z0-9_.-]*$"),
                "description isn't as filesystem safe as we thought... (#1266)"
                );

            string fullName   = String.Format("{0}-{1}", hash, Path.GetFileName(description));
            string targetPath = Path.Combine(cachePath, fullName);

            log.DebugFormat("Storing {0} in {1}", path, targetPath);

            if (move)
            {
                tx_file.Move(path, targetPath);
            }
            else
            {
                tx_file.Copy(path, targetPath, true);
            }

            // We've changed our cache, so signal that immediately.
            OnCacheChanged();

            return(targetPath);
        }
예제 #3
0
        private static void _CopyDirectory(string sourceDirPath, string destDirPath, bool copySubDirs, TxFileManager file_transaction)
        {
            DirectoryInfo sourceDir = new DirectoryInfo(sourceDirPath);

            if (!sourceDir.Exists)
            {
                throw new DirectoryNotFoundKraken(
                    sourceDirPath,
                    "Source directory does not exist or could not be found.");
            }

            // If the destination directory doesn't exist, create it.
            if (!Directory.Exists(destDirPath))
            {
                file_transaction.CreateDirectory(destDirPath);
            }
            else if (Directory.GetDirectories(destDirPath).Length != 0 || Directory.GetFiles(destDirPath).Length != 0)
            {
                throw new PathErrorKraken(destDirPath, "Directory not empty: ");
            }

            // Get the files in the directory and copy them to the new location.
            FileInfo[] files = sourceDir.GetFiles();
            foreach (FileInfo file in files)
            {
                if (file.Name == "registry.locked")
                {
                    continue;
                }

                else if (file.Name == "playtime.json")
                {
                    continue;
                }

                string temppath = Path.Combine(destDirPath, file.Name);
                file_transaction.Copy(file.FullName, temppath, false);
            }

            // Create all first level subdirectories
            DirectoryInfo[] dirs = sourceDir.GetDirectories();

            foreach (DirectoryInfo subdir in dirs)
            {
                string temppath = Path.Combine(destDirPath, subdir.Name);
                file_transaction.CreateDirectory(temppath);

                // If copying subdirectories, copy their contents to new location.
                if (copySubDirs)
                {
                    _CopyDirectory(subdir.FullName, temppath, copySubDirs, file_transaction);
                }
            }
        }
예제 #4
0
 /// <summary>
 ///   Copies the source to the destination.
 /// </summary>
 /// <remarks>
 ///   If the source is a directory, it is copied recursively.
 /// </remarks>
 /// <param name="p_tfmFileManager">The transactional file manager to use to copy the files.</param>
 /// <param name="p_strSource">The path from which to copy.</param>
 /// <param name="p_strDestination">The path to which to copy.</param>
 /// <param name="p_fncCopyCallback">
 ///   A callback method that notifies the caller when a file has been copied,
 ///   and provides the opportunity to cancel the copy operation.
 /// </param>
 /// <returns><lang langref="true" /> if the copy operation wasn't cancelled; <lang langref="false" /> otherwise.</returns>
 public static bool Copy(TxFileManager p_tfmFileManager, string p_strSource, string p_strDestination,
                         Func <string, bool> p_fncCopyCallback)
 {
     if (File.Exists(p_strSource))
     {
         if (!Directory.Exists(Path.GetDirectoryName(p_strDestination)))
         {
             p_tfmFileManager.CreateDirectory(Path.GetDirectoryName(p_strDestination));
         }
         p_tfmFileManager.Copy(p_strSource, p_strDestination, true);
         if ((p_fncCopyCallback != null) && p_fncCopyCallback(p_strSource))
         {
             return(false);
         }
     }
     else if (Directory.Exists(p_strSource))
     {
         if (!Directory.Exists(p_strDestination))
         {
             p_tfmFileManager.CreateDirectory(p_strDestination);
         }
         var strFiles = Directory.GetFiles(p_strSource);
         foreach (var strFile in strFiles)
         {
             p_tfmFileManager.Copy(strFile, Path.Combine(p_strDestination, Path.GetFileName(strFile)), true);
             if ((p_fncCopyCallback != null) && p_fncCopyCallback(strFile))
             {
                 return(false);
             }
         }
         var strDirectories = Directory.GetDirectories(p_strSource);
         foreach (var strDirectory in strDirectories)
         {
             if (!Copy(strDirectory, Path.Combine(p_strDestination, Path.GetFileName(strDirectory)), p_fncCopyCallback))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
예제 #5
0
        /// <summary>
        /// Stores the results of a given URL in the cache.
        /// Description is adjusted to be filesystem-safe and then appended to the file hash when saving.
        /// If not present, the filename will be used.
        /// If `move` is true, then the file will be moved; otherwise, it will be copied.
        ///
        /// Returns a path to the newly cached file.
        ///
        /// This method is filesystem transaction aware.
        /// </summary>
        public string Store(Uri url, string path, string description = null, bool move = false)
        {
            log.DebugFormat("Storing {0}", url);

            TxFileManager tx_file = new TxFileManager();

            // Make sure we clear our cache entry first.
            Remove(url);

            string hash = CreateURLHash(url);

            description = description ?? Path.GetFileName(path);

            Debug.Assert(
                Regex.IsMatch(description, "^[A-Za-z0-9_.-]*$"),
                "description isn't as filesystem safe as we thought... (#1266)"
                );

            string fullName   = String.Format("{0}-{1}", hash, Path.GetFileName(description));
            string targetPath = Path.Combine(cachePath, fullName);

            // Purge hashes associated with the new file
            PurgeHashes(tx_file, targetPath);

            log.InfoFormat("Storing {0} in {1}", path, targetPath);

            if (move)
            {
                tx_file.Move(path, targetPath);
            }
            else
            {
                tx_file.Copy(path, targetPath, true);
            }

            // We've changed our cache, so signal that immediately.
            if (!cachedFiles?.ContainsKey(hash) ?? false)
            {
                cachedFiles?.Add(hash, targetPath);
            }

            return(targetPath);
        }
예제 #6
0
        private void DoWork(Govbody govBody)
        {
            //List<ScheduledMeeting> scheduled = govBody.ScheduledMeetings;
            IReadOnlyCollection <ScheduledMeeting> scheduled = govBody.ScheduledMeetings;

            // Get all meetings that have should occured
            IEnumerable <ScheduledMeeting> results = scheduled.Where(
                s => s.Date < (DateTime.Now));

            foreach (ScheduledMeeting result in results)
            {
                DateTime   actualDate;
                SourceType type;

                // Do actual retrieval
                string retrievedFile = retrieveNewFiles.RetrieveFile(govBody, result.Date, out actualDate, out type);

                // create a work folder for this meeting
                string workfolderName = govBody.LongName + "_" + actualDate.ToString("yyyy-MM-dd");
                string workFolderPath = Path.Combine(config.DatafilesPath, workfolderName);

                string extension      = Path.GetExtension(retrievedFile);
                string sourceFilename = "source" + extension;
                string sourceFilePath = Path.Combine(workFolderPath, sourceFilename);

                // Create the meeting record
                Meeting meeting = new Meeting(actualDate, type, sourceFilename);
                meeting.WorkStatus = WorkStatus.Received;
                meeting.Approved   = false;

                TxFileManager fileMgr = new TxFileManager();
                using (TransactionScope scope = new TransactionScope())

                {
                    Directory.CreateDirectory(workFolderPath);
                    fileMgr.Copy(retrievedFile, sourceFilePath, true);
                    //// TODO - implement
                    ////dBOperations.Add(meeting);
                    ////dBOperations.WriteChanges();
                    scope.Complete();
                }
            }
        }
예제 #7
0
        /// <summary>
        /// Stores the results of a given URL in the cache.
        /// Description is adjusted to be filesystem-safe and then appended to the file hash when saving.
        /// If not present, the filename will be used.
        /// If `move` is true, then the file will be moved; otherwise, it will be copied.
        ///
        /// Returns a path to the newly cached file.
        ///
        /// This method is filesystem transaction aware.
        /// </summary>
        public string Store(Uri url, string path, string description = null, bool move = false)
        {
            log.DebugFormat("Storing {0}", url);

            // Make sure we clear our cache entry first.
            Remove(url);

            string hash = CreateURLHash(url);

            if (description != null)
            {
                // Versions can contain ALL SORTS OF WACKY THINGS! Colons, friggin newlines,
                // slashes, and heaven knows what use mod authors try to smoosh into them.
                // We'll reduce this down to "friendly" characters, replacing everything else with
                // dashes. This doesn't change look-ups, as we use the hash prefix for that.

                description = Regex.Replace(description, "[^A-Za-z0-9_.-]", "-");
            }

            description = description ?? Path.GetFileName(path);

            string fullName   = String.Format("{0}-{1}", hash, Path.GetFileName(description));
            string targetPath = Path.Combine(cachePath, fullName);

            log.DebugFormat("Storing {0} in {1}", path, targetPath);

            if (move)
            {
                tx_file.Move(path, targetPath);
            }
            else
            {
                tx_file.Copy(path, targetPath, true);
            }

            return(targetPath);
        }
예제 #8
0
        /// <summary>
        ///   This performs the mirgration.
        /// </summary>
        /// <param name="p_objArgs">The path to the old FOMM installation.</param>
        protected void DoMigration(object p_objArgs)
        {
            var strOldFOMMLocation = (string)p_objArgs;
            var tfmFileManager     = new TxFileManager();

            //copy the mods
            //do we need to copy?
            if (
                !Path.Combine(strOldFOMMLocation, "mods")
                .Equals(Program.GameMode.ModDirectory, StringComparison.InvariantCultureIgnoreCase))
            {
                var lstModFiles = new List <string>();
                lstModFiles.AddRange(Directory.GetFiles(Path.Combine(strOldFOMMLocation, "mods"), "*.fomod"));
                lstModFiles.AddRange(Directory.GetFiles(Path.Combine(strOldFOMMLocation, "mods"), "*.xml"));
                m_bwdProgress.ItemMessage         = "Copying mods...";
                m_bwdProgress.ItemProgressMaximum = lstModFiles.Count;
                m_bwdProgress.ItemProgress        = 0;
                foreach (var strMod in lstModFiles)
                {
                    var strModFileName = Path.GetFileName(strMod);
                    m_bwdProgress.ItemMessage = "Copying mods (" + strModFileName + ")...";
                    tfmFileManager.Copy(strMod, Path.Combine(Program.GameMode.ModDirectory, strModFileName), true);
                    //File.Copy(strMod, Path.Combine(Program.GameMode.ModDirectory, Path.GetFileName(strMod)));
                    m_bwdProgress.StepItemProgress();
                    if (m_bwdProgress.Cancelled())
                    {
                        return;
                    }
                }
            }

            m_bwdProgress.StepOverallProgress();

            //copy overwrites folder
            //do we need to?
            if (
                !Path.Combine(strOldFOMMLocation, "overwrites")
                .Equals(Program.GameMode.OverwriteDirectory,
                        StringComparison.InvariantCultureIgnoreCase))
            {
                var strOverwriteFiles = Directory.GetFiles(Path.Combine(strOldFOMMLocation, "overwrites"), "*.*",
                                                           SearchOption.AllDirectories);
                m_bwdProgress.ItemMessage         = "Copying overwrites...";
                m_bwdProgress.ItemProgressMaximum = strOverwriteFiles.Length;
                m_bwdProgress.ItemProgress        = 0;
                FileUtil.Copy(tfmFileManager, Path.Combine(strOldFOMMLocation, "overwrites"),
                              Program.GameMode.OverwriteDirectory, OverwriteFileCopied);
            }

            m_bwdProgress.StepOverallProgress();

            //copy install logs
            //do we need to?
            if (
                !Path.Combine(strOldFOMMLocation, "fomm")
                .Equals(Program.GameMode.InstallInfoDirectory, StringComparison.InvariantCultureIgnoreCase))
            {
                var strMiscFiles = Directory.GetFiles(Path.Combine(strOldFOMMLocation, "fomm"), "InstallLog.xml*");
                m_bwdProgress.ItemMessage         = "Copying info files...";
                m_bwdProgress.ItemProgressMaximum = strMiscFiles.Length;
                m_bwdProgress.ItemProgress        = 0;
                foreach (var strFile in strMiscFiles)
                {
                    tfmFileManager.Copy(strFile, Path.Combine(Program.GameMode.InstallInfoDirectory, Path.GetFileName(strFile)),
                                        true);
                    m_bwdProgress.StepItemProgress();
                    if (m_bwdProgress.Cancelled())
                    {
                        return;
                    }
                }
            }

            m_bwdProgress.StepOverallProgress();
        }
예제 #9
0
        static void RunStressTest()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            {
                // Pre-test checks
                string       tempDir;
                IFileManager fm = new TxFileManager();
                using (TransactionScope s1 = new TransactionScope())
                {
                    tempDir = (new DirectoryInfo(fm.CreateTempDirectory())).Parent.FullName;
                    Console.WriteLine("Temp path: " + tempDir);
                }

                string[] directories = Directory.GetDirectories(tempDir);
                string[] files       = Directory.GetFiles(tempDir);
                if (directories.Length > 0 || files.Length > 0)
                {
                    Console.WriteLine(string.Format("ERROR  Please ensure temp path {0} has no children before running this test.", tempDir));
                    return;
                }
            }

            // Start each test in its own thread and repeat for a few interations
            const int    numThreads = 10;
            const int    iterations = 250;
            const string text       = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

            long count = 0;

            IList <Thread> threads = new List <Thread>();

            for (int i = 0; i < numThreads; i++)
            {
                Thread t = new Thread(() =>
                {
                    IFileManager fm = new TxFileManager();
                    for (int j = 0; j < iterations; j++)
                    {
                        using (TransactionScope s1 = new TransactionScope())
                        {
                            TransactionOptions to = new TransactionOptions();
                            to.Timeout            = TimeSpan.FromMinutes(30);

                            long myCount = Interlocked.Increment(ref count);
                            if (myCount % 250 == 0)
                            {
                                Console.WriteLine(myCount + " (" + myCount * 100 / (numThreads * iterations) + " %)");
                            }

                            string f1 = fm.CreateTempFileName();
                            string f2 = fm.CreateTempFileName();
                            string d1 = fm.CreateTempDirectory();

                            if (i % 100 == 0)
                            {
                                Console.WriteLine(i);
                            }

                            fm.AppendAllText(f1, text);
                            fm.Copy(f1, f2, false);

                            fm.CreateDirectory(d1);
                            fm.Delete(f2);
                            fm.DeleteDirectory(d1);
                            bool b1   = fm.DirectoryExists(d1);
                            bool b2   = fm.FileExists(f1);
                            string f3 = fm.CreateTempFileName();
                            fm.Move(f1, f3);
                            string f4 = fm.CreateTempFileName();
                            fm.Snapshot(f4);
                            fm.WriteAllBytes(f4, new byte[] { 64, 65 });
                            string f5 = fm.CreateTempFileName();
                            fm.WriteAllText(f5, text);

                            fm.Delete(f1);
                            fm.Delete(f2);
                            fm.Delete(f3);
                            fm.Delete(f4);
                            fm.Delete(f5);
                            fm.DeleteDirectory(d1);
                            s1.Complete();
                        }
                    }
                });

                threads.Add(t);
            }

            foreach (Thread t in threads)
            {
                t.Start();
            }

            Console.WriteLine("All threads started.");

            foreach (Thread t in threads)
            {
                t.Join();
            }

            sw.Stop();

            Console.WriteLine("All threads joined. Elapsed: {0}.", sw.ElapsedMilliseconds);
        }