Merge pull request #2212 from unxed/ctrl_yo
[far2l.git] / far2l / src / mix / cvtname.cpp
blob985f27c539c77bce1f5d9478b3af58aeec28c324
1 /*
2 cvtname.cpp
4 Функций для преобразования имен файлов/путей.
5 */
6 /*
7 Copyright (c) 1996 Eugene Roshal
8 Copyright (c) 2000 Far Group
9 All rights reserved.
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions
13 are met:
14 1. Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
16 2. Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in the
18 documentation and/or other materials provided with the distribution.
19 3. The name of the authors may not be used to endorse or promote products
20 derived from this software without specific prior written permission.
22 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "headers.hpp"
36 #include "cvtname.hpp"
37 #include "cddrv.hpp"
38 #include "syslog.hpp"
39 #include "pathmix.hpp"
40 #include "drivemix.hpp"
41 #include "strmix.hpp"
42 #include <errno.h>
43 #include <set>
45 #define IsDot(str) (str == L'.')
47 void MixToFullPath(FARString &strPath)
49 // Skip all path to root (with slash if exists)
50 LPWSTR pstPath = strPath.GetBuffer();
51 // size_t PathOffset=0;
52 // Point2Root(pstPath,PathOffset);
53 // pstPath+=PathOffset;
55 // Process "." and ".." if exists
56 for (int m = 0; pstPath[m];) {
57 // fragment "."
58 if (IsDot(pstPath[m]) && (!m || IsSlash(pstPath[m - 1]))) {
59 LPCWSTR pstSrc;
60 LPWSTR pstDst;
62 switch (pstPath[m + 1]) {
63 // fragment ".\"
64 // case L'\\':
65 // fragment "./"
66 case LGOOD_SLASH: {
67 for (pstSrc = pstPath + m + 2, pstDst = pstPath + m; *pstSrc; pstSrc++, pstDst++) {
68 *pstDst = *pstSrc;
71 *pstDst = 0;
72 continue;
73 } break;
74 // fragment "." at the end
75 case 0: {
76 pstPath[m] = 0;
77 continue;
78 } break;
79 // fragment "..\" or "../" or ".." at the end
80 case L'.': {
81 if (IsSlash(pstPath[m + 2]) || !pstPath[m + 2]) {
82 int n;
84 // Calculate subdir name offset
85 for (n = m - 2; (n >= 0) && (!IsSlash(pstPath[n])); n--)
88 n = (n < 0) ? 0 : n + 1;
90 // fragment "..\" or "../"
91 if (pstPath[m + 2]) {
92 for (pstSrc = pstPath + m + 3, pstDst = pstPath + n; *pstSrc; pstSrc++,
93 pstDst++) {
94 *pstDst = *pstSrc;
97 *pstDst = 0;
99 // fragment ".." at the end
100 else if (n > 0) {
101 pstPath[n] = 0;
102 } else { // dont go to nowhere
103 pstPath[0] = GOOD_SLASH;
104 pstPath[1] = 0;
107 m = n;
108 continue;
110 } break;
114 m++;
116 strPath.ReleaseBuffer();
117 if (strPath.GetLength() > 1 && strPath[strPath.GetLength() - 1] == GOOD_SLASH)
118 strPath.Truncate(strPath.GetLength() - 1); // #249
121 bool MixToFullPath(LPCWSTR stPath, FARString &strDest, LPCWSTR stCurrentDir)
123 if (stPath && *stPath == GOOD_SLASH) {
124 strDest = stPath;
125 MixToFullPath(strDest);
126 return true;
129 strDest.Clear();
131 if (stCurrentDir && *stCurrentDir) {
132 strDest = stCurrentDir;
135 if (strDest.IsEmpty()) {
136 apiGetCurrentDirectory(strDest);
137 if (strDest.IsEmpty()) { // wtf
138 strDest = L"." WGOOD_SLASH;
142 if (strDest.At(strDest.GetLength() - 1) != GOOD_SLASH)
143 strDest+= GOOD_SLASH;
145 if (stPath) {
146 while (stPath[0] == '.' && (!stPath[1] || stPath[1] == GOOD_SLASH)) {
147 ++stPath;
148 if (*stPath == GOOD_SLASH)
149 ++stPath;
151 strDest+= stPath;
154 MixToFullPath(strDest);
155 return true;
158 size_t lPath=wcslen(NullToEmpty(stPath)),
159 lCurrentDir=wcslen(NullToEmpty(stCurrentDir)),
160 lFullPath=lPath+lCurrentDir;
162 if (lFullPath > 0)
164 strDest.Clear();
165 LPCWSTR pstPath = nullptr, pstCurrentDir = nullptr;
166 bool blIgnore = false;
167 size_t PathOffset=0;
168 PATH_PFX_TYPE PathType=Point2Root(stPath,PathOffset);
169 pstPath=stPath+PathOffset;
171 switch (PathType)
173 case PPT_NONE: //"abc"
175 pstCurrentDir=stCurrentDir;
177 break;
178 case PPT_DRIVE: //"C:" or "C:abc"
180 WCHAR DriveVar[]={L'=',*stPath,L':',L'\0'};
181 FARString strValue;
183 if (apiGetEnvironmentVariable(DriveVar,strValue))
185 strDest=strValue;
187 else
189 if (Upper(*stPath)==Upper(*stCurrentDir))
191 strDest=stCurrentDir;
193 else
195 strDest=DriveVar+1;
199 AddEndSlash(strDest);
201 break;
202 case PPT_ROOT: //"\" or "\abc"
204 if (stCurrentDir)
206 size_t PathOffset=0;
208 if (Point2Root(stCurrentDir,PathOffset)!=PPT_NONE)
210 strDest=FARString(stCurrentDir,PathOffset);
214 break;
215 case PPT_PREFIX: //"C:\abc"
217 pstPath=stPath;
219 break;
220 case PPT_NT: //"\\?\abc"
222 blIgnore=true;
223 pstPath=stPath;
225 break;
228 if (pstCurrentDir)
230 strDest+=pstCurrentDir;
231 AddEndSlash(strDest);
234 if (pstPath)
236 strDest+=pstPath;
239 if (!blIgnore)
240 MixToFullPath(strDest);
242 return true;
245 return false;*/
249 Преобразует Src в полный РЕАЛЬНЫЙ путь с учетом reparse point.
250 Note that Src can be partially non-existent.
252 void ConvertNameToReal(const wchar_t *Src, FARString &strDest)
254 char buf[PATH_MAX + 1];
255 std::string s = Wide2MB(Src);
256 if (*Src == GOOD_SLASH) {
257 std::string cutoff;
258 for (;;) {
259 if (sdc_realpath(s.c_str(), buf)) {
260 buf[sizeof(buf) - 1] = 0;
261 if (strcmp(buf, s.c_str()) != 0) {
262 strDest = buf;
263 if (!cutoff.empty())
264 strDest.Append(cutoff.c_str());
265 return;
267 break;
270 size_t p = s.rfind(GOOD_SLASH);
271 if (p == std::string::npos || p == 0)
272 break;
273 cutoff.insert(0, s.c_str() + p);
274 s.resize(p);
276 } else {
277 if (sdc_realpath(s.c_str(), buf)) {
278 if (strcmp(buf, s.c_str()) != 0) {
279 strDest = buf;
280 return;
282 } else {
283 ssize_t r = sdc_readlink(s.c_str(), buf, sizeof(buf) - 1);
284 if (r > 0 && r < (ssize_t)sizeof(buf) && buf[0]) {
285 buf[r] = 0;
286 if (buf[0] != GOOD_SLASH) {
287 strDest = s;
288 CutToSlash(strDest);
289 strDest+= buf;
290 } else
291 strDest = buf;
292 ConvertNameToFull(strDest);
293 return;
297 strDest = Src;
300 bool ReadSymlink(const wchar_t *lnk, FARString &dest)
302 char buf[PATH_MAX + 1];
303 const auto &lnk_mb = Wide2MB(lnk);
304 ssize_t r = sdc_readlink(lnk_mb.c_str(), buf, sizeof(buf) - 1);
305 if (r < 0 || r >= (ssize_t)sizeof(buf)) {
306 fprintf(stderr, "%s(%ls): error %u\n", __FUNCTION__, lnk, errno);
307 return false;
309 buf[r] = 0;
310 dest = buf;
311 return true;
314 // Косметические преобразования строки пути.
315 // CheckFullPath используется в FCTL_SET[ANOTHER]PANELDIR
316 FARString &PrepareDiskPath(FARString &strPath, bool CheckFullPath)
318 // elevation not required during cosmetic operation
319 if (!strPath.IsEmpty()) {
320 if (CheckFullPath && strPath[0] != LGOOD_SLASH) {
321 ConvertNameToFull(strPath, strPath);
326 if (strPath.At(1)==L':' || (strPath.At(0)==L'/' && strPath.At(1)==L'/'))
327 if (!strPath.IsEmpty())
330 bool DoubleSlash = strPath.At(1)==L'/';
331 while(ReplaceStrings(strPath,L"//",L"/"));
332 if(DoubleSlash)
334 strPath = "/"+strPath;
337 if (CheckFullPath)
339 ConvertNameToFull(strPath,strPath);
340 size_t FullLen=strPath.GetLength();
341 wchar_t *lpwszPath=strPath.GetBuffer(),*Src=lpwszPath;
343 if (IsLocalPath(lpwszPath))
345 Src+=2;
347 if (IsSlash(*Src))
348 Src++;
351 if (*Src)
353 wchar_t *Dst=Src;
355 for (wchar_t c=*Src; ; Src++,c=*Src)
357 if (IsSlash(c) || (!c && !IsSlash(*(Src-1))))
359 *Src=0;
360 FAR_FIND_DATA_EX fd;
361 BOOL find=apiGetFindDataEx(lpwszPath,fd);
362 *Src=c;
364 if (find)
366 size_t n=fd.strFileName.GetLength();
367 int n1=(int)(n-(Src-Dst));
369 if (n1>0)
371 size_t dSrc=Src-lpwszPath,dDst=Dst-lpwszPath;
372 strPath.ReleaseBuffer(FullLen);
373 lpwszPath=strPath.GetBuffer(FullLen+n1+1);
374 Src=lpwszPath+dSrc;
375 Dst=lpwszPath+dDst;
378 if (n1)
380 wmemmove(Src+n1,Src,FullLen-(Src-lpwszPath)+1);
381 Src+=n1;
382 FullLen+=n1;
385 wmemcpy(Dst,fd.strFileName,n);
388 if (c)
390 Dst=Src+1;
394 if (!*Src)
395 break;
399 strPath.ReleaseBuffer(FullLen);
402 wchar_t *lpwszPath = strPath.GetBuffer();
404 if (lpwszPath[0]==L'/' && lpwszPath[1]==L'/')
406 if (IsLocalPrefixPath(lpwszPath))
408 lpwszPath[4] = Upper(lpwszPath[4]);
410 else
412 wchar_t *ptr=&lpwszPath[2];
414 while (*ptr && !IsSlash(*ptr))
416 *ptr=Upper(*ptr);
417 ptr++;
421 else
423 lpwszPath[0]=Upper(lpwszPath[0]);
426 strPath.ReleaseBuffer(strPath.GetLength());
430 return strPath;
433 void ConvertNameToFull(const wchar_t *lpwszSrc, FARString &strDest)
435 if (*lpwszSrc != GOOD_SLASH) {
436 FARString strCurDir;
437 apiGetCurrentDirectory(strCurDir);
438 FARString strSrc = lpwszSrc;
439 MixToFullPath(strSrc, strDest, strCurDir);
440 } else {
441 strDest = lpwszSrc;
442 MixToFullPath(strDest);
446 void ConvertNameToFull(FARString &strSrcDest)
448 ConvertNameToFull(strSrcDest, strSrcDest);
451 void ConvertHomePrefixInPath(FARString &strFileName)
453 if (strFileName.GetLength() > 1 && strFileName[0] == L'~' && strFileName[1] == GOOD_SLASH) {
454 strFileName.Replace(0, 1, FARString(GetMyHome()));