From 075f5df111805e88c94bb652c2361037a1364050 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Mon, 4 May 2015 04:04:45 +0100 Subject: [PATCH] * Claunia.IO/Claunia.IO.csproj: * Claunia.IO/Interop/Windows/Interop.Windows.Backup.cs: Added backup functions that allow to access ADSs and EAs on any Windows NT version. * Claunia.IO/Interop/Windows/Interop.Windows.EAs.cs: * Claunia.IO/Interop/Windows/Interop.Windows.Files.cs: Code enhancement. --- Claunia.IO/ChangeLog | 11 ++ Claunia.IO/Claunia.IO.csproj | 1 + .../Interop/Windows/Interop.Windows.Backup.cs | 166 ++++++++++++++++++ .../Interop/Windows/Interop.Windows.EAs.cs | 3 +- .../Interop/Windows/Interop.Windows.Files.cs | 2 +- 5 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 Claunia.IO/Interop/Windows/Interop.Windows.Backup.cs diff --git a/Claunia.IO/ChangeLog b/Claunia.IO/ChangeLog index 8ce69f8..603eaf0 100644 --- a/Claunia.IO/ChangeLog +++ b/Claunia.IO/ChangeLog @@ -1,3 +1,14 @@ +2015-05-04 Natalia Portillo + + * Claunia.IO.csproj: + * Interop/Windows/Interop.Windows.Backup.cs: + Added backup functions that allow to access ADSs and EAs on + any Windows NT version. + + * Interop/Windows/Interop.Windows.EAs.cs: + * Interop/Windows/Interop.Windows.Files.cs: + Code enhancement. + 2015-05-04 Natalia Portillo * Claunia.IO.csproj: diff --git a/Claunia.IO/Claunia.IO.csproj b/Claunia.IO/Claunia.IO.csproj index 34a4226..30e5959 100644 --- a/Claunia.IO/Claunia.IO.csproj +++ b/Claunia.IO/Claunia.IO.csproj @@ -73,6 +73,7 @@ + diff --git a/Claunia.IO/Interop/Windows/Interop.Windows.Backup.cs b/Claunia.IO/Interop/Windows/Interop.Windows.Backup.cs new file mode 100644 index 0000000..5500912 --- /dev/null +++ b/Claunia.IO/Interop/Windows/Interop.Windows.Backup.cs @@ -0,0 +1,166 @@ +// +// Interop.Windows.Streams.cs +// +// Author: +// Natalia Portillo +// +// Copyright (c) 2015 © Claunia.com +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software", to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System.Runtime.InteropServices; +using System; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Windows + { + /// + /// Type of data on alternate stream. + /// + public enum StreamType + { + /// + /// Standard data. This corresponds to the NTFS $DATA stream type on the default (unnamed) data stream. + /// + BACKUP_DATA = 0x00000001, + /// + /// Extended attribute data. This corresponds to the NTFS $EA stream type. + /// + BACKUP_EA_DATA = 0x00000002, + /// + /// Security descriptor data. + /// + BACKUP_SECURITY_DATA = 0x00000003, + /// + /// Alternative data streams. This corresponds to the NTFS $DATA stream type on a named data stream. + /// + BACKUP_ALTERNATE_DATA = 0x00000004, + /// + /// Hard link information. This corresponds to the NTFS $FILE_NAME stream type. + /// + BACKUP_LINK = 0x00000005, + /// + /// Property data. + /// + BACKUP_PROPERTY_DATA = 0x00000006, + /// + /// Objects identifiers. This corresponds to the NTFS $OBJECT_ID stream type. + /// + BACKUP_OBJECT_ID = 0x00000007, + /// + /// Reparse points. This corresponds to the NTFS $REPARSE_POINT stream type. + /// + BACKUP_REPARSE_DATA = 0x00000008, + /// + /// Sparse file. This corresponds to the NTFS $DATA stream type for a sparse file. + /// + BACKUP_SPARSE_BLOCK = 0x00000009, + /// + /// Transactional NTFS (TxF) data stream. This corresponds to the NTFS $TXF_DATA stream type. + /// + BACKUP_TXFS_DATA = 0x0000000A + } + + [Flags] + public enum StreamAttributes + { + /// + /// Normal attribute + /// + STREAM_NORMAL_ATTRIBUTE = 0x00000000, + /// + /// Attribute set if the stream contains data that is modified when read. Allows the backup application to know that verification of data will fail. + /// + STREAM_MODIFIED_WHEN_READ = 0x00000001, + /// + /// Stream contains security data (general attributes). Allows the stream to be ignored on cross-operations restore. + /// + STREAM_CONTAINS_SECURITY = 0x00000002, + STREAM_CONTAINS_PROPERTIES = 0x00000004, + STREAM_SPARSE_ATTRIBUTE = 0x00000008 + } + + /// + /// Contains stream data. + /// + [StructLayout(LayoutKind.Sequential, Pack = 4)] + public struct WIN32_STREAM_ID + { + /// + /// Type of data. + /// + public StreamType dwStreamId; + /// + /// Attributes of data to facilitate cross-operating system transfer. + /// + public int dwStreamAttributes; + /// + /// Size of data, in bytes. + /// + public long Size; + /// + /// Length of the name of the alternative data stream, in bytes. + /// + public int dwStreamNameSize; + /// + /// Unicode string that specifies the name of the alternative data stream. + /// + public string cStreamName; + } + + /// + /// The BackupRead function can be used to back up a file or directory, including the security information. The function reads data associated with a specified file or directory into a buffer. + /// + /// true on success, false otherwise. + /// Handle to the file or directory to be backed up. + /// Pointer to a buffer that receives the data. + /// Length of the buffer, in bytes. The buffer size must be greater than the size of a structure. + /// Pointer to a variable that receives the number of bytes read. + /// Indicates whether you have finished using BackupRead on the handle. While you are backing up the file, specify this parameter as false. Once you are done using BackupRead, you must call BackupRead one more time specifying true for this parameter and passing the appropriate . must be passed when is true; all other parameters are ignored. + /// Indicates whether the function will restore the access-control list (ACL) data for the file or directory. + /// If bProcessSecurity is true, the ACL data will be backed up. + /// Pointer to a variable that receives a pointer to an internal data structure used by BackupRead to maintain context information during a backup operation. + /// You must set the variable pointed to by to null before the first call to BackupRead for the specified file or directory. The function allocates memory for the data structure, and then sets the variable to point to that structure. You must not change or the variable that it points to between calls to BackupRead. + /// To release the memory used by the data structure, call BackupRead with the parameter set to true when the backup operation is complete. + [DllImport(Libraries.Kernel32)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool BackupRead(SafeFileHandle hFile, IntPtr lpBuffer, + uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, + [MarshalAs(UnmanagedType.Bool)] bool bAbort, + [MarshalAs(UnmanagedType.Bool)] bool bProcessSecurity, + ref IntPtr lpContext); + + /// + /// The BackupSeek function seeks forward in a data stream initially accessed by using the or function. + /// + /// true, if seek was backuped, false otherwise. + /// Handle to the file or directory. This handle is created by using the function. + /// Low-order part of the number of bytes to seek. + /// High-order part of the number of bytes to seek. + /// Pointer to a variable that receives the low-order bits of the number of bytes the function actually seeks. + /// Pointer to a variable that receives the high-order bits of the number of bytes the function actually seeks. + /// Pointer to an internal data structure used by the function. This structure must be the same structure that was initialized by the or function. An application must not touch the contents of this structure. + [DllImport(Libraries.Kernel32)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool BackupSeek(SafeFileHandle hFile, + uint dwLowBytesToSeek, uint dwHighBytesToSeek, out uint lpdwLowByteSeeked, + out uint lpdwHighByteSeeked, ref IntPtr lpContext); + } +} \ No newline at end of file diff --git a/Claunia.IO/Interop/Windows/Interop.Windows.EAs.cs b/Claunia.IO/Interop/Windows/Interop.Windows.EAs.cs index 0b45033..aa3e48e 100644 --- a/Claunia.IO/Interop/Windows/Interop.Windows.EAs.cs +++ b/Claunia.IO/Interop/Windows/Interop.Windows.EAs.cs @@ -113,8 +113,9 @@ internal static partial class Interop [DllImport(Libraries.NTDLL)] public static extern NTSTATUS NtQueryEaFile(IntPtr FileHandle, ref IO_STATUS_BLOCK IoStatusBlock, IntPtr Buffer, ulong Length, - bool ReturnSingleEntry, IntPtr EaList, UInt32 EaListLength, ref UInt32 EaIndex, + [MarshalAs(UnmanagedType.Bool)] bool ReturnSingleEntry, IntPtr EaList, UInt32 EaListLength, ref UInt32 EaIndex, UInt32 RestartScan); + /// /// Sets extended-attribute (EA) values for a file. /// diff --git a/Claunia.IO/Interop/Windows/Interop.Windows.Files.cs b/Claunia.IO/Interop/Windows/Interop.Windows.Files.cs index a3550fc..764db2c 100644 --- a/Claunia.IO/Interop/Windows/Interop.Windows.Files.cs +++ b/Claunia.IO/Interop/Windows/Interop.Windows.Files.cs @@ -184,7 +184,7 @@ internal static partial class Interop public static extern NTSTATUS NtCreateFile(out SafeFileHandle FileHandle, ACCESS_MASK DesiredAccess, ref OBJECT_ATTRIBUTES ObjectAttributes, ref IO_STATUS_BLOCK IoStatusBlock, ref long AllocationSize, uint FileAttributes, - System.IO.FileShare ShareAccess, CreateDispositionFlags CreateDisposition, CreateOptionsFlags CreateOptions, + FileShare ShareAccess, CreateDispositionFlags CreateDisposition, CreateOptionsFlags CreateOptions, IntPtr EaBuffer, uint EaLength); ///