pal_io.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. #include "il2cpp-config.h"
  2. #include "pal_platform.h"
  3. #if IL2CPP_USES_POSIX_CLASS_LIBRARY_PAL
  4. #include "os/ClassLibraryPAL/pal_mirror_structs.h"
  5. #include "os/File.h"
  6. #include "os/Posix/FileHandle.h"
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <sys/stat.h>
  10. #include <unistd.h>
  11. #if IL2CPP_HAVE_FCOPYFILE
  12. #include <copyfile.h>
  13. #endif
  14. struct DirectoryEntry;
  15. extern "C"
  16. {
  17. struct DIRWrapper
  18. {
  19. DIR* dir;
  20. DirectoryEntry* result;
  21. size_t curIndex;
  22. size_t numEntries;
  23. #if !IL2CPP_HAVE_REWINDDIR
  24. char* dirPath;
  25. #endif
  26. };
  27. // Items needed by mscorlib
  28. IL2CPP_EXPORT int32_t SystemNative_Stat2(const char* path, struct FileStatus* output); // 179
  29. IL2CPP_EXPORT int32_t SystemNative_LStat2(const char* path, struct FileStatus* output); // 207
  30. IL2CPP_EXPORT int32_t SystemNative_Unlink(const char* path); // 305
  31. IL2CPP_EXPORT int32_t SystemNative_GetReadDirRBufferSize(void); // 371
  32. IL2CPP_EXPORT int32_t SystemNative_ReadDirR(struct DIRWrapper* dirWrapper, uint8_t* buffer, int32_t bufferSize, struct DirectoryEntry* outputEntry); // 388
  33. IL2CPP_EXPORT struct DIRWrapper* SystemNative_OpenDir(const char* path); // 468
  34. IL2CPP_EXPORT int32_t SystemNative_CloseDir(struct DIRWrapper* dirWrapper); // 473
  35. IL2CPP_EXPORT int32_t SystemNative_MkDir(const char* path, int32_t mode); // 592
  36. IL2CPP_EXPORT int32_t SystemNative_ChMod(const char* path, int32_t mode); // 599
  37. IL2CPP_EXPORT int32_t SystemNative_Link(const char* source, const char* linkTarget); // 660
  38. IL2CPP_EXPORT int32_t SystemNative_Symlink(const char* target, const char* linkPath);
  39. IL2CPP_EXPORT int32_t SystemNative_ReadLink(const char* path, char* buffer, int32_t bufferSize); // 1142
  40. IL2CPP_EXPORT int32_t SystemNative_Rename(const char* oldPath, const char* newPath); // 1159
  41. IL2CPP_EXPORT int32_t SystemNative_RmDir(const char* path); // 1166
  42. IL2CPP_EXPORT int32_t SystemNative_CopyFile(intptr_t sourceFd, intptr_t destinationFd); // 1251
  43. IL2CPP_EXPORT int32_t SystemNative_LChflags(const char* path, uint32_t flags);
  44. IL2CPP_EXPORT int32_t SystemNative_LChflagsCanSetHiddenFlag(); // 1482
  45. }
  46. /* Provide consistent access to nanosecond fields, if they exist. */
  47. /* Seconds are always available through st_atime, st_mtime, st_ctime. */
  48. #if IL2CPP_HAVE_STAT_TIMESPEC
  49. #define ST_ATIME_NSEC(statstruct) ((statstruct)->st_atimespec.tv_nsec)
  50. #define ST_MTIME_NSEC(statstruct) ((statstruct)->st_mtimespec.tv_nsec)
  51. #define ST_CTIME_NSEC(statstruct) ((statstruct)->st_ctimespec.tv_nsec)
  52. #else /* HAVE_STAT_TIMESPEC */
  53. #if IL2CPP_HAVE_STAT_TIM
  54. #define ST_ATIME_NSEC(statstruct) ((statstruct)->st_atim.tv_nsec)
  55. #define ST_MTIME_NSEC(statstruct) ((statstruct)->st_mtim.tv_nsec)
  56. #define ST_CTIME_NSEC(statstruct) ((statstruct)->st_ctim.tv_nsec)
  57. #else /* HAVE_STAT_TIM */
  58. #if IL2CPP_HAVE_STAT_NSEC
  59. #define ST_ATIME_NSEC(statstruct) ((statstruct)->st_atimensec)
  60. #define ST_MTIME_NSEC(statstruct) ((statstruct)->st_mtimensec)
  61. #define ST_CTIME_NSEC(statstruct) ((statstruct)->st_ctimensec)
  62. #else /* HAVE_STAT_NSEC */
  63. #define ST_ATIME_NSEC(statstruct) 0
  64. #define ST_MTIME_NSEC(statstruct) 0
  65. #define ST_CTIME_NSEC(statstruct) 0
  66. #endif /* HAVE_STAT_NSEC */
  67. #endif /* HAVE_STAT_TIM */
  68. #endif /* HAVE_STAT_TIMESPEC */
  69. /**
  70. * Constants for interpreting FileStatus.Flags.
  71. */
  72. enum
  73. {
  74. FILESTATUS_FLAGS_NONE = 0,
  75. FILESTATUS_FLAGS_HAS_BIRTHTIME = 1,
  76. };
  77. /**
  78. * Constants for interpreting FileStatus.UserFlags.
  79. */
  80. enum
  81. {
  82. PAL_UF_HIDDEN = 0x8000
  83. };
  84. /**
  85. * Constants from dirent.h for the inode type returned from readdir variants
  86. */
  87. enum NodeType
  88. {
  89. PAL_DT_UNKNOWN = 0, // Unknown file type
  90. PAL_DT_FIFO = 1, // Named Pipe
  91. PAL_DT_CHR = 2, // Character Device
  92. PAL_DT_DIR = 4, // Directory
  93. PAL_DT_BLK = 6, // Block Device
  94. PAL_DT_REG = 8, // Regular file
  95. PAL_DT_LNK = 10, // Symlink
  96. PAL_DT_SOCK = 12, // Socket
  97. PAL_DT_WHT = 14 // BSD Whiteout
  98. };
  99. /**
  100. * Our intermediate dirent struct that only gives back the data we need
  101. */
  102. struct DirectoryEntry
  103. {
  104. char* Name; // Address of the name of the inode
  105. int32_t NameLength; // Length (in chars) of the inode name
  106. int32_t InodeType; // The inode type as described in the NodeType enum
  107. };
  108. static void ConvertFileStatus(const struct stat_* src, struct FileStatus* dst)
  109. {
  110. dst->Dev = (int64_t)src->st_dev;
  111. dst->Ino = (int64_t)src->st_ino;
  112. dst->Flags = FILESTATUS_FLAGS_NONE;
  113. dst->Mode = (int32_t)src->st_mode;
  114. dst->Uid = src->st_uid;
  115. dst->Gid = src->st_gid;
  116. dst->Size = src->st_size;
  117. dst->ATime = src->st_atime;
  118. dst->MTime = src->st_mtime;
  119. dst->CTime = src->st_ctime;
  120. dst->ATimeNsec = ST_ATIME_NSEC(src);
  121. dst->MTimeNsec = ST_MTIME_NSEC(src);
  122. dst->CTimeNsec = ST_CTIME_NSEC(src);
  123. #if IL2CPP_HAVE_STAT_BIRTHTIME
  124. dst->BirthTime = src->st_birthtimespec.tv_sec;
  125. dst->BirthTimeNsec = src->st_birthtimespec.tv_nsec;
  126. dst->Flags |= FILESTATUS_FLAGS_HAS_BIRTHTIME;
  127. #else
  128. // Linux path: until we use statx() instead
  129. dst->BirthTime = 0;
  130. dst->BirthTimeNsec = 0;
  131. #endif
  132. #if defined(IL2CPP_HAVE_STAT_FLAGS) && defined(UF_HIDDEN)
  133. dst->UserFlags = ((src->st_flags & UF_HIDDEN) == UF_HIDDEN) ? PAL_UF_HIDDEN : 0;
  134. #else
  135. dst->UserFlags = 0;
  136. #endif
  137. }
  138. #if IL2CPP_HAVE_REMAP_PATH
  139. #define REMAP_PATH(path) pal_remap_path(path).c_str()
  140. #else
  141. #define REMAP_PATH(path) path
  142. #endif
  143. // CoreCLR expects the "2" suffixes on these: they should be cleaned up in our
  144. // next coordinated System.Native changes
  145. int32_t SystemNative_Stat2(const char* path, struct FileStatus* output)
  146. {
  147. struct stat_ result = {};
  148. int ret;
  149. while ((ret = stat_(REMAP_PATH(path), &result)) < 0 && errno == EINTR)
  150. ;
  151. if (ret == 0)
  152. {
  153. ConvertFileStatus(&result, output);
  154. }
  155. return ret;
  156. }
  157. int32_t SystemNative_LStat2(const char* path, struct FileStatus* output)
  158. {
  159. struct stat_ result = {};
  160. int ret = lstat_(REMAP_PATH(path), &result);
  161. if (ret == 0)
  162. {
  163. ConvertFileStatus(&result, output);
  164. }
  165. return ret;
  166. }
  167. int32_t SystemNative_Unlink(const char* path)
  168. {
  169. int32_t result = 0;
  170. while ((result = unlink(REMAP_PATH(path))) < 0 && errno == EINTR)
  171. ;
  172. return result;
  173. }
  174. static void ConvertDirent(const struct dirent* entry, struct DirectoryEntry* outputEntry)
  175. {
  176. // We use Marshal.PtrToStringAnsi on the managed side, which takes a pointer to
  177. // the start of the unmanaged string. Give the caller back a pointer to the
  178. // location of the start of the string that exists in their own byte buffer.
  179. outputEntry->Name = strdup(entry->d_name);
  180. IL2CPP_ASSERT(outputEntry->Name != NULL);
  181. #if !defined(DT_UNKNOWN)
  182. // AIX has no d_type, and since we can't get the directory that goes with
  183. // the filename from ReadDir, we can't stat the file. Return unknown and
  184. // hope that managed code can properly stat the file.
  185. outputEntry->InodeType = PAL_DT_UNKNOWN;
  186. #else
  187. outputEntry->InodeType = (int32_t)entry->d_type;
  188. #endif
  189. #if IL2CPP_HAVE_DIRENT_NAME_LEN
  190. #if !defined(IL2CPP_DIRENT_MEMBER_NAME_LEN)
  191. outputEntry->NameLength = entry->d_namlen;
  192. #else
  193. outputEntry->NameLength = entry->IL2CPP_DIRENT_MEMBER_NAME_LEN;
  194. #endif
  195. #else
  196. outputEntry->NameLength = -1; // sentinel value to mean we have to walk to find the first \0
  197. #endif
  198. }
  199. #if IL2CPP_HAVE_READDIR_R_DEPRECATED_DO_NOT_USE
  200. // struct dirent typically contains 64-bit numbers (e.g. d_ino), so we align it at 8-byte.
  201. static const size_t dirent_alignment = 8;
  202. #endif
  203. int32_t SystemNative_GetReadDirRBufferSize(void)
  204. {
  205. #if IL2CPP_HAVE_READDIR_R_DEPRECATED_DO_NOT_USE
  206. // dirent should be under 2k in size
  207. IL2CPP_ASSERT(sizeof(struct dirent) < 2048);
  208. // add some extra space so we can align the buffer to dirent.
  209. return sizeof(struct dirent) + dirent_alignment - 1;
  210. #else
  211. return 0;
  212. #endif
  213. }
  214. static int CompareByName(const void *p1, const void *p2)
  215. {
  216. auto directoryEntry1 = ((struct DirectoryEntry*)p1);
  217. auto directoryEntry2 = ((struct DirectoryEntry*)p2);
  218. // Sort NULL values to the end of the array. This can happen when
  219. // a file is deleted while GetFiles is called.
  220. if (directoryEntry1->Name == directoryEntry2->Name)
  221. return 0;
  222. if (directoryEntry1->Name == NULL)
  223. return 1;
  224. if (directoryEntry2->Name == NULL)
  225. return -1;
  226. return strcmp(directoryEntry1->Name, directoryEntry2->Name);
  227. }
  228. // To reduce the number of string copies, the caller of this function is responsible to ensure the memory
  229. // referenced by outputEntry remains valid until it is read.
  230. // If the platform supports readdir_r, the caller provides a buffer into which the data is read.
  231. // If the platform uses readdir, the caller must ensure no calls are made to readdir/closedir since those will invalidate
  232. // the current dirent. We assume the platform supports concurrent readdir calls to different DIRs.
  233. int32_t SystemNative_ReadDirR(struct DIRWrapper* dirWrapper, uint8_t* buffer, int32_t bufferSize, struct DirectoryEntry* outputEntry)
  234. {
  235. IL2CPP_ASSERT(dirWrapper != NULL);
  236. IL2CPP_ASSERT(dirWrapper->dir != NULL);
  237. IL2CPP_ASSERT(outputEntry != NULL);
  238. #if IL2CPP_HAVE_READDIR_R_DEPRECATED_DO_NOT_USE
  239. IL2CPP_ASSERT(buffer != NULL);
  240. // align to dirent
  241. struct dirent* entry = (struct dirent*)((size_t)(buffer + dirent_alignment - 1) & ~(dirent_alignment - 1));
  242. // check there is dirent size available at entry
  243. if ((buffer + bufferSize) < ((uint8_t*)entry + sizeof(struct dirent)))
  244. {
  245. IL2CPP_ASSERT(false && "Buffer size too small; use GetReadDirRBufferSize to get required buffer size");
  246. return ERANGE;
  247. }
  248. struct dirent* result = NULL;
  249. #ifdef _AIX
  250. // AIX returns 0 on success, but bizarrely, it returns 9 for both error and
  251. // end-of-directory. result is NULL for both cases. The API returns the
  252. // same thing for EOD/error, so disambiguation between the two is nearly
  253. // impossible without clobbering errno for yourself and seeing if the API
  254. // changed it. See:
  255. // https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.basetrf2/readdir_r.htm
  256. errno = 0; // create a success condition for the API to clobber
  257. int error = readdir_r(dir, entry, &result);
  258. if (error == 9)
  259. {
  260. memset(outputEntry, 0, sizeof(*outputEntry)); // managed out param must be initialized
  261. return errno == 0 ? -1 : errno;
  262. }
  263. #else
  264. int error = readdir_r(dir, entry, &result);
  265. // positive error number returned -> failure
  266. if (error != 0)
  267. {
  268. IL2CPP_ASSERT(error > 0);
  269. memset(outputEntry, 0, sizeof(*outputEntry)); // managed out param must be initialized
  270. return error;
  271. }
  272. // 0 returned with null result -> end-of-stream
  273. if (result == NULL)
  274. {
  275. memset(outputEntry, 0, sizeof(*outputEntry)); // managed out param must be initialized
  276. return -1; // shim convention for end-of-stream
  277. }
  278. #endif
  279. // 0 returned with non-null result (guaranteed to be set to entry arg) -> success
  280. IL2CPP_ASSERT(result == entry);
  281. #else
  282. (void)buffer; // unused
  283. (void)bufferSize; // unused
  284. errno = 0;
  285. bool endOfEntries = false;
  286. if (!dirWrapper->result)
  287. {
  288. struct dirent* entry;
  289. size_t numEntries = 0;
  290. while ((entry = readdir(dirWrapper->dir)))
  291. numEntries++;
  292. if (numEntries)
  293. {
  294. // Use calloc to ensure the array is zero-initialized.
  295. dirWrapper->result = (DirectoryEntry*)calloc(numEntries, sizeof(struct DirectoryEntry));
  296. dirWrapper->curIndex = 0;
  297. #if IL2CPP_HAVE_REWINDDIR
  298. rewinddir(dirWrapper->dir);
  299. #else
  300. closedir(dirWrapper->dir);
  301. dirWrapper->dir = opendir(dirWrapper->dirPath);
  302. #endif
  303. // If we iterate fewer entries than exist because some files were deleted
  304. // since the time we computed numEntries above, that will be fine. Those
  305. // extra entries will be zero-initialized and will be sorted to the end
  306. // of the array by the qsort below.
  307. size_t index = 0;
  308. while ((entry = readdir(dirWrapper->dir)) && index < numEntries)
  309. {
  310. ConvertDirent(entry, &dirWrapper->result[index]);
  311. index++;
  312. }
  313. qsort(dirWrapper->result, numEntries, sizeof(struct DirectoryEntry), CompareByName);
  314. dirWrapper->numEntries = index;
  315. }
  316. }
  317. if (dirWrapper->curIndex < dirWrapper->numEntries)
  318. {
  319. *outputEntry = dirWrapper->result[dirWrapper->curIndex];
  320. dirWrapper->curIndex++;
  321. }
  322. else
  323. {
  324. endOfEntries = true;
  325. }
  326. // 0 returned with null result -> end-of-stream
  327. if (endOfEntries)
  328. {
  329. memset(outputEntry, 0, sizeof(*outputEntry)); // managed out param must be initialized
  330. // kernel set errno -> failure
  331. if (errno != 0)
  332. {
  333. IL2CPP_ASSERT(errno == EBADF && "Invalid directory stream descriptor dir" && errno);
  334. return errno;
  335. }
  336. return -1;
  337. }
  338. #endif
  339. return 0;
  340. }
  341. struct DIRWrapper* SystemNative_OpenDir(const char* path)
  342. {
  343. const char* remapped_path = NULL;
  344. #if IL2CPP_HAVE_REMAP_PATH
  345. auto remapped_path_string = pal_remap_path(path);
  346. remapped_path = remapped_path_string.c_str();
  347. #else
  348. remapped_path = path;
  349. #endif
  350. DIR* dir = opendir(remapped_path);
  351. if (dir == NULL)
  352. return NULL;
  353. struct DIRWrapper* ret = (struct DIRWrapper*)malloc(sizeof(struct DIRWrapper));
  354. ret->dir = dir;
  355. ret->result = NULL;
  356. ret->curIndex = 0;
  357. ret->numEntries = 0;
  358. #if !IL2CPP_HAVE_REWINDDIR
  359. ret->dirPath = strdup(remapped_path);
  360. #endif
  361. return ret;
  362. }
  363. int32_t SystemNative_CloseDir(struct DIRWrapper* dirWrapper)
  364. {
  365. IL2CPP_ASSERT(dirWrapper != NULL);
  366. int32_t ret = closedir(dirWrapper->dir);
  367. if (dirWrapper->result)
  368. {
  369. for (int i = 0; i < dirWrapper->numEntries; i++)
  370. free(dirWrapper->result[i].Name);
  371. free(dirWrapper->result);
  372. }
  373. dirWrapper->result = NULL;
  374. #if !IL2CPP_HAVE_REWINDDIR
  375. if (dirWrapper->dirPath)
  376. free(dirWrapper->dirPath);
  377. #endif
  378. free(dirWrapper);
  379. return ret;
  380. }
  381. int32_t SystemNative_MkDir(const char* path, int32_t mode)
  382. {
  383. int32_t result = 0;
  384. while ((result = mkdir(REMAP_PATH(path), (mode_t)mode)) < 0 && errno == EINTR)
  385. ;
  386. return result;
  387. }
  388. int32_t SystemNative_ChMod(const char* path, int32_t mode)
  389. {
  390. int32_t result = 0;
  391. while ((result = chmod_(REMAP_PATH(path), (mode_t)mode)) < 0 && errno == EINTR)
  392. ;
  393. return result;
  394. }
  395. int32_t SystemNative_Link(const char* source, const char* linkTarget)
  396. {
  397. int32_t result = 0;
  398. while ((result = link_(REMAP_PATH(source), REMAP_PATH(linkTarget))) < 0 && errno == EINTR)
  399. ;
  400. return result;
  401. }
  402. int32_t SystemNative_Symlink(const char* target, const char* linkPath)
  403. {
  404. return symlink_(REMAP_PATH(target), REMAP_PATH(linkPath));
  405. }
  406. int32_t SystemNative_ReadLink(const char* path, char* buffer, int32_t bufferSize)
  407. {
  408. IL2CPP_ASSERT(buffer != NULL || bufferSize == 0);
  409. IL2CPP_ASSERT(bufferSize >= 0);
  410. if (bufferSize <= 0)
  411. {
  412. errno = EINVAL;
  413. return -1;
  414. }
  415. ssize_t count = readlink_(REMAP_PATH(path), buffer, (size_t)bufferSize);
  416. IL2CPP_ASSERT(count >= -1 && count <= bufferSize);
  417. return (int32_t)count;
  418. }
  419. int32_t SystemNative_Rename(const char* oldPath, const char* newPath)
  420. {
  421. int32_t result;
  422. while ((result = rename(REMAP_PATH(oldPath), REMAP_PATH(newPath))) < 0 && errno == EINTR)
  423. ;
  424. return result;
  425. }
  426. int32_t SystemNative_RmDir(const char* path)
  427. {
  428. int32_t result = 0;
  429. while ((result = rmdir(REMAP_PATH(path))) < 0 && errno == EINTR)
  430. ;
  431. return result;
  432. }
  433. #if !IL2CPP_HAVE_FCOPYFILE
  434. // Read all data from inFd and write it to outFd
  435. static int32_t CopyFile_ReadWrite(int inFd, int outFd)
  436. {
  437. // Allocate a buffer
  438. const int BufferLength = 80 * 1024 * sizeof(char);
  439. char* buffer = (char*)malloc(BufferLength);
  440. if (buffer == NULL)
  441. {
  442. return -1;
  443. }
  444. // Repeatedly read from the source and write to the destination
  445. while (true)
  446. {
  447. // Read up to what will fit in our buffer. We're done if we get back 0 bytes.
  448. ssize_t bytesRead;
  449. while ((bytesRead = read(inFd, buffer, BufferLength)) < 0 && errno == EINTR)
  450. ;
  451. if (bytesRead == -1)
  452. {
  453. int tmp = errno;
  454. free(buffer);
  455. errno = tmp;
  456. return -1;
  457. }
  458. if (bytesRead == 0)
  459. {
  460. break;
  461. }
  462. IL2CPP_ASSERT(bytesRead > 0);
  463. // Write what was read.
  464. ssize_t offset = 0;
  465. while (bytesRead > 0)
  466. {
  467. ssize_t bytesWritten;
  468. while ((bytesWritten = write(outFd, buffer + offset, (size_t)bytesRead)) < 0 && errno == EINTR)
  469. ;
  470. if (bytesWritten == -1)
  471. {
  472. int tmp = errno;
  473. free(buffer);
  474. errno = tmp;
  475. return -1;
  476. }
  477. IL2CPP_ASSERT(bytesWritten >= 0);
  478. bytesRead -= bytesWritten;
  479. offset += bytesWritten;
  480. }
  481. }
  482. free(buffer);
  483. return 0;
  484. }
  485. #endif // !IL2CPP_HAVE_FCOPYFILE
  486. int32_t SystemNative_CopyFile(intptr_t sourceFd, intptr_t destinationFd)
  487. {
  488. #if IL2CPP_HAVE_CUSTOM_COPYFILE
  489. return pal_custom_copy_file(sourceFd, destinationFd);
  490. #else
  491. int inFd, outFd;
  492. inFd = il2cpp::os::File::IsHandleOpenFileHandle(sourceFd) ? reinterpret_cast<il2cpp::os::FileHandle*>(sourceFd)->fd : static_cast<int>(sourceFd);
  493. outFd = il2cpp::os::File::IsHandleOpenFileHandle(destinationFd) ? reinterpret_cast<il2cpp::os::FileHandle*>(destinationFd)->fd : static_cast<int>(destinationFd);
  494. #if IL2CPP_HAVE_FCOPYFILE
  495. // If fcopyfile is available (OS X), try to use it, as the whole copy
  496. // can be performed in the kernel, without lots of unnecessary copying.
  497. // Copy data and metadata.
  498. return fcopyfile(inFd, outFd, NULL, COPYFILE_ALL) == 0 ? 0 : -1;
  499. #else
  500. // Get the stats on the source file.
  501. int ret;
  502. struct stat_ sourceStat;
  503. bool copied = false;
  504. // First, stat the source file.
  505. while ((ret = fstat_(inFd, &sourceStat)) < 0 && errno == EINTR)
  506. ;
  507. if (ret != 0)
  508. {
  509. // If we can't stat() it, then we likely don't have permission to read it.
  510. return -1;
  511. }
  512. // Copy permissions. This fchmod() needs to happen prior to writing anything into
  513. // the file to avoid possibly leaking any private data.
  514. while ((ret = fchmod(outFd, sourceStat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))) < 0 && errno == EINTR)
  515. ;
  516. #if !IL2CPP_CANNOT_MODIFY_FILE_PERMISSIONS
  517. if (ret != 0)
  518. {
  519. return -1;
  520. }
  521. #endif
  522. #if IL2CPP_HAVE_SENDFILE_4
  523. // If sendfile is available (Linux), try to use it, as the whole copy
  524. // can be performed in the kernel, without lots of unnecessary copying.
  525. // On 32-bit, if you use 64-bit offsets, the last argument of `sendfile' will be a
  526. // `size_t' a 32-bit integer while the `st_size' field of the stat structure will be off64_t.
  527. // So `size' will have to be `uint64_t'. In all other cases, it will be `size_t'.
  528. uint64_t size = (uint64_t)sourceStat.st_size;
  529. // Note that per man page for large files, you have to iterate until the
  530. // whole file is copied (Linux has a limit of 0x7ffff000 bytes copied).
  531. while (size > 0)
  532. {
  533. ssize_t sent = sendfile(outFd, inFd, NULL, (size >= SSIZE_MAX ? SSIZE_MAX : (size_t)size));
  534. if (sent < 0)
  535. {
  536. if (errno != EINVAL && errno != ENOSYS)
  537. {
  538. return -1;
  539. }
  540. else
  541. {
  542. break;
  543. }
  544. }
  545. else
  546. {
  547. IL2CPP_ASSERT((size_t)sent <= size);
  548. size -= (size_t)sent;
  549. }
  550. }
  551. if (size == 0)
  552. {
  553. copied = true;
  554. }
  555. // sendfile couldn't be used; fall back to a manual copy below. This could happen
  556. // if we're on an old kernel, for example, where sendfile could only be used
  557. // with sockets and not regular files.
  558. #endif // IL2CPP_HAVE_SENDFILE_4
  559. // Manually read all data from the source and write it to the destination.
  560. if (!copied && CopyFile_ReadWrite(inFd, outFd) != 0)
  561. {
  562. return -1;
  563. }
  564. // Now that the data from the file has been copied, copy over metadata
  565. // from the source file. First copy the file times.
  566. // If futimes nor futimes are available on this platform, file times will
  567. // not be copied over.
  568. #if IL2CPP_HAVE_FUTIMENS
  569. // futimens is prefered because it has a higher resolution.
  570. struct timespec origTimes[2];
  571. origTimes[0].tv_sec = (time_t)sourceStat.st_atime;
  572. origTimes[0].tv_nsec = ST_ATIME_NSEC(&sourceStat);
  573. origTimes[1].tv_sec = (time_t)sourceStat.st_mtime;
  574. origTimes[1].tv_nsec = ST_MTIME_NSEC(&sourceStat);
  575. while ((ret = futimens(outFd, origTimes)) < 0 && errno == EINTR)
  576. ;
  577. #elif IL2CPP_HAVE_FUTIMES
  578. struct timeval origTimes[2];
  579. origTimes[0].tv_sec = sourceStat.st_atime;
  580. origTimes[0].tv_usec = ST_ATIME_NSEC(&sourceStat) / 1000;
  581. origTimes[1].tv_sec = sourceStat.st_mtime;
  582. origTimes[1].tv_usec = ST_MTIME_NSEC(&sourceStat) / 1000;
  583. while ((ret = futimes(outFd, origTimes)) < 0 && errno == EINTR)
  584. ;
  585. #endif
  586. #if !IL2CPP_CANNOT_MODIFY_FILE_PERMISSIONS
  587. if (ret != 0)
  588. {
  589. return -1;
  590. }
  591. #endif
  592. return 0;
  593. #endif // IL2CPP_HAVE_FCOPYFILE
  594. #endif // IL2CPP_HAVE_CUSTOM_COPYFILE
  595. }
  596. int32_t SystemNative_LChflags(const char* path, uint32_t flags)
  597. {
  598. #if defined(UF_HIDDEN) && defined(IL2CPP_HAVE_STAT_FLAGS) && defined(IL2CPP_HAVE_LCHFLAGS)
  599. int32_t result;
  600. while ((result = lchflags(path, flags)) < 0 && errno == EINTR)
  601. ;
  602. return result;
  603. #else
  604. return -1;
  605. #endif
  606. }
  607. int32_t SystemNative_LChflagsCanSetHiddenFlag(void)
  608. {
  609. #if defined(UF_HIDDEN) && defined(IL2CPP_HAVE_STAT_FLAGS) && defined(IL2CPP_HAVE_LCHFLAGS)
  610. return true;
  611. #else
  612. return false;
  613. #endif
  614. }
  615. #endif