Skip to content

Commit

Permalink
SymbolicLink も集約。
Browse files Browse the repository at this point in the history
  • Loading branch information
guitarrapc committed Jan 21, 2016
1 parent 5959bef commit 7b074c2
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 460 deletions.
19 changes: 0 additions & 19 deletions valentia/CS/CreateSymLink.cs

This file was deleted.

23 changes: 0 additions & 23 deletions valentia/CS/GetSymLink.cs

This file was deleted.

59 changes: 59 additions & 0 deletions valentia/CS/SymbolicLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Text;
using Microsoft.Win32.SafeHandles;

namespace Valentia.CS
{
public class SymbolicLink
{
private const int FileShareRead = 1;
private const int FileShareWrite = 2;
private const int CreationDispositionOpenExisting = 3;
private const int FileFlagBackupSemantics = 0x02000000;

internal static class Win32
{
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, SymLinkFlag dwFlags);

[DllImport("kernel32.dll", EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetFinalPathNameByHandle(IntPtr handle, [In, Out] StringBuilder path, int bufLen, int flags);

[DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, IntPtr SecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);

internal enum SymLinkFlag
{
File = 0,
Directory = 1
}
}

public static void CreateSymLink(string name, string target, bool isDirectory = false)
{
if (!Win32.CreateSymbolicLink(name, target, isDirectory ? Win32.SymLinkFlag.Directory : Win32.SymLinkFlag.File))
{
throw new Win32Exception();
}
}

public static string GetSymbolicLinkTarget(System.IO.DirectoryInfo symlink)
{
var directoryHandle = Win32.CreateFile(symlink.FullName, 0, 2, IntPtr.Zero, CreationDispositionOpenExisting, FileFlagBackupSemantics, IntPtr.Zero);
if (directoryHandle.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error());

var path = new StringBuilder(512);
var size = Win32.GetFinalPathNameByHandle(directoryHandle.DangerousGetHandle(), path, path.Capacity, 0);
if (size < 0) throw new Win32Exception(Marshal.GetLastWin32Error()); // The remarks section of GetFinalPathNameByHandle mentions the return being prefixed with "\\?\" // More information about "\\?\" here -> http://msdn.microsoft.com/en-us/library/aa365247(v=VS.85).aspx

if (path[0] == '\\' && path[1] == '\\' && path[2] == '?' && path[3] == '\\')
{
return path.ToString().Substring(4);
}
return path.ToString();
}
}
}

This file was deleted.

95 changes: 36 additions & 59 deletions valentia/Functions/Helper/SymbolicLink/Get-ValentiaSymbolicLink.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,69 +35,10 @@ function Get-ValentiaSymbolicLink
[String[]]$Path
)

process
{
try
{
$Path `
| %{
if ($file = IsFile -Path $_)
{
if (IsFileReparsePoint -Path $file.FullName)
{
# [Valentia.SymbolicLinkGet]::GetSymbolicLinkTarget()
# [System.Type]::GetType($typeQualifiedName)::GetSymbolicLinkTarget()
$symTarget = $SymbolicLinkGet::GetSymbolicLinkTarget($file.FullName)
Add-Member -InputObject $file -MemberType NoteProperty -Name SymbolicPath -Value $symTarget -Force
return $file
}
}
elseif ($directory = IsDirectory -Path $_)
{
if (IsDirectoryReparsePoint -Path $directory.FullName)
{
# [Valentia.SymbolicLinkGet]::GetSymbolicLinkTarget()
# [System.Type]::GetType($typeQualifiedName)::GetSymbolicLinkTarget()
$symTarget = $SymbolicLinkGet::GetSymbolicLinkTarget($directory.FullName)
Add-Member -InputObject $directory -MemberType NoteProperty -Name SymbolicPath -Value $symTarget -Force
return $directory
}
}
}
}
catch
{
throw $_
}
}

begin
{
$private:ErrorActionPreference = $valentia.preference.ErrorActionPreference.custom

try
{
$private:CSPath = Join-Path $valentia.modulePath $valentia.cSharpPath -Resolve
$private:SymbolicCS = Join-Path $CSPath GetSymLink.cs -Resolve
$private:sig = Get-Content -Path $SymbolicCS -Raw

$private:addType = @{
MemberDefinition = $sig
Namespace = "Valentia"
Name = "SymbolicLinkGet"
UsingNameSpace = "System.Text", "Microsoft.Win32.SafeHandles", "System.ComponentModel"
}
Add-ValentiaTypeMemberDefinition @addType -PassThru `
| select -First 1 `
| %{
$SymbolicLinkGet = $_.AssemblyQualifiedName -as [type]
}
}
catch
{
# catch Exception and ignore it
}

function IsFile ([string]$Path)
{
if ([System.IO.File]::Exists($Path))
Expand Down Expand Up @@ -150,4 +91,40 @@ function Get-ValentiaSymbolicLink
}
}
}

process
{
try
{
$Path `
| %{
if ($file = IsFile -Path $_)
{
if (IsFileReparsePoint -Path $file.FullName)
{
# [Valentia.SymbolicLinkGet]::GetSymbolicLinkTarget()
# [System.Type]::GetType($typeQualifiedName)::GetSymbolicLinkTarget()
$symTarget = [Valentia.CS.SymbolicLink]::GetSymbolicLinkTarget($file.FullName)
Add-Member -InputObject $file -MemberType NoteProperty -Name SymbolicPath -Value $symTarget -Force
return $file
}
}
elseif ($directory = IsDirectory -Path $_)
{
if (IsDirectoryReparsePoint -Path $directory.FullName)
{
# [Valentia.SymbolicLinkGet]::GetSymbolicLinkTarget()
# [System.Type]::GetType($typeQualifiedName)::GetSymbolicLinkTarget()
$symTarget = [Valentia.CS.SymbolicLink]::GetSymbolicLinkTarget($directory.FullName)
Add-Member -InputObject $directory -MemberType NoteProperty -Name SymbolicPath -Value $symTarget -Force
return $directory
}
}
}
}
catch
{
throw $_
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,6 @@ function Remove-ValentiaSymbolicLink
[String[]]$Path
)

process
{
try
{
$Path `
| %{
if ($file = IsFile -Path $_)
{
if (IsFileReparsePoint -Path $file)
{
RemoveFileReparsePoint -Path $file
}
}
elseif ($directory = IsDirectory -Path $_)
{
if (IsDirectoryReparsePoint -Path $directory)
{
RemoveDirectoryReparsePoint -Path $directory
}
}
}
}
catch
{
throw $_
}
}

begin
{
$script:ErrorActionPreference = $valentia.preference.ErrorActionPreference.custom
Expand Down Expand Up @@ -130,4 +102,32 @@ function Remove-ValentiaSymbolicLink
[System.IO.Directory]::Delete($Path.FullName)
}
}

process
{
try
{
$Path `
| %{
if ($file = IsFile -Path $_)
{
if (IsFileReparsePoint -Path $file)
{
RemoveFileReparsePoint -Path $file
}
}
elseif ($directory = IsDirectory -Path $_)
{
if (IsDirectoryReparsePoint -Path $directory)
{
RemoveDirectoryReparsePoint -Path $directory
}
}
}
}
catch
{
throw $_
}
}
}
Loading

0 comments on commit 7b074c2

Please sign in to comment.