forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.cpp
More file actions
758 lines (622 loc) · 23.6 KB
/
Helpers.cpp
File metadata and controls
758 lines (622 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include <sys/stat.h>
//TODO: x-plat definitions
#ifdef _WIN32
typedef char16 TTDHostCharType;
typedef struct _wfinddata_t TTDHostFileInfo;
typedef intptr_t TTDHostFindHandle;
typedef struct _stat TTDHostStatType;
#define TTDHostPathSeparator _u("\\")
#define TTDHostPathSeparatorChar _u('\\')
#define TTDHostFindInvalid -1
size_t TTDHostStringLength(const TTDHostCharType* str)
{
return wcslen(str);
}
void TTDHostInitEmpty(TTDHostCharType* dst)
{
dst[0] = _u('\0');
}
void TTDHostInitFromUriBytes(TTDHostCharType* dst, const byte* uriBytes, size_t uriBytesLength)
{
memcpy_s(dst, MAX_PATH * sizeof(TTDHostCharType), uriBytes, uriBytesLength);
dst[uriBytesLength / sizeof(TTDHostCharType)] = _u('\0');
AssertMsg(wcslen(dst) == (uriBytesLength / sizeof(TTDHostCharType)), "We have an null in the uri or our math is wrong somewhere.");
}
void TTDHostAppend(TTDHostCharType* dst, size_t dstLength, const TTDHostCharType* src)
{
size_t srcLength = TTDHostStringLength(src);
size_t dpos = TTDHostStringLength(dst);
Helpers::TTReportLastIOErrorAsNeeded(dpos < dstLength, "The end of the string already exceeds the buffer");
size_t srcByteLength = srcLength * sizeof(TTDHostCharType);
size_t dstRemainingByteLength = (dstLength - dpos - 1) * sizeof(TTDHostCharType);
Helpers::TTReportLastIOErrorAsNeeded(srcByteLength <= dstRemainingByteLength, "The source string must be able to fit within the destination buffer");
memcpy_s(dst + dpos, dstRemainingByteLength, src, srcByteLength);
dst[dpos + srcLength] = _u('\0');
}
void TTDHostAppendWChar(TTDHostCharType* dst, size_t dstLength, const wchar* src)
{
size_t srcLength = wcslen(src);
size_t dpos = TTDHostStringLength(dst);
Helpers::TTReportLastIOErrorAsNeeded(dpos < dstLength, "The end of the string already exceeds the buffer");
size_t dstRemainingLength = dstLength - dpos - 1;
Helpers::TTReportLastIOErrorAsNeeded(srcLength <= dstRemainingLength, "The source string must be able to fit within the destination buffer");
for(size_t i = 0; i < srcLength; ++i)
{
dst[dpos + i] = (char16)src[i];
}
dst[dpos + srcLength] = _u('\0');
}
void TTDHostAppendAscii(TTDHostCharType* dst, size_t dstLength, const char* src)
{
size_t srcLength = strlen(src);
size_t dpos = TTDHostStringLength(dst);
Helpers::TTReportLastIOErrorAsNeeded(dpos < dstLength, "The end of the string already exceeds the buffer");
size_t dstRemainingLength = dstLength - dpos - 1;
Helpers::TTReportLastIOErrorAsNeeded(srcLength <= dstRemainingLength, "The source string must be able to fit within the destination buffer");
for(size_t i = 0; i < srcLength; ++i)
{
dst[dpos + i] = (char16)src[i];
}
dst[dpos + srcLength] = _u('\0');
}
void TTDHostBuildCurrentExeDirectory(TTDHostCharType* path, size_t pathBufferLength)
{
wchar exePath[MAX_PATH];
GetModuleFileName(NULL, exePath, MAX_PATH);
size_t i = wcslen(exePath) - 1;
while(exePath[i] != TTDHostPathSeparatorChar)
{
--i;
}
exePath[i + 1] = _u('\0');
TTDHostAppendWChar(path, pathBufferLength, exePath);
}
JsTTDStreamHandle TTDHostOpen(const TTDHostCharType* path, bool isWrite)
{
FILE* res = nullptr;
_wfopen_s(&res, path, isWrite ? _u("w+b") : _u("r+b"));
return (JsTTDStreamHandle)res;
}
#define TTDHostCWD(dst, dstLength) _wgetcwd(dst, dstLength)
#define TTDDoPathInit(dst, dstLength)
#define TTDHostTok(opath, TTDHostPathSeparator, context) wcstok_s(opath, TTDHostPathSeparator, context)
#define TTDHostStat(cpath, statVal) _wstat(cpath, statVal)
#define TTDHostMKDir(cpath) _wmkdir(cpath)
#define TTDHostCHMod(cpath, flags) _wchmod(cpath, flags)
#define TTDHostRMFile(cpath) _wremove(cpath)
#define TTDHostFindFirst(strPattern, FileInformation) _wfindfirst(strPattern, FileInformation)
#define TTDHostFindNext(hFile, FileInformation) _wfindnext(hFile, FileInformation)
#define TTDHostFindClose(hFile) _findclose(hFile)
#define TTDHostDirInfoName(FileInformation) FileInformation.name
#define TTDHostRead(buff, size, handle) fread_s(buff, size, 1, size, (FILE*)handle);
#define TTDHostWrite(buff, size, handle) fwrite(buff, 1, size, (FILE*)handle)
#else
#include <unistd.h>
#include <cstring>
#include <libgen.h>
#include <dirent.h>
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
typedef utf8char_t TTDHostCharType;
typedef struct dirent* TTDHostFileInfo;
typedef DIR* TTDHostFindHandle;
typedef struct stat TTDHostStatType;
#define TTDHostPathSeparator ((TTDHostCharType*)"/")
#define TTDHostPathSeparatorChar ((TTDHostCharType)'/')
#define TTDHostFindInvalid nullptr
#define TTDHostCharConvert(X) ((char*)X)
#define TTDHostUtf8CharConvert(X) ((TTDHostCharType*)X)
size_t TTDHostStringLength(const TTDHostCharType* str)
{
return strlen(TTDHostCharConvert(str));
}
void TTDHostInitEmpty(TTDHostCharType* dst)
{
dst[0] = '\0';
}
void TTDHostInitFromUriBytes(TTDHostCharType* dst, const byte* uriBytes, size_t uriBytesLength)
{
memcpy_s(dst, MAX_PATH * sizeof(TTDHostCharType), uriBytes, uriBytesLength);
dst[uriBytesLength / sizeof(TTDHostCharType)] = '\0';
AssertMsg(TTDHostStringLength(dst) == (uriBytesLength / sizeof(TTDHostCharType)), "We have an null in the uri or our math is wrong somewhere.");
}
void TTDHostAppend(TTDHostCharType* dst, size_t dstLength, const TTDHostCharType* src)
{
size_t srcLength = TTDHostStringLength(src);
size_t dpos = TTDHostStringLength(dst);
Helpers::TTReportLastIOErrorAsNeeded(dpos < dstLength, "The end of the string already exceeds the buffer");
size_t srcByteLength = srcLength * sizeof(TTDHostCharType);
size_t dstRemainingByteLength = (dstLength - dpos - 1) * sizeof(TTDHostCharType);
Helpers::TTReportLastIOErrorAsNeeded(srcByteLength <= dstRemainingByteLength, "The source string must be able to fit within the destination buffer");
memcpy_s(dst + dpos, dstRemainingByteLength, src, srcByteLength);
dst[dpos + srcLength] = '\0';
}
void TTDHostAppendWChar(TTDHostCharType* dst, size_t dstLength, const wchar* src)
{
size_t srcLength = wcslen(src);
size_t dpos = TTDHostStringLength(dst);
Helpers::TTReportLastIOErrorAsNeeded(dpos < dstLength, "The end of the string already exceeds the buffer");
size_t dstRemainingLength = dstLength - dpos - 1;
Helpers::TTReportLastIOErrorAsNeeded(srcLength <= dstRemainingLength, "The source string must be able to fit within the destination buffer");
// TODO - analyze this function further
utf8::EncodeIntoAndNullTerminate(dst + dpos, src, srcLength);
}
void TTDHostAppendAscii(TTDHostCharType* dst, size_t dstLength, const char* src)
{
size_t srcLength = strlen(src);
size_t dpos = TTDHostStringLength(dst);
Helpers::TTReportLastIOErrorAsNeeded(dpos < dstLength, "The end of the string already exceeds the buffer");
size_t srcByteLength = srcLength * sizeof(TTDHostCharType);
size_t dstRemainingByteLength = (dstLength - dpos - 1) * sizeof(TTDHostCharType);
Helpers::TTReportLastIOErrorAsNeeded(srcByteLength <= dstRemainingByteLength, "The source string must be able to fit within the destination buffer");
memcpy_s(dst + dpos, dstRemainingByteLength, src, srcByteLength);
dst[dpos + srcLength] = '\0';
}
void TTDHostBuildCurrentExeDirectory(TTDHostCharType* path, size_t pathBufferLength)
{
TTDHostCharType exePath[MAX_PATH];
//TODO: xplattodo move this logic to PAL
#ifdef __APPLE__
uint32_t tmpPathSize = sizeof(exePath);
_NSGetExecutablePath(TTDHostCharConvert(exePath), &tmpPathSize);
size_t len = TTDHostStringLength(exePath);
#else
size_t len = readlink("/proc/self/exe", TTDHostCharConvert(exePath), MAX_PATH);
#endif
size_t i = len - 1;
while(exePath[i] != TTDHostPathSeparatorChar)
{
--i;
}
exePath[i + 1] = '\0';
TTDHostAppend(path, pathBufferLength, exePath);
}
JsTTDStreamHandle TTDHostOpen(const TTDHostCharType* path, bool isWrite)
{
return (JsTTDStreamHandle)fopen(TTDHostCharConvert(path), isWrite ? "w+b" : "r+b");
}
#define TTDHostCWD(dst, dstLength) TTDHostUtf8CharConvert(getcwd(TTDHostCharConvert(dst), dstLength))
#define TTDDoPathInit(dst, dstLength) TTDHostAppend(dst, dstLength, TTDHostPathSeparator)
#define TTDHostTok(opath, TTDHostPathSeparator, context) TTDHostUtf8CharConvert(strtok(TTDHostCharConvert(opath), TTDHostCharConvert(TTDHostPathSeparator)))
#define TTDHostStat(cpath, statVal) stat(TTDHostCharConvert(cpath), statVal)
#define TTDHostMKDir(cpath) mkdir(TTDHostCharConvert(cpath), 0777)
#define TTDHostCHMod(cpath, flags) chmod(TTDHostCharConvert(cpath), flags)
#define TTDHostRMFile(cpath) remove(TTDHostCharConvert(cpath))
#define TTDHostFindFirst(strPattern, FileInformation) opendir(TTDHostCharConvert(strPattern))
#define TTDHostFindNext(hFile, FileInformation) (*FileInformation = readdir(hFile))
#define TTDHostFindClose(hFile) closedir(hFile)
#define TTDHostDirInfoName(FileInformation) TTDHostUtf8CharConvert(FileInformation->d_name)
#define TTDHostRead(buff, size, handle) fread(buff, 1, size, (FILE*)handle)
#define TTDHostWrite(buff, size, handle) fwrite(buff, 1, size, (FILE*)handle)
#endif
HRESULT Helpers::LoadScriptFromFile(LPCSTR filename, LPCSTR& contents, UINT* lengthBytesOut /*= nullptr*/)
{
HRESULT hr = S_OK;
BYTE * pRawBytes = nullptr;
UINT lengthBytes = 0;
contents = nullptr;
FILE * file = NULL;
//
// Open the file as a binary file to prevent CRT from handling encoding, line-break conversions,
// etc.
//
if (fopen_s(&file, filename, "rb") != 0)
{
#ifdef _WIN32
DWORD lastError = GetLastError();
char16 wszBuff[512];
fprintf(stderr, "Error in opening file '%s' ", filename);
wszBuff[0] = 0;
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
nullptr,
lastError,
0,
wszBuff,
_countof(wszBuff),
nullptr))
{
fwprintf(stderr, _u(": %s"), wszBuff);
}
fwprintf(stderr, _u("\n"));
#elif defined(_POSIX_VERSION)
fprintf(stderr, "Error in opening file: ");
perror(filename);
#endif
IfFailGo(E_FAIL);
}
if (file != NULL)
{
// Determine the file length, in bytes.
fseek(file, 0, SEEK_END);
lengthBytes = ftell(file);
fseek(file, 0, SEEK_SET);
const size_t bufferLength = lengthBytes + sizeof(BYTE);
pRawBytes = (LPBYTE)malloc(bufferLength);
if (nullptr == pRawBytes)
{
fwprintf(stderr, _u("out of memory"));
IfFailGo(E_OUTOFMEMORY);
}
//
// Read the entire content as a binary block.
//
size_t readBytes = fread(pRawBytes, sizeof(BYTE), lengthBytes, file);
if (readBytes < lengthBytes * sizeof(BYTE))
{
IfFailGo(E_FAIL);
}
pRawBytes[lengthBytes] = 0; // Null terminate it. Could be UTF16
//
// Read encoding to make sure it's supported
//
// Warning: The UNICODE buffer for parsing is supposed to be provided by the host.
// This is not a complete read of the encoding. Some encodings like UTF7, UTF1, EBCDIC, SCSU, BOCU could be
// wrongly classified as ANSI
//
{
C_ASSERT(sizeof(WCHAR) == 2);
if (bufferLength > 2)
{
if ((pRawBytes[0] == 0xFE && pRawBytes[1] == 0xFF) ||
(pRawBytes[0] == 0xFF && pRawBytes[1] == 0xFE) ||
(bufferLength > 4 && pRawBytes[0] == 0x00 && pRawBytes[1] == 0x00 &&
((pRawBytes[2] == 0xFE && pRawBytes[3] == 0xFF) ||
(pRawBytes[2] == 0xFF && pRawBytes[3] == 0xFE))))
{
// unicode unsupported
fwprintf(stderr, _u("unsupported file encoding. Only ANSI and UTF8 supported"));
IfFailGo(E_UNEXPECTED);
}
}
}
}
contents = reinterpret_cast<LPCSTR>(pRawBytes);
Error:
if (SUCCEEDED(hr))
{
if (lengthBytesOut)
{
*lengthBytesOut = lengthBytes;
}
}
if (file != NULL)
{
fclose(file);
}
if (pRawBytes && reinterpret_cast<LPCSTR>(pRawBytes) != contents)
{
free(pRawBytes);
}
return hr;
}
LPCWSTR Helpers::JsErrorCodeToString(JsErrorCode jsErrorCode)
{
bool hasException = false;
ChakraRTInterface::JsHasException(&hasException);
if (hasException)
{
WScriptJsrt::PrintException("", JsErrorScriptException);
}
switch (jsErrorCode)
{
case JsNoError:
return _u("JsNoError");
break;
case JsErrorInvalidArgument:
return _u("JsErrorInvalidArgument");
break;
case JsErrorNullArgument:
return _u("JsErrorNullArgument");
break;
case JsErrorNoCurrentContext:
return _u("JsErrorNoCurrentContext");
break;
case JsErrorInExceptionState:
return _u("JsErrorInExceptionState");
break;
case JsErrorNotImplemented:
return _u("JsErrorNotImplemented");
break;
case JsErrorWrongThread:
return _u("JsErrorWrongThread");
break;
case JsErrorRuntimeInUse:
return _u("JsErrorRuntimeInUse");
break;
case JsErrorBadSerializedScript:
return _u("JsErrorBadSerializedScript");
break;
case JsErrorInDisabledState:
return _u("JsErrorInDisabledState");
break;
case JsErrorCannotDisableExecution:
return _u("JsErrorCannotDisableExecution");
break;
case JsErrorHeapEnumInProgress:
return _u("JsErrorHeapEnumInProgress");
break;
case JsErrorOutOfMemory:
return _u("JsErrorOutOfMemory");
break;
case JsErrorScriptException:
return _u("JsErrorScriptException");
break;
case JsErrorScriptCompile:
return _u("JsErrorScriptCompile");
break;
case JsErrorScriptTerminated:
return _u("JsErrorScriptTerminated");
break;
case JsErrorFatal:
return _u("JsErrorFatal");
break;
default:
return _u("<unknown>");
break;
}
}
void Helpers::LogError(__in __nullterminated const char16 *msg, ...)
{
va_list args;
va_start(args, msg);
wprintf(_u("ERROR: "));
vfwprintf(stderr, msg, args);
wprintf(_u("\n"));
fflush(stdout);
va_end(args);
}
HRESULT Helpers::LoadBinaryFile(LPCSTR filename, LPCSTR& contents, UINT& lengthBytes, bool printFileOpenError)
{
HRESULT hr = S_OK;
contents = nullptr;
lengthBytes = 0;
size_t result;
FILE * file;
//
// Open the file as a binary file to prevent CRT from handling encoding, line-break conversions,
// etc.
//
if (fopen_s(&file, filename, "rb") != 0)
{
if (printFileOpenError)
{
#ifdef _WIN32
DWORD lastError = GetLastError();
char16 wszBuff[512];
fprintf(stderr, "Error in opening file '%s' ", filename);
wszBuff[0] = 0;
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
nullptr,
lastError,
0,
wszBuff,
_countof(wszBuff),
nullptr))
{
fwprintf(stderr, _u(": %s"), wszBuff);
}
#endif
fprintf(stderr, "\n");
IfFailGo(E_FAIL);
}
else
{
return E_FAIL;
}
}
// file will not be nullptr if _wfopen_s succeeds
__analysis_assume(file != nullptr);
//
// Determine the file length, in bytes.
//
fseek(file, 0, SEEK_END);
lengthBytes = ftell(file);
fseek(file, 0, SEEK_SET);
contents = (LPCSTR)HeapAlloc(GetProcessHeap(), 0, lengthBytes);
if (nullptr == contents)
{
fwprintf(stderr, _u("out of memory"));
IfFailGo(E_OUTOFMEMORY);
}
//
// Read the entire content as a binary block.
//
result = fread((void*)contents, sizeof(char), lengthBytes, file);
if (result != lengthBytes)
{
fwprintf(stderr, _u("Read error"));
IfFailGo(E_FAIL);
}
fclose(file);
Error:
if (contents && FAILED(hr))
{
HeapFree(GetProcessHeap(), 0, (void*)contents);
contents = nullptr;
}
return hr;
}
void Helpers::TTReportLastIOErrorAsNeeded(BOOL ok, const char* msg)
{
if(!ok)
{
#ifdef _WIN32
DWORD lastError = GetLastError();
LPTSTR pTemp = NULL;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY, NULL, lastError, 0, (LPTSTR)&pTemp, 0, NULL);
fwprintf(stderr, _u("Error is: %s\n"), pTemp);
#else
fprintf(stderr, "Error is: %i %s\n", errno, strerror(errno));
#endif
fprintf(stderr, "Message is: %s\n", msg);
AssertMsg(false, "IO Error!!!");
exit(1);
}
}
void Helpers::CreateDirectoryIfNeeded(size_t uriByteLength, const byte* uriBytes)
{
TTDHostCharType opath[MAX_PATH];
TTDHostInitFromUriBytes(opath, uriBytes, uriByteLength);
TTDHostCharType cpath[MAX_PATH];
TTDHostInitEmpty(cpath);
TTDDoPathInit(cpath, MAX_PATH);
TTDHostStatType statVal;
TTDHostCharType* context = nullptr;
TTDHostCharType* token = TTDHostTok(opath, TTDHostPathSeparator, &context);
TTDHostAppend(cpath, MAX_PATH, token);
//At least 1 part of the path must exist so iterate until we find it
while(TTDHostStat(cpath, &statVal) == -1)
{
token = TTDHostTok(nullptr, TTDHostPathSeparator, &context);
TTDHostAppend(cpath, MAX_PATH, TTDHostPathSeparator);
TTDHostAppend(cpath, MAX_PATH, token);
}
//Now continue until we hit the part that doesn't exist (or the end of the path)
while(token != nullptr && TTDHostStat(cpath, &statVal) != -1)
{
token = TTDHostTok(nullptr, TTDHostPathSeparator, &context);
if(token != nullptr)
{
TTDHostAppend(cpath, MAX_PATH, TTDHostPathSeparator);
TTDHostAppend(cpath, MAX_PATH, token);
}
}
//Now if there is path left then continue build up the directory tree as we go
while(token != nullptr)
{
int success = TTDHostMKDir(cpath);
if(success != 0)
{
//we may fail because someone else created the directory -- that is ok
Helpers::TTReportLastIOErrorAsNeeded(errno != ENOENT, "Failed to create directory");
}
token = TTDHostTok(nullptr, TTDHostPathSeparator, &context);
if(token != nullptr)
{
TTDHostAppend(cpath, MAX_PATH, TTDHostPathSeparator);
TTDHostAppend(cpath, MAX_PATH, token);
}
}
}
void Helpers::CleanDirectory(size_t uriByteLength, const byte* uriBytes)
{
TTDHostFindHandle hFile;
TTDHostFileInfo FileInformation;
TTDHostCharType strPattern[MAX_PATH];
TTDHostInitFromUriBytes(strPattern, uriBytes, uriByteLength);
TTDHostAppendAscii(strPattern, MAX_PATH, "*.*");
hFile = TTDHostFindFirst(strPattern, &FileInformation);
if(hFile != TTDHostFindInvalid)
{
do
{
if(TTDHostDirInfoName(FileInformation)[0] != '.')
{
TTDHostCharType strFilePath[MAX_PATH];
TTDHostInitFromUriBytes(strFilePath, uriBytes, uriByteLength);
TTDHostAppend(strFilePath, MAX_PATH, TTDHostDirInfoName(FileInformation));
// Set file attributes
int statusch = TTDHostCHMod(strFilePath, S_IREAD | S_IWRITE);
Helpers::TTReportLastIOErrorAsNeeded(statusch == 0, "Failed to chmod directory");
int statusrm = TTDHostRMFile(strFilePath);
Helpers::TTReportLastIOErrorAsNeeded(statusrm == 0, "Failed to delete file directory");
}
} while(TTDHostFindNext(hFile, &FileInformation) != TTDHostFindInvalid);
// Close handle
TTDHostFindClose(hFile);
}
}
void Helpers::GetTTDDirectory(const wchar* curi, size_t* uriByteLength, byte* uriBytes)
{
TTDHostCharType turi[MAX_PATH];
TTDHostInitEmpty(turi);
if(curi[0] != _u('~'))
{
TTDHostCharType* status = TTDHostCWD(turi, MAX_PATH);
Helpers::TTReportLastIOErrorAsNeeded(status != nullptr, "Failed to chmod directory");
TTDHostAppend(turi, MAX_PATH, TTDHostPathSeparator);
TTDHostAppendWChar(turi, MAX_PATH, curi);
}
else
{
TTDHostBuildCurrentExeDirectory(turi, MAX_PATH);
TTDHostAppendAscii(turi, MAX_PATH, "_ttdlog");
TTDHostAppend(turi, MAX_PATH, TTDHostPathSeparator);
TTDHostAppendWChar(turi, MAX_PATH, curi + 1);
}
//add a path separator if one is not already present
if(curi[wcslen(curi) - 1] != (wchar)TTDHostPathSeparatorChar)
{
TTDHostAppend(turi, MAX_PATH, TTDHostPathSeparator);
}
size_t turiLength = TTDHostStringLength(turi);
size_t byteLengthWNull = (turiLength + 1) * sizeof(TTDHostCharType);
memcpy_s(uriBytes, byteLengthWNull, turi, byteLengthWNull);
*uriByteLength = turiLength * sizeof(TTDHostCharType);
}
void CALLBACK Helpers::TTInitializeForWriteLogStreamCallback(size_t uriByteLength, const byte* uriBytes)
{
//If the directory does not exist then we want to create it
Helpers::CreateDirectoryIfNeeded(uriByteLength, uriBytes);
//Clear the logging directory so it is ready for us to write into
Helpers::CleanDirectory(uriByteLength, uriBytes);
}
JsTTDStreamHandle CALLBACK Helpers::TTCreateStreamCallback(size_t uriByteLength, const byte* uriBytes, const char* asciiResourceName, bool read, bool write, byte** relocatedUri, size_t* relocatedUriLength)
{
AssertMsg((read | write) & (!read | !write), "Read/Write streams not supported yet -- defaulting to read only");
//relocatedUri and relocatedUriLength are ignored since we don't do code relocation for debugging in CH
void* res = nullptr;
TTDHostCharType path[MAX_PATH];
TTDHostInitFromUriBytes(path, uriBytes, uriByteLength);
TTDHostAppendAscii(path, MAX_PATH, asciiResourceName);
res = TTDHostOpen(path, write);
if(res == nullptr)
{
#if _WIN32
fwprintf(stderr, _u("Failed to open file: %ls\n"), path);
#else
fprintf(stderr, "Failed to open file: %s\n", path);
#endif
}
Helpers::TTReportLastIOErrorAsNeeded(res != nullptr, "Failed File Open");
return res;
}
bool CALLBACK Helpers::TTReadBytesFromStreamCallback(JsTTDStreamHandle handle, byte* buff, size_t size, size_t* readCount)
{
AssertMsg(handle != nullptr, "Bad file handle.");
if(size > MAXDWORD)
{
*readCount = 0;
return false;
}
BOOL ok = FALSE;
*readCount = TTDHostRead(buff, size, (FILE*)handle);
ok = (*readCount != 0);
Helpers::TTReportLastIOErrorAsNeeded(ok, "Failed Read!!!");
return ok ? true : false;
}
bool CALLBACK Helpers::TTWriteBytesToStreamCallback(JsTTDStreamHandle handle, const byte* buff, size_t size, size_t* writtenCount)
{
AssertMsg(handle != nullptr, "Bad file handle.");
if(size > MAXDWORD)
{
*writtenCount = 0;
return false;
}
BOOL ok = FALSE;
*writtenCount = TTDHostWrite(buff, size, (FILE*)handle);
ok = (*writtenCount == size);
Helpers::TTReportLastIOErrorAsNeeded(ok, "Failed Read!!!");
return ok ? true : false;
}
void CALLBACK Helpers::TTFlushAndCloseStreamCallback(JsTTDStreamHandle handle, bool read, bool write)
{
fflush((FILE*)handle);
fclose((FILE*)handle);
}