SkOSFile_stdio.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkTypes.h"
  8. #include "src/core/SkOSFile.h"
  9. #include <errno.h>
  10. #include <stdio.h>
  11. #include <sys/stat.h>
  12. #ifdef SK_BUILD_FOR_UNIX
  13. #include <unistd.h>
  14. #endif
  15. #ifdef _WIN32
  16. #include <direct.h>
  17. #include <io.h>
  18. #include <vector>
  19. #include "src/utils/SkUTF.h"
  20. #endif
  21. #ifdef SK_BUILD_FOR_IOS
  22. #include "src/ports/SkOSFile_ios.h"
  23. #endif
  24. #ifdef _WIN32
  25. static bool is_ascii(const char* s) {
  26. while (char v = *s++) {
  27. if ((v & 0x80) != 0) {
  28. return false;
  29. }
  30. }
  31. return true;
  32. }
  33. static FILE* fopen_win(const char* utf8path, const char* perm) {
  34. if (is_ascii(utf8path)) {
  35. return fopen(utf8path, perm);
  36. }
  37. const char* ptr = utf8path;
  38. const char* end = utf8path + strlen(utf8path);
  39. size_t n = 0;
  40. while (ptr < end) {
  41. SkUnichar u = SkUTF::NextUTF8(&ptr, end);
  42. if (u < 0) {
  43. return nullptr; // malformed UTF-8
  44. }
  45. n += SkUTF::ToUTF16(u);
  46. }
  47. std::vector<uint16_t> wchars(n + 1);
  48. uint16_t* out = wchars.data();
  49. for (const char* ptr = utf8path; ptr < end;) {
  50. out += SkUTF::ToUTF16(SkUTF::NextUTF8(&ptr, end), out);
  51. }
  52. SkASSERT(out == &wchars[n]);
  53. *out = 0; // final null
  54. wchar_t wperms[4] = {(wchar_t)perm[0], (wchar_t)perm[1], (wchar_t)perm[2], (wchar_t)perm[3]};
  55. return _wfopen((wchar_t*)wchars.data(), wperms);
  56. }
  57. #endif
  58. FILE* sk_fopen(const char path[], SkFILE_Flags flags) {
  59. char perm[4] = {0, 0, 0, 0};
  60. char* p = perm;
  61. if (flags & kRead_SkFILE_Flag) {
  62. *p++ = 'r';
  63. }
  64. if (flags & kWrite_SkFILE_Flag) {
  65. *p++ = 'w';
  66. }
  67. *p = 'b';
  68. FILE* file = nullptr;
  69. #ifdef _WIN32
  70. file = fopen_win(path, perm);
  71. #else
  72. file = fopen(path, perm);
  73. #endif
  74. #ifdef SK_BUILD_FOR_IOS
  75. // if not found in default path and read-only, try to open from bundle
  76. if (!file && kRead_SkFILE_Flag == flags) {
  77. SkString bundlePath;
  78. if (ios_get_path_in_bundle(path, &bundlePath)) {
  79. file = fopen(bundlePath.c_str(), perm);
  80. }
  81. }
  82. #endif
  83. if (nullptr == file && (flags & kWrite_SkFILE_Flag)) {
  84. SkDEBUGF("sk_fopen: fopen(\"%s\", \"%s\") returned nullptr (errno:%d): %s\n",
  85. path, perm, errno, strerror(errno));
  86. }
  87. return file;
  88. }
  89. size_t sk_fgetsize(FILE* f) {
  90. SkASSERT(f);
  91. long curr = ftell(f); // remember where we are
  92. if (curr < 0) {
  93. return 0;
  94. }
  95. fseek(f, 0, SEEK_END); // go to the end
  96. long size = ftell(f); // record the size
  97. if (size < 0) {
  98. size = 0;
  99. }
  100. fseek(f, curr, SEEK_SET); // go back to our prev location
  101. return size;
  102. }
  103. size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* f) {
  104. SkASSERT(f);
  105. return fwrite(buffer, 1, byteCount, f);
  106. }
  107. void sk_fflush(FILE* f) {
  108. SkASSERT(f);
  109. fflush(f);
  110. }
  111. void sk_fsync(FILE* f) {
  112. #if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \
  113. && !defined(_NEWLIB_VERSION)
  114. int fd = fileno(f);
  115. fsync(fd);
  116. #endif
  117. }
  118. size_t sk_ftell(FILE* f) {
  119. long curr = ftell(f);
  120. if (curr < 0) {
  121. return 0;
  122. }
  123. return curr;
  124. }
  125. void sk_fclose(FILE* f) {
  126. if (f) {
  127. fclose(f);
  128. }
  129. }
  130. bool sk_isdir(const char *path) {
  131. struct stat status;
  132. if (0 != stat(path, &status)) {
  133. #ifdef SK_BUILD_FOR_IOS
  134. // check the bundle directory if not in default path
  135. SkString bundlePath;
  136. if (ios_get_path_in_bundle(path, &bundlePath)) {
  137. if (0 != stat(bundlePath.c_str(), &status)) {
  138. return false;
  139. }
  140. }
  141. #else
  142. return false;
  143. #endif
  144. }
  145. return SkToBool(status.st_mode & S_IFDIR);
  146. }
  147. bool sk_mkdir(const char* path) {
  148. if (sk_isdir(path)) {
  149. return true;
  150. }
  151. if (sk_exists(path)) {
  152. fprintf(stderr,
  153. "sk_mkdir: path '%s' already exists but is not a directory\n",
  154. path);
  155. return false;
  156. }
  157. int retval;
  158. #ifdef _WIN32
  159. retval = _mkdir(path);
  160. #else
  161. retval = mkdir(path, 0777);
  162. #endif
  163. return 0 == retval;
  164. }