| 30 | |
| 31 | |
| 32 | bool 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 | } |
no test coverage detected