Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge shared directory mapping into core
- Loading branch information
Showing
21 changed files
with
200 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma warning disable SA1310 // Field names should not contain underscore | ||
|
||
using System.Runtime.InteropServices; | ||
using static WinSW.Native.Libraries; | ||
|
||
namespace WinSW.Native | ||
{ | ||
internal static class NetworkApis | ||
{ | ||
internal const uint RESOURCETYPE_DISK = 0x00000001; | ||
|
||
[DllImport(Mpr, SetLastError = true, CharSet = CharSet.Unicode)] | ||
internal static extern int WNetAddConnection2W(in NETRESOURCEW netResource, string? password = null, string? userName = null, uint flags = 0); | ||
|
||
[DllImport(Mpr, SetLastError = true, CharSet = CharSet.Unicode)] | ||
internal static extern int WNetCancelConnection2W(string name, uint flags = 0, bool force = false); | ||
|
||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] | ||
internal struct NETRESOURCEW | ||
{ | ||
public uint Scope; | ||
public uint Type; | ||
public uint DisplayType; | ||
public uint Usage; | ||
public string LocalName; | ||
public string RemoteName; | ||
public string Comment; | ||
public string Provider; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Collections.Generic; | ||
using WinSW.Native; | ||
using static WinSW.Native.NetworkApis; | ||
|
||
namespace WinSW | ||
{ | ||
public sealed class SharedDirectoryMapper | ||
{ | ||
private readonly List<SharedDirectoryMapperConfig> entries; | ||
|
||
public SharedDirectoryMapper(List<SharedDirectoryMapperConfig> entries) | ||
{ | ||
this.entries = entries; | ||
} | ||
|
||
public void Map() | ||
{ | ||
foreach (var config in this.entries) | ||
{ | ||
string label = config.Label; | ||
string uncPath = config.UncPath; | ||
|
||
int error = WNetAddConnection2W(new() | ||
{ | ||
Type = RESOURCETYPE_DISK, | ||
LocalName = label, | ||
RemoteName = uncPath, | ||
}); | ||
if (error != 0) | ||
{ | ||
Throw.Command.Win32Exception(error, $"Failed to map {label}."); | ||
} | ||
} | ||
} | ||
|
||
public void Unmap() | ||
{ | ||
foreach (var config in this.entries) | ||
{ | ||
string label = config.Label; | ||
|
||
int error = WNetCancelConnection2W(label); | ||
if (error != 0) | ||
{ | ||
Throw.Command.Win32Exception(error, $"Failed to unmap {label}."); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace WinSW | ||
{ | ||
public sealed class SharedDirectoryMapperConfig | ||
{ | ||
public string Label { get; } | ||
|
||
public string UncPath { get; } | ||
|
||
public SharedDirectoryMapperConfig(string driveLabel, string directoryUncPath) | ||
{ | ||
this.Label = driveLabel; | ||
this.UncPath = directoryUncPath; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.