Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b5982a3

Browse files
committed
Move helper methods into common.
This facilitates further IO work around long paths.
1 parent 2bfa5bb commit b5982a3

13 files changed

+231
-186
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal partial class Interop
8+
{
9+
internal partial class mincore
10+
{
11+
internal static int CopyFile(string src, string dst, bool failIfExists)
12+
{
13+
int copyFlags = failIfExists ? Interop.mincore.FileOperations.COPY_FILE_FAIL_IF_EXISTS : 0;
14+
int cancel = 0;
15+
if (!Interop.mincore.CopyFileEx(src, dst, IntPtr.Zero, IntPtr.Zero, ref cancel, copyFlags))
16+
{
17+
return Marshal.GetLastWin32Error();
18+
}
19+
20+
return Interop.mincore.Errors.ERROR_SUCCESS;
21+
}
22+
}
23+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.IO;
6+
using System.Runtime.InteropServices;
7+
using Microsoft.Win32.SafeHandles;
8+
9+
internal partial class Interop
10+
{
11+
internal partial class mincore
12+
{
13+
internal static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); // WinBase.h
14+
15+
/// <summary>
16+
/// Does not allow access to non-file devices. This disallows DOS devices like "con:", "com1:",
17+
/// "lpt1:", etc. Use this to avoid security problems, like allowing a web client asking a server
18+
/// for "http://server/com1.aspx" and then causing a worker process to hang.
19+
/// </summary>
20+
[System.Security.SecurityCritical] // auto-generated
21+
internal static SafeFileHandle SafeCreateFile(
22+
String lpFileName,
23+
int dwDesiredAccess,
24+
System.IO.FileShare dwShareMode,
25+
ref Interop.mincore.SECURITY_ATTRIBUTES securityAttrs,
26+
FileMode dwCreationDisposition,
27+
int dwFlagsAndAttributes,
28+
IntPtr hTemplateFile)
29+
{
30+
SafeFileHandle handle = UnsafeCreateFile(lpFileName, dwDesiredAccess, dwShareMode, ref securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
31+
32+
if (!handle.IsInvalid)
33+
{
34+
int fileType = Interop.mincore.GetFileType(handle);
35+
if (fileType != Interop.mincore.FileTypes.FILE_TYPE_DISK)
36+
{
37+
handle.Dispose();
38+
throw new NotSupportedException(SR.NotSupported_FileStreamOnNonFiles);
39+
}
40+
}
41+
42+
return handle;
43+
}
44+
}
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.IO;
6+
using Microsoft.Win32.SafeHandles;
7+
8+
internal partial class Interop
9+
{
10+
internal partial class mincore
11+
{
12+
[System.Security.SecurityCritical] // auto-generated
13+
internal static SafeFileHandle UnsafeCreateFile(
14+
string lpFileName,
15+
int dwDesiredAccess,
16+
FileShare dwShareMode,
17+
ref Interop.mincore.SECURITY_ATTRIBUTES securityAttrs,
18+
FileMode dwCreationDisposition,
19+
int dwFlagsAndAttributes,
20+
IntPtr hTemplateFile)
21+
{
22+
return CreateFile(lpFileName, dwDesiredAccess, dwShareMode, ref securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
23+
}
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.IO;
6+
using System.Runtime.InteropServices;
7+
8+
internal partial class Interop
9+
{
10+
internal partial class mincore
11+
{
12+
internal static int CopyFile(String src, String dst, bool failIfExists)
13+
{
14+
uint copyFlags = failIfExists ? (uint)Interop.mincore.FileOperations.COPY_FILE_FAIL_IF_EXISTS : 0;
15+
Interop.mincore.COPYFILE2_EXTENDED_PARAMETERS parameters = new Interop.mincore.COPYFILE2_EXTENDED_PARAMETERS()
16+
{
17+
dwSize = (uint)Marshal.SizeOf<Interop.mincore.COPYFILE2_EXTENDED_PARAMETERS>(),
18+
dwCopyFlags = copyFlags
19+
};
20+
21+
int hr = Interop.mincore.CopyFile2(src, dst, ref parameters);
22+
23+
return Win32Marshal.TryMakeWin32ErrorCodeFromHR(hr);
24+
}
25+
}
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
6+
internal partial class Interop
7+
{
8+
internal partial class mincore
9+
{
10+
internal static int DeleteVolumeMountPoint(string mountPoint)
11+
{
12+
// DeleteVolumeMountPointW is not available to store apps.
13+
// The expectation is that no store app would even have permission
14+
// to call this from the app container
15+
throw new UnauthorizedAccessException();
16+
}
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
6+
internal partial class Interop
7+
{
8+
internal partial class mincore
9+
{
10+
internal static uint SetErrorMode(uint uMode)
11+
{
12+
// Prompting behavior no longer occurs in all platforms supported
13+
return 0;
14+
}
15+
}
16+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.IO;
6+
using System.Runtime.InteropServices;
7+
using Microsoft.Win32.SafeHandles;
8+
9+
internal partial class Interop
10+
{
11+
internal partial class mincore
12+
{
13+
internal static unsafe SafeFileHandle UnsafeCreateFile(
14+
string lpFileName,
15+
int dwDesiredAccess,
16+
System.IO.FileShare dwShareMode,
17+
ref Interop.mincore.SECURITY_ATTRIBUTES securityAttrs,
18+
System.IO.FileMode dwCreationDisposition,
19+
int dwFlagsAndAttributes,
20+
IntPtr hTemplateFile)
21+
{
22+
Interop.mincore.CREATEFILE2_EXTENDED_PARAMETERS parameters;
23+
parameters.dwSize = (uint)Marshal.SizeOf<Interop.mincore.CREATEFILE2_EXTENDED_PARAMETERS>();
24+
25+
parameters.dwFileAttributes = (uint)dwFlagsAndAttributes & 0x0000FFFF;
26+
parameters.dwSecurityQosFlags = (uint)dwFlagsAndAttributes & 0x000F0000;
27+
parameters.dwFileFlags = (uint)dwFlagsAndAttributes & 0xFFF00000;
28+
29+
parameters.hTemplateFile = hTemplateFile;
30+
fixed (Interop.mincore.SECURITY_ATTRIBUTES* lpSecurityAttributes = &securityAttrs)
31+
{
32+
parameters.lpSecurityAttributes = (IntPtr)lpSecurityAttributes;
33+
return Interop.mincore.CreateFile2(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, ref parameters);
34+
}
35+
}
36+
}
37+
}

src/System.IO.FileSystem/src/Common/Helpers.Win7.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/System.IO.FileSystem/src/Common/Helpers.Win8.cs

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/System.IO.FileSystem/src/Common/Helpers.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)