MCPcopy Create free account
hub / github.com/abbeycode/UnrarKit / GetWinLongPath

Function GetWinLongPath

Libraries/unrar/pathfn.cpp:908–972  ·  view source on GitHub ↗

We should return 'true' even if resulting path is shorter than MAX_PATH, because we can also use this function to open files with non-standard characters, even if their path length is normal.

Source from the content-addressed store, hash-verified

906// because we can also use this function to open files with non-standard
907// characters, even if their path length is normal.
908bool GetWinLongPath(const wchar *Src,wchar *Dest,size_t MaxSize)
909{
910 if (*Src==0)
911 return false;
912 const wchar *Prefix=L"\\\\?\\";
913 const size_t PrefixLength=4;
914 bool FullPath=IsDriveLetter(Src) && IsPathDiv(Src[2]);
915 size_t SrcLength=wcslen(Src);
916 if (IsFullPath(Src)) // Paths in d:\path\name format.
917 {
918 if (IsDriveLetter(Src))
919 {
920 if (MaxSize<=PrefixLength+SrcLength)
921 return false;
922 wcsncpyz(Dest,Prefix,MaxSize);
923 wcsncatz(Dest,Src,MaxSize); // "\\?\D:\very long path".
924 return true;
925 }
926 else
927 if (Src[0]=='\\' && Src[1]=='\\')
928 {
929 if (MaxSize<=PrefixLength+SrcLength+2)
930 return false;
931 wcsncpyz(Dest,Prefix,MaxSize);
932 wcsncatz(Dest,L"UNC",MaxSize);
933 wcsncatz(Dest,Src+1,MaxSize); // "\\?\UNC\server\share".
934 return true;
935 }
936 // We may be here only if we modify IsFullPath in the future.
937 return false;
938 }
939 else
940 {
941 wchar CurDir[NM];
942 DWORD DirCode=GetCurrentDirectory(ASIZE(CurDir)-1,CurDir);
943 if (DirCode==0 || DirCode>ASIZE(CurDir)-1)
944 return false;
945
946 if (IsPathDiv(Src[0])) // Paths in \path\name format.
947 {
948 if (MaxSize<=PrefixLength+SrcLength+2)
949 return false;
950 wcsncpyz(Dest,Prefix,MaxSize);
951 CurDir[2]=0;
952 wcsncatz(Dest,CurDir,MaxSize); // Copy drive letter 'd:'.
953 wcsncatz(Dest,Src,MaxSize);
954 return true;
955 }
956 else // Paths in path\name format.
957 {
958 AddEndSlash(CurDir,ASIZE(CurDir));
959 if (MaxSize<=PrefixLength+wcslen(CurDir)+SrcLength)
960 return false;
961 wcsncpyz(Dest,Prefix,MaxSize);
962 wcsncatz(Dest,CurDir,MaxSize);
963
964 if (Src[0]=='.' && IsPathDiv(Src[1])) // Remove leading .\ in pathname.
965 Src+=2;

Callers 12

OpenMethod · 0.85
CreateMethod · 0.85
ExtractACLFunction · 0.85
MakeDirFunction · 0.85
SetDirTimeFunction · 0.85
GetFileAttrFunction · 0.85
SetFileAttrFunction · 0.85
RenameFileFunction · 0.85
DelFileFunction · 0.85
SetFileCompressionFunction · 0.85
Win32FindMethod · 0.85
ConvertNameToFullFunction · 0.85

Calls 6

IsDriveLetterFunction · 0.85
IsPathDivFunction · 0.85
wcsncpyzFunction · 0.85
wcsncatzFunction · 0.85
AddEndSlashFunction · 0.85
IsFullPathFunction · 0.70

Tested by

no test coverage detected