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

Function CreateReparsePoint

Libraries/unrar/win32lnk.cpp:32–174  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

30
31
32bool CreateReparsePoint(CommandData *Cmd,const wchar *Name,FileHeader *hd)
33{
34 static bool PrivSet=false;
35 if (!PrivSet)
36 {
37 SetPrivilege(SE_RESTORE_NAME);
38 // Not sure if we really need it, but let's request anyway.
39 SetPrivilege(SE_CREATE_SYMBOLIC_LINK_NAME);
40 PrivSet=true;
41 }
42
43 const DWORD BufSize=sizeof(REPARSE_DATA_BUFFER)+2*NM+1024;
44 Array<byte> Buf(BufSize);
45 REPARSE_DATA_BUFFER *rdb=(REPARSE_DATA_BUFFER *)&Buf[0];
46
47 wchar SubstName[NM];
48 wcsncpyz(SubstName,hd->RedirName,ASIZE(SubstName));
49 size_t SubstLength=wcslen(SubstName);
50
51 wchar PrintName[NM],*PrintNameSrc=SubstName,*PrintNameDst=PrintName;
52 bool WinPrefix=wcsncmp(PrintNameSrc,L"\\??\\",4)==0;
53 if (WinPrefix)
54 PrintNameSrc+=4;
55 if (WinPrefix && wcsncmp(PrintNameSrc,L"UNC\\",4)==0)
56 {
57 *(PrintNameDst++)='\\'; // Insert second \ in beginning of share name.
58 PrintNameSrc+=3;
59 }
60 wcscpy(PrintNameDst,PrintNameSrc);
61
62 size_t PrintLength=wcslen(PrintName);
63
64 bool AbsPath=WinPrefix;
65 // IsFullPath is not really needed here, AbsPath check is enough.
66 // We added it just for extra safety, in case some Windows version would
67 // allow to create absolute targets with SYMLINK_FLAG_RELATIVE.
68 // Use hd->FileName instead of Name, since Name can include the destination
69 // path as a prefix, which can confuse IsRelativeSymlinkSafe algorithm.
70 if (!Cmd->AbsoluteLinks && (AbsPath || IsFullPath(hd->RedirName) ||
71 !IsRelativeSymlinkSafe(Cmd,hd->FileName,Name,hd->RedirName)))
72 return false;
73
74 CreatePath(Name,true);
75
76 // 'DirTarget' check is important for Unix symlinks to directories.
77 // Unix symlinks do not have their own 'directory' attribute.
78 if (hd->Dir || hd->DirTarget)
79 {
80 if (!CreateDirectory(Name,NULL))
81 return false;
82 }
83 else
84 {
85 HANDLE hFile=CreateFile(Name,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
86 if (hFile == INVALID_HANDLE_VALUE)
87 return false;
88 CloseHandle(hFile);
89 }

Callers 1

ExtractSymlinkFunction · 0.85

Calls 13

SetPrivilegeFunction · 0.85
wcsncpyzFunction · 0.85
IsRelativeSymlinkSafeFunction · 0.85
CreatePathFunction · 0.85
uiMsgFunction · 0.85
IsUserAdminFunction · 0.85
SetFileAttrFunction · 0.85
SysErrMsgMethod · 0.80
SetErrorCodeMethod · 0.80
SetHandleMethod · 0.80
SetOpenFileTimeMethod · 0.80
IsFullPathFunction · 0.70

Tested by

no test coverage detected