Skip to content

Commit a05762e

Browse files
committed
v3.0 ndkr16b clang5.0 support.
1 parent 0381300 commit a05762e

File tree

5 files changed

+100
-69
lines changed

5 files changed

+100
-69
lines changed

libwsls/libwsls.cpp

Lines changed: 73 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// TODO: reference any additional headers you need in STDAFX.H
66
// and not in this file
7-
// version: V2.2 2018.9.10
7+
// version: V2.2 2018.9.15 r6
88
#include <Shlwapi.h>
99
#include "libwsls.h"
1010
#pragma comment(lib, "Shlwapi.lib")
@@ -176,41 +176,41 @@ namespace wsls {
176176
return data;
177177
}
178178

179-
int writeFileData(const char* fileName, const std::string& content, bool append)
180-
{
181-
auto styledPath = makeStyledPath(fileName);
182-
if (styledPath.empty()) {
183-
styledPath = transcode$IL(fileName);
184-
}
185-
HANDLE hFile = CreateFileW(styledPath.c_str(),
186-
GENERIC_READ | GENERIC_WRITE,
187-
FILE_SHARE_READ,
188-
NULL,
189-
OPEN_ALWAYS,
190-
0,
191-
nullptr);
192-
193-
if (hFile != INVALID_HANDLE_VALUE) {
194-
LARGE_INTEGER li;
195-
::GetFileSizeEx(hFile, &li);
196-
auto size = li.QuadPart;
197-
if (size > 0 && append) {
198-
li.QuadPart = 0;
199-
::SetFilePointerEx(hFile, li, nullptr, FILE_END);
200-
}
201-
else {
202-
::SetEndOfFile(hFile);
179+
int writeFileData(const char* fileName, const std::string& content, bool append)
180+
{
181+
auto styledPath = makeStyledPath(fileName);
182+
if (styledPath.empty()) {
183+
styledPath = transcode$IL(fileName);
203184
}
185+
HANDLE hFile = CreateFileW(styledPath.c_str(),
186+
GENERIC_READ | GENERIC_WRITE,
187+
FILE_SHARE_READ,
188+
NULL,
189+
OPEN_ALWAYS,
190+
0,
191+
nullptr);
192+
193+
if (hFile != INVALID_HANDLE_VALUE) {
194+
LARGE_INTEGER li;
195+
::GetFileSizeEx(hFile, &li);
196+
auto size = li.QuadPart;
197+
if (size > 0 && append) {
198+
li.QuadPart = 0;
199+
::SetFilePointerEx(hFile, li, nullptr, FILE_END);
200+
}
201+
else {
202+
::SetEndOfFile(hFile);
203+
}
204204

205-
DWORD bytesToWrite = 0;
206-
WriteFile(hFile, content.c_str(), static_cast<DWORD>(content.size()), &bytesToWrite, nullptr);
205+
DWORD bytesToWrite = 0;
206+
WriteFile(hFile, content.c_str(), static_cast<DWORD>(content.size()), &bytesToWrite, nullptr);
207207

208-
CloseHandle(hFile);
209-
return 0;
210-
}
208+
CloseHandle(hFile);
209+
return 0;
210+
}
211211

212-
return GetLastError();
213-
}
212+
return GetLastError();
213+
}
214214

215215
void convertPathToWinStyle(std::string& path, size_t offset)
216216
{
@@ -253,29 +253,6 @@ int writeFileData(const char* fileName, const std::string& content, bool append)
253253
return buffer;
254254
}
255255

256-
std::wstring makeStyledPath(const char* _FileName)
257-
{
258-
if (_FileName != nullptr && strlen(_FileName) > LONG_PATH_THRESHOLD && !isStyledLongPath(_FileName))
259-
{
260-
auto wFileName = transcode$IL(_FileName);
261-
DWORD nBufferLength = GetFullPathNameW(wFileName.c_str(), 0, nullptr, nullptr);
262-
if (nBufferLength > 0) {
263-
std::wstring uncPath(nBufferLength + UNC_PREFIX_LEN, '\0');
264-
memcpy(&uncPath.front(), UNC_PREFIXW, UNC_PREFIX_LEN << 1);
265-
266-
if (GetFullPathNameW(wFileName.c_str(), nBufferLength, &uncPath.front() + UNC_PREFIX_LEN, nullptr) < nBufferLength)
267-
{
268-
#if defined(_DEBUG)
269-
_wsystem(sfmt(LR"(echo "wsLongPath.dll: convert NON-UNC long path to UNC Path: %s")", uncPath.c_str()).c_str());
270-
#endif
271-
return uncPath;
272-
}
273-
}
274-
}
275-
276-
return L"";
277-
}
278-
279256
bool isFileExists(const wchar_t* _Path)
280257
{
281258
auto attr = ::GetFileAttributesW(_Path);
@@ -295,16 +272,23 @@ int writeFileData(const char* fileName, const std::string& content, bool append)
295272

296273
}
297274

298-
std::wstring makeStyledPath(const wchar_t* _FileName)
299-
{
300-
if (_FileName != nullptr && wcslen(_FileName) > LONG_PATH_THRESHOLD && !isStyledLongPath(_FileName))
301-
{
302-
DWORD nBufferLength = GetFullPathNameW(_FileName, 0, nullptr, nullptr);
275+
static std::wstring makeStyledPathInternal(bool uncPrefix, const wchar_t* _FileName)
276+
{ /*
277+
clang 5.0.2
278+
android-ndk-r16b clang 5.0.300080
279+
will simply add prefix R"(\\?\)" for too long path,
280+
but it's not ok, we should convert it to windows styled path for windows File APIs happy.
281+
*/
282+
if (!uncPrefix || !isStyledPath(_FileName + UNC_PREFIX_LEN)) {
283+
size_t offset = UNC_PREFIX_LEN;
284+
if (uncPrefix) offset = 0;
285+
286+
int nBufferLength = GetFullPathNameW(_FileName, 0, nullptr, nullptr);
303287
if (nBufferLength > 0) {
304-
std::wstring uncPath(nBufferLength + UNC_PREFIX_LEN, '\0');
305-
memcpy(&uncPath.front(), UNC_PREFIXW, UNC_PREFIX_LEN << 1);
288+
std::wstring uncPath(nBufferLength + offset, '\0');
289+
memcpy(&uncPath.front(), UNC_PREFIXW, offset << 1);
306290

307-
if (GetFullPathNameW(_FileName, nBufferLength, &uncPath.front() + UNC_PREFIX_LEN, nullptr) < nBufferLength)
291+
if (GetFullPathNameW(_FileName, nBufferLength, &uncPath.front() + offset, nullptr) < nBufferLength)
308292
{
309293
#if defined(_DEBUG)
310294
_wsystem(sfmt(LR"(echo "wsLongPath.dll: convert NON-UNC long path to UNC Path: %s")", uncPath.c_str()).c_str());
@@ -317,12 +301,37 @@ int writeFileData(const char* fileName, const std::string& content, bool append)
317301
return L"";
318302
}
319303

304+
std::wstring makeStyledPath(const char* _FileName)
305+
{
306+
bool uncPrefix = hasUNCPrefix(_FileName);
307+
if ((_FileName != nullptr && strlen(_FileName) > LONG_PATH_THRESHOLD)
308+
|| uncPrefix) // If already prefix, we need to fix path to styled windows path.
309+
{
310+
auto wFileName = transcode$IL(_FileName);
311+
return makeStyledPathInternal(uncPrefix, wFileName.c_str());
312+
}
313+
314+
return L"";
315+
}
316+
317+
std::wstring makeStyledPath(const wchar_t* _FileName)
318+
{
319+
bool uncPrefix = hasUNCPrefix(_FileName);
320+
if ((_FileName != nullptr && wcslen(_FileName) > LONG_PATH_THRESHOLD)
321+
|| uncPrefix) // If already prefix, we need to fix path to styled windows path.
322+
{
323+
return makeStyledPathInternal(uncPrefix, _FileName);
324+
}
325+
326+
return L"";
327+
}
328+
320329
template<typename _Elem, typename _Fty> inline
321330
void dir_split(_Elem* s, const _Fty& op)
322331
{
323332
_Elem* _Start = s; // the start of every string
324333
_Elem* _Ptr = s; // source string iterator
325-
if (isStyledLongPath(_Ptr))
334+
if (hasUNCPrefix(_Ptr))
326335
_Ptr += (sizeof(UNC_PREFIX) - 1);
327336
while (*_Ptr != '\0')
328337
{

libwsls/libwsls.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,34 @@ namespace wsls {
3535
int mkdir(std::wstring&& _Path);
3636

3737
template<typename _Elem> inline
38-
bool isStyledLongPath(const _Elem* _Path)
38+
bool hasUNCPrefix(const _Elem* _Path)
3939
{
4040
return _Path[0] == '\\' && _Path[1] == '\\' && _Path[2] == '?' && _Path[3] == '\\';
4141
}
42+
43+
template<typename _Elem> inline
44+
bool isStyledPath(const _Elem* _Path)
45+
{
46+
int slashs = 0;
47+
int dots = 0;
48+
while (*_Path) {
49+
switch (*_Path) {
50+
case '/': return false;
51+
case '\\':
52+
++slashs;
53+
if (dots > 0) return false;
54+
break;
55+
case '.':
56+
++dots;
57+
if (slashs > 0) return false;
58+
default:
59+
if(slashs > 0) --slashs;
60+
if(dots > 0) --dots;
61+
break;
62+
}
63+
++_Path;
64+
}
65+
66+
return true;
67+
}
4268
};

wsLongPaths/wsLongPaths.cpp

1.93 KB
Binary file not shown.

wsls-echo/wsls-echo.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ int main(int argc, char** argv)
7373
{
7474
redirectPath[pathend] = endCh;
7575
// Try redirect again
76-
if (!wsls::isFileExists(L"D:\\workspace\\ndk-workaround.log"))
77-
{
78-
wsls::writeFileData("D:\\workspace\\ndk-workaround.log", content);
79-
}
8076
iRet = wsls::writeFileData(redirectPath.c_str(), content, redirectType == 2);
8177
}
8278
}

wsls-md/wsls-md.cpp

-8 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)