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

Function UpdateExistingShortName

Libraries/unrar/filcreat.cpp:85–162  ·  view source on GitHub ↗

If we find a file, which short name is equal to 'Name', we try to change its short name, while preserving the long name. It helps when unpacking an archived file, which long name is equal to short name of already existing file. Otherwise we would overwrite the already existing file, even though its long name does not match the name of unpacking file.

Source from the content-addressed store, hash-verified

83// existing file. Otherwise we would overwrite the already existing file,
84// even though its long name does not match the name of unpacking file.
85bool UpdateExistingShortName(const wchar *Name)
86{
87 wchar LongPathName[NM];
88 DWORD Res=GetLongPathName(Name,LongPathName,ASIZE(LongPathName));
89 if (Res==0 || Res>=ASIZE(LongPathName))
90 return false;
91 wchar ShortPathName[NM];
92 Res=GetShortPathName(Name,ShortPathName,ASIZE(ShortPathName));
93 if (Res==0 || Res>=ASIZE(ShortPathName))
94 return false;
95 wchar *LongName=PointToName(LongPathName);
96 wchar *ShortName=PointToName(ShortPathName);
97
98 // We continue only if file has a short name, which does not match its
99 // long name, and this short name is equal to name of file which we need
100 // to create.
101 if (*ShortName==0 || wcsicomp(LongName,ShortName)==0 ||
102 wcsicomp(PointToName(Name),ShortName)!=0)
103 return false;
104
105 // Generate the temporary new name for existing file.
106 wchar NewName[NM];
107 *NewName=0;
108 for (int I=0;I<10000 && *NewName==0;I+=123)
109 {
110 // Here we copy the path part of file to create. We'll make the temporary
111 // file in the same folder.
112 wcsncpyz(NewName,Name,ASIZE(NewName));
113
114 // Here we set the random name part.
115 swprintf(PointToName(NewName),ASIZE(NewName),L"rtmp%d",I);
116
117 // If such file is already exist, try next random name.
118 if (FileExist(NewName))
119 *NewName=0;
120 }
121
122 // If we could not generate the name not used by any other file, we return.
123 if (*NewName==0)
124 return false;
125
126 // FastFind returns the name without path, but we need the fully qualified
127 // name for renaming, so we use the path from file to create and long name
128 // from existing file.
129 wchar FullName[NM];
130 wcsncpyz(FullName,Name,ASIZE(FullName));
131 SetName(FullName,LongName,ASIZE(FullName));
132
133 // Rename the existing file to randomly generated name. Normally it changes
134 // the short name too.
135 if (!MoveFile(FullName,NewName))
136 return false;
137
138 // Now we need to create the temporary empty file with same name as
139 // short name of our already existing file. We do it to occupy its previous
140 // short name and not allow to use it again when renaming the file back to
141 // its original long name.
142 File KeepShortFile;

Callers 1

FileCreateFunction · 0.85

Calls 8

PointToNameFunction · 0.85
wcsicompFunction · 0.85
wcsncpyzFunction · 0.85
FileExistFunction · 0.85
SetNameFunction · 0.85
CreateMethod · 0.80
DeleteMethod · 0.80
CloseMethod · 0.45

Tested by

no test coverage detected