Skip to content

Commit

Permalink
Push 2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Sampson committed Dec 29, 2016
1 parent ce49f4e commit e093e5b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
4 changes: 2 additions & 2 deletions SteamCleaner/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("2.2.0.0")]
[assembly: AssemblyFileVersion("2.2.0.0")]
[assembly: AssemblyVersion("2.3.0.0")]
[assembly: AssemblyFileVersion("2.3.0.0")]
63 changes: 33 additions & 30 deletions SteamCleaner/Utilities/Files/SymbolicLink.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
using System;
using System.Collections.Generic;
#region

using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32.SafeHandles;

#endregion

namespace SteamCleaner.Utilities.Files
{
public static class SymbolicLink
{
private const uint genericReadAccess = 0x80000000;
private const uint GenericReadAccess = 0x80000000;

private const uint fileFlagsForOpenReparsePointAndBackupSemantics = 0x02200000;
private const uint FileFlagsForOpenReparsePointAndBackupSemantics = 0x02200000;

private const int ioctlCommandGetReparsePoint = 0x000900A8;
private const int IoctlCommandGetReparsePoint = 0x000900A8;

private const uint openExisting = 0x3;
private const uint OpenExisting = 0x3;

private const uint pathNotAReparsePointError = 0x80071126;
private const uint PathNotAReparsePointError = 0x80071126;

private const uint shareModeAll = 0x7; // Read, Write, Delete
private const uint ShareModeAll = 0x7; // Read, Write, Delete

private const uint symLinkTag = 0xA000000C;
private const uint SymLinkTag = 0xA000000C;

private const int targetIsAFile = 0;
private const int TargetIsAFile = 0;

private const int targetIsADirectory = 1;
private const int TargetIsADirectory = 1;

[DllImport("kernel32.dll", SetLastError = true)]
private static extern SafeFileHandle CreateFile(
Expand All @@ -40,7 +41,7 @@ private static extern SafeFileHandle CreateFile(
IntPtr hTemplateFile);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags);
private static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool DeviceIoControl(
Expand All @@ -52,14 +53,16 @@ private static extern bool DeviceIoControl(
int nOutBufferSize,
out int lpBytesReturned,
IntPtr lpOverlapped);

public static bool IsSymbolic(string path)
{
FileInfo pathInfo = new FileInfo(path);
var pathInfo = new FileInfo(path);
return pathInfo.Attributes.HasFlag(FileAttributes.ReparsePoint);
}

public static void CreateDirectoryLink(string linkPath, string targetPath)
{
if (!CreateSymbolicLink(linkPath, targetPath, targetIsADirectory) || Marshal.GetLastWin32Error() != 0)
if (!CreateSymbolicLink(linkPath, targetPath, TargetIsADirectory) || Marshal.GetLastWin32Error() != 0)
{
try
{
Expand All @@ -74,7 +77,7 @@ public static void CreateDirectoryLink(string linkPath, string targetPath)

public static void CreateFileLink(string linkPath, string targetPath)
{
if (!CreateSymbolicLink(linkPath, targetPath, targetIsAFile))
if (!CreateSymbolicLink(linkPath, targetPath, TargetIsAFile))
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
Expand All @@ -86,62 +89,62 @@ public static bool Exists(string path)
{
return false;
}
string target = GetTarget(path);
var target = GetTarget(path);
return target != null;
}

private static SafeFileHandle getFileHandle(string path)
private static SafeFileHandle GetFileHandle(string path)
{
return CreateFile(path, genericReadAccess, shareModeAll, IntPtr.Zero, openExisting,
fileFlagsForOpenReparsePointAndBackupSemantics, IntPtr.Zero);
return CreateFile(path, GenericReadAccess, ShareModeAll, IntPtr.Zero, OpenExisting,
FileFlagsForOpenReparsePointAndBackupSemantics, IntPtr.Zero);
}

public static string GetTarget(string path)
{
SymbolicLinkReparseData reparseDataBuffer;

using (SafeFileHandle fileHandle = getFileHandle(path))
using (var fileHandle = GetFileHandle(path))
{
if (fileHandle.IsInvalid)
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}

int outBufferSize = Marshal.SizeOf(typeof(SymbolicLinkReparseData));
IntPtr outBuffer = IntPtr.Zero;
var outBufferSize = Marshal.SizeOf(typeof(SymbolicLinkReparseData));
var outBuffer = IntPtr.Zero;
try
{
outBuffer = Marshal.AllocHGlobal(outBufferSize);
int bytesReturned;
bool success = DeviceIoControl(
fileHandle.DangerousGetHandle(), ioctlCommandGetReparsePoint, IntPtr.Zero, 0,
var success = DeviceIoControl(
fileHandle.DangerousGetHandle(), IoctlCommandGetReparsePoint, IntPtr.Zero, 0,
outBuffer, outBufferSize, out bytesReturned, IntPtr.Zero);

fileHandle.Close();

if (!success)
{
if (((uint)Marshal.GetHRForLastWin32Error()) == pathNotAReparsePointError)
if ((uint) Marshal.GetHRForLastWin32Error() == PathNotAReparsePointError)
{
return null;
}
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}

reparseDataBuffer = (SymbolicLinkReparseData)Marshal.PtrToStructure(
reparseDataBuffer = (SymbolicLinkReparseData) Marshal.PtrToStructure(
outBuffer, typeof(SymbolicLinkReparseData));
}
finally
{
Marshal.FreeHGlobal(outBuffer);
}
}
if (reparseDataBuffer.ReparseTag != symLinkTag)
if (reparseDataBuffer.ReparseTag != SymLinkTag)
{
return null;
}

string target = Encoding.Unicode.GetString(reparseDataBuffer.PathBuffer,
var target = Encoding.Unicode.GetString(reparseDataBuffer.PathBuffer,
reparseDataBuffer.PrintNameOffset, reparseDataBuffer.PrintNameLength);

return target;
Expand Down
18 changes: 8 additions & 10 deletions SteamCleaner/Utilities/Files/SymbolicLinkReparseData.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
#region

using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

#endregion

namespace SteamCleaner.Utilities.Files
{
/// <remarks>
/// Refer to http://msdn.microsoft.com/en-us/library/windows/hardware/ff552012%28v=vs.85%29.aspx
/// Refer to http://msdn.microsoft.com/en-us/library/windows/hardware/ff552012%28v=vs.85%29.aspx
/// </remarks>
[StructLayout(LayoutKind.Sequential)]
public struct SymbolicLinkReparseData
{
// Not certain about this!
private const int maxUnicodePathLength = 260 * 2;
private const int MaxUnicodePathLength = 260*2;

public uint ReparseTag;
public ushort ReparseDataLength;
Expand All @@ -24,7 +23,6 @@ public struct SymbolicLinkReparseData
public ushort PrintNameOffset;
public ushort PrintNameLength;
public uint Flags;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = maxUnicodePathLength)]
public byte[] PathBuffer;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxUnicodePathLength)] public byte[] PathBuffer;
}
}
}
2 changes: 1 addition & 1 deletion version.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<steamcleaner>
<version>2.2</version>
<version>2.3</version>
<url>https://github.com/Codeusa/SteamCleaner/releases/latest</url>
</steamcleaner>

0 comments on commit e093e5b

Please sign in to comment.