Beispiel #1
0
        internal static void Compress(string source, string archiveEntryName, out string tempArchivePath)
        {
            string archiveFullPath = Utility.GetTempFileName();

            Utility.PerformIOWithRetries(
                () =>
            {
                using (FileStream fs = new FileStream(archiveFullPath, FileMode.Create))
                {
                    // ZipArchive archive;
                    object archive = null;
                    try
                    {
                        try
                        {
                            // archive = new ZipArchive(fs, ZipArchiveMode.Create);
                            archive = ZipArchiveConstructor.Invoke(new[] { fs, ZipArchiveModeCreate });

                            // archive.CreateEntryFromFile(source, archiveEntryName, CompressionLevel.Optimal);
                            ZipFileExtCreateEntryFromFile.Invoke(
                                null,
                                new[] { archive, source, archiveEntryName, CompressionLevelOptimal });
                        }
                        catch (TargetInvocationException e)
                        {
                            throw e.InnerException;
                        }
                    }
                    finally
                    {
                        if (null != archive)
                        {
                            // archive.Dispose();
                            ZipArchiveDispose.Invoke(archive, null);
                        }
                    }
                }
            });

            tempArchivePath = archiveFullPath;
        }
Beispiel #2
0
        private bool SaveToFile()
        {
            // Create a new temp file
            string tempFilePath = Utility.GetTempFileName();

            try
            {
                // Open the temp file
                StreamWriter writer = null;
                try
                {
                    Utility.PerformIOWithRetries(
                        () =>
                    {
                        FileStream fileStream = FabricFile.Open(tempFilePath, FileMode.Create, FileAccess.Write);
#if !DotNetCoreClrLinux
                        Helpers.SetIoPriorityHint(fileStream.SafeFileHandle, Kernel32Types.PRIORITY_HINT.IoPriorityHintVeryLow);
#endif
                        writer = new StreamWriter(fileStream);
                    });
                }
                catch (Exception e)
                {
                    this.traceSource.WriteExceptionAsError(
                        this.logSourceId,
                        e,
                        "Failed to open temp file {0} for persisting index map.",
                        tempFilePath);
                    return(false);
                }

                try
                {
                    // Write the version information to the map file
                    string versionString = string.Concat(VersionPrefix, this.fileFormatVersion.ToString(CultureInfo.InvariantCulture));
                    try
                    {
                        Utility.PerformIOWithRetries(
                            () =>
                        {
                            writer.WriteLine(versionString);
                        });
                    }
                    catch (Exception e)
                    {
                        this.traceSource.WriteExceptionAsError(
                            this.logSourceId,
                            e,
                            "Failed to write version information to temp file {0} for persisting index map.",
                            tempFilePath);
                        return(false);
                    }

                    // Write the map records to the map file
                    foreach (TItem item in this.dictionary.Keys)
                    {
                        string mapRecord = string.Concat(
                            item.ToString(),
                            ", ",
                            this.dictionary[item].ToString(CultureInfo.InvariantCulture));

                        try
                        {
                            Utility.PerformIOWithRetries(
                                () =>
                            {
                                writer.WriteLine(mapRecord);
                            });
                        }
                        catch (Exception e)
                        {
                            this.traceSource.WriteExceptionAsError(
                                this.logSourceId,
                                e,
                                "Failed to write record {0} to temp file {1} for persisting index map.",
                                mapRecord,
                                tempFilePath);
                            return(false);
                        }
                    }
                }
                finally
                {
                    writer.Dispose();
                }

                // Copy the temp file as the new map file
                try
                {
                    Utility.PerformIOWithRetries(
                        () =>
                    {
                        FabricFile.Copy(tempFilePath, this.fileFullPath, true);
                    });
                }
                catch (Exception e)
                {
                    this.traceSource.WriteExceptionAsError(
                        this.logSourceId,
                        e,
                        "Failed to copy file {0} to {1} for persisting index map",
                        tempFilePath,
                        this.fileFullPath);
                    return(false);
                }

                this.traceSource.WriteInfo(
                    this.logSourceId,
                    "Index map file {0} created.",
                    this.fileFullPath);
            }
            finally
            {
                try
                {
                    Utility.PerformIOWithRetries(
                        () =>
                    {
                        FabricFile.Delete(tempFilePath);
                    });
                }
                catch (Exception e)
                {
                    this.traceSource.WriteExceptionAsError(
                        this.logSourceId,
                        e,
                        "Failed to delete temp file {0} which was created for persisting index map.",
                        tempFilePath);
                }
            }

            return(true);
        }
        private static bool SaveToFile()
        {
            // Create a new temp file
            string tempFilePath = Utility.GetTempFileName();

            try
            {
                // Open the temp file
                StreamWriter writer = null;
                try
                {
                    Utility.PerformIOWithRetries(
                        () =>
                    {
                        FileStream fileStream = FabricFile.Open(tempFilePath, FileMode.Create, FileAccess.Write);
#if !DotNetCoreClrLinux
                        Helpers.SetIoPriorityHint(fileStream.SafeFileHandle, Kernel32Types.PRIORITY_HINT.IoPriorityHintVeryLow);
#endif
                        writer = new StreamWriter(fileStream);
                    });
                }
                catch (Exception e)
                {
                    Utility.TraceSource.WriteExceptionAsError(
                        LogSourceId,
                        e,
                        "Failed to open temp file {0} for backing up application activation table.",
                        tempFilePath);
                    return(false);
                }

                try
                {
                    // Write the version information to the backup file
                    try
                    {
                        Utility.PerformIOWithRetries(
                            () =>
                        {
                            writer.WriteLine(VersionString);
                        });
                    }
                    catch (Exception e)
                    {
                        Utility.TraceSource.WriteExceptionAsError(
                            LogSourceId,
                            e,
                            "Failed to write version information to temp file {0} for backing up application activation table.",
                            tempFilePath);
                        return(false);
                    }

                    // Write the activation records to the backup file
                    foreach (string appType in applicationEtwTracesStartTime.Keys)
                    {
                        foreach (string appInstanceId in applicationEtwTracesStartTime[appType].Keys)
                        {
                            DateTime activationTime   = applicationEtwTracesStartTime[appType][appInstanceId];
                            string   activationRecord = string.Join(
                                ",",
                                appInstanceId,
                                appType,
                                activationTime.ToBinary().ToString(),
                                activationTime.ToString());
                            try
                            {
                                Utility.PerformIOWithRetries(
                                    () =>
                                {
                                    writer.WriteLine(activationRecord);
                                });
                            }
                            catch (Exception e)
                            {
                                Utility.TraceSource.WriteExceptionAsError(
                                    LogSourceId,
                                    e,
                                    "Failed to write record {0} to temp file {1} for backing up application activation table.",
                                    activationRecord,
                                    tempFilePath);
                                return(false);
                            }
                        }
                    }
                }
                finally
                {
                    writer.Dispose();
                }

                // Copy the temp file as the new backup file
                try
                {
                    Utility.PerformIOWithRetries(
                        () =>
                    {
                        FabricFile.Copy(tempFilePath, backupFileFullPath, true);
                    });
                }
                catch (Exception e)
                {
                    Utility.TraceSource.WriteExceptionAsError(
                        LogSourceId,
                        e,
                        "Failed to copy file {0} to {1} for backing up application activation table.",
                        tempFilePath,
                        backupFileFullPath);
                    return(false);
                }

                Utility.TraceSource.WriteInfo(
                    LogSourceId,
                    "Application activation table backup file {0} created.",
                    backupFileFullPath);
            }
            finally
            {
                try
                {
                    Utility.PerformIOWithRetries(
                        () =>
                    {
                        FabricFile.Delete(tempFilePath);
                    });
                }
                catch (Exception e)
                {
                    Utility.TraceSource.WriteExceptionAsError(
                        LogSourceId,
                        e,
                        "Failed to delete temp file {0} which was created for backing up application activation table.",
                        tempFilePath);
                }
            }

            return(true);
        }