/// <summary> /// Gets the LazyCopy reparse data from the <paramref name="path"/> given. /// </summary> /// <param name="path">Path to the file to get the reparse data from.</param> /// <returns> /// Reparse data found or <see langword="null"/>, if it's not set. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/> or empty.</exception> /// <exception cref="IOException">File cannot be opened.</exception> /// <exception cref="InvalidOperationException">Reparse point data cannot be retrieved.</exception> public static LazyCopyFileData GetReparseData(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } string normalizedPath = LongPathCommon.NormalizePath(path); LongPathFileInfo fileInfo = new LongPathFileInfo(normalizedPath); if (!fileInfo.Exists || !fileInfo.Attributes.HasFlag(FileAttributes.ReparsePoint)) { return(null); } try { LazyCopyReparseData data = ReparsePointHelper.GetReparsePointData <LazyCopyReparseData>(path, LazyCopyFileHelper.LazyCopyReparseTag, LazyCopyFileHelper.LazyCopyReparseGuid); bool useCustomHandler = data.UseCustomHandler > 0; return(new LazyCopyFileData { UseCustomHandler = useCustomHandler, FileSize = data.FileSize, RemotePath = useCustomHandler ? data.RemotePath : PathHelper.ChangeDeviceNameToDriveLetter(data.RemotePath) }); } catch (InvalidOperationException) { return(null); } }
protected override void ProcessPath(PscxPathInfo pscxPath) { var name = ReparsePointHelper.EnsurePathSlash(pscxPath.ProviderPath); if (!NativeMethods.DeleteVolumeMountPoint(name)) { ErrorHandler.WriteLastWin32Error("DeleteVolumeMountPointFailed", name); } base.ProcessPath(pscxPath); }
protected override void ProcessRecord() { var volume = Path.GetPathRoot(Volume); if (String.IsNullOrEmpty(volume)) { EnumerateVolumes(); } else { volume = ReparsePointHelper.EnsurePathSlash(volume); EnumerateVolumeMountPoints(volume); } }
protected override void ProcessPath(PscxPathInfo pscxPath) { var filePath = pscxPath.ProviderPath; if (ReparsePointHelper.IsReparsePoint(filePath)) { if (Raw) { WriteObject(ReparsePointHelper.GetReparsePointData(filePath)); } else { WriteObject(ReparsePointHelper.GetReparsePoint(filePath)); } } }
protected override void ProcessRecord() { if (ShouldProcess(TargetPath.ProviderPath, String.Format("New NTFS junction via {0}", LiteralPath.ProviderPath))) { Directory.CreateDirectory(LiteralPath.ProviderPath); if (!ReparsePointHelper.CreateJunction(LiteralPath.ProviderPath, TargetPath.ProviderPath)) { ErrorHandler.WriteLastWin32Error("CreateJunctionFailed", LiteralPath); } else { WriteObject(new DirectoryInfo(LiteralPath.ProviderPath)); } } }
/// <summary> /// Creates a new <c>LazyCopy</c> file. /// </summary> /// <param name="path">Path to the reparse point to get data from.</param> /// <param name="fileData">Reparse file data to be set for the <paramref name="path"/>.</param> /// <exception cref="ArgumentNullException"> /// <paramref name="path"/> is <see langword="null"/> or empty. /// <para>-or-</para> /// <paramref name="fileData"/> is <see langword="null"/>. /// <para>-or-</para> /// <paramref name="fileData"/> contains <see langword="null"/> or empty file path. /// </exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="fileData"/> contains negative file size.</exception> /// <exception cref="IOException">File cannot be created.</exception> /// <exception cref="InvalidOperationException">Reparse point data cannot be set.</exception> public static void CreateLazyCopyFile(string path, LazyCopyFileData fileData) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } if (fileData == null) { throw new ArgumentNullException(nameof(fileData)); } if (string.IsNullOrEmpty(fileData.RemotePath)) { throw new ArgumentNullException(nameof(fileData)); } if (fileData.FileSize < 0) { throw new ArgumentOutOfRangeException(nameof(fileData), fileData.FileSize, "File size is negative."); } string normalizedPath = LongPathCommon.NormalizePath(path); LongPathFileInfo fileInfo = new LongPathFileInfo(normalizedPath); bool shouldCreateFile = false; if (!fileInfo.Exists || fileInfo.Length > 0) { LongPathDirectory.CreateDirectory(fileInfo.DirectoryName); shouldCreateFile = true; } else if (!fileInfo.Attributes.HasFlag(FileAttributes.ReparsePoint)) { shouldCreateFile = true; } else if (fileInfo.Attributes.HasFlag(FileAttributes.ReadOnly)) { fileInfo.Attributes &= ~FileAttributes.ReadOnly; } if (shouldCreateFile) { using (fileInfo.Create()) { // Do nothing. } } // If the original file is empty, we don't need to set a reparse point for it. if (fileData.FileSize == 0) { return; } ReparsePointHelper.SetReparsePointData( path, new object[] // Custom serialization layout for the LazyCopyFileData object. { (long)(fileData.UseCustomHandler ? 1L : 0L), (long)fileData.FileSize, // Add the prefix, if the custom handling is needed for the file. Encoding.Unicode.GetBytes( (fileData.UseCustomHandler ? fileData.RemotePath : PathHelper.ChangeDriveLetterToDeviceName(fileData.RemotePath)) + '\0') }, LazyCopyFileHelper.LazyCopyReparseTag, LazyCopyFileHelper.LazyCopyReparseGuid); // Set the proper file attributes. LongPathCommon.SetAttributes(path, FileAttributes.ReparsePoint | FileAttributes.NotContentIndexed | FileAttributes.Offline); }