4 Функций для преобразования имен файлов/путей.
7 Copyright (c) 1996 Eugene Roshal
8 Copyright (c) 2000 Far Group
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions
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"
39 #include "pathmix.hpp"
40 #include "drivemix.hpp"
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
];) {
58 if (IsDot(pstPath
[m
]) && (!m
|| IsSlash(pstPath
[m
- 1]))) {
62 switch (pstPath
[m
+ 1]) {
67 for (pstSrc
= pstPath
+ m
+ 2, pstDst
= pstPath
+ m
; *pstSrc
; pstSrc
++, pstDst
++) {
74 // fragment "." at the end
79 // fragment "..\" or "../" or ".." at the end
81 if (IsSlash(pstPath
[m
+ 2]) || !pstPath
[m
+ 2]) {
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 "../"
92 for (pstSrc
= pstPath
+ m
+ 3, pstDst
= pstPath
+ n
; *pstSrc
; pstSrc
++,
99 // fragment ".." at the end
102 } else { // dont go to nowhere
103 pstPath
[0] = GOOD_SLASH
;
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
) {
125 MixToFullPath(strDest
);
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
;
146 while (stPath
[0] == '.' && (!stPath
[1] || stPath
[1] == GOOD_SLASH
)) {
148 if (*stPath
== GOOD_SLASH
)
154 MixToFullPath(strDest
);
158 size_t lPath=wcslen(NullToEmpty(stPath)),
159 lCurrentDir=wcslen(NullToEmpty(stCurrentDir)),
160 lFullPath=lPath+lCurrentDir;
165 LPCWSTR pstPath = nullptr, pstCurrentDir = nullptr;
166 bool blIgnore = false;
168 PATH_PFX_TYPE PathType=Point2Root(stPath,PathOffset);
169 pstPath=stPath+PathOffset;
173 case PPT_NONE: //"abc"
175 pstCurrentDir=stCurrentDir;
178 case PPT_DRIVE: //"C:" or "C:abc"
180 WCHAR DriveVar[]={L'=',*stPath,L':',L'\0'};
183 if (apiGetEnvironmentVariable(DriveVar,strValue))
189 if (Upper(*stPath)==Upper(*stCurrentDir))
191 strDest=stCurrentDir;
199 AddEndSlash(strDest);
202 case PPT_ROOT: //"\" or "\abc"
208 if (Point2Root(stCurrentDir,PathOffset)!=PPT_NONE)
210 strDest=FARString(stCurrentDir,PathOffset);
215 case PPT_PREFIX: //"C:\abc"
220 case PPT_NT: //"\\?\abc"
230 strDest+=pstCurrentDir;
231 AddEndSlash(strDest);
240 MixToFullPath(strDest);
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
) {
259 if (sdc_realpath(s
.c_str(), buf
)) {
260 buf
[sizeof(buf
) - 1] = 0;
261 if (strcmp(buf
, s
.c_str()) != 0) {
264 strDest
.Append(cutoff
.c_str());
270 size_t p
= s
.rfind(GOOD_SLASH
);
271 if (p
== std::string::npos
|| p
== 0)
273 cutoff
.insert(0, s
.c_str() + p
);
277 if (sdc_realpath(s
.c_str(), buf
)) {
278 if (strcmp(buf
, s
.c_str()) != 0) {
283 ssize_t r
= sdc_readlink(s
.c_str(), buf
, sizeof(buf
) - 1);
284 if (r
> 0 && r
< (ssize_t
)sizeof(buf
) && buf
[0]) {
286 if (buf
[0] != GOOD_SLASH
) {
292 ConvertNameToFull(strDest
);
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
);
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"/"));
334 strPath = "/"+strPath;
339 ConvertNameToFull(strPath,strPath);
340 size_t FullLen=strPath.GetLength();
341 wchar_t *lpwszPath=strPath.GetBuffer(),*Src=lpwszPath;
343 if (IsLocalPath(lpwszPath))
355 for (wchar_t c=*Src; ; Src++,c=*Src)
357 if (IsSlash(c) || (!c && !IsSlash(*(Src-1))))
361 BOOL find=apiGetFindDataEx(lpwszPath,fd);
366 size_t n=fd.strFileName.GetLength();
367 int n1=(int)(n-(Src-Dst));
371 size_t dSrc=Src-lpwszPath,dDst=Dst-lpwszPath;
372 strPath.ReleaseBuffer(FullLen);
373 lpwszPath=strPath.GetBuffer(FullLen+n1+1);
380 wmemmove(Src+n1,Src,FullLen-(Src-lpwszPath)+1);
385 wmemcpy(Dst,fd.strFileName,n);
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]);
412 wchar_t *ptr=&lpwszPath[2];
414 while (*ptr && !IsSlash(*ptr))
423 lpwszPath[0]=Upper(lpwszPath[0]);
426 strPath.ReleaseBuffer(strPath.GetLength());
433 void ConvertNameToFull(const wchar_t *lpwszSrc
, FARString
&strDest
)
435 if (*lpwszSrc
!= GOOD_SLASH
) {
437 apiGetCurrentDirectory(strCurDir
);
438 FARString strSrc
= lpwszSrc
;
439 MixToFullPath(strSrc
, strDest
, strCurDir
);
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()));