diff --git a/components/lazutils/lazfileutils.pas b/components/lazutils/lazfileutils.pas index 0cea9ef99c..d0b91e0d48 100644 --- a/components/lazutils/lazfileutils.pas +++ b/components/lazutils/lazfileutils.pas @@ -79,6 +79,11 @@ function CreateDirUTF8(const NewDir: String): Boolean; function RemoveDirUTF8(const Dir: String): Boolean; function ForceDirectoriesUTF8(const Dir: string): Boolean; +// UNC paths +function IsUNCPath(const Path: String): Boolean; +function ExtractUNCVolume(const Path: String): String; + + type TInvalidateFileStateCacheEvent = procedure(const Filename: string); var @@ -1190,5 +1195,53 @@ begin OnInvalidateFileStateCache(Filename); end; +function IsUNCPath(const Path: String): Boolean; +begin + Result := (Length(Path) > 2) and (Path[1] = PathDelim) and (Path[2] = PathDelim); +end; + +function ExtractUNCVolume(const Path: String): String; +var + I, Len: Integer; + + // the next function reuses Len variable + function NextPathDelim(const Start: Integer): Integer;// inline; + begin + Result := Start; + while (Result <= Len) and (Path[Result] <> PathDelim) do + inc(Result); + end; + +begin + if not IsUNCPath(Path) then + Exit(''); + I := 3; + Len := Length(Path); + if Path[I] = '?' then + begin + // Long UNC path form like: + // \\?\UNC\ComputerName\SharedFolder\Resource or + // \\?\C:\Directory + inc(I); + if Path[I] <> PathDelim then + Exit(''); + if UpperCase(Copy(Path, I + 1, 3)) = 'UNC' then + begin + inc(I, 4); + if I < Len then + I := NextPathDelim(I + 1); + if I < Len then + I := NextPathDelim(I + 1); + end; + end + else + begin + I := NextPathDelim(I); + if I < Len then + I := NextPathDelim(I + 1); + end; + Result := Copy(Path, 1, I); +end; + end.