forked from focus-creative-games/il2cpp_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpal_io.cpp
More file actions
711 lines (615 loc) · 21.7 KB
/
pal_io.cpp
File metadata and controls
711 lines (615 loc) · 21.7 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
#include "il2cpp-config.h"
#include "pal_platform.h"
#if IL2CPP_USES_POSIX_CLASS_LIBRARY_PAL
#include "os/ClassLibraryPAL/pal_mirror_structs.h"
#include "os/File.h"
#include "os/Posix/FileHandle.h"
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#if IL2CPP_HAVE_FCOPYFILE
#include <copyfile.h>
#endif
struct DirectoryEntry;
extern "C"
{
struct DIRWrapper
{
DIR* dir;
DirectoryEntry* result;
size_t curIndex;
size_t numEntries;
#if !IL2CPP_HAVE_REWINDDIR
char* dirPath;
#endif
};
// Items needed by mscorlib
IL2CPP_EXPORT int32_t SystemNative_Stat2(const char* path, struct FileStatus* output); // 179
IL2CPP_EXPORT int32_t SystemNative_LStat2(const char* path, struct FileStatus* output); // 207
IL2CPP_EXPORT int32_t SystemNative_Unlink(const char* path); // 305
IL2CPP_EXPORT int32_t SystemNative_GetReadDirRBufferSize(void); // 371
IL2CPP_EXPORT int32_t SystemNative_ReadDirR(struct DIRWrapper* dirWrapper, uint8_t* buffer, int32_t bufferSize, struct DirectoryEntry* outputEntry); // 388
IL2CPP_EXPORT struct DIRWrapper* SystemNative_OpenDir(const char* path); // 468
IL2CPP_EXPORT int32_t SystemNative_CloseDir(struct DIRWrapper* dirWrapper); // 473
IL2CPP_EXPORT int32_t SystemNative_MkDir(const char* path, int32_t mode); // 592
IL2CPP_EXPORT int32_t SystemNative_ChMod(const char* path, int32_t mode); // 599
IL2CPP_EXPORT int32_t SystemNative_Link(const char* source, const char* linkTarget); // 660
IL2CPP_EXPORT int32_t SystemNative_Symlink(const char* target, const char* linkPath);
IL2CPP_EXPORT int32_t SystemNative_ReadLink(const char* path, char* buffer, int32_t bufferSize); // 1142
IL2CPP_EXPORT int32_t SystemNative_Rename(const char* oldPath, const char* newPath); // 1159
IL2CPP_EXPORT int32_t SystemNative_RmDir(const char* path); // 1166
IL2CPP_EXPORT int32_t SystemNative_CopyFile(intptr_t sourceFd, intptr_t destinationFd); // 1251
IL2CPP_EXPORT int32_t SystemNative_LChflags(const char* path, uint32_t flags);
IL2CPP_EXPORT int32_t SystemNative_LChflagsCanSetHiddenFlag(); // 1482
}
/* Provide consistent access to nanosecond fields, if they exist. */
/* Seconds are always available through st_atime, st_mtime, st_ctime. */
#if IL2CPP_HAVE_STAT_TIMESPEC
#define ST_ATIME_NSEC(statstruct) ((statstruct)->st_atimespec.tv_nsec)
#define ST_MTIME_NSEC(statstruct) ((statstruct)->st_mtimespec.tv_nsec)
#define ST_CTIME_NSEC(statstruct) ((statstruct)->st_ctimespec.tv_nsec)
#else /* HAVE_STAT_TIMESPEC */
#if IL2CPP_HAVE_STAT_TIM
#define ST_ATIME_NSEC(statstruct) ((statstruct)->st_atim.tv_nsec)
#define ST_MTIME_NSEC(statstruct) ((statstruct)->st_mtim.tv_nsec)
#define ST_CTIME_NSEC(statstruct) ((statstruct)->st_ctim.tv_nsec)
#else /* HAVE_STAT_TIM */
#if IL2CPP_HAVE_STAT_NSEC
#define ST_ATIME_NSEC(statstruct) ((statstruct)->st_atimensec)
#define ST_MTIME_NSEC(statstruct) ((statstruct)->st_mtimensec)
#define ST_CTIME_NSEC(statstruct) ((statstruct)->st_ctimensec)
#else /* HAVE_STAT_NSEC */
#define ST_ATIME_NSEC(statstruct) 0
#define ST_MTIME_NSEC(statstruct) 0
#define ST_CTIME_NSEC(statstruct) 0
#endif /* HAVE_STAT_NSEC */
#endif /* HAVE_STAT_TIM */
#endif /* HAVE_STAT_TIMESPEC */
/**
* Constants for interpreting FileStatus.Flags.
*/
enum
{
FILESTATUS_FLAGS_NONE = 0,
FILESTATUS_FLAGS_HAS_BIRTHTIME = 1,
};
/**
* Constants for interpreting FileStatus.UserFlags.
*/
enum
{
PAL_UF_HIDDEN = 0x8000
};
/**
* Constants from dirent.h for the inode type returned from readdir variants
*/
enum NodeType
{
PAL_DT_UNKNOWN = 0, // Unknown file type
PAL_DT_FIFO = 1, // Named Pipe
PAL_DT_CHR = 2, // Character Device
PAL_DT_DIR = 4, // Directory
PAL_DT_BLK = 6, // Block Device
PAL_DT_REG = 8, // Regular file
PAL_DT_LNK = 10, // Symlink
PAL_DT_SOCK = 12, // Socket
PAL_DT_WHT = 14 // BSD Whiteout
};
/**
* Our intermediate dirent struct that only gives back the data we need
*/
struct DirectoryEntry
{
char* Name; // Address of the name of the inode
int32_t NameLength; // Length (in chars) of the inode name
int32_t InodeType; // The inode type as described in the NodeType enum
};
static void ConvertFileStatus(const struct stat_* src, struct FileStatus* dst)
{
dst->Dev = (int64_t)src->st_dev;
dst->Ino = (int64_t)src->st_ino;
dst->Flags = FILESTATUS_FLAGS_NONE;
dst->Mode = (int32_t)src->st_mode;
dst->Uid = src->st_uid;
dst->Gid = src->st_gid;
dst->Size = src->st_size;
dst->ATime = src->st_atime;
dst->MTime = src->st_mtime;
dst->CTime = src->st_ctime;
dst->ATimeNsec = ST_ATIME_NSEC(src);
dst->MTimeNsec = ST_MTIME_NSEC(src);
dst->CTimeNsec = ST_CTIME_NSEC(src);
#if IL2CPP_HAVE_STAT_BIRTHTIME
dst->BirthTime = src->st_birthtimespec.tv_sec;
dst->BirthTimeNsec = src->st_birthtimespec.tv_nsec;
dst->Flags |= FILESTATUS_FLAGS_HAS_BIRTHTIME;
#else
// Linux path: until we use statx() instead
dst->BirthTime = 0;
dst->BirthTimeNsec = 0;
#endif
#if defined(IL2CPP_HAVE_STAT_FLAGS) && defined(UF_HIDDEN)
dst->UserFlags = ((src->st_flags & UF_HIDDEN) == UF_HIDDEN) ? PAL_UF_HIDDEN : 0;
#else
dst->UserFlags = 0;
#endif
}
#if IL2CPP_HAVE_REMAP_PATH
#define REMAP_PATH(path) pal_remap_path(path).c_str()
#else
#define REMAP_PATH(path) path
#endif
// CoreCLR expects the "2" suffixes on these: they should be cleaned up in our
// next coordinated System.Native changes
int32_t SystemNative_Stat2(const char* path, struct FileStatus* output)
{
struct stat_ result = {};
int ret;
while ((ret = stat_(REMAP_PATH(path), &result)) < 0 && errno == EINTR)
;
if (ret == 0)
{
ConvertFileStatus(&result, output);
}
return ret;
}
int32_t SystemNative_LStat2(const char* path, struct FileStatus* output)
{
struct stat_ result = {};
int ret = lstat_(REMAP_PATH(path), &result);
if (ret == 0)
{
ConvertFileStatus(&result, output);
}
return ret;
}
int32_t SystemNative_Unlink(const char* path)
{
int32_t result = 0;
while ((result = unlink(REMAP_PATH(path))) < 0 && errno == EINTR)
;
return result;
}
static void ConvertDirent(const struct dirent* entry, struct DirectoryEntry* outputEntry)
{
// We use Marshal.PtrToStringAnsi on the managed side, which takes a pointer to
// the start of the unmanaged string. Give the caller back a pointer to the
// location of the start of the string that exists in their own byte buffer.
outputEntry->Name = strdup(entry->d_name);
IL2CPP_ASSERT(outputEntry->Name != NULL);
#if !defined(DT_UNKNOWN)
// AIX has no d_type, and since we can't get the directory that goes with
// the filename from ReadDir, we can't stat the file. Return unknown and
// hope that managed code can properly stat the file.
outputEntry->InodeType = PAL_DT_UNKNOWN;
#else
outputEntry->InodeType = (int32_t)entry->d_type;
#endif
#if IL2CPP_HAVE_DIRENT_NAME_LEN
#if !defined(IL2CPP_DIRENT_MEMBER_NAME_LEN)
outputEntry->NameLength = entry->d_namlen;
#else
outputEntry->NameLength = entry->IL2CPP_DIRENT_MEMBER_NAME_LEN;
#endif
#else
outputEntry->NameLength = -1; // sentinel value to mean we have to walk to find the first \0
#endif
}
#if IL2CPP_HAVE_READDIR_R_DEPRECATED_DO_NOT_USE
// struct dirent typically contains 64-bit numbers (e.g. d_ino), so we align it at 8-byte.
static const size_t dirent_alignment = 8;
#endif
int32_t SystemNative_GetReadDirRBufferSize(void)
{
#if IL2CPP_HAVE_READDIR_R_DEPRECATED_DO_NOT_USE
// dirent should be under 2k in size
IL2CPP_ASSERT(sizeof(struct dirent) < 2048);
// add some extra space so we can align the buffer to dirent.
return sizeof(struct dirent) + dirent_alignment - 1;
#else
return 0;
#endif
}
static int CompareByName(const void *p1, const void *p2)
{
auto directoryEntry1 = ((struct DirectoryEntry*)p1);
auto directoryEntry2 = ((struct DirectoryEntry*)p2);
// Sort NULL values to the end of the array. This can happen when
// a file is deleted while GetFiles is called.
if (directoryEntry1->Name == directoryEntry2->Name)
return 0;
if (directoryEntry1->Name == NULL)
return 1;
if (directoryEntry2->Name == NULL)
return -1;
return strcmp(directoryEntry1->Name, directoryEntry2->Name);
}
// To reduce the number of string copies, the caller of this function is responsible to ensure the memory
// referenced by outputEntry remains valid until it is read.
// If the platform supports readdir_r, the caller provides a buffer into which the data is read.
// If the platform uses readdir, the caller must ensure no calls are made to readdir/closedir since those will invalidate
// the current dirent. We assume the platform supports concurrent readdir calls to different DIRs.
int32_t SystemNative_ReadDirR(struct DIRWrapper* dirWrapper, uint8_t* buffer, int32_t bufferSize, struct DirectoryEntry* outputEntry)
{
IL2CPP_ASSERT(dirWrapper != NULL);
IL2CPP_ASSERT(dirWrapper->dir != NULL);
IL2CPP_ASSERT(outputEntry != NULL);
#if IL2CPP_HAVE_READDIR_R_DEPRECATED_DO_NOT_USE
IL2CPP_ASSERT(buffer != NULL);
// align to dirent
struct dirent* entry = (struct dirent*)((size_t)(buffer + dirent_alignment - 1) & ~(dirent_alignment - 1));
// check there is dirent size available at entry
if ((buffer + bufferSize) < ((uint8_t*)entry + sizeof(struct dirent)))
{
IL2CPP_ASSERT(false && "Buffer size too small; use GetReadDirRBufferSize to get required buffer size");
return ERANGE;
}
struct dirent* result = NULL;
#ifdef _AIX
// AIX returns 0 on success, but bizarrely, it returns 9 for both error and
// end-of-directory. result is NULL for both cases. The API returns the
// same thing for EOD/error, so disambiguation between the two is nearly
// impossible without clobbering errno for yourself and seeing if the API
// changed it. See:
// https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.basetrf2/readdir_r.htm
errno = 0; // create a success condition for the API to clobber
int error = readdir_r(dir, entry, &result);
if (error == 9)
{
memset(outputEntry, 0, sizeof(*outputEntry)); // managed out param must be initialized
return errno == 0 ? -1 : errno;
}
#else
int error = readdir_r(dir, entry, &result);
// positive error number returned -> failure
if (error != 0)
{
IL2CPP_ASSERT(error > 0);
memset(outputEntry, 0, sizeof(*outputEntry)); // managed out param must be initialized
return error;
}
// 0 returned with null result -> end-of-stream
if (result == NULL)
{
memset(outputEntry, 0, sizeof(*outputEntry)); // managed out param must be initialized
return -1; // shim convention for end-of-stream
}
#endif
// 0 returned with non-null result (guaranteed to be set to entry arg) -> success
IL2CPP_ASSERT(result == entry);
#else
(void)buffer; // unused
(void)bufferSize; // unused
errno = 0;
bool endOfEntries = false;
if (!dirWrapper->result)
{
struct dirent* entry;
size_t numEntries = 0;
while ((entry = readdir(dirWrapper->dir)))
numEntries++;
if (numEntries)
{
// Use calloc to ensure the array is zero-initialized.
dirWrapper->result = (DirectoryEntry*)calloc(numEntries, sizeof(struct DirectoryEntry));
dirWrapper->curIndex = 0;
#if IL2CPP_HAVE_REWINDDIR
rewinddir(dirWrapper->dir);
#else
closedir(dirWrapper->dir);
dirWrapper->dir = opendir(dirWrapper->dirPath);
#endif
// If we iterate fewer entries than exist because some files were deleted
// since the time we computed numEntries above, that will be fine. Those
// extra entries will be zero-initialized and will be sorted to the end
// of the array by the qsort below.
size_t index = 0;
while ((entry = readdir(dirWrapper->dir)) && index < numEntries)
{
ConvertDirent(entry, &dirWrapper->result[index]);
index++;
}
qsort(dirWrapper->result, numEntries, sizeof(struct DirectoryEntry), CompareByName);
dirWrapper->numEntries = index;
}
}
if (dirWrapper->curIndex < dirWrapper->numEntries)
{
*outputEntry = dirWrapper->result[dirWrapper->curIndex];
dirWrapper->curIndex++;
}
else
{
endOfEntries = true;
}
// 0 returned with null result -> end-of-stream
if (endOfEntries)
{
memset(outputEntry, 0, sizeof(*outputEntry)); // managed out param must be initialized
// kernel set errno -> failure
if (errno != 0)
{
IL2CPP_ASSERT(errno == EBADF && "Invalid directory stream descriptor dir" && errno);
return errno;
}
return -1;
}
#endif
return 0;
}
struct DIRWrapper* SystemNative_OpenDir(const char* path)
{
const char* remapped_path = NULL;
#if IL2CPP_HAVE_REMAP_PATH
auto remapped_path_string = pal_remap_path(path);
remapped_path = remapped_path_string.c_str();
#else
remapped_path = path;
#endif
DIR* dir = opendir(remapped_path);
if (dir == NULL)
return NULL;
struct DIRWrapper* ret = (struct DIRWrapper*)malloc(sizeof(struct DIRWrapper));
ret->dir = dir;
ret->result = NULL;
ret->curIndex = 0;
ret->numEntries = 0;
#if !IL2CPP_HAVE_REWINDDIR
ret->dirPath = strdup(remapped_path);
#endif
return ret;
}
int32_t SystemNative_CloseDir(struct DIRWrapper* dirWrapper)
{
IL2CPP_ASSERT(dirWrapper != NULL);
int32_t ret = closedir(dirWrapper->dir);
if (dirWrapper->result)
{
for (int i = 0; i < dirWrapper->numEntries; i++)
free(dirWrapper->result[i].Name);
free(dirWrapper->result);
}
dirWrapper->result = NULL;
#if !IL2CPP_HAVE_REWINDDIR
if (dirWrapper->dirPath)
free(dirWrapper->dirPath);
#endif
free(dirWrapper);
return ret;
}
int32_t SystemNative_MkDir(const char* path, int32_t mode)
{
int32_t result = 0;
while ((result = mkdir(REMAP_PATH(path), (mode_t)mode)) < 0 && errno == EINTR)
;
return result;
}
int32_t SystemNative_ChMod(const char* path, int32_t mode)
{
int32_t result = 0;
while ((result = chmod_(REMAP_PATH(path), (mode_t)mode)) < 0 && errno == EINTR)
;
return result;
}
int32_t SystemNative_Link(const char* source, const char* linkTarget)
{
int32_t result = 0;
while ((result = link_(REMAP_PATH(source), REMAP_PATH(linkTarget))) < 0 && errno == EINTR)
;
return result;
}
int32_t SystemNative_Symlink(const char* target, const char* linkPath)
{
return symlink_(REMAP_PATH(target), REMAP_PATH(linkPath));
}
int32_t SystemNative_ReadLink(const char* path, char* buffer, int32_t bufferSize)
{
IL2CPP_ASSERT(buffer != NULL || bufferSize == 0);
IL2CPP_ASSERT(bufferSize >= 0);
if (bufferSize <= 0)
{
errno = EINVAL;
return -1;
}
ssize_t count = readlink_(REMAP_PATH(path), buffer, (size_t)bufferSize);
IL2CPP_ASSERT(count >= -1 && count <= bufferSize);
return (int32_t)count;
}
int32_t SystemNative_Rename(const char* oldPath, const char* newPath)
{
int32_t result;
while ((result = rename(REMAP_PATH(oldPath), REMAP_PATH(newPath))) < 0 && errno == EINTR)
;
return result;
}
int32_t SystemNative_RmDir(const char* path)
{
int32_t result = 0;
while ((result = rmdir(REMAP_PATH(path))) < 0 && errno == EINTR)
;
return result;
}
#if !IL2CPP_HAVE_FCOPYFILE
// Read all data from inFd and write it to outFd
static int32_t CopyFile_ReadWrite(int inFd, int outFd)
{
// Allocate a buffer
const int BufferLength = 80 * 1024 * sizeof(char);
char* buffer = (char*)malloc(BufferLength);
if (buffer == NULL)
{
return -1;
}
// Repeatedly read from the source and write to the destination
while (true)
{
// Read up to what will fit in our buffer. We're done if we get back 0 bytes.
ssize_t bytesRead;
while ((bytesRead = read(inFd, buffer, BufferLength)) < 0 && errno == EINTR)
;
if (bytesRead == -1)
{
int tmp = errno;
free(buffer);
errno = tmp;
return -1;
}
if (bytesRead == 0)
{
break;
}
IL2CPP_ASSERT(bytesRead > 0);
// Write what was read.
ssize_t offset = 0;
while (bytesRead > 0)
{
ssize_t bytesWritten;
while ((bytesWritten = write(outFd, buffer + offset, (size_t)bytesRead)) < 0 && errno == EINTR)
;
if (bytesWritten == -1)
{
int tmp = errno;
free(buffer);
errno = tmp;
return -1;
}
IL2CPP_ASSERT(bytesWritten >= 0);
bytesRead -= bytesWritten;
offset += bytesWritten;
}
}
free(buffer);
return 0;
}
#endif // !IL2CPP_HAVE_FCOPYFILE
int32_t SystemNative_CopyFile(intptr_t sourceFd, intptr_t destinationFd)
{
#if IL2CPP_HAVE_CUSTOM_COPYFILE
return pal_custom_copy_file(sourceFd, destinationFd);
#else
int inFd, outFd;
inFd = il2cpp::os::File::IsHandleOpenFileHandle(sourceFd) ? reinterpret_cast<il2cpp::os::FileHandle*>(sourceFd)->fd : static_cast<int>(sourceFd);
outFd = il2cpp::os::File::IsHandleOpenFileHandle(destinationFd) ? reinterpret_cast<il2cpp::os::FileHandle*>(destinationFd)->fd : static_cast<int>(destinationFd);
#if IL2CPP_HAVE_FCOPYFILE
// If fcopyfile is available (OS X), try to use it, as the whole copy
// can be performed in the kernel, without lots of unnecessary copying.
// Copy data and metadata.
return fcopyfile(inFd, outFd, NULL, COPYFILE_ALL) == 0 ? 0 : -1;
#else
// Get the stats on the source file.
int ret;
struct stat_ sourceStat;
bool copied = false;
// First, stat the source file.
while ((ret = fstat_(inFd, &sourceStat)) < 0 && errno == EINTR)
;
if (ret != 0)
{
// If we can't stat() it, then we likely don't have permission to read it.
return -1;
}
// Copy permissions. This fchmod() needs to happen prior to writing anything into
// the file to avoid possibly leaking any private data.
while ((ret = fchmod(outFd, sourceStat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))) < 0 && errno == EINTR)
;
#if !IL2CPP_CANNOT_MODIFY_FILE_PERMISSIONS
if (ret != 0)
{
return -1;
}
#endif
#if IL2CPP_HAVE_SENDFILE_4
// If sendfile is available (Linux), try to use it, as the whole copy
// can be performed in the kernel, without lots of unnecessary copying.
// On 32-bit, if you use 64-bit offsets, the last argument of `sendfile' will be a
// `size_t' a 32-bit integer while the `st_size' field of the stat structure will be off64_t.
// So `size' will have to be `uint64_t'. In all other cases, it will be `size_t'.
uint64_t size = (uint64_t)sourceStat.st_size;
// Note that per man page for large files, you have to iterate until the
// whole file is copied (Linux has a limit of 0x7ffff000 bytes copied).
while (size > 0)
{
ssize_t sent = sendfile(outFd, inFd, NULL, (size >= SSIZE_MAX ? SSIZE_MAX : (size_t)size));
if (sent < 0)
{
if (errno != EINVAL && errno != ENOSYS)
{
return -1;
}
else
{
break;
}
}
else
{
IL2CPP_ASSERT((size_t)sent <= size);
size -= (size_t)sent;
}
}
if (size == 0)
{
copied = true;
}
// sendfile couldn't be used; fall back to a manual copy below. This could happen
// if we're on an old kernel, for example, where sendfile could only be used
// with sockets and not regular files.
#endif // IL2CPP_HAVE_SENDFILE_4
// Manually read all data from the source and write it to the destination.
if (!copied && CopyFile_ReadWrite(inFd, outFd) != 0)
{
return -1;
}
// Now that the data from the file has been copied, copy over metadata
// from the source file. First copy the file times.
// If futimes nor futimes are available on this platform, file times will
// not be copied over.
#if IL2CPP_HAVE_FUTIMENS
// futimens is prefered because it has a higher resolution.
struct timespec origTimes[2];
origTimes[0].tv_sec = (time_t)sourceStat.st_atime;
origTimes[0].tv_nsec = ST_ATIME_NSEC(&sourceStat);
origTimes[1].tv_sec = (time_t)sourceStat.st_mtime;
origTimes[1].tv_nsec = ST_MTIME_NSEC(&sourceStat);
while ((ret = futimens(outFd, origTimes)) < 0 && errno == EINTR)
;
#elif IL2CPP_HAVE_FUTIMES
struct timeval origTimes[2];
origTimes[0].tv_sec = sourceStat.st_atime;
origTimes[0].tv_usec = ST_ATIME_NSEC(&sourceStat) / 1000;
origTimes[1].tv_sec = sourceStat.st_mtime;
origTimes[1].tv_usec = ST_MTIME_NSEC(&sourceStat) / 1000;
while ((ret = futimes(outFd, origTimes)) < 0 && errno == EINTR)
;
#endif
#if !IL2CPP_CANNOT_MODIFY_FILE_PERMISSIONS
if (ret != 0)
{
return -1;
}
#endif
return 0;
#endif // IL2CPP_HAVE_FCOPYFILE
#endif // IL2CPP_HAVE_CUSTOM_COPYFILE
}
int32_t SystemNative_LChflags(const char* path, uint32_t flags)
{
#if defined(UF_HIDDEN) && defined(IL2CPP_HAVE_STAT_FLAGS) && defined(IL2CPP_HAVE_LCHFLAGS)
int32_t result;
while ((result = lchflags(path, flags)) < 0 && errno == EINTR)
;
return result;
#else
return -1;
#endif
}
int32_t SystemNative_LChflagsCanSetHiddenFlag(void)
{
#if defined(UF_HIDDEN) && defined(IL2CPP_HAVE_STAT_FLAGS) && defined(IL2CPP_HAVE_LCHFLAGS)
return true;
#else
return false;
#endif
}
#endif