Skip to content

Commit

Permalink
Merge pull request #74 from guitarrapc/DetectSymbolicTarget
Browse files Browse the repository at this point in the history
Detect symbolic target
  • Loading branch information
guitarrapc committed Feb 12, 2015
2 parents 4394605 + 9e09f18 commit c8e4fae
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 5 deletions.
36 changes: 36 additions & 0 deletions VersionHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,42 @@ This indicate Version History for valentia.
## Version 0.4.x
====

- version : 0.4.12

[ author : guitarrapc ]

[ Feb 12, 2015 ]

#### Enhancement
* Now Get-ValentiaSymbolicLink returns SymbolicLink Target Path.

- version : 0.4.11

[ author : guitarrapc ]

[ Dec 11, 2015 ]

#### Enhancement
* Added WorkingDirectory support for ```Set-ValentiaScheduledTask```
* Added Verbose Stream support for ```Get/Set/Test-ValentiaACL```

#### Unremarkable change
* Hide loaded path from valentia.ps1

- version : 0.4.10

[ author : guitarrapc ]

[ Nov 28, 2014 ]

#### Enhancement
* ```-Strict``` parameter([bool]) was added to support UserName strict Checking.

#### Bug fix
* fix Bug for Get/Set/Test-ValetniaACL. Now ACL detect collect.

#### Unremarkable change
* Installer update.

- version : 0.4.9

Expand Down
23 changes: 23 additions & 0 deletions valentia/CS/GetSymLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
private const int FILE_SHARE_READ = 1;
private const int FILE_SHARE_WRITE = 2;
private const int CREATION_DISPOSITION_OPEN_EXISTING = 3;
private const int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;

[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);

public static string GetSymbolicLinkTarget(System.IO.DirectoryInfo symlink)
{
SafeFileHandle directoryHandle = CreateFile(symlink.FullName, 0, 2, System.IntPtr.Zero, CREATION_DISPOSITION_OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, System.IntPtr.Zero);

if (directoryHandle.IsInvalid)
throw new Win32Exception(Marshal.GetLastWin32Error());
StringBuilder path = new StringBuilder(512);
int size = 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);
else return path.ToString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ function Add-ValentiaTypeMemberDefinition
[string]$Name,

[Parameter(mandatory = 0, position = 3)]
[ValidateNotNullOrEmpty()]
[string[]]$UsingNameSpace,

[Parameter(mandatory = 0, position = 4)]
[switch]$PassThru
)

Expand All @@ -25,6 +29,10 @@ function Add-ValentiaTypeMemberDefinition
Namespace = $NameSpace
Name = $Name + $guid
}
if (@($UsingNameSpace).Count -ne 0)
{
$addType.UsingNameSpace = $UsingNameSpace
}

$private:result = Add-Type @addType -PassThru
if ($PSBoundParameters.PassThru.IsPresent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Parameter Input to detect SymbolicLink items.
#>
function Get-ValentiaSymbolicLink
{
[OutputType([System.IO.DirectoryInfo[]])]
[cmdletBinding()]
param
(
Expand All @@ -44,13 +45,19 @@ function Get-ValentiaSymbolicLink
{
if (IsFileReparsePoint -Path $file.FullName)
{
# [Valentia.SymbolicLink2]::GetSymbolicLinkTarget()
$symTarget = [System.Type]::GetType($typeQualifiedName)::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.SymbolicLink2]::GetSymbolicLinkTarget()
$symTarget = [System.Type]::GetType($typeQualifiedName)::GetSymbolicLinkTarget($directory.FullName)
Add-Member -InputObject $directory -MemberType NoteProperty -Name SymbolicPath -Value $symTarget -Force
return $directory
}
}
Expand All @@ -66,6 +73,31 @@ function Get-ValentiaSymbolicLink
{
$script:ErrorActionPreference = $valentia.preference.ErrorActionPreference.custom

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

$script:addType = @{
MemberDefinition = $sig
Namespace = "Valentia"
Name = "SymbolicLink"
UsingNameSpace = "System.Text", "Microsoft.Win32.SafeHandles", "System.ComponentModel"
}
Add-ValentiaTypeMemberDefinition @addType -PassThru `
| select -First 1 `
| %{
$script:typeQualifiedName = $_.AssemblyQualifiedName
$script:typeFullName = $_.FullName
$valentia.typeQualifiedName = $_.AssemblyQualifiedName
}
}
catch
{
# catch Exception and ignore it
}

function IsFile ([string]$Path)
{
if ([System.IO.File]::Exists($Path))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Parameter Input to detect SymbolicLink items.
#>
function Remove-ValentiaSymbolicLink
{
[OutputType([Void])]
[cmdletBinding()]
param
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ As number input was less with -Path, d:\hoge3 will be ignore.
#>
function Set-ValentiaSymbolicLink
{
[OutputType([Void])]
[cmdletBinding(DefaultParameterSetName = "ForceFile")]
param
(
Expand Down
2 changes: 1 addition & 1 deletion valentia/Tools/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ $valentia.combineTempfunction = '{0}.ps1' -f $valentia.name
$valentia.combineTemptype = 'Type.ps1'
$valentia.fileEncode = [Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding]'utf8'

$valentia.moduleVersion = "0.4.11"
$valentia.moduleVersion = "0.4.12"
$valentia.description = "PowerShell Remote deployment library for Windows Servers";
$valentia.copyright = "28/June/2013 -"
$valentia.RequiredModules = @()
Expand Down
42 changes: 42 additions & 0 deletions valentia/valentia.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ function Add-ValentiaTypeMemberDefinition
[string]$Name,

[Parameter(mandatory = 0, position = 3)]
[ValidateNotNullOrEmpty()]
[string[]]$UsingNameSpace,

[Parameter(mandatory = 0, position = 4)]
[switch]$PassThru
)

Expand All @@ -406,6 +410,10 @@ function Add-ValentiaTypeMemberDefinition
Namespace = $NameSpace
Name = $Name + $guid
}
if (@($UsingNameSpace).Count -ne 0)
{
$addType.UsingNameSpace = $UsingNameSpace
}

$private:result = Add-Type @addType -PassThru
if ($PSBoundParameters.PassThru.IsPresent)
Expand Down Expand Up @@ -4989,6 +4997,7 @@ Parameter Input to detect SymbolicLink items.
#>
function Get-ValentiaSymbolicLink
{
[OutputType([System.IO.DirectoryInfo[]])]
[cmdletBinding()]
param
(
Expand All @@ -5007,13 +5016,19 @@ function Get-ValentiaSymbolicLink
{
if (IsFileReparsePoint -Path $file.FullName)
{
# [Valentia.SymbolicLink2]::GetSymbolicLinkTarget()
$symTarget = [System.Type]::GetType($typeQualifiedName)::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.SymbolicLink2]::GetSymbolicLinkTarget()
$symTarget = [System.Type]::GetType($typeQualifiedName)::GetSymbolicLinkTarget($directory.FullName)
Add-Member -InputObject $directory -MemberType NoteProperty -Name SymbolicPath -Value $symTarget -Force
return $directory
}
}
Expand All @@ -5029,6 +5044,31 @@ function Get-ValentiaSymbolicLink
{
$script:ErrorActionPreference = $valentia.preference.ErrorActionPreference.custom

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

$script:addType = @{
MemberDefinition = $sig
Namespace = "Valentia"
Name = "SymbolicLink"
UsingNameSpace = "System.Text", "Microsoft.Win32.SafeHandles", "System.ComponentModel"
}
Add-ValentiaTypeMemberDefinition @addType -PassThru `
| select -First 1 `
| %{
$script:typeQualifiedName = $_.AssemblyQualifiedName
$script:typeFullName = $_.FullName
$valentia.typeQualifiedName = $_.AssemblyQualifiedName
}
}
catch
{
# catch Exception and ignore it
}

function IsFile ([string]$Path)
{
if ([System.IO.File]::Exists($Path))
Expand Down Expand Up @@ -5113,6 +5153,7 @@ Parameter Input to detect SymbolicLink items.
#>
function Remove-ValentiaSymbolicLink
{
[OutputType([Void])]
[cmdletBinding()]
param
(
Expand Down Expand Up @@ -5256,6 +5297,7 @@ As number input was less with -Path, d:\hoge3 will be ignore.
#>
function Set-ValentiaSymbolicLink
{
[OutputType([Void])]
[cmdletBinding(DefaultParameterSetName = "ForceFile")]
param
(
Expand Down
8 changes: 4 additions & 4 deletions valentia/valentia.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: guitarrapc
#
# Generated on: 12/11/2014
# Generated on: 2/12/2015
#

@{
Expand All @@ -12,10 +12,10 @@
# RootModule = ''

# Version number of this module.
ModuleVersion = '0.4.11'
ModuleVersion = '0.4.12'

# ID used to uniquely identify this module
GUID = '367e8de7-d1b1-4252-acf1-7c7e44ce40d4'
GUID = '931ab02f-2090-4271-be26-cad75219984b'

# Author of this module
Author = 'guitarrapc'
Expand All @@ -24,7 +24,7 @@ Author = 'guitarrapc'
CompanyName = 'guitarrapc'

# Copyright statement for this module
Copyright = '(c) 2014 guitarrapc. All rights reserved.'
Copyright = '(c) 2015 guitarrapc. All rights reserved.'

# Description of the functionality provided by this module
Description = 'PowerShell Remote deployment library for Windows Servers'
Expand Down
1 change: 1 addition & 0 deletions valentia/valentia.pssproj
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
</ItemGroup>
<ItemGroup>
<Content Include="Config\valentia-config.ps1" />
<Content Include="CS\GetSymLink.cs" />
<Content Include="CS\CreateSymLink.cs" />
<Content Include="Example\Deployment\Application\execute\execute.ps1" />
<Content Include="Example\Deployment\Application\Task-InstallChocolatey.bat" />
Expand Down

0 comments on commit c8e4fae

Please sign in to comment.