ADD: Overlong path support in several functions

This commit is contained in:
Alexander Koblov 2016-08-20 06:26:08 +00:00
commit 7be83161e7
3 changed files with 34 additions and 40 deletions

View file

@ -568,16 +568,11 @@ end;
function CreateHardLink(const Path, LinkName: String) : Boolean;
{$IFDEF MSWINDOWS}
var
wsPath, wsLinkName: WideString;
wsPath, wsLinkName: UnicodeString;
begin
Result:= True;
try
wsPath:= UTF8Decode(Path);
wsLinkName:= UTF8Decode(LinkName);
Result:= uNTFSLinks.CreateHardlink(wsPath, wsLinkName);
except
Result:= False;
end;
wsPath:= UTF16LongName(Path);
wsLinkName:= UTF16LongName(LinkName);
Result:= uNTFSLinks.CreateHardlink(wsPath, wsLinkName);
end;
{$ELSE}
begin
@ -588,16 +583,11 @@ end;
function CreateSymLink(const Path, LinkName: string) : Boolean;
{$IFDEF MSWINDOWS}
var
wsPath, wsLinkName: WideString;
wsPath, wsLinkName: UnicodeString;
begin
Result := True;
try
wsPath:= UTF8Decode(Path);
wsLinkName:= UTF8Decode(LinkName);
Result:= uNTFSLinks.CreateSymlink(wsPath, wsLinkName);
except
Result := False;
end;
wsPath:= UTF8Decode(Path);
wsLinkName:= UTF16LongName(LinkName);
Result:= uNTFSLinks.CreateSymlink(wsPath, wsLinkName);
end;
{$ELSE}
begin
@ -613,15 +603,11 @@ var
wsLinkName,
wsTarget: UnicodeString;
begin
try
wsLinkName:= UTF8Decode(LinkName);
if uNTFSLinks.ReadSymLink(wsLinkName, wsTarget) then
Result := UTF16ToUTF8(wsTarget)
else
Result := '';
except
Result := '';
end;
wsLinkName:= UTF16LongName(LinkName);
if uNTFSLinks.ReadSymLink(wsLinkName, wsTarget) then
Result := UTF16ToUTF8(wsTarget)
else
Result := EmptyStr;
end;
{$ELSE}
begin
@ -1040,7 +1026,7 @@ function mbSetEnvironmentVariable(const sName, sValue: String): Boolean;
{$IFDEF MSWINDOWS}
var
wsName,
wsValue: WideString;
wsValue: UnicodeString;
begin
wsName:= UTF8Decode(sName);
wsValue:= UTF8Decode(sValue);

View file

@ -4,7 +4,7 @@
This unit contains functions to work with hard and symbolic links
on the NTFS file system.
Copyright (C) 2012 Alexander Koblov (alexx2000@mail.ru)
Copyright (C) 2012-2016 Alexander Koblov (alexx2000@mail.ru)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@ -116,6 +116,9 @@ implementation
uses
LCLProc, uDebug;
const
ERROR_DIRECTORY_NOT_SUPPORTED = 336;
type
TCreateSymbolicLinkW = function(
pwcSymlinkFileName,
@ -218,14 +221,19 @@ var
dwAttributes: DWORD;
begin
dwAttributes := Windows.GetFileAttributesW(PWideChar(AFileName));
if dwAttributes = FILE_DOES_NOT_EXIST then
raise Exception.Create('File "' + AFileName + '" does not exist.');
if dwAttributes = FILE_DOES_NOT_EXIST then Exit(False);
if (dwAttributes and FILE_ATTRIBUTE_DIRECTORY) <> 0 then
raise Exception.Create('Can''t create hardlink for directory (file "' + AFileName + '").');
begin
SetLastError(ERROR_DIRECTORY_NOT_SUPPORTED);
Exit(False);
end;
dwAttributes := Windows.GetFileAttributesW(PWideChar(ALinkName));
if dwAttributes <> FILE_DOES_NOT_EXIST then
raise Exception.Create('File "' + ALinkName + '" already exists.');
begin
SetLastError(ERROR_FILE_EXISTS);
Exit(False);
end;
if HasNewApi then
Result:= _CreateHardLink_New(AFileName, ALinkName)

View file

@ -60,14 +60,14 @@ function InsertMenuItemEx(hMenu, SubMenu: HMENU; Caption: PWideChar; Position, I
{en
Extracts volume GUID from a volume GUID path
}
function ExtractVolumeGUID(const VolumeName: WideString): WideString;
function ExtractVolumeGUID(const VolumeName: UnicodeString): UnicodeString;
{en
Retrieves a volume GUID path for the volume that is associated with the specified
volume mount point (drive letter, volume GUID path, or mounted folder)
@param(Path The string that contains the path of a mounted folder or a drive letter)
@returns(Volume GUID path)
}
function GetMountPointVolumeName(const Path: WideString): WideString;
function GetMountPointVolumeName(const Path: UnicodeString): UnicodeString;
{en
Checks readiness of a drive
@param(sDrv String specifying the root directory of a file system volume)
@ -239,7 +239,7 @@ begin
SetLength(Result, Pos('(', Result) - 2);
end;
function ExtractVolumeGUID(const VolumeName: WideString): WideString;
function ExtractVolumeGUID(const VolumeName: UnicodeString): UnicodeString;
var
I, J: LongInt;
begin
@ -249,11 +249,11 @@ begin
Result:= Copy(VolumeName, I, J - I + 1);
end;
function GetMountPointVolumeName(const Path: WideString): WideString;
function GetMountPointVolumeName(const Path: UnicodeString): UnicodeString;
const
MAX_VOLUME_NAME = 50;
var
wsPath: WideString;
wsPath: UnicodeString;
wsVolumeName: array[0..Pred(MAX_VOLUME_NAME)] of WideChar;
begin
FillByte(wsVolumeName, MAX_VOLUME_NAME, 0);
@ -261,7 +261,7 @@ begin
if not GetVolumeNameForVolumeMountPointW(PWideChar(wsPath), wsVolumeName, MAX_VOLUME_NAME) then
Result:= EmptyWideStr
else
Result:= WideString(wsVolumeName);
Result:= UnicodeString(wsVolumeName);
end;
(* Drive ready *)
@ -563,7 +563,7 @@ end;
function mbGetFileSystem(const sRootPath: String): String;
var
Buf: array [0..MAX_PATH] of WideChar;
NotUsed: DWORD;
NotUsed: DWORD = 0;
begin
// Available since Windows XP.
if ((Win32MajorVersion > 5) or ((Win32MajorVersion = 5) and (Win32MinorVersion >= 1))) and