File.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_LUMIN
  3. #include "os/Lumin/File.h"
  4. #include "os/Lumin/Lifecycle.h"
  5. #include "os/Posix/FileHandle.h"
  6. #include "utils/PathUtils.h"
  7. #include <fcntl.h>
  8. #include <sys/errno.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <string>
  12. namespace il2cpp
  13. {
  14. namespace os
  15. {
  16. namespace lumin
  17. {
  18. int s_StandardErrDescriptor = -1;
  19. int s_StandardOutDescriptor = -1;
  20. void InitializeFileHandles()
  21. {
  22. std::string base = GetPackageTempPath();
  23. std::string errPath = PathForErrorLog();
  24. std::string outPath = PathForOutputLog();
  25. s_StandardErrDescriptor = creat(errPath.c_str(), S_IRUSR | S_IWUSR);
  26. s_StandardOutDescriptor = creat(outPath.c_str(), S_IRUSR | S_IWUSR);
  27. }
  28. void CleanupFileHandles()
  29. {
  30. if (s_StandardErrDescriptor >= 0)
  31. {
  32. fdatasync(s_StandardErrDescriptor);
  33. close(s_StandardErrDescriptor);
  34. }
  35. if (s_StandardOutDescriptor >= 0)
  36. {
  37. fdatasync(s_StandardOutDescriptor);
  38. close(s_StandardOutDescriptor);
  39. }
  40. }
  41. std::string PathForErrorLog()
  42. {
  43. return il2cpp::utils::PathUtils::Combine(GetPackageTempPath(), std::string("stderr.log"));
  44. }
  45. std::string PathForOutputLog()
  46. {
  47. return il2cpp::utils::PathUtils::Combine(GetPackageTempPath(), std::string("stdout.log"));
  48. }
  49. }
  50. FileHandle* File::GetStdError()
  51. {
  52. static FileHandle* s_handle = NULL;
  53. if (s_handle)
  54. return s_handle;
  55. s_handle = new FileHandle();
  56. s_handle->fd = lumin::s_StandardErrDescriptor;
  57. s_handle->type = kFileTypeChar;
  58. s_handle->options = 0;
  59. s_handle->accessMode = kFileAccessReadWrite;
  60. s_handle->shareMode = -1; // Only used for files
  61. return s_handle;
  62. }
  63. FileHandle* File::GetStdInput()
  64. {
  65. static FileHandle* s_handle = NULL;
  66. if (s_handle)
  67. return s_handle;
  68. s_handle = new FileHandle();
  69. s_handle->fd = 0;
  70. s_handle->type = kFileTypeChar;
  71. s_handle->options = 0;
  72. s_handle->accessMode = kFileAccessRead;
  73. s_handle->shareMode = -1; // Only used for files
  74. return s_handle;
  75. }
  76. FileHandle* File::GetStdOutput()
  77. {
  78. static FileHandle* s_handle = NULL;
  79. if (s_handle)
  80. return s_handle;
  81. s_handle = new FileHandle();
  82. s_handle->fd = lumin::s_StandardOutDescriptor;
  83. s_handle->type = kFileTypeChar;
  84. s_handle->options = 0;
  85. s_handle->accessMode = kFileAccessReadWrite;
  86. s_handle->shareMode = -1; // Only used for files
  87. return s_handle;
  88. }
  89. }
  90. }
  91. #endif