internal static unsafe SafeFileHandle CreateFile(
            string lpFileName,
            int dwDesiredAccess,
            FileShare dwShareMode,
            ref SECURITY_ATTRIBUTES securityAttrs,
            FileMode dwCreationDisposition,
            int dwFlagsAndAttributes,
            IntPtr hTemplateFile)
        {
            lpFileName = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName);
            fixed(SECURITY_ATTRIBUTES *sa = &securityAttrs)
            {
                IntPtr handle = CreateFilePrivate(lpFileName, dwDesiredAccess, dwShareMode, sa, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);

                try
                {
                    return(new SafeFileHandle(handle, ownsHandle: true));
                }
                catch
                {
                    CloseHandle(handle);
                    throw;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a symbolic link.
        /// </summary>
        /// <param name="symlinkFileName">The symbolic link to be created.</param>
        /// <param name="targetFileName">The name of the target for the symbolic link to be created.
        /// If it has a device name associated with it, the link is treated as an absolute link; otherwise, the link is treated as a relative link.</param>
        /// <param name="isDirectory"><see langword="true" /> if the link target is a directory; <see langword="false" /> otherwise.</param>
        internal static void CreateSymbolicLink(string symlinkFileName, string targetFileName, bool isDirectory)
        {
            string originalPath = symlinkFileName;

            symlinkFileName = PathInternal.EnsureExtendedPrefixIfNeeded(symlinkFileName);
            targetFileName  = PathInternal.EnsureExtendedPrefixIfNeeded(targetFileName);

            int flags = 0;

            Version osVersion = Environment.OSVersion.Version;
            bool    isAtLeastWin10Build14972 = osVersion.Major >= 11 || osVersion.Major == 10 && osVersion.Build >= 14972;

            if (isAtLeastWin10Build14972)
            {
                flags = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
            }

            if (isDirectory)
            {
                flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
            }

            bool success = CreateSymbolicLinkPrivate(symlinkFileName, targetFileName, flags);

            if (!success)
            {
                throw Win32Marshal.GetExceptionForLastWin32Error(originalPath);
            }
        }
Ejemplo n.º 3
0
        internal static bool GetFileAttributesEx(string name, GET_FILEEX_INFO_LEVELS fileInfoLevel, ref WIN32_FILE_ATTRIBUTE_DATA lpFileInformation)
        {
            string?nameWithExtendedPrefix = PathInternal.EnsureExtendedPrefixIfNeeded(name);

            Debug.Assert(nameWithExtendedPrefix != null, "null not expected when non-null is passed"); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
            return(GetFileAttributesExPrivate(nameWithExtendedPrefix, fileInfoLevel, ref lpFileInformation));
        }
        internal static SafeFindHandle FindFirstFile(string fileName, ref WIN32_FIND_DATA data)
        {
            fileName = PathInternal.EnsureExtendedPrefixIfNeeded(fileName);

            // use FindExInfoBasic since we don't care about short name and it has better perf
            return(FindFirstFileExPrivate(fileName, FINDEX_INFO_LEVELS.FindExInfoBasic, ref data, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, 0));
        }
Ejemplo n.º 5
0
        internal static SafeFindHandle FindFirstFile(string fileName, ref WIN32_FIND_DATA data)
        {
            string?fileNameWithPrefix = PathInternal.EnsureExtendedPrefixIfNeeded(fileName);

            Debug.Assert(fileNameWithPrefix != null, "null not expected when non-null passed"); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761

            // use FindExInfoBasic since we don't care about short name and it has better perf
            return(FindFirstFileExPrivate(fileNameWithPrefix, FINDEX_INFO_LEVELS.FindExInfoBasic, ref data, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, 0));
        }
Ejemplo n.º 6
0
 internal static SafeFileHandle CreateFile2(
     string lpFileName,
     int dwDesiredAccess,
     FileShare dwShareMode,
     FileMode dwCreationDisposition,
     ref Kernel32.CREATEFILE2_EXTENDED_PARAMETERS pCreateExParams)
 {
     lpFileName = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName) !; // TODO-NULLABLE: Remove ! when [NotNullIfNotNull] respected
     return(CreateFile2Private(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, ref pCreateExParams));
 }
Ejemplo n.º 7
0
 internal static SafeFileHandle CreateFile2(
     string lpFileName,
     int dwDesiredAccess,
     FileShare dwShareMode,
     FileMode dwCreationDisposition,
     ref Kernel32.CREATEFILE2_EXTENDED_PARAMETERS pCreateExParams)
 {
     lpFileName = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName);
     return(CreateFile2Private(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, ref pCreateExParams));
 }
 internal static unsafe IntPtr CreateFile_IntPtr(
     string lpFileName,
     int dwDesiredAccess,
     FileShare dwShareMode,
     FileMode dwCreationDisposition,
     int dwFlagsAndAttributes)
 {
     lpFileName = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName);
     return(CreateFilePrivate_IntPtr(lpFileName, dwDesiredAccess, dwShareMode, null, dwCreationDisposition, dwFlagsAndAttributes, IntPtr.Zero));
 }
Ejemplo n.º 9
0
 internal static bool CopyFileEx(
     string src,
     string dst,
     IntPtr progressRoutine,
     IntPtr progressData,
     ref int cancel,
     int flags)
 {
     src = PathInternal.EnsureExtendedPrefixIfNeeded(src);
     dst = PathInternal.EnsureExtendedPrefixIfNeeded(dst);
     return(CopyFileExPrivate(src, dst, progressRoutine, progressData, ref cancel, flags));
 }
Ejemplo n.º 10
0
        internal static SafeFileHandle CreateFile2(
            string lpFileName,
            int dwDesiredAccess,
            FileShare dwShareMode,
            FileMode dwCreationDisposition,
            ref Kernel32.CREATEFILE2_EXTENDED_PARAMETERS pCreateExParams)
        {
            string?lpFileNameWithPrefix = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName);

            Debug.Assert(lpFileNameWithPrefix != null, "null not expected when non-null passed"); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
            return(CreateFile2Private(lpFileNameWithPrefix, dwDesiredAccess, dwShareMode, dwCreationDisposition, ref pCreateExParams));
        }
Ejemplo n.º 11
0
        internal static bool ReplaceFile(
            string replacedFileName, string replacementFileName, string?backupFileName,
            int dwReplaceFlags, IntPtr lpExclude, IntPtr lpReserved)
        {
            replacedFileName    = PathInternal.EnsureExtendedPrefixIfNeeded(replacedFileName);
            replacementFileName = PathInternal.EnsureExtendedPrefixIfNeeded(replacementFileName);
            backupFileName      = PathInternal.EnsureExtendedPrefixIfNeeded(backupFileName);

            return(ReplaceFilePrivate(
                       replacedFileName, replacementFileName, backupFileName,
                       dwReplaceFlags, lpExclude, lpReserved));
        }
Ejemplo n.º 12
0
 internal static SafeFileHandle CreateFile(
     string lpFileName,
     int dwDesiredAccess,
     System.IO.FileShare dwShareMode,
     ref SECURITY_ATTRIBUTES securityAttrs,
     System.IO.FileMode dwCreationDisposition,
     int dwFlagsAndAttributes,
     IntPtr hTemplateFile)
 {
     lpFileName = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName) !; // TODO-NULLABLE: Remove ! when nullable attributes are respected
     return(CreateFilePrivate(lpFileName, dwDesiredAccess, dwShareMode, ref securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile));
 }
Ejemplo n.º 13
0
 internal static unsafe SafeFileHandle CreateFile(
     string lpFileName,
     int dwDesiredAccess,
     FileShare dwShareMode,
     SECURITY_ATTRIBUTES *lpSecurityAttributes,
     FileMode dwCreationDisposition,
     int dwFlagsAndAttributes,
     IntPtr hTemplateFile)
 {
     lpFileName = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName);
     return(CreateFilePrivate(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile));
 }
Ejemplo n.º 14
0
        internal static SafeFileHandle CreateFile(
            string lpFileName,
            int dwDesiredAccess,
            System.IO.FileShare dwShareMode,
            ref SECURITY_ATTRIBUTES securityAttrs,
            System.IO.FileMode dwCreationDisposition,
            int dwFlagsAndAttributes,
            IntPtr hTemplateFile)
        {
            string?lpFileNameWithPrefix = PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName);

            Debug.Assert(lpFileNameWithPrefix != null, "null not expected when non-null passed"); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
            return(CreateFilePrivate(lpFileNameWithPrefix, dwDesiredAccess, dwShareMode, ref securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile));
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Moves a file or directory, optionally overwriting existing destination file. NOTE: overwrite must be false for directories.
        /// </summary>
        /// <param name="src">Source file or directory</param>
        /// <param name="dst">Destination file or directory</param>
        /// <param name="overwrite">True to overwrite existing destination file. NOTE: must pass false for directories as overwrite of directories is not supported.</param>
        /// <returns></returns>
        internal static bool MoveFile(string src, string dst, bool overwrite)
        {
            src = PathInternal.EnsureExtendedPrefixIfNeeded(src);
            dst = PathInternal.EnsureExtendedPrefixIfNeeded(dst);

            uint flags = MOVEFILE_COPY_ALLOWED;

            if (overwrite)
            {
                flags |= MOVEFILE_REPLACE_EXISTING;
            }

            return(MoveFileExPrivate(src, dst, flags));
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Creates a symbolic link.
        /// </summary>
        /// <param name="symlinkFileName">The symbolic link to be created.</param>
        /// <param name="targetFileName">The name of the target for the symbolic link to be created.
        /// If it has a device name associated with it, the link is treated as an absolute link; otherwise, the link is treated as a relative link.</param>
        /// <param name="isDirectory"><see langword="true" /> if the link target is a directory; <see langword="false" /> otherwise.</param>
        internal static void CreateSymbolicLink(string symlinkFileName, string targetFileName, bool isDirectory)
        {
            string originalPath = symlinkFileName;

            symlinkFileName = PathInternal.EnsureExtendedPrefixIfNeeded(symlinkFileName);
            targetFileName  = PathInternal.EnsureExtendedPrefixIfNeeded(targetFileName);

            int flags = 0;

            bool isAtLeastWin10Build14972 =
                Environment.OSVersion.Version.Major == 10 && Environment.OSVersion.Version.Build >= 14972 ||
                Environment.OSVersion.Version.Major >= 11;

            if (isAtLeastWin10Build14972)
            {
                flags = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
            }

            if (isDirectory)
            {
                flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
            }

            bool success = CreateSymbolicLinkPrivate(symlinkFileName, targetFileName, flags);

            int error;

            if (!success)
            {
                throw Win32Marshal.GetExceptionForLastWin32Error(originalPath);
            }
            // In older versions we need to check GetLastWin32Error regardless of the return value of CreateSymbolicLink,
            // e.g: if the user doesn't have enough privileges to create a symlink the method returns success which we can consider as a silent failure.
            else if (!isAtLeastWin10Build14972 && (error = Marshal.GetLastWin32Error()) != 0)
            {
                throw Win32Marshal.GetExceptionForWin32Error(error, originalPath);
            }
        }
Ejemplo n.º 17
0
        internal static bool DeleteFile(string path)
        {
            path = PathInternal.EnsureExtendedPrefixIfNeeded(path);

            return(DeleteFilePrivate(path));
        }
 internal static bool SetFileAttributes(string name, int attr)
 {
     name = PathInternal.EnsureExtendedPrefixIfNeeded(name);
     return(SetFileAttributesPrivate(name, attr));
 }
Ejemplo n.º 19
0
        internal static bool GetFileAttributesEx(string?name, GET_FILEEX_INFO_LEVELS fileInfoLevel, ref WIN32_FILE_ATTRIBUTE_DATA lpFileInformation)
        {
            name = PathInternal.EnsureExtendedPrefixIfNeeded(name);

            return(GetFileAttributesExPrivate(name, fileInfoLevel, ref lpFileInformation));
        }
Ejemplo n.º 20
0
        internal static bool RemoveDirectory(string path)
        {
            path = PathInternal.EnsureExtendedPrefixIfNeeded(path);

            return(RemoveDirectoryPrivate(path));
        }
Ejemplo n.º 21
0
 internal FileSystemSecurity(bool isContainer, string name, AccessControlSections includeSections, bool isDirectory)
     : base(isContainer, s_ResourceType, PathInternal.EnsureExtendedPrefixIfNeeded(Path.GetFullPath(name)), includeSections, _HandleErrorCode, isDirectory)
 {
 }
Ejemplo n.º 22
0
 internal static bool GetFileAttributesEx(string name, GET_FILEEX_INFO_LEVELS fileInfoLevel, ref WIN32_FILE_ATTRIBUTE_DATA lpFileInformation)
 {
     name = PathInternal.EnsureExtendedPrefixIfNeeded(name) !; // TODO-NULLABLE: Remove ! when [NotNullIfNotNull] respected
     return(GetFileAttributesExPrivate(name, fileInfoLevel, ref lpFileInformation));
 }
Ejemplo n.º 23
0
 internal static bool DeleteVolumeMountPoint(string mountPoint)
 {
     mountPoint = PathInternal.EnsureExtendedPrefixIfNeeded(mountPoint);
     return(DeleteVolumeMountPointPrivate(mountPoint));
 }
Ejemplo n.º 24
0
 internal static int GetLongPathName(string path, [Out] StringBuilder longPathBuffer, int bufferLength)
 {
     path = PathInternal.EnsureExtendedPrefixIfNeeded(path);
     return(GetLongPathNamePrivate(path, longPathBuffer, bufferLength));
 }
Ejemplo n.º 25
0
 internal static bool MoveFile(string src, string dst)
 {
     src = PathInternal.EnsureExtendedPrefixIfNeeded(src);
     dst = PathInternal.EnsureExtendedPrefixIfNeeded(dst);
     return(MoveFileExPrivate(src, dst, 2 /* MOVEFILE_COPY_ALLOWED */));
 }