public static void ValidateExecutePermission(string directory) { ArgUtil.Directory(directory, nameof(directory)); string dir = directory; string failsafeString = Environment.GetEnvironmentVariable("AGENT_TEST_VALIDATE_EXECUTE_PERMISSIONS_FAILSAFE"); int failsafe; if (string.IsNullOrEmpty(failsafeString) || !int.TryParse(failsafeString, out failsafe)) { failsafe = 100; } for (int i = 0; i < failsafe; i++) { try { Directory.EnumerateFileSystemEntries(dir).FirstOrDefault(); } catch (UnauthorizedAccessException ex) { // Permission to read the directory contents is required for '{0}' and each directory up the hierarchy. {1} string message = StringUtil.Loc("DirectoryHierarchyUnauthorized", directory, ex.Message); throw new UnauthorizedAccessException(message, ex); } dir = Path.GetDirectoryName(dir); if (string.IsNullOrEmpty(dir)) { return; } } // This should never happen. throw new NotSupportedException($"Unable to validate execute permissions for directory '{directory}'. Exceeded maximum iterations."); }
public static void PrependPath(string directory) { ArgUtil.Directory(directory, nameof(directory)); // Build the new value. string currentPath = Environment.GetEnvironmentVariable(PathVariable); string path = PrependPath(directory, currentPath); // Update the PATH environment variable. Environment.SetEnvironmentVariable(PathVariable, path); }
public void PrependPath(string directory) { ArgUtil.Directory(directory, nameof(directory)); // Build the new value. string path = Environment.GetEnvironmentVariable(Constants.PathVariable); if (string.IsNullOrEmpty(path)) { // Careful not to add a trailing separator if the PATH is empty. // On OSX/Linux, a trailing separator indicates that "current directory" // is added to the PATH, which is considered a security risk. path = directory; } else { path = directory + Path.PathSeparator + path; } // Update the PATH environment variable. Environment.SetEnvironmentVariable(Constants.PathVariable, path); }
public static void CopyDirectory(string source, string target, CancellationToken cancellationToken) { // Validate args. ArgUtil.Directory(source, nameof(source)); ArgUtil.NotNullOrEmpty(target, nameof(target)); ArgUtil.NotNull(cancellationToken, nameof(cancellationToken)); cancellationToken.ThrowIfCancellationRequested(); // Create the target directory. Directory.CreateDirectory(target); // Get the file contents of the directory to copy. DirectoryInfo sourceDir = new DirectoryInfo(source); foreach (FileInfo sourceFile in sourceDir.GetFiles() ?? new FileInfo[0]) { // Check if the file already exists. cancellationToken.ThrowIfCancellationRequested(); FileInfo targetFile = new FileInfo(Path.Combine(target, sourceFile.Name)); if (!targetFile.Exists || sourceFile.Length != targetFile.Length || sourceFile.LastWriteTime != targetFile.LastWriteTime) { // Copy the file. sourceFile.CopyTo(targetFile.FullName, true); } } // Copy the subdirectories. foreach (DirectoryInfo subDir in sourceDir.GetDirectories() ?? new DirectoryInfo[0]) { CopyDirectory( source: subDir.FullName, target: Path.Combine(target, subDir.Name), cancellationToken: cancellationToken); } }
public static void MoveDirectory(string sourceDir, string targetDir, string stagingDir, CancellationToken token) { ArgUtil.Directory(sourceDir, nameof(sourceDir)); ArgUtil.NotNullOrEmpty(targetDir, nameof(targetDir)); ArgUtil.NotNullOrEmpty(stagingDir, nameof(stagingDir)); // delete existing stagingDir DeleteDirectory(stagingDir, token); // make sure parent dir of stagingDir exist Directory.CreateDirectory(Path.GetDirectoryName(stagingDir)); // move source to staging Directory.Move(sourceDir, stagingDir); // delete existing targetDir DeleteDirectory(targetDir, token); // make sure parent dir of targetDir exist Directory.CreateDirectory(Path.GetDirectoryName(targetDir)); // move staging to target Directory.Move(stagingDir, targetDir); }