/* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
*
* 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.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
namespace Alphaleonis.Win32.Filesystem
{
internal static partial class NativeMethods
{
///
/// Creates a new directory.
/// If the underlying file system supports security on files and directories,
/// the function applies a specified security descriptor to the new directory.
///
///
/// Some file systems, such as the NTFS file system, support compression or encryption for individual files and
/// directories.
/// On volumes formatted for such a file system, a new directory inherits the compression and encryption attributes of its parent
/// directory.
/// An application can obtain a handle to a directory by calling with the FILE_FLAG_BACKUP_SEMANTICS
/// flag set.
/// Minimum supported client: Windows XP [desktop apps | Windows Store apps]
/// Minimum supported server: Windows Server 2003 [desktop apps | Windows Store apps]
///
/// Full pathname of the file.
/// The security attributes.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateDirectoryW"), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CreateDirectory([MarshalAs(UnmanagedType.LPWStr)] string lpPathName, [MarshalAs(UnmanagedType.LPStruct)] Security.NativeMethods.SecurityAttributes lpSecurityAttributes);
///
/// Creates a new directory with the attributes of a specified template directory.
/// If the underlying file system supports security on files and directories,
/// the function applies a specified security descriptor to the new directory.
/// The new directory retains the other attributes of the specified template directory.
///
///
/// The CreateDirectoryEx function allows you to create directories that inherit stream information from other directories.
/// This function is useful, for example, when you are using Macintosh directories, which have a resource stream
/// that is needed to properly identify directory contents as an attribute.
/// Some file systems, such as the NTFS file system, support compression or encryption for individual files and
/// directories.
/// On volumes formatted for such a file system, a new directory inherits the compression and encryption attributes of its parent
/// directory.
/// You can obtain a handle to a directory by calling the function with the FILE_FLAG_BACKUP_SEMANTICS
/// flag set.
/// Minimum supported client: Windows XP [desktop apps only]
/// Minimum supported server: Windows Server 2003 [desktop apps only]
///
/// Pathname of the template directory.
/// Full pathname of the file.
/// The security attributes.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
///
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateDirectoryExW"), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CreateDirectoryEx([MarshalAs(UnmanagedType.LPWStr)] string lpTemplateDirectory, [MarshalAs(UnmanagedType.LPWStr)] string lpPathName, [MarshalAs(UnmanagedType.LPStruct)] Security.NativeMethods.SecurityAttributes lpSecurityAttributes);
///
/// Creates a new directory as a transacted operation, with the attributes of a specified template directory.
/// If the underlying file system supports security on files and directories,
/// the function applies a specified security descriptor to the new directory.
/// The new directory retains the other attributes of the specified template directory.
///
///
/// The CreateDirectoryTransacted function allows you to create directories that inherit stream information from other
/// directories.
/// This function is useful, for example, when you are using Macintosh directories, which have a resource stream
/// that is needed to properly identify directory contents as an attribute.
/// Some file systems, such as the NTFS file system, support compression or encryption for individual files and
/// directories.
/// On volumes formatted for such a file system, a new directory inherits the compression and encryption attributes of its parent
/// directory.
/// You can obtain a handle to a directory by calling the function with the
/// FILE_FLAG_BACKUP_SEMANTICS flag set.
/// Minimum supported client: Windows XP [desktop apps only]
/// Minimum supported server: Windows Server 2003 [desktop apps only]
///
/// Pathname of the template directory.
/// Pathname of the new directory.
/// The security attributes.
/// The transaction.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
/// This function fails with ERROR_EFS_NOT_ALLOWED_IN_TRANSACTION if you try to create a
/// child directory with a parent directory that has encryption disabled.
///
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateDirectoryTransactedW"), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CreateDirectoryTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpTemplateDirectory, [MarshalAs(UnmanagedType.LPWStr)] string lpNewDirectory, [MarshalAs(UnmanagedType.LPStruct)] Security.NativeMethods.SecurityAttributes lpSecurityAttributes, SafeHandle hTransaction);
///
/// Retrieves the current directory for the current process.
///
///
/// The RemoveDirectory function marks a directory for deletion on close.
/// Therefore, the directory is not removed until the last handle to the directory is closed.
/// RemoveDirectory removes a directory junction, even if the contents of the target are not empty;
/// the function removes directory junctions regardless of the state of the target object.
/// Minimum supported client: Windows XP [desktop apps | Windows Store apps]
/// Minimum supported server: Windows Server 2003 [desktop apps | Windows Store apps]
///
/// The length of the buffer for the current directory string, in TCHARs. The buffer length must include room for a terminating null character.
///
/// A pointer to the buffer that receives the current directory string. This null-terminated string specifies the absolute path to the current directory.
/// To determine the required buffer size, set this parameter to NULL and the nBufferLength parameter to 0.
///
///
/// If the function succeeds, the return value specifies the number of characters that are written to the buffer, not including the terminating null character.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
[SuppressMessage("Microsoft.Usage", "CA2205:UseManagedEquivalentsOfWin32Api")]
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetCurrentDirectoryW"), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.U4)]
internal static extern uint GetCurrentDirectory([MarshalAs(UnmanagedType.U4)] uint nBufferLength, StringBuilder lpBuffer);
///
/// Deletes an existing empty directory.
///
///
/// The RemoveDirectory function marks a directory for deletion on close.
/// Therefore, the directory is not removed until the last handle to the directory is closed.
/// RemoveDirectory removes a directory junction, even if the contents of the target are not empty;
/// the function removes directory junctions regardless of the state of the target object.
/// Minimum supported client: Windows XP [desktop apps | Windows Store apps]
/// Minimum supported server: Windows Server 2003 [desktop apps | Windows Store apps]
///
/// Full pathname of the file.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "RemoveDirectoryW"), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool RemoveDirectory([MarshalAs(UnmanagedType.LPWStr)] string lpPathName);
///
/// Deletes an existing empty directory as a transacted operation.
///
///
/// The RemoveDirectoryTransacted function marks a directory for deletion on close.
/// Therefore, the directory is not removed until the last handle to the directory is closed.
/// RemoveDirectory removes a directory junction, even if the contents of the target are not empty;
/// the function removes directory junctions regardless of the state of the target object.
/// Minimum supported client: Windows Vista [desktop apps only]
/// Minimum supported server: Windows Server 2008 [desktop apps only]
///
/// Full pathname of the file.
/// The transaction.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "RemoveDirectoryTransactedW"), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool RemoveDirectoryTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpPathName, SafeHandle hTransaction);
///
/// Changes the current directory for the current process.
///
///
/// The path to the new current directory. This parameter may specify a relative path or a full path. In either case, the full path of the specified directory is calculated and stored as the current directory.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
[SuppressMessage("Microsoft.Usage", "CA2205:UseManagedEquivalentsOfWin32Api")]
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SetCurrentDirectoryW"), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetCurrentDirectory([MarshalAs(UnmanagedType.LPWStr)] string lpPathName);
}
}