internal static void DeleteEmptySubdirectoriesInternal(FileSystemEntryInfo fileSystemEntryInfo, KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly, bool initialize, PathFormat pathFormat) { #region Setup if (pathFormat == PathFormat.RelativePath) { Path.CheckValidPath(path, true, true); } if (fileSystemEntryInfo == null) { if (!File.ExistsInternal(true, transaction, path, pathFormat)) { NativeError.ThrowException(Win32Errors.ERROR_PATH_NOT_FOUND, path); } fileSystemEntryInfo = File.GetFileSystemEntryInfoInternal(transaction, Path.GetExtendedLengthPathInternal(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck) , false, pathFormat); } if (fileSystemEntryInfo == null) { throw new ArgumentNullException("path"); } string pathLp = fileSystemEntryInfo.LongFullPath; #endregion // Setup // Ensure path is a directory. if (!fileSystemEntryInfo.IsDirectory) { throw new IOException(String.Format(CultureInfo.CurrentCulture, Resources.FileExistsWithSameNameSpecifiedByPath, pathLp)); } var dirEnumOptions = DirectoryEnumerationOptions.Folders; if (recursive) { dirEnumOptions |= DirectoryEnumerationOptions.Recursive; } foreach (var fsei in EnumerateFileSystemEntryInfosInternal <FileSystemEntryInfo>(transaction, pathLp, Path.WildcardStarMatchAll, dirEnumOptions, PathFormat.LongFullPath)) { DeleteEmptySubdirectoriesInternal(fsei, transaction, null, recursive, ignoreReadOnly, false, PathFormat.LongFullPath); } if (!EnumerateFileSystemEntryInfosInternal <string>(transaction, pathLp, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.LongFullPath).Any()) { // Prevent deleting path itself. if (!initialize) { DeleteDirectoryInternal(fileSystemEntryInfo, transaction, null, false, ignoreReadOnly, true, true, PathFormat.LongFullPath); } } }
/// <summary>[AlphaFS] Initializes the specified file name.</summary> /// <param name="isFolder">Specifies that <paramref name="path"/> is a file or directory.</param> /// <param name="transaction">The transaction.</param> /// <param name="path">The full path and name of the file.</param> /// <param name="isFullPath"> /// <para><c>true</c> <paramref name="path"/> is an absolute path. Unicode prefix is applied.</para> /// <para><c>false</c> <paramref name="path"/> will be checked and resolved to an absolute path. Unicode prefix is applied.</para> /// <para><c>null</c> <paramref name="path"/> is already an absolute path with Unicode prefix. Use as is.</para> /// </param> internal void InitializeInternal(bool isFolder, KernelTransaction transaction, string path, bool?isFullPath) { if (isFullPath != null && (bool)!isFullPath) { Path.CheckValidPath(path, true, true); } LongFullName = isFullPath == null ? path : (bool)isFullPath ? Path.GetLongPathInternal(path, false, false, false, false) #if NET35 : Path.GetFullPathInternal(transaction, path, true, false, false, !isFolder, true, false, false);
internal static void SetFsoDateTimeInternal(bool isFolder, KernelTransaction transaction, string path, DateTime?creationTimeUtc, DateTime?lastAccessTimeUtc, DateTime?lastWriteTimeUtc, PathFormat pathFormat) { // Because we already check here, use false for CreateFileInternal() to prevent another check. if (pathFormat == PathFormat.RelativePath) { Path.CheckValidPath(path, false, false); } using (SafeGlobalMemoryBufferHandle creationTime = SafeGlobalMemoryBufferHandle.FromLong(creationTimeUtc.HasValue ? creationTimeUtc.Value.ToFileTimeUtc() : (long?)null)) using (SafeGlobalMemoryBufferHandle lastAccessTime = SafeGlobalMemoryBufferHandle.FromLong(lastAccessTimeUtc.HasValue ? lastAccessTimeUtc.Value.ToFileTimeUtc() : (long?)null)) using (SafeGlobalMemoryBufferHandle lastWriteTime = SafeGlobalMemoryBufferHandle.FromLong(lastWriteTimeUtc.HasValue ? lastWriteTimeUtc.Value.ToFileTimeUtc() : (long?)null)) using (SafeFileHandle safeHandle = CreateFileInternal(transaction, path, isFolder ? ExtendedFileAttributes.BackupSemantics : ExtendedFileAttributes.Normal, null, FileMode.Open, FileSystemRights.WriteAttributes, FileShare.Delete | FileShare.Write, false, pathFormat)) if (!NativeMethods.SetFileTime(safeHandle, creationTime, lastAccessTime, lastWriteTime)) { NativeError.ThrowException(path); } }
internal static T GetAttributesExInternal <T>(KernelTransaction transaction, string path, PathFormat pathFormat) { if (pathFormat == PathFormat.RelativePath) { Path.CheckValidPath(path, true, true); } string pathLp = Path.GetExtendedLengthPathInternal(transaction, path, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.CheckInvalidPathChars); var data = new NativeMethods.Win32FileAttributeData(); int dataInitialised = FillAttributeInfoInternal(transaction, pathLp, ref data, false, true); if (dataInitialised != Win32Errors.ERROR_SUCCESS) { NativeError.ThrowException(dataInitialised, pathLp); } return(typeof(T) == typeof(FileAttributes) ? (T)(object)data.FileAttributes : (T)(object)data); }
/// <summary>[AlphaFS] Initializes the specified file name.</summary> /// <param name="isFolder">Specifies that <paramref name="path"/> is a file or directory.</param> /// <param name="transaction">The transaction.</param> /// <param name="path">The full path and name of the file.</param> /// <param name="pathFormat">Indicates the format of the path parameter(s).</param> internal void InitializeInternal(bool isFolder, KernelTransaction transaction, string path, PathFormat pathFormat) { if (pathFormat == PathFormat.RelativePath) { Path.CheckValidPath(path, true, true); } LongFullName = Path.GetExtendedLengthPathInternal(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | (isFolder ? GetFullPathOptions.RemoveTrailingDirectorySeparator : 0) | GetFullPathOptions.ContinueOnNonExist); // (Not on MSDN): .NET 4+ Trailing spaces are removed from the end of the path parameter before creating the FileSystemInfo instance. FullPath = Path.GetRegularPathInternal(LongFullName, GetFullPathOptions.None); IsDirectory = isFolder; Transaction = transaction; OriginalPath = FullPath.Length == 2 && (FullPath[1] == Path.VolumeSeparatorChar) ? Path.CurrentDirectoryPrefix : path; DisplayPath = OriginalPath.Length != 2 || OriginalPath[1] != Path.VolumeSeparatorChar ? OriginalPath : Path.CurrentDirectoryPrefix; }
internal static void SetAccessControlInternal(string path, SafeHandle handle, ObjectSecurity objectSecurity, AccessControlSections includeSections, PathFormat pathFormat) { if (pathFormat == PathFormat.RelativePath) { Path.CheckValidPath(path, true, true); } if (objectSecurity == null) { throw new ArgumentNullException("objectSecurity"); } byte[] managedDescriptor = objectSecurity.GetSecurityDescriptorBinaryForm(); using (var safeBuffer = new SafeGlobalMemoryBufferHandle(managedDescriptor.Length)) { string pathLp = Path.GetExtendedLengthPathInternal(null, path, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.CheckInvalidPathChars); safeBuffer.CopyFrom(managedDescriptor, 0, managedDescriptor.Length); SecurityDescriptorControl control; uint revision; if (!Security.NativeMethods.GetSecurityDescriptorControl(safeBuffer, out control, out revision)) { NativeError.ThrowException(Marshal.GetLastWin32Error(), pathLp); } PrivilegeEnabler privilegeEnabler = null; try { var securityInfo = SecurityInformation.None; IntPtr pDacl = IntPtr.Zero; if ((includeSections & AccessControlSections.Access) != 0) { bool daclDefaulted, daclPresent; if (!Security.NativeMethods.GetSecurityDescriptorDacl(safeBuffer, out daclPresent, out pDacl, out daclDefaulted)) { NativeError.ThrowException(Marshal.GetLastWin32Error(), pathLp); } if (daclPresent) { securityInfo |= SecurityInformation.Dacl; securityInfo |= (control & SecurityDescriptorControl.DaclProtected) != 0 ? SecurityInformation.ProtectedDacl : SecurityInformation.UnprotectedDacl; } } IntPtr pSacl = IntPtr.Zero; if ((includeSections & AccessControlSections.Audit) != 0) { bool saclDefaulted, saclPresent; if (!Security.NativeMethods.GetSecurityDescriptorSacl(safeBuffer, out saclPresent, out pSacl, out saclDefaulted)) { NativeError.ThrowException(Marshal.GetLastWin32Error(), pathLp); } if (saclPresent) { securityInfo |= SecurityInformation.Sacl; securityInfo |= (control & SecurityDescriptorControl.SaclProtected) != 0 ? SecurityInformation.ProtectedSacl : SecurityInformation.UnprotectedSacl; privilegeEnabler = new PrivilegeEnabler(Privilege.Security); } } IntPtr pOwner = IntPtr.Zero; if ((includeSections & AccessControlSections.Owner) != 0) { bool ownerDefaulted; if (!Security.NativeMethods.GetSecurityDescriptorOwner(safeBuffer, out pOwner, out ownerDefaulted)) { NativeError.ThrowException(Marshal.GetLastWin32Error(), pathLp); } if (pOwner != IntPtr.Zero) { securityInfo |= SecurityInformation.Owner; } } IntPtr pGroup = IntPtr.Zero; if ((includeSections & AccessControlSections.Group) != 0) { bool groupDefaulted; if (!Security.NativeMethods.GetSecurityDescriptorGroup(safeBuffer, out pGroup, out groupDefaulted)) { NativeError.ThrowException(Marshal.GetLastWin32Error(), pathLp); } if (pGroup != IntPtr.Zero) { securityInfo |= SecurityInformation.Group; } } uint lastError; if (!Utils.IsNullOrWhiteSpace(pathLp)) { // SetNamedSecurityInfo() // In the ANSI version of this function, the name is limited to MAX_PATH characters. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. // 2013-01-13: MSDN does not confirm LongPath usage but a Unicode version of this function exists. lastError = Security.NativeMethods.SetNamedSecurityInfo(pathLp, ObjectType.FileObject, securityInfo, pOwner, pGroup, pDacl, pSacl); if (lastError != Win32Errors.ERROR_SUCCESS) { NativeError.ThrowException(lastError, pathLp); } } else if (NativeMethods.IsValidHandle(handle)) { lastError = Security.NativeMethods.SetSecurityInfo(handle, ObjectType.FileObject, securityInfo, pOwner, pGroup, pDacl, pSacl); if (lastError != Win32Errors.ERROR_SUCCESS) { NativeError.ThrowException((int)lastError); } } } finally { if (privilegeEnabler != null) { privilegeEnabler.Dispose(); } } } }
internal static void DeleteFileInternal(KernelTransaction transaction, string path, bool ignoreReadOnly, PathFormat pathFormat) { #region Setup if (pathFormat == PathFormat.RelativePath) { Path.CheckValidPath(path, true, true); } string pathLp = Path.GetExtendedLengthPathInternal(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator); // If the path points to a symbolic link, the symbolic link is deleted, not the target. #endregion // Setup startDeleteFile: if (!(transaction == null || !NativeMethods.IsAtLeastWindowsVista // DeleteFile() / DeleteFileTransacted() // In the ANSI version of this function, the name is limited to MAX_PATH characters. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. // 2013-01-13: MSDN confirms LongPath usage. ? NativeMethods.DeleteFile(pathLp) : NativeMethods.DeleteFileTransacted(pathLp, transaction.SafeHandle))) { int lastError = Marshal.GetLastWin32Error(); switch ((uint)lastError) { case Win32Errors.ERROR_FILE_NOT_FOUND: // MSDN: .NET 3.5+: If the file to be deleted does not exist, no exception is thrown. return; case Win32Errors.ERROR_PATH_NOT_FOUND: // MSDN: .NET 3.5+: DirectoryNotFoundException: The specified path is invalid (for example, it is on an unmapped drive). NativeError.ThrowException(lastError, pathLp); return; case Win32Errors.ERROR_SHARING_VIOLATION: // MSDN: .NET 3.5+: IOException: The specified file is in use or there is an open handle on the file. NativeError.ThrowException(lastError, pathLp); break; case Win32Errors.ERROR_ACCESS_DENIED: var data = new NativeMethods.Win32FileAttributeData(); int dataInitialised = FillAttributeInfoInternal(transaction, pathLp, ref data, false, true); if (data.FileAttributes != (FileAttributes)(-1)) { if ((data.FileAttributes & FileAttributes.Directory) == FileAttributes.Directory) { // MSDN: .NET 3.5+: UnauthorizedAccessException: Path is a directory. throw new UnauthorizedAccessException(string.Format(CultureInfo.CurrentCulture, "({0}) {1}", Win32Errors.ERROR_INVALID_PARAMETER, string.Format(CultureInfo.CurrentCulture, Resources.DirectoryExistsWithSameNameSpecifiedByPath, pathLp))); } if ((data.FileAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { if (ignoreReadOnly) { // Reset file attributes. SetAttributesInternal(false, transaction, pathLp, FileAttributes.Normal, true, PathFormat.LongFullPath); goto startDeleteFile; } // MSDN: .NET 3.5+: UnauthorizedAccessException: Path specified a read-only file. throw new FileReadOnlyException(pathLp); } } if (dataInitialised == Win32Errors.ERROR_SUCCESS) { // MSDN: .NET 3.5+: UnauthorizedAccessException: The caller does not have the required permission. NativeError.ThrowException(lastError, pathLp); } break; } // MSDN: .NET 3.5+: IOException: // The specified file is in use. // There is an open handle on the file, and the operating system is Windows XP or earlier. NativeError.ThrowException(lastError, pathLp); } }
internal static void DeleteDirectoryInternal(FileSystemEntryInfo fileSystemEntryInfo, KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly, bool requireEmpty, bool continueOnNotExist, PathFormat pathFormat) { #region Setup if (pathFormat == PathFormat.RelativePath) Path.CheckValidPath(path, true, true); if (fileSystemEntryInfo == null) { // MSDN: .NET 3.5+: DirectoryNotFoundException: // Path does not exist or could not be found. // Path refers to a file instead of a directory. // The specified path is invalid (for example, it is on an unmapped drive). fileSystemEntryInfo = File.GetFileSystemEntryInfoInternal(transaction, Path.GetExtendedLengthPathInternal(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator), continueOnNotExist, pathFormat); } if (fileSystemEntryInfo == null) return; string pathLp = fileSystemEntryInfo.LongFullPath; #endregion // Setup // Do not follow mount points nor symbolic links, but do delete the reparse point itself. // If directory is reparse point, disable recursion. if (recursive && fileSystemEntryInfo.IsReparsePoint) recursive = false; // Check to see if this is a mount point, and unmount it. if (fileSystemEntryInfo.IsMountPoint) { int lastError = Volume.DeleteVolumeMountPointInternal(pathLp, true); if (lastError != Win32Errors.ERROR_SUCCESS && lastError != Win32Errors.ERROR_PATH_NOT_FOUND) NativeError.ThrowException(lastError, pathLp); // Now it is safe to delete the actual directory. } if (recursive) { // Enumerate all file system objects. foreach (FileSystemEntryInfo fsei in EnumerateFileSystemEntryInfosInternal<FileSystemEntryInfo>(transaction, pathLp, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.LongFullPath)) { if (fsei.IsDirectory) DeleteDirectoryInternal(fsei, transaction, null, true, ignoreReadOnly, requireEmpty, true, PathFormat.LongFullPath); else File.DeleteFileInternal(transaction, fsei.LongFullPath, ignoreReadOnly, PathFormat.LongFullPath); } } #region Remove startRemoveDirectory: if (!(transaction == null || !NativeMethods.IsAtLeastWindowsVista // RemoveDirectory() / RemoveDirectoryTransacted() // In the ANSI version of this function, the name is limited to MAX_PATH characters. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. // 2014-09-09: MSDN confirms LongPath usage. // RemoveDirectory on a symbolic link will remove the link itself. ? NativeMethods.RemoveDirectory(pathLp) : NativeMethods.RemoveDirectoryTransacted(pathLp, transaction.SafeHandle))) { int lastError = Marshal.GetLastWin32Error(); switch ((uint)lastError) { case Win32Errors.ERROR_DIR_NOT_EMPTY: if (requireEmpty) // MSDN: .NET 3.5+: IOException: The directory specified by path is not an empty directory. throw new DirectoryNotEmptyException(pathLp); goto startRemoveDirectory; case Win32Errors.ERROR_DIRECTORY: // MSDN: .NET 3.5+: DirectoryNotFoundException: Path refers to a file instead of a directory. if (File.ExistsInternal(false, transaction, pathLp, PathFormat.LongFullPath)) throw new DirectoryNotFoundException(String.Format(CultureInfo.CurrentCulture, "({0}) {1}", Win32Errors.ERROR_INVALID_PARAMETER, String.Format(CultureInfo.CurrentCulture, Resources.FileExistsWithSameNameSpecifiedByPath, pathLp))); break; case Win32Errors.ERROR_PATH_NOT_FOUND: if (continueOnNotExist) return; break; case Win32Errors.ERROR_SHARING_VIOLATION: // MSDN: .NET 3.5+: IOException: The directory is being used by another process or there is an open handle on the directory. NativeError.ThrowException(lastError, pathLp); break; case Win32Errors.ERROR_ACCESS_DENIED: NativeMethods.Win32FileAttributeData data = new NativeMethods.Win32FileAttributeData(); int dataInitialised = File.FillAttributeInfoInternal(transaction, pathLp, ref data, false, true); if (data.FileAttributes != (FileAttributes)(-1)) { if ((data.FileAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { // MSDN: .NET 3.5+: IOException: The directory specified by path is read-only. if (ignoreReadOnly) { // Reset directory attributes. File.SetAttributesInternal(true, transaction, pathLp, FileAttributes.Normal, true, PathFormat.LongFullPath); goto startRemoveDirectory; } // MSDN: .NET 3.5+: IOException: The directory is read-only. throw new DirectoryReadOnlyException(pathLp); } } if (dataInitialised == Win32Errors.ERROR_SUCCESS) // MSDN: .NET 3.5+: UnauthorizedAccessException: The caller does not have the required permission. NativeError.ThrowException(lastError, pathLp); break; } // MSDN: .NET 3.5+: IOException: // A file with the same name and location specified by path exists. // The directory specified by path is read-only, or recursive is false and path is not an empty directory. // The directory is the application's current working directory. // The directory contains a read-only file. // The directory is being used by another process. NativeError.ThrowException(lastError, pathLp); } #endregion // Remove }
internal static SafeFileHandle CreateFileInternal(KernelTransaction transaction, string path, ExtendedFileAttributes attributes, FileSecurity fileSecurity, FileMode fileMode, FileSystemRights fileSystemRights, FileShare fileShare, bool checkPath, PathFormat pathFormat) { if (checkPath && pathFormat == PathFormat.RelativePath) { Path.CheckValidPath(path, true, true); } // When isFile == null, we're working with a device. // When opening a VOLUME or removable media drive (for example, a floppy disk drive or flash memory thumb drive), // the path string should be the following form: "\\.\X:" // Do not use a trailing backslash (\), which indicates the root. string pathLp = Path.GetExtendedLengthPathInternal(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator); PrivilegeEnabler privilegeEnabler = null; try { if (fileSecurity != null) { fileSystemRights |= (FileSystemRights)0x1000000; } // AccessSystemSecurity = 0x1000000 AccessSystemAcl access type. // MaximumAllowed = 0x2000000 MaximumAllowed access type. if ((fileSystemRights & (FileSystemRights)0x1000000) != 0 || (fileSystemRights & (FileSystemRights)0x2000000) != 0) { privilegeEnabler = new PrivilegeEnabler(Privilege.Security); } using (var securityAttributes = new Security.NativeMethods.SecurityAttributes(fileSecurity)) { SafeFileHandle handle = transaction == null || !NativeMethods.IsAtLeastWindowsVista // CreateFile() / CreateFileTransacted() // In the ANSI version of this function, the name is limited to MAX_PATH characters. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. // 2013-01-13: MSDN confirms LongPath usage. ? NativeMethods.CreateFile(pathLp, fileSystemRights, fileShare, securityAttributes, fileMode, attributes, IntPtr.Zero) : NativeMethods.CreateFileTransacted(pathLp, fileSystemRights, fileShare, securityAttributes, fileMode, attributes, IntPtr.Zero, transaction.SafeHandle, IntPtr.Zero, IntPtr.Zero); int lastError = Marshal.GetLastWin32Error(); if (handle.IsInvalid) { handle.Close(); NativeError.ThrowException(lastError, pathLp); } return(handle); } } finally { if (privilegeEnabler != null) { privilegeEnabler.Dispose(); } } }