private static void PopulateVolumes( ContainerConfig containerConfig, ContainerDescription containerDesc) { foreach (var volume in containerDesc.Volumes) { #if !DotNetCoreClrLinux if (string.IsNullOrEmpty(volume.Driver) && !FabricDirectory.Exists(volume.Source)) { throw new FabricException( string.Format( "The source location specified for container volume does not exist. Source:{0}", volume.Source), FabricErrorCode.DirectoryNotFound); } #endif var bindFormat = volume.IsReadOnly ? "{0}:{1}:ro" : "{0}:{1}"; containerConfig.HostConfig.Binds.Add(string.Format(bindFormat, volume.Source, volume.Destination)); } foreach (var bind in containerDesc.BindMounts) { containerConfig.HostConfig.Binds.Add( string.Format("{0}:{1}:ro", bind.Value, bind.Key)); } }
public static string ComputeHash(string resourcePath, bool imageStoreServiceEnabled) { byte[] hash = null; if (FabricFile.Exists(resourcePath)) { hash = ComputeHashOnFile(resourcePath); } else if (FabricDirectory.Exists(resourcePath)) { hash = ComputeHashOnDirectory(resourcePath, imageStoreServiceEnabled); } else { System.Fabric.Interop.Utility.ReleaseAssert(true, "{0} is not found."); } StringBuilder hashStringBuilder = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { hashStringBuilder.Append(string.Format(CultureInfo.InvariantCulture, "{0:X2}", hash[i])); } return(hashStringBuilder.ToString()); }
/// <summary> /// Check if content is present in the store. /// </summary> /// <param name="tag"> Location (relative to RootUri) where to check the presence of the content. </param> /// <param name="timeout">Check existence timeout</param> /// <returns> True if the content exists, false otherwise. </returns> public bool DoesContentExist(string tag, TimeSpan timeout) { TimeoutHelper helper = timeout == TimeSpan.MaxValue ? null : new TimeoutHelper(timeout); #if !DotNetCoreClr using (WindowsImpersonationContext impersonationContext = this.GetImpersonationContext()) #endif { string smbTag = this.ConvertTagToSMBPath(tag); if (FabricFile.Exists(smbTag)) { return(!FileLock.DoesWriterLockExist(smbTag)); } if (helper != null) { helper.ThrowIfExpired(); } if (FabricDirectory.Exists(smbTag)) { return(!FileLock.DoesWriterLockExist(smbTag)); } } return(false); }
/// <summary> /// Initializes a new instance of the FileImageStore class. /// </summary> /// <param name="uri">The root URI (the root folder) of the image store.</param> /// <param name="localRoot">The default path for the file image store root.</param> /// <param name="accessDescription">Access rights to specific users or groups.</param> public FileImageStore( string uri, string localRoot = null, ImageStoreAccessDescription accessDescription = null) { if (uri == null) { throw new ArgumentException(StringResources.ImageStoreError_InvalidParameterSpecified, "uri"); } this.localRoot = localRoot; this.accessDescription = accessDescription; this.storeParams = new FileProviderParameters(uri); if (!this.storeParams.RootUri.StartsWith(SchemeTag, StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, StringResources.ImageStoreError_InvalidImageStoreUriScheme, this.storeParams.RootUri)); } this.rootUri = this.storeParams.RootUri.TrimStart().Substring(SchemeTag.Length).Trim(); #if !DotNetCoreClr using (WindowsImpersonationContext impersonationContext = this.GetImpersonationContext()) #endif { if (!FabricDirectory.Exists(this.rootUri)) { throw new DirectoryNotFoundException(string.Format(CultureInfo.CurrentCulture, StringResources.ImageStoreError_UnaccessibleImageStore, this.rootUri)); } } }
/// <summary> /// All files at the given folder /// </summary> /// <param name="storeSource">Location (relative to root)</param> /// <param name="timeout">List timeout</param> /// <returns>File name of each files within the given location</returns> public IEnumerable <string> ListContent(string storeSource, TimeSpan timeout) { List <string> files = new List <string>(); try { string smbTag = this.ConvertTagToSMBPath(storeSource); if (!FabricDirectory.Exists(smbTag)) { TraceSource.WriteWarning( TraceType, "Directory {0} doesn't exist", smbTag); } else { this.GetAllFileFullNames(smbTag, timeout).ForEach <string>(fullName => files.Add(fullName.Substring(this.rootUri.Length + 1))); } } catch (IOException exception) { if (exception.GetType() == typeof(IOException)) { throw new FabricImageStoreException(StringResources.Error_ImageStoreIOException, exception); } else { throw; } } return(files); }
internal static void WriteStringToFile(string fileName, string value, bool writeLine = true, Encoding encoding = null) { var directory = FabricPath.GetDirectoryName(fileName); if (!FabricDirectory.Exists(directory)) { FabricDirectory.CreateDirectory(directory); } if (encoding == null) { #if DotNetCoreClrLinux encoding = new UTF8Encoding(false); #else encoding = Encoding.GetEncoding(0); #endif } using (StreamWriter writer = new StreamWriter(FabricFile.Open(fileName, FileMode.OpenOrCreate), encoding)) { if (writeLine) { writer.WriteLine(value); } else { writer.Write(value); } #if DotNetCoreClrLinux Helpers.UpdateFilePermission(fileName); #endif } }
public void TestCleanup() { if (FabricDirectory.Exists(this.testPath)) { FabricDirectory.Delete(this.testPath, true); } }
/// <summary> /// Post-processing of copying (from XStore to SMB) /// </summary> /// <param name="task">The task to end.</param> private static void EndCopyFromXStoreToSMB(XStoreFileOperationTask task) { if (task.IsSucceeded) { // Step 3, 4 and 5 for atomic copy // 3. delete dstFolder.old // 4. rename dstFolder -> dstFolder.old // 5. rename dstFolder.new -> dstFolder // get the original destination path string newDstRootUri = task.DstUri + NewExtension; string oldDstRootUri = task.DstUri + OldExtension; // Step 3, 4 & 5 if (FabricDirectory.Exists(oldDstRootUri)) { // delete dstFolder.old FabricDirectory.Delete(oldDstRootUri, recursive: true, deleteReadOnlyFiles: true); } if (FabricDirectory.Exists(task.DstUri)) { // rename dstFolder -> dstFolder.old RenameFolder(task.DstUri, oldDstRootUri); } // rename dstFolder.new -> dstFolder; and clean up RenameFolder(newDstRootUri, task.DstUri); if (FabricDirectory.Exists(oldDstRootUri)) { FabricDirectory.Delete(oldDstRootUri, recursive: true, deleteReadOnlyFiles: true); } } }
public static void CreateFolderIfNotExists(string directoryPath) { if (!FabricDirectory.Exists(directoryPath)) { FabricDirectory.CreateDirectory(directoryPath); } }
bool CheckIfBackupExists(string fileOrFolderPath) { if (fileOrFolderPath.EndsWith(ZipFileExtension) || fileOrFolderPath.EndsWith(RecoveryPointMetadataFileExtension)) { return(FabricFile.Exists(fileOrFolderPath)); } return(FabricDirectory.Exists(fileOrFolderPath)); }
public void FabricDirectory_DeleteNonRecursiveEmptyFolder() { Directory.CreateDirectory(this.testPath); LogHelper.Log("FabricDirectory.Delete {0}", this.testPath); FabricDirectory.Delete(this.testPath, false, false); Assert.IsFalse(FabricDirectory.Exists(this.testPath)); }
private void CreateDirectoryPath(string path) { string directoryName = FabricPath.GetDirectoryName(path); if ((directoryName != null) && (directoryName.Length > 0) && (!FabricDirectory.Exists(directoryName))) { FabricDirectory.CreateDirectory(directoryName); } }
internal async Task GenerateWithValueType <ValueType>(string logFolder, ValueType value, IStateSerializer <ValueType> valueSerializer) { // directory setup if (FabricDirectory.Exists(logFolder)) { FabricDirectory.Delete(logFolder, true); } FabricDirectory.CreateDirectory(logFolder); var rand = new Random(); var reliabilitySimulator = new ReliabilitySimulator( logFolder, new Uri("fabric:/unittest/service" + rand.Next()), // random service name. OnDataLossCallback, // we are never calling OnDataLossAsync on this ReliabilitySimulator. CreateStateProvider); reliabilitySimulator.CreateReplica(true, false); var replicator = reliabilitySimulator.GetTransactionalReplicator(); replicator.TryAddStateSerializer <ValueType>(valueSerializer); // Constants var distributedDictionary = new DistributedDictionary <long, ValueType>(); var distributedQueue = new DistributedQueue <ValueType>(); var concurrentQueue = new ReliableConcurrentQueue <ValueType>(); // Setup await reliabilitySimulator.OpenReplicaAsync(ReplicaOpenMode.New).ConfigureAwait(false); await reliabilitySimulator.PromoteReplicaAsync(false).ConfigureAwait(false); var result = replicator.CreateAsyncEnumerable(false, false); Assert.AreEqual(0, result.ToEnumerable().Count(), "State Manager must be empty"); // Write data. using (var txn = replicator.CreateTransaction()) { await replicator.AddStateProviderAsync(txn, DictionaryName, distributedDictionary).ConfigureAwait(false); await txn.CommitAsync().ConfigureAwait(false); } result = replicator.CreateAsyncEnumerable(false, false); Assert.AreEqual(2, result.ToEnumerable().Count(), "State Manager must include all the state providers"); await PopulateDictionaryAsync(replicator, DictionaryName, 0, 8, 8, value).ConfigureAwait(false); // Take backup await replicator.BackupAsync(ComplexDataBackupCallbackAsync).ConfigureAwait(false); // Clean up. await reliabilitySimulator.DropReplicaAsync(); }
public void UploadContent(string tag, string sourceSoftLink, IImageStoreProgressHandler handler, TimeSpan timeout, CopyFlag copyFlag, bool acquireSourceReaderLock) { TimeoutHelper helper = timeout == TimeSpan.MaxValue ? null : new TimeoutHelper(timeout); sourceSoftLink = this.GetLocalPath(sourceSoftLink); if ((!FabricFile.Exists(sourceSoftLink)) && (!FabricDirectory.Exists(sourceSoftLink))) { throw new IOException( string.Format( CultureInfo.CurrentCulture, StringResources.ImageStoreError_DoesNotExistError, sourceSoftLink)); } try { using (XStoreFileOperations xstoreFileOperation = new XStoreFileOperations(new XStoreParameters(this.providerParams.ConnectionString, this.providerParams.SecondaryConnectionString, this.providerParams.ContainerName))) { tag = XStoreCommon.FormatXStoreUri(tag); if (copyFlag == CopyFlag.AtomicCopy) { this.DeleteContent(tag, helper == null ? timeout : helper.GetRemainingTime()); } if (FabricFile.Exists(sourceSoftLink)) { // This is a file copy xstoreFileOperation.CopyFile( sourceSoftLink, tag, XStoreFileOperationType.CopyFromSMBToXStore, helper); } else { // this is a folder copy xstoreFileOperation.CopyFolder( sourceSoftLink, tag, XStoreFileOperationType.CopyFromSMBToXStore, copyFlag, null, null, helper); } } } catch (Exception ex) { this.HandleException(ex); throw; } }
/// <summary> /// Async copy method that begins the copy operation. Wait on AsyncWaitHandle to be notified of completion. /// </summary> /// <param name="source"> /// Source folder that needs to be copied /// </param> /// <param name="destination"> /// Destination folder /// </param> public void BeginCopy(string source, string destination) { this.ThrowIfDisposed(); // make sure that the source folder exists if (!FabricDirectory.Exists(source)) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, StringResources.ImageStoreError_DoesNotExistError, source)); } // make sure that the event indicating completion is non-signalled this.asyncEvent.Reset(); this.internalCopyAsyncEvent.Reset(); // To make folder copy atomic, the sequence of actions are as below. // 1. delete dstFolder.new // 2. copy srcFolder -> dstFolder.new // 3. delete dstFolder.old // 4. rename dstFolder -> dstFolder.old // 5. rename dstFolder.new -> dstFolder string newDstFolder = destination; if (this.copyFlag == CopyFlag.AtomicCopySkipIfExists && FabricDirectory.Exists(newDstFolder)) { // We will skip the copy because the final destination exists this.skipCopy = true; } else if (this.copyFlag == CopyFlag.AtomicCopy || this.copyFlag == CopyFlag.AtomicCopySkipIfExists) { newDstFolder = destination + FolderCopy.NewExtension; // Step 1. if (FabricDirectory.Exists(newDstFolder)) { FabricDirectory.Delete(newDstFolder, recursive: true, deleteReadOnlyFiles: true); } } // Queue up post copy completion task that needs to perform steps 3,4 & 5 mentioned above. ThreadPool.QueueUserWorkItem(this.EndCopy, new CopyArgs(source, destination, true)); // set the work items to 1 and enqueue the first item to be copied this.workItemsInProgress = 1; if (!this.skipCopy) { ThreadPool.QueueUserWorkItem(this.CopyItem, new CopyArgs(source, newDstFolder, true)); } else { // If we are skipping then just decrement and signal immediately. // We still Queue up the EndCopy because it translates the inner event into the outer event. this.DecrementWorkItem(); } }
public static void DeleteTempLocation(string location) { if (!string.IsNullOrEmpty(location) && FabricDirectory.Exists(location)) { FabricDirectory.Delete(location, recursive: true, deleteReadOnlyFiles: true); } else if (!string.IsNullOrEmpty(location) && FabricFile.Exists(location)) { FabricFile.Delete(location, deleteReadonly: true); } }
private static ulong GetNewContainerInstanceIdAtomic( ContainerDescription containerDesc) { #if DotNetCoreClrLinux // There is a file called containerid in // /log/application/partitionid/servicepackageactivationid/codepackagename/ // This file contains the number corresponding to the latest folder for logs // /mnt/log/application/partitionid/servicepackageactivationid/codepackagename/number/application.log // // For the first instance of thecontainer, create a new file and start at 0, // For every container afterwards, check the file exists, read from the file // and add 1 to the value in the file in order to get the new current instance id // and save the value to the file. var applicationNamePart = containerDesc.ApplicationName; var applicationNameExtraPart = string.Format("{0}/", RootNamingUriString); if (applicationNamePart.StartsWith(applicationNameExtraPart)) { applicationNamePart = applicationNamePart.Substring(applicationNameExtraPart.Length); } applicationNamePart = applicationNamePart.Replace('/', '_'); var digestedApplicationName = applicationNamePart + DigestedApplicationNameDockerSuffix; var logRoots = "/mnt/logs"; var logPath = Path.Combine(logRoots, digestedApplicationName, containerDesc.PartitionId, containerDesc.ServicePackageActivationId, containerDesc.CodePackageName); if (!FabricDirectory.Exists(logPath)) { FabricDirectory.CreateDirectory(logPath); } ulong containerId = 0; var containerInstanceIdFilePath = Path.Combine(logPath, CodePackageInstanceIdFileName); if (!FabricFile.Exists(containerInstanceIdFilePath)) { using (var containerIdFile = FabricFile.Create(containerInstanceIdFilePath)) { containerId = 0; } } else { containerId = ReadContainerIdFromFile(containerInstanceIdFilePath) + 1; } WriteContainerIdToFile(containerInstanceIdFilePath, containerId); return(containerId); #else return(0); #endif }
/// <summary> /// Get the image store content information including all files and subfolders /// </summary> /// <param name="storeSource">The relative path to file or folder from image store root</param> /// <param name="isRecursive">flag list subhierarchy</param> /// <param name="timeout">The timeout for performing the listing operation</param> /// <returns>ImageStoreContent object including files and subfolders</returns> public ImageStoreContent ListContentWithDetails(string storeSource, bool isRecursive, TimeSpan timeout) { ImageStoreContent content = null; try { string smbTag = this.ConvertTagToSMBPath(storeSource); if (!FabricDirectory.Exists(smbTag)) { if (FabricFile.Exists(smbTag)) { var fullSourcePath = FabricPath.GetFullPath(storeSource); content = new ImageStoreContent() { Files = new List <ImageStoreFile>() { this.ConvertToImageStoreFile(fullSourcePath, storeSource) } }; } else { TraceSource.WriteWarning( TraceType, "Directory/File {0} doesn't exist", smbTag); } } else { content = isRecursive ? new ImageStoreContent() { Files = this.GetAllFiles(smbTag, storeSource).ToList() } : this.GetFilesAndSubFolders(smbTag, storeSource, timeout); } } catch (IOException exception) { if (exception.GetType() == typeof(IOException)) { throw new FabricImageStoreException(string.Format(CultureInfo.CurrentCulture, StringResources.Error_ImageStoreIOException, exception)); } else { throw; } } return(content); }
public void FabricDirectory_Rename() { string renameFolderName = Path.Combine(Environment.ExpandEnvironmentVariables(TestPathPrefix), TestRenameFolderName); CreateNonEmptyDirectory(this.testPath); LogHelper.Log("FabricDirectory.Rename {0}", this.testPath); FabricDirectory.Rename(this.testPath, renameFolderName, true); Assert.IsFalse(FabricDirectory.Exists(this.testPath)); Assert.IsTrue(FabricDirectory.Exists(renameFolderName)); FabricDirectory.Delete(renameFolderName, true, false); Assert.IsFalse(FabricDirectory.Exists(renameFolderName)); }
private bool TryUpdateLocalMap(int retryCount, string localMapDestinationDir, string localMapDestination, DateTime sourceLastWriteTime) { // Record the source file's last write time based on the snapshot // we took before copying the source file. We know that we copied // over a version that it at least as new as the timestamp in the // snapshot. // Figure out in which directory in the local map we will // record information about this file. Create that directory // if it doesn't exist. if ((false == string.IsNullOrEmpty(localMapDestinationDir)) && (false == FabricDirectory.Exists(localMapDestinationDir))) { try { Utility.PerformIOWithRetries( () => { FabricDirectory.CreateDirectory(localMapDestinationDir); }, retryCount); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( this.LogSourceId, e, "Failed to create directory {0} in the local map of uploaded files.", localMapDestinationDir); return(false); } } // If the file doesn't exist in the local map, create it. if (false == FabricFile.Exists(localMapDestination)) { if (!this.TryCreateLocalMapFile(retryCount, localMapDestination, sourceLastWriteTime)) { return(false); } } else { // File already exists in the local map. Set the last // write time for the file in the local map to the last // write time from the source file snapshot // It's a Best Effort operation. this.TryUpdateLastWriteTime(localMapDestination, sourceLastWriteTime); } return(true); }
/// <summary> /// Add a full backup folder to the chain. /// </summary> /// <param name="backupFolder">Path of the input full backup folder.</param> /// <param name="cancellationToken">Cancellation token.</param> /// <returns>Task that represents the asynchronous work.</returns> /// <exception cref="ArgumentException">The relevant folder is not a valid full backup folder.</exception> /// <exception cref="InvalidDataException">There is a corruption in the file.</exception> private async Task AddFullBackupFolderAsync(string backupFolder, CancellationToken cancellationToken) { Utility.Assert(this.StateManagerBackupFolderPath == null, "There can only be one full backup."); Utility.Assert(FabricDirectory.Exists(backupFolder), "Directory must exist."); var stateManagerBackupFolderPath = Path.Combine(backupFolder, Replicator.Constants.StateManagerBackupFolderName); if (false == FabricDirectory.Exists(stateManagerBackupFolderPath)) { throw new ArgumentException(SR.Error_BackupFolderInfo_NullFolder, "backupFolder"); } this.fullBackupFolderPath = backupFolder; this.stateManagerBackupFolderPath = stateManagerBackupFolderPath; var metadataPath = Path.Combine(backupFolder, Replicator.Constants.ReplicatorBackupMetadataFileName); // Can throw InvalidDataException BackupMetadataFile backupMetadataFile; try { backupMetadataFile = await BackupMetadataFile.OpenAsync(metadataPath, cancellationToken).ConfigureAwait(false); } catch (Exception e) { FabricEvents.Events.RestoreException( this.traceType, Guid.Empty, e.GetType().ToString(), e.Message, e.StackTrace); throw; } var version = new BackupInfo.BackupVersion( backupMetadataFile.BackupEpoch, backupMetadataFile.BackupLsn); this.backupVersionList.Add(version); this.backupMetadataFileList.Add(backupMetadataFile); var logFolderPath = Path.Combine( backupFolder, Replicator.Constants.ReplicatorBackupFolderName, Replicator.Constants.ReplicatorBackupLogName); this.logFileList.Add(logFolderPath); }
private static void RenameFolder(string srcFolderName, string dstFolderName) { if (string.IsNullOrEmpty(srcFolderName) || !FabricDirectory.Exists(srcFolderName)) { throw new ArgumentException("Invalid source folder name specified"); } if (string.IsNullOrEmpty(dstFolderName) || FabricDirectory.Exists(dstFolderName)) { throw new ArgumentException("Invalid destination folder name specified"); } for (int i = 1; i <= MaxRetryAttempts; i++) { try { FabricFile.Move(srcFolderName, dstFolderName); if (i > 1) { // Try sleeping second time around Thread.Sleep(500); } //This is for the rare case of renaming failed without any error from the common library; referring to incident #45017448 if (!FabricDirectory.Exists(dstFolderName)) { traceSource.WriteWarning( ClassName, "Failed to rename folder {0} to {1} without any error, and retrying", srcFolderName, dstFolderName); } else { break; } } catch (Exception ex) { traceSource.WriteError( ClassName, "Failed to rename folder {0} to {1} with error {2}, and detail exception {3}", srcFolderName, dstFolderName, ex.Message, ex); } } }
public static void UpdateFileLocation(string traceFolder) { string drive = Path.GetPathRoot(traceFolder); #if DotNetCoreClrLinux string traceFileName = string.Format(CultureInfo.InvariantCulture, "FabricDeployer-{0}.trace", DateTime.UtcNow.Ticks); if (!FabricDirectory.Exists(traceFolder)) { WriteInfo("Trace folder doesn't exist. Creating trace folder: {0}", traceFolder); FabricDirectory.CreateDirectory(traceFolder); } #else if (!Directory.Exists(drive)) { string newTraceFolder = Path.Combine(Path.GetTempPath(), "FabricDeployerTraces"); WriteInfo("Default trace destination does not exist: {0}. Using directory instead: {1}.", traceFolder, newTraceFolder); traceFolder = newTraceFolder; } string traceFileName = string.Format(CultureInfo.InvariantCulture, "FabricDeployer-{0}.trace", DateTime.Now.Ticks); if (!Directory.Exists(traceFolder)) { WriteInfo("Trace folder doesn't exist. Creating trace folder: {0}", traceFolder); Directory.CreateDirectory(traceFolder); } #endif else { WriteInfo("Trace folder already exists. Traces will be written to existing trace folder: {0}", traceFolder); } string traceFileFullPath = Path.Combine(traceFolder, traceFileName); #if DotNetCoreClr /* TBD: Fix following code. * if (deployerFileTraces != null) * { * deployerFileTraces.Dispose(); * } * deployerFileTraces = new FileEventListener(traceFileFullPath); * deployerFileTraces.EnableEvents(FabricEvents.Events, EventLevel.Informational); */ #else TraceTextFileSink.SetPath(traceFileFullPath); TraceConfig.SetDefaultLevel(TraceSinkType.TextFile, EventLevel.Informational); #endif }
private bool AddSubFoldersToQueueWorker(object context) { SubFolderEnumerationData enumData = (SubFolderEnumerationData)context; string folder = enumData.FolderInfo.FolderName; Queue <FolderDeletionInfo> folderQueue = enumData.FolderQueue; SetFolderDeletionInfoContext setContext = enumData.SetContext; if (false == FabricDirectory.Exists(folder)) { // Directory does not exist. Nothing more to be done here. return(true); } // Create a DirectoryInfo object representing the current folder DirectoryInfo dirInfo = new DirectoryInfo(folder); // Add the current folder's sub-folders to the queue IEnumerable <DirectoryInfo> subFolders = dirInfo.EnumerateDirectories(); foreach (DirectoryInfo subFolder in subFolders) { if (this.stopping) { this.traceSource.WriteInfo( this.logSourceId, "The consumer is being stopped. Therefore, enumeration of subfolders of folder {0} during old file deletion was interrupted.", folder); break; } FolderDeletionInfo subFolderInfo = new FolderDeletionInfo(); subFolderInfo.FolderName = subFolder.FullName; subFolderInfo.Context = null; if (null != setContext) { setContext(subFolderInfo, enumData.FolderInfo, subFolder.Name); } folderQueue.Enqueue(subFolderInfo); } return(true); }
private static void ValidateXCopyPackageAndGetVersion(string packagePath, out string codeVersion) { foreach (string childFolder in ChildFoldersInXcopyPackage) { string newPath = Path.Combine(packagePath, childFolder); if (!FabricDirectory.Exists(newPath)) { ImageBuilderUtility.TraceAndThrowValidationError( FabricProvisionOperation.TraceType, StringResources.ImageStoreError_ChildFolderDoesNotExist_Formatted, childFolder, packagePath); } } codeVersion = "0.0.0.0"; var fabricExeCollection = FabricDirectory.GetFiles(packagePath, "fabric.exe", true, SearchOption.AllDirectories); if (fabricExeCollection.Count() == 0) { ImageBuilderUtility.TraceAndThrowValidationError( FabricProvisionOperation.TraceType, StringResources.ImageStoreError_FabricExeNotFound); } // Build number in version might have leading 0's i.e. 3.0.00123.0 // This causes issues during download, hence trimming the leading 0's #if DotNetCoreClrLinux var versionString = FileVersionInfo.GetVersionInfo(fabricExeCollection.First()).ProductVersion; #else var versionString = FabricFile.GetVersionInfo(fabricExeCollection.First()); #endif Version productVersion = null; if (!Version.TryParse(versionString, out productVersion)) { ImageBuilderUtility.TraceAndThrowValidationError( FabricProvisionOperation.TraceType, StringResources.ImageStoreError_InvalidProductVersion, versionString); } codeVersion = productVersion.ToString(); }
public static void RemoveReadOnlyFlag(string path) { if (FabricDirectory.Exists(path)) { var fileFullPaths = FabricDirectory.GetFiles(path, "*", true, SearchOption.AllDirectories); foreach (var file in fileFullPaths) { FabricFile.RemoveReadOnlyAttribute(file); } } else if (FabricFile.Exists(path)) { FabricFile.RemoveReadOnlyAttribute(path); } else { return; } }
public static void WriteXml <T>(string fileName, T value) { var directoryName = FabricPath.GetDirectoryName(fileName); if (!FabricDirectory.Exists(directoryName)) { FabricDirectory.CreateDirectory(directoryName); } using (var stream = FabricFile.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.Read)) using (var writer = XmlWriter.Create(stream)) { var serializer = new XmlSerializer(typeof(T)); serializer.Serialize(writer, value); } #if DotNetCoreClrLinux Helpers.UpdateFilePermission(fileName); #endif }
private bool UpdateLocalMap(string localMapDestinationDir, string localMapDestination) { // Figure out in which directory in the local map we will // record information about this file. Create that directory // if it doesn't exist. if ((false == string.IsNullOrEmpty(localMapDestinationDir)) && (false == FabricDirectory.Exists(localMapDestinationDir))) { try { Utility.PerformIOWithRetries( () => { FabricDirectory.CreateDirectory(localMapDestinationDir); }, AzureBlobEtwConstants.MethodExecutionInitialRetryIntervalMs, AzureBlobEtwConstants.MethodExecutionMaxRetryCount, AzureBlobEtwConstants.MethodExecutionMaxRetryIntervalMs); } catch (Exception e) { this.traceSource.WriteExceptionAsError( this.logSourceId, e, "Failed to create directory {0} in the local map of uploaded files.", localMapDestinationDir); return(false); } } // If the file doesn't exist in the local map, create it. if (false == FabricFile.Exists(localMapDestination)) { if (false == this.CreateLocalMapFile(localMapDestination)) { return(false); } } return(true); }
public static T ReadXml <T>(string fileName) { if (FabricDirectory.Exists(fileName)) { TraceAndThrowValidationError( TraceType, StringResources.ImageBuilderError_InvalidXmlFile, fileName); } using (var stream = FabricFile.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) #if DotNetCoreClr using (var xmlReader = XmlReader.Create(stream)) #else using (var xmlReader = XmlReader.Create(stream, new XmlReaderSettings() { XmlResolver = null })) #endif { return(ReadXml <T>(fileName, xmlReader)); } }
private string CreateAndAclDirectory() { string tempDirectory = Path.GetRandomFileName(); if (FabricDirectory.Exists(tempDirectory)) { throw new InvalidOperationException("Could not generate temporary local directory"); } try { // DirectoryInfo does not support long path names. Since this is only used // when the ImageStoreConnectionString is for a file share and specifies // user account information (ImageStoreFactory.ParseFileImageStoreConnectionString), // we can leave this usage here until it's absolutely needed (file share is not // supported in production). // DirectoryInfo tempDirectoryInfo = new DirectoryInfo(tempDirectory); tempDirectoryInfo.Create(); #if !DotNetCoreClr DirectorySecurity directorySecurity = tempDirectoryInfo.GetAccessControl(); directorySecurity.AddAccessRule( new FileSystemAccessRule(accessDescription.WindowsIdentity.Name, FileSystemRights.Read | FileSystemRights.Write, AccessControlType.Allow)); Directory.SetAccessControl(tempDirectory, directorySecurity); #endif return(tempDirectory); } catch (Exception) { if (FabricDirectory.Exists(tempDirectory)) { FabricDirectory.Delete(tempDirectory, recursive: true, deleteReadOnlyFiles: true); } throw; } }