pal_io.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. outputEntry->NameLength = entry->d_namlen;
  191. #else
  192. outputEntry->NameLength = -1; // sentinel value to mean we have to walk to find the first \0
  193. #endif
  194. }
  195. #if IL2CPP_HAVE_READDIR_R_DEPRECATED_DO_NOT_USE
  196. // struct dirent typically contains 64-bit numbers (e.g. d_ino), so we align it at 8-byte.
  197. static const size_t dirent_alignment = 8;
  198. #endif
  199. int32_t SystemNative_GetReadDirRBufferSize(void)
  200. {
  201. #if IL2CPP_HAVE_READDIR_R_DEPRECATED_DO_NOT_USE
  202. // dirent should be under 2k in size
  203. IL2CPP_ASSERT(sizeof(struct dirent) < 2048);
  204. // add some extra space so we can align the buffer to dirent.
  205. return sizeof(struct dirent) + dirent_alignment - 1;
  206. #else
  207. return 0;
  208. #endif
  209. }
  210. static int CompareByName(const void *p1, const void *p2)
  211. {
  212. auto directoryEntry1 = ((struct DirectoryEntry*)p1);
  213. auto directoryEntry2 = ((struct DirectoryEntry*)p2);
  214. // Sort NULL values to the end of the array. This can happen when
  215. // a file is deleted while GetFiles is called.
  216. if (directoryEntry1->Name == directoryEntry2->Name)
  217. return 0;
  218. if (directoryEntry1->Name == NULL)
  219. return 1;
  220. if (directoryEntry2->Name == NULL)
  221. return -1;
  222. return strcmp(directoryEntry1->Name, directoryEntry2->Name);
  223. }
  224. // To reduce the number of string copies, the caller of this function is responsible to ensure the memory
  225. // referenced by outputEntry remains valid until it is read.
  226. // If the platform supports readdir_r, the caller provides a buffer into which the data is read.
  227. // If the platform uses readdir, the caller must ensure no calls are made to readdir/closedir since those will invalidate
  228. // the current dirent. We assume the platform supports concurrent readdir calls to different DIRs.
  229. int32_t SystemNative_ReadDirR(struct DIRWrapper* dirWrapper, uint8_t* buffer, int32_t bufferSize, struct DirectoryEntry* outputEntry)
  230. {
  231. IL2CPP_ASSERT(dirWrapper != NULL);
  232. IL2CPP_ASSERT(dirWrapper->dir != NULL);
  233. IL2CPP_ASSERT(outputEntry != NULL);
  234. #if IL2CPP_HAVE_READDIR_R_DEPRECATED_DO_NOT_USE
  235. IL2CPP_ASSERT(buffer != NULL);
  236. // align to dirent
  237. struct dirent* entry = (struct dirent*)((size_t)(buffer + dirent_alignment - 1) & ~(dirent_alignment - 1));
  238. // check there is dirent size available at entry
  239. if ((buffer + bufferSize) < ((uint8_t*)entry + sizeof(struct dirent)))
  240. {
  241. IL2CPP_ASSERT(false && "Buffer size too small; use GetReadDirRBufferSize to get required buffer size");
  242. return ERANGE;
  243. }
  244. struct dirent* result = NULL;
  245. #ifdef _AIX
  246. // AIX returns 0 on success, but bizarrely, it returns 9 for both error and
  247. // end-of-directory. result is NULL for both cases. The API returns the
  248. // same thing for EOD/error, so disambiguation between the two is nearly
  249. // impossible without clobbering errno for yourself and seeing if the API
  250. // changed it. See:
  251. // https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.basetrf2/readdir_r.htm
  252. errno = 0; // create a success condition for the API to clobber
  253. int error = readdir_r(dir, entry, &result);
  254. if (error == 9)
  255. {
  256. memset(outputEntry, 0, sizeof(*outputEntry)); // managed out param must be initialized
  257. return errno == 0 ? -1 : errno;
  258. }
  259. #else
  260. int error = readdir_r(dir, entry, &result);
  261. // positive error number returned -> failure
  262. if (error != 0)
  263. {
  264. IL2CPP_ASSERT(error > 0);
  265. memset(outputEntry, 0, sizeof(*outputEntry)); // managed out param must be initialized
  266. return error;
  267. }
  268. // 0 returned with null result -> end-of-stream
  269. if (result == NULL)
  270. {
  271. memset(outputEntry, 0, sizeof(*outputEntry)); // managed out param must be initialized
  272. return -1; // shim convention for end-of-stream
  273. }
  274. #endif
  275. // 0 returned with non-null result (guaranteed to be set to entry arg) -> success
  276. IL2CPP_ASSERT(result == entry);
  277. #else
  278. (void)buffer; // unused
  279. (void)bufferSize; // unused
  280. errno = 0;
  281. bool endOfEntries = false;
  282. if (!dirWrapper->result)
  283. {
  284. struct dirent* entry;
  285. size_t numEntries = 0;
  286. while ((entry = readdir(dirWrapper->dir)))
  287. numEntries++;
  288. if (numEntries)
  289. {
  290. // Use calloc to ensure the array is zero-initialized.
  291. dirWrapper->result = (DirectoryEntry*)calloc(numEntries, sizeof(struct DirectoryEntry));
  292. dirWrapper->curIndex = 0;
  293. #if IL2CPP_HAVE_REWINDDIR
  294. rewinddir(dirWrapper->dir);
  295. #else
  296. closedir(dirWrapper->dir);
  297. dirWrapper->dir = opendir(dirWrapper->dirPath);
  298. #endif
  299. // If we iterate fewer entries than exist because some files were deleted
  300. // since the time we computed numEntries above, that will be fine. Those
  301. // extra entries will be zero-initialized and will be sorted to the end
  302. // of the array by the qsort below.
  303. size_t index = 0;
  304. while ((entry = readdir(dirWrapper->dir)) && index < numEntries)
  305. {
  306. ConvertDirent(entry, &dirWrapper->result[index]);
  307. index++;
  308. }
  309. qsort(dirWrapper->result, numEntries, sizeof(struct DirectoryEntry), CompareByName);
  310. dirWrapper->numEntries = index;
  311. }
  312. }
  313. if (dirWrapper->curIndex < dirWrapper->numEntries)
  314. {
  315. *outputEntry = dirWrapper->result[dirWrapper->curIndex];
  316. dirWrapper->curIndex++;
  317. }
  318. else
  319. {
  320. endOfEntries = true;
  321. }
  322. // 0 returned with null result -> end-of-stream
  323. if (endOfEntries)
  324. {
  325. memset(outputEntry, 0, sizeof(*outputEntry)); // managed out param must be initialized
  326. // kernel set errno -> failure
  327. if (errno != 0)
  328. {
  329. IL2CPP_ASSERT(errno == EBADF && "Invalid directory stream descriptor dir" && errno);
  330. return errno;
  331. }
  332. return -1;
  333. }
  334. #endif
  335. return 0;
  336. }
  337. struct DIRWrapper* SystemNative_OpenDir(const char* path)
  338. {
  339. const char* remapped_path = NULL;
  340. #if IL2CPP_HAVE_REMAP_PATH
  341. auto remapped_path_string = pal_remap_path(path);
  342. remapped_path = remapped_path_string.c_str();
  343. #else
  344. remapped_path = path;
  345. #endif
  346. DIR* dir = opendir(remapped_path);
  347. if (dir == NULL)
  348. return NULL;
  349. struct DIRWrapper* ret = (struct DIRWrapper*)malloc(sizeof(struct DIRWrapper));
  350. ret->dir = dir;
  351. ret->result = NULL;
  352. ret->curIndex = 0;
  353. ret->numEntries = 0;
  354. #if !IL2CPP_HAVE_REWINDDIR
  355. ret->dirPath = strdup(remapped_path);
  356. #endif
  357. return ret;
  358. }
  359. int32_t SystemNative_CloseDir(struct DIRWrapper* dirWrapper)
  360. {
  361. IL2CPP_ASSERT(dirWrapper != NULL);
  362. int32_t ret = closedir(dirWrapper->dir);
  363. if (dirWrapper->result)
  364. {
  365. for (int i = 0; i < dirWrapper->numEntries; i++)
  366. free(dirWrapper->result[i].Name);
  367. free(dirWrapper->result);
  368. }
  369. dirWrapper->result = NULL;
  370. #if !IL2CPP_HAVE_REWINDDIR
  371. if (dirWrapper->dirPath)
  372. free(dirWrapper->dirPath);
  373. #endif
  374. free(dirWrapper);
  375. return ret;
  376. }
  377. int32_t SystemNative_MkDir(const char* path, int32_t mode)
  378. {
  379. int32_t result = 0;
  380. while ((result = mkdir(REMAP_PATH(path), (mode_t)mode)) < 0 && errno == EINTR)
  381. ;
  382. return result;
  383. }
  384. int32_t SystemNative_ChMod(const char* path, int32_t mode)
  385. {
  386. int32_t result = 0;
  387. while ((result = chmod_(REMAP_PATH(path), (mode_t)mode)) < 0 && errno == EINTR)
  388. ;
  389. return result;
  390. }
  391. int32_t SystemNative_Link(const char* source, const char* linkTarget)
  392. {
  393. int32_t result = 0;
  394. while ((result = link_(REMAP_PATH(source), REMAP_PATH(linkTarget))) < 0 && errno == EINTR)
  395. ;
  396. return result;
  397. }
  398. int32_t SystemNative_Symlink(const char* target, const char* linkPath)
  399. {
  400. return symlink_(REMAP_PATH(target), REMAP_PATH(linkPath));
  401. }
  402. int32_t SystemNative_ReadLink(const char* path, char* buffer, int32_t bufferSize)
  403. {
  404. IL2CPP_ASSERT(buffer != NULL || bufferSize == 0);
  405. IL2CPP_ASSERT(bufferSize >= 0);
  406. if (bufferSize <= 0)
  407. {
  408. errno = EINVAL;
  409. return -1;
  410. }
  411. ssize_t count = readlink_(REMAP_PATH(path), buffer, (size_t)bufferSize);
  412. IL2CPP_ASSERT(count >= -1 && count <= bufferSize);
  413. return (int32_t)count;
  414. }
  415. int32_t SystemNative_Rename(const char* oldPath, const char* newPath)
  416. {
  417. int32_t result;
  418. while ((result = rename(REMAP_PATH(oldPath), REMAP_PATH(newPath))) < 0 && errno == EINTR)
  419. ;
  420. return result;
  421. }
  422. int32_t SystemNative_RmDir(const char* path)
  423. {
  424. int32_t result = 0;
  425. while ((result = rmdir(REMAP_PATH(path))) < 0 && errno == EINTR)
  426. ;
  427. return result;
  428. }
  429. #if !IL2CPP_HAVE_FCOPYFILE
  430. // Read all data from inFd and write it to outFd
  431. static int32_t CopyFile_ReadWrite(int inFd, int outFd)
  432. {
  433. // Allocate a buffer
  434. const int BufferLength = 80 * 1024 * sizeof(char);
  435. char* buffer = (char*)malloc(BufferLength);
  436. if (buffer == NULL)
  437. {
  438. return -1;
  439. }
  440. // Repeatedly read from the source and write to the destination
  441. while (true)
  442. {
  443. // Read up to what will fit in our buffer. We're done if we get back 0 bytes.
  444. ssize_t bytesRead;
  445. while ((bytesRead = read(inFd, buffer, BufferLength)) < 0 && errno == EINTR)
  446. ;
  447. if (bytesRead == -1)
  448. {
  449. int tmp = errno;
  450. free(buffer);
  451. errno = tmp;
  452. return -1;
  453. }
  454. if (bytesRead == 0)
  455. {
  456. break;
  457. }
  458. IL2CPP_ASSERT(bytesRead > 0);
  459. // Write what was read.
  460. ssize_t offset = 0;
  461. while (bytesRead > 0)
  462. {
  463. ssize_t bytesWritten;
  464. while ((bytesWritten = write(outFd, buffer + offset, (size_t)bytesRead)) < 0 && errno == EINTR)
  465. ;
  466. if (bytesWritten == -1)
  467. {
  468. int tmp = errno;
  469. free(buffer);
  470. errno = tmp;
  471. return -1;
  472. }
  473. IL2CPP_ASSERT(bytesWritten >= 0);
  474. bytesRead -= bytesWritten;
  475. offset += bytesWritten;
  476. }
  477. }
  478. free(buffer);
  479. return 0;
  480. }
  481. #endif // !IL2CPP_HAVE_FCOPYFILE
  482. int32_t SystemNative_CopyFile(intptr_t sourceFd, intptr_t destinationFd)
  483. {
  484. #if IL2CPP_HAVE_CUSTOM_COPYFILE
  485. return pal_custom_copy_file(sourceFd, destinationFd);
  486. #else
  487. int inFd, outFd;
  488. inFd = il2cpp::os::File::IsHandleOpenFileHandle(sourceFd) ? reinterpret_cast<il2cpp::os::FileHandle*>(sourceFd)->fd : static_cast<int>(sourceFd);
  489. outFd = il2cpp::os::File::IsHandleOpenFileHandle(destinationFd) ? reinterpret_cast<il2cpp::os::FileHandle*>(destinationFd)->fd : static_cast<int>(destinationFd);
  490. #if IL2CPP_HAVE_FCOPYFILE
  491. // If fcopyfile is available (OS X), try to use it, as the whole copy
  492. // can be performed in the kernel, without lots of unnecessary copying.
  493. // Copy data and metadata.
  494. return fcopyfile(inFd, outFd, NULL, COPYFILE_ALL) == 0 ? 0 : -1;
  495. #else
  496. // Get the stats on the source file.
  497. int ret;
  498. struct stat_ sourceStat;
  499. bool copied = false;
  500. // First, stat the source file.
  501. while ((ret = fstat_(inFd, &sourceStat)) < 0 && errno == EINTR)
  502. ;
  503. if (ret != 0)
  504. {
  505. // If we can't stat() it, then we likely don't have permission to read it.
  506. return -1;
  507. }
  508. // Copy permissions. This fchmod() needs to happen prior to writing anything into
  509. // the file to avoid possibly leaking any private data.
  510. while ((ret = fchmod(outFd, sourceStat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))) < 0 && errno == EINTR)
  511. ;
  512. #if !IL2CPP_CANNOT_MODIFY_FILE_PERMISSIONS
  513. if (ret != 0)
  514. {
  515. return -1;
  516. }
  517. #endif
  518. #if IL2CPP_HAVE_SENDFILE_4
  519. // If sendfile is available (Linux), try to use it, as the whole copy
  520. // can be performed in the kernel, without lots of unnecessary copying.
  521. // On 32-bit, if you use 64-bit offsets, the last argument of `sendfile' will be a
  522. // `size_t' a 32-bit integer while the `st_size' field of the stat structure will be off64_t.
  523. // So `size' will have to be `uint64_t'. In all other cases, it will be `size_t'.
  524. uint64_t size = (uint64_t)sourceStat.st_size;
  525. // Note that per man page for large files, you have to iterate until the
  526. // whole file is copied (Linux has a limit of 0x7ffff000 bytes copied).
  527. while (size > 0)
  528. {
  529. ssize_t sent = sendfile(outFd, inFd, NULL, (size >= SSIZE_MAX ? SSIZE_MAX : (size_t)size));
  530. if (sent < 0)
  531. {
  532. if (errno != EINVAL && errno != ENOSYS)
  533. {
  534. return -1;
  535. }
  536. else
  537. {
  538. break;
  539. }
  540. }
  541. else
  542. {
  543. IL2CPP_ASSERT((size_t)sent <= size);
  544. size -= (size_t)sent;
  545. }
  546. }
  547. if (size == 0)
  548. {
  549. copied = true;
  550. }
  551. // sendfile couldn't be used; fall back to a manual copy below. This could happen
  552. // if we're on an old kernel, for example, where sendfile could only be used
  553. // with sockets and not regular files.
  554. #endif // IL2CPP_HAVE_SENDFILE_4
  555. // Manually read all data from the source and write it to the destination.
  556. if (!copied && CopyFile_ReadWrite(inFd, outFd) != 0)
  557. {
  558. return -1;
  559. }
  560. // Now that the data from the file has been copied, copy over metadata
  561. // from the source file. First copy the file times.
  562. // If futimes nor futimes are available on this platform, file times will
  563. // not be copied over.
  564. #if IL2CPP_HAVE_FUTIMENS
  565. // futimens is prefered because it has a higher resolution.
  566. struct timespec origTimes[2];
  567. origTimes[0].tv_sec = (time_t)sourceStat.st_atime;
  568. origTimes[0].tv_nsec = ST_ATIME_NSEC(&sourceStat);
  569. origTimes[1].tv_sec = (time_t)sourceStat.st_mtime;
  570. origTimes[1].tv_nsec = ST_MTIME_NSEC(&sourceStat);
  571. while ((ret = futimens(outFd, origTimes)) < 0 && errno == EINTR)
  572. ;
  573. #elif IL2CPP_HAVE_FUTIMES
  574. struct timeval origTimes[2];
  575. origTimes[0].tv_sec = sourceStat.st_atime;
  576. origTimes[0].tv_usec = ST_ATIME_NSEC(&sourceStat) / 1000;
  577. origTimes[1].tv_sec = sourceStat.st_mtime;
  578. origTimes[1].tv_usec = ST_MTIME_NSEC(&sourceStat) / 1000;
  579. while ((ret = futimes(outFd, origTimes)) < 0 && errno == EINTR)
  580. ;
  581. #endif
  582. #if !IL2CPP_CANNOT_MODIFY_FILE_PERMISSIONS
  583. if (ret != 0)
  584. {
  585. return -1;
  586. }
  587. #endif
  588. return 0;
  589. #endif // IL2CPP_HAVE_FCOPYFILE
  590. #endif // IL2CPP_HAVE_CUSTOM_COPYFILE
  591. }
  592. int32_t SystemNative_LChflags(const char* path, uint32_t flags)
  593. {
  594. #if defined(UF_HIDDEN) && defined(IL2CPP_HAVE_STAT_FLAGS) && defined(IL2CPP_HAVE_LCHFLAGS)
  595. int32_t result;
  596. while ((result = lchflags(path, flags)) < 0 && errno == EINTR)
  597. ;
  598. return result;
  599. #else
  600. return -1;
  601. #endif
  602. }
  603. int32_t SystemNative_LChflagsCanSetHiddenFlag(void)
  604. {
  605. #if defined(UF_HIDDEN) && defined(IL2CPP_HAVE_STAT_FLAGS) && defined(IL2CPP_HAVE_LCHFLAGS)
  606. return true;
  607. #else
  608. return false;
  609. #endif
  610. }
  611. #endif