Пример #1
0
        /// <summary>
        /// Copy the entry from the opened zipfile to the path specified.
        /// </summary>
        internal static void CopyZipEntry(string absoluteDirectoryRoot, IInstallable file)
        {
            string absolutePath = Path.Combine(absoluteDirectoryRoot, file.Destination);

            if (file.IsDirectory)
            {
                // Skip if we're not making directories for this install.
                if (!file.makeDirs)
                {
                    log.DebugFormat("Skipping {0}, we don't make directories for this path", absolutePath);
                    return;
                }

                log.DebugFormat("Making directory {0}", absolutePath);
                file_transaction.CreateDirectory(absolutePath);
            }
            else
            {
                log.DebugFormat("Writing file {0}", absolutePath);

                // Sometimes there are zipfiles that don't contain entries for the
                // directories their files are in. No, I understand either, but
                // the result is we have to make sure our directories exist, just in case.
                if (file.makeDirs)
                {
                    string directory = Path.GetDirectoryName(absolutePath);
                    file_transaction.CreateDirectory(directory);
                }

                // We don't allow for the overwriting of files. See #208.
                if (File.Exists(absolutePath))
                {
                    throw new FileExistsKraken(absolutePath, string.Format("Trying to write {0} but it already exists.", absolutePath));
                }

                // Snapshot whatever was there before. If there's nothing, this will just
                // remove our file on rollback. We still need this even thought we won't
                // overwite files, as it ensures deletiion on rollback.
                file_transaction.Snapshot(absolutePath);

                try
                {
                    // It's a file! Prepare the streams
                    using (Stream zipStream = file.stream)
                        using (FileStream writer = File.Create(absolutePath))
                        {
                            // 4k is the block size on practically every disk and OS.
                            byte[] buffer = new byte[4096];
                            StreamUtils.Copy(zipStream, writer, buffer);
                        }
                }
                catch (DirectoryNotFoundException ex)
                {
                    throw new DirectoryNotFoundKraken("", ex.Message, ex);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Called to perform the upgrade.
        /// </summary>
        /// <remarks>
        /// Sets up the resources required to upgrade the install log.
        /// </remarks>
        /// <param name="p_mdrManagedModRegistry">The <see cref="ModRegistry"/> that contains the list
        /// of managed mods.</param>
        /// <param name="p_strModInstallDirectory">The path of the directory where all of the mods are installed.</param>
        /// <param name="p_strLogPath">The path from which to load the install log information.</param>
        public void Upgrade(string p_strLogPath, string p_strModInstallDirectory, ModRegistry p_mdrManagedModRegistry)
        {
            Trace.WriteLine("Beginning Install Log Upgrade.");

            m_tfmFileManager = new TxFileManager();
            using (TransactionScope tsTransaction = new TransactionScope())
            {
                m_tfmFileManager.Snapshot(p_strLogPath);
                Start(p_strLogPath, p_strModInstallDirectory, p_mdrManagedModRegistry);
                tsTransaction.Complete();
                m_tfmFileManager = null;
            }
        }
Пример #3
0
        /// <summary>
        /// Copy the entry from the opened zipfile to the path specified.
        /// </summary>
        internal static void CopyZipEntry(ZipFile zipfile, ZipEntry entry, string fullPath, bool makeDirs)
        {
            if (entry.IsDirectory)
            {
                // Skip if we're not making directories for this install.
                if (!makeDirs)
                {
                    log.DebugFormat("Skipping {0}, we don't make directories for this path", fullPath);
                    return;
                }

                log.DebugFormat("Making directory {0}", fullPath);
                file_transaction.CreateDirectory(fullPath);
            }
            else
            {
                log.DebugFormat("Writing file {0}", fullPath);

                // Sometimes there are zipfiles that don't contain entries for the
                // directories their files are in. No, I understand either, but
                // the result is we have to make sure our directories exist, just in case.
                if (makeDirs)
                {
                    string directory = Path.GetDirectoryName(fullPath);
                    file_transaction.CreateDirectory(directory);
                }

                // We don't allow for the overwriting of files. See #208.
                if (File.Exists(fullPath))
                {
                    throw new FileExistsKraken(fullPath, string.Format("Trying to write {0} but it already exists.", fullPath));
                }

                // Snapshot whatever was there before. If there's nothing, this will just
                // remove our file on rollback. We still need this even thought we won't
                // overwite files, as it ensures deletiion on rollback.
                file_transaction.Snapshot(fullPath);

                // It's a file! Prepare the streams
                using (Stream zipStream = zipfile.GetInputStream(entry))
                    using (FileStream writer = File.Create(fullPath))
                    {
                        // 4k is the block size on practically every disk and OS.
                        byte[] buffer = new byte[4096];
                        StreamUtils.Copy(zipStream, writer, buffer);
                    }
            }

            return;
        }
Пример #4
0
        /// <summary>
        ///   Called to perform the upgrade.
        /// </summary>
        /// <remarks>
        ///   Sets up the resources required to upgrade the install log, and then
        ///   call <see cref="DoUpgrade()" /> so implementers can do the upgrade.
        /// </remarks>
        /// <returns>
        ///   <lang langref="true" /> if the upgrade completed; <lang langref="false" />
        ///   if the user cancelled.
        /// </returns>
        internal bool PerformUpgrade()
        {
            FileManager = new TxFileManager();
            var booComplete = false;

            using (var tsTransaction = new TransactionScope())
            {
                FileManager.Snapshot(InstallLog.Current.InstallLogPath);

                using (ProgressWorker = new BackgroundWorkerProgressDialog(DoUpgrade))
                {
                    ProgressWorker.OverallMessage = "Upgrading FOMM Files";
                    if (ProgressWorker.ShowDialog() == DialogResult.OK)
                    {
                        booComplete = true;
                        tsTransaction.Complete();
                    }
                }
                FileManager = null;
            }
            return(booComplete);
        }
Пример #5
0
        /// <summary>
        /// Downloads our files, returning an array of filenames that we're writing to.
        /// The .onCompleted delegate will be called on completion.
        /// </summary>
        public string[] Download(ICollection <Uri> urls)
        {
            foreach (Uri url in urls)
            {
                var download = new NetAsyncDownloaderDownloadPart(url);
                this.downloads.Add(download);
            }

            var filePaths = new string[downloads.Count];

            for (int i = 0; i < downloads.Count; i++)
            {
                User.WriteLine("Downloading \"{0}\"", downloads[i].url);

                // We need a new variable for our closure/lambda, hence index = i.
                int index = i;

                // Schedule for us to get back progress reports.
                downloads[i].agent.DownloadProgressChanged +=
                    (sender, args) =>
                    FileProgressReport(index, args.ProgressPercentage, args.BytesReceived,
                                       args.TotalBytesToReceive - args.BytesReceived);

                // And schedule a notification if we're done (or if something goes wrong)
                downloads[i].agent.DownloadFileCompleted += (sender, args) => FileDownloadComplete(index, args.Error);

                // Snapshot whatever was in that location, in case we need to roll-back.
                file_transaction.Snapshot(downloads[i].path);

                // Bytes ahoy!
                downloads[i].agent.DownloadFileAsync(downloads[i].url, downloads[i].path);
            }

            // The user hasn't cancelled us yet. :)
            downloadCanceled = false;

            return(filePaths);
        }
Пример #6
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);
        }