public void CreateLargePackage()
        {
            using (var package = new SimplePackage())
            {
                DirectoryInfo repository = new DirectoryInfo(LocalRepository.CalculateRepositoryPath(Log.SessionSummary.Product));

                FileInfo[] allExistingFileFragments = repository.GetFiles("*." + Log.LogExtension, SearchOption.TopDirectoryOnly);

                foreach (var fileFragment in allExistingFileFragments)
                {
                    var sourceFile = FileHelper.OpenFileStream(fileFragment.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
                    if (sourceFile != null)
                    {
                        package.AddSession(sourceFile);
                    }
                }

                using (ProgressMonitorStack stack = new ProgressMonitorStack("saving Package"))
                {
                    package.Save(stack, m_OutputFileNamePath);
                }
            }

            Assert.IsTrue(File.Exists(m_OutputFileNamePath), "Packge was not created");
            Assert.Greater(new FileInfo(m_OutputFileNamePath).Length, 100, "The package was likely empty but should have contained multiple sessions.");

            File.Delete(m_OutputFileNamePath);
        }
        protected override PackageSendEventArgs OnSend(ProgressMonitorStack progressMonitors)
        {
            int             fileSizeBytes = 0;
            AsyncTaskResult result;
            string          statusMessage;
            Exception       taskException = null;

            try
            {
                //all we do is save the file out to our target path.
                Package.Save(progressMonitors, FileNamePath); // Uh-oh, we have to save it again!

                result        = AsyncTaskResult.Success;
                fileSizeBytes = (int)FileSystemTools.GetFileSize(FileNamePath);
                statusMessage = string.Format(CultureInfo.CurrentCulture, "Package written to file {0}",
                                              Path.GetFileNameWithoutExtension(FileNamePath));
            }
            catch (Exception ex)
            {
                result        = AsyncTaskResult.Error;
                statusMessage =
                    "Unable to save the package to disk.\r\n\r\nIt's possible that you don't have sufficient access to the directory to write the file or the media is read-only.";
                taskException = ex;
            }

            return(new PackageSendEventArgs(fileSizeBytes, result, statusMessage, taskException));
        }
        /// <summary>
        /// Perform the actual package transport
        /// </summary>
        /// <param name="progressMonitors"></param>
        /// <returns></returns>
        public void Send(ProgressMonitorStack progressMonitors)
        {
            Status = OnSend(progressMonitors);
#if DEBUG
            Debug.Assert(Status != null);
#endif
        }
Exemplo n.º 4
0
        /// <summary>
        /// Save the package, overwriting any existing data
        /// </summary>
        /// <param name="progressMonitors"></param>
        public void Save(ProgressMonitorStack progressMonitors)
        {
            if (string.IsNullOrEmpty(m_FileNamePath))
            {
                throw new FileNotFoundException("Unable to save the current package because no path has been set to save to.");
            }

            Save(progressMonitors, m_FileNamePath);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Save the package to the specified file name and path, overwriting any existing data
        /// </summary>
        /// <param name="progressMonitors"></param>
        /// <param name="fileNamePath"></param>
        public void Save(ProgressMonitorStack progressMonitors, string fileNamePath)
        {
            if (string.IsNullOrEmpty(fileNamePath))
            {
                throw new ArgumentNullException(nameof(fileNamePath), "Unable to save the current package because no path has been set to save to.");
            }

            //normalize the destination
            fileNamePath = Path.GetFullPath(fileNamePath);

            //make sure the path exists so we can save into it.
            FileSystemTools.EnsurePathExists(fileNamePath);

            lock (m_Lock)
            {
                int steps          = 2;
                int completedSteps = 0;
                using (ProgressMonitor ourMonitor = progressMonitors.NewMonitor(this, "Saving Package", steps))
                {
                    //Do the save (if this fails, we failed)
                    ourMonitor.Update("Saving Package File to Disk", completedSteps++);

                    m_Archive.Dispose(); // ...So we need to dispose and reopen the archive.

                    //check to see if we're saving to the same file we *are*...
                    if (fileNamePath.Equals(m_FileNamePath, StringComparison.OrdinalIgnoreCase) == false)
                    {
                        File.Copy(m_FileNamePath, fileNamePath, true);
                        try
                        {
                            File.Delete(m_FileNamePath);
                        }
                        catch (Exception ex)
                        {
                            if (!Log.SilentMode)
                            {
                                Log.Write(LogMessageSeverity.Error, LogWriteMode.Queued, ex, LogCategory,
                                          "Unable to Delete Package Temporary File due to " + ex.GetType(),
                                          "Unable to delete the temporary working file '{0}'. This means we'll use more disk space than we should but otherwise should not impact the application.",
                                          m_FileNamePath);
                            }
                        }

                        m_FileNamePath = fileNamePath;
                    }

                    ourMonitor.Update("Confirming package contents", completedSteps++);

                    //Since we were successful at saving, this is our new path and we are no longer dirty.
                    m_FileNamePath = fileNamePath;
                    m_Archive      = ZipFile.Open(m_FileNamePath, ZipArchiveMode.Update); // Now we should again be able to read any entry.
                    LoadIndex();
                    IsDirty = false;
                }
            }
        }
        public void CreateEmptyPackage()
        {
            using (var package = new SimplePackage())
            {
                using (ProgressMonitorStack stack = new ProgressMonitorStack("saving Package"))
                {
                    package.Save(stack, m_OutputFileNamePath);
                }
            }

            Assert.IsTrue(File.Exists(m_OutputFileNamePath), "Package was not created");

            File.Delete(m_OutputFileNamePath);
        }
 /// <summary>
 /// Overridden by our inheritors to implement the package send routine.
 /// </summary>
 /// <param name="progressMonitors"></param>
 /// <returns></returns>
 protected abstract PackageSendEventArgs OnSend(ProgressMonitorStack progressMonitors);