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 {
0 commit comments