epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkOSFile.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 9 | #include "SkTypes.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include <errno.h> |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <sys/stat.h> |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 14 | |
abarth | 6fc8ff0 | 2016-07-15 15:15:15 -0700 | [diff] [blame] | 15 | #ifdef SK_BUILD_FOR_UNIX |
| 16 | #include <unistd.h> |
| 17 | #endif |
| 18 | |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 19 | #ifdef _WIN32 |
| 20 | #include <direct.h> |
| 21 | #include <io.h> |
Hal Canary | 2d0e124 | 2018-03-01 12:32:18 -0500 | [diff] [blame] | 22 | #include <vector> |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 23 | #include "SkUTF.h" |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 24 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 26 | #ifdef SK_BUILD_FOR_IOS |
Jim Van Verth | 329c5a6 | 2017-11-29 11:42:33 -0500 | [diff] [blame] | 27 | #include "SkOSFile_ios.h" |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 28 | #endif |
| 29 | |
Hal Canary | 2d0e124 | 2018-03-01 12:32:18 -0500 | [diff] [blame] | 30 | #ifdef _WIN32 |
| 31 | static bool is_ascii(const char* s) { |
| 32 | while (char v = *s++) { |
| 33 | if ((v & 0x80) != 0) { |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | static FILE* fopen_win(const char* utf8path, const char* perm) { |
| 41 | if (is_ascii(utf8path)) { |
| 42 | return fopen(utf8path, perm); |
| 43 | } |
| 44 | |
| 45 | const char* ptr = utf8path; |
| 46 | const char* end = utf8path + strlen(utf8path); |
| 47 | size_t n = 0; |
| 48 | while (ptr < end) { |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 49 | SkUnichar u = SkUTF::NextUTF8(&ptr, end); |
Hal Canary | 2d0e124 | 2018-03-01 12:32:18 -0500 | [diff] [blame] | 50 | if (u < 0) { |
| 51 | return nullptr; // malformed UTF-8 |
| 52 | } |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 53 | n += SkUTF::ToUTF16(u); |
Hal Canary | 2d0e124 | 2018-03-01 12:32:18 -0500 | [diff] [blame] | 54 | } |
| 55 | std::vector<uint16_t> wchars(n + 1); |
| 56 | uint16_t* out = wchars.data(); |
| 57 | for (const char* ptr = utf8path; ptr < end;) { |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 58 | out += SkUTF::ToUTF16(SkUTF::NextUTF8(&ptr, end), out); |
Hal Canary | 2d0e124 | 2018-03-01 12:32:18 -0500 | [diff] [blame] | 59 | } |
| 60 | SkASSERT(out == &wchars[n]); |
| 61 | *out = 0; // final null |
| 62 | wchar_t wperms[4] = {(wchar_t)perm[0], (wchar_t)perm[1], (wchar_t)perm[2], (wchar_t)perm[3]}; |
| 63 | return _wfopen((wchar_t*)wchars.data(), wperms); |
| 64 | } |
| 65 | #endif |
| 66 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 67 | FILE* sk_fopen(const char path[], SkFILE_Flags flags) { |
Hal Canary | 2d0e124 | 2018-03-01 12:32:18 -0500 | [diff] [blame] | 68 | char perm[4] = {0, 0, 0, 0}; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | char* p = perm; |
| 70 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 71 | if (flags & kRead_SkFILE_Flag) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | *p++ = 'r'; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 73 | } |
| 74 | if (flags & kWrite_SkFILE_Flag) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | *p++ = 'w'; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 76 | } |
Hal Canary | 2d0e124 | 2018-03-01 12:32:18 -0500 | [diff] [blame] | 77 | *p = 'b'; |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 78 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 79 | FILE* file = nullptr; |
Hal Canary | 2d0e124 | 2018-03-01 12:32:18 -0500 | [diff] [blame] | 80 | #ifdef _WIN32 |
| 81 | file = fopen_win(path, perm); |
| 82 | #else |
Jim Van Verth | 329c5a6 | 2017-11-29 11:42:33 -0500 | [diff] [blame] | 83 | file = fopen(path, perm); |
Hal Canary | 2d0e124 | 2018-03-01 12:32:18 -0500 | [diff] [blame] | 84 | #endif |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 85 | #ifdef SK_BUILD_FOR_IOS |
Jim Van Verth | 329c5a6 | 2017-11-29 11:42:33 -0500 | [diff] [blame] | 86 | // if not found in default path and read-only, try to open from bundle |
| 87 | if (!file && kRead_SkFILE_Flag == flags) { |
| 88 | SkString bundlePath; |
| 89 | if (ios_get_path_in_bundle(path, &bundlePath)) { |
| 90 | file = fopen(bundlePath.c_str(), perm); |
| 91 | } |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 92 | } |
| 93 | #endif |
Jim Van Verth | 329c5a6 | 2017-11-29 11:42:33 -0500 | [diff] [blame] | 94 | |
bungeman | 0881b95 | 2015-09-02 12:41:35 -0700 | [diff] [blame] | 95 | if (nullptr == file && (flags & kWrite_SkFILE_Flag)) { |
Hal Canary | 2b0e6cd | 2018-07-09 12:43:39 -0400 | [diff] [blame] | 96 | SkDEBUGF("sk_fopen: fopen(\"%s\", \"%s\") returned nullptr (errno:%d): %s\n", |
| 97 | path, perm, errno, strerror(errno)); |
bungeman | 0881b95 | 2015-09-02 12:41:35 -0700 | [diff] [blame] | 98 | } |
| 99 | return file; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Leon Scroggins | 564ad05 | 2017-05-23 13:29:14 +0000 | [diff] [blame] | 102 | size_t sk_fgetsize(FILE* f) { |
| 103 | SkASSERT(f); |
| 104 | |
| 105 | long curr = ftell(f); // remember where we are |
| 106 | if (curr < 0) { |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | fseek(f, 0, SEEK_END); // go to the end |
| 111 | long size = ftell(f); // record the size |
| 112 | if (size < 0) { |
| 113 | size = 0; |
| 114 | } |
| 115 | |
| 116 | fseek(f, curr, SEEK_SET); // go back to our prev location |
| 117 | return size; |
| 118 | } |
| 119 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 120 | size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* f) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | SkASSERT(f); |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 122 | return fwrite(buffer, 1, byteCount, f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | } |
| 124 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 125 | void sk_fflush(FILE* f) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | SkASSERT(f); |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 127 | fflush(f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | } |
| 129 | |
caryclark | 7471fa4 | 2015-12-16 13:41:23 -0800 | [diff] [blame] | 130 | void sk_fsync(FILE* f) { |
| 131 | #if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \ |
| 132 | && !defined(_NEWLIB_VERSION) |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 133 | int fd = fileno(f); |
| 134 | fsync(fd); |
caryclark | 7471fa4 | 2015-12-16 13:41:23 -0800 | [diff] [blame] | 135 | #endif |
| 136 | } |
| 137 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 138 | size_t sk_ftell(FILE* f) { |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 139 | long curr = ftell(f); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 140 | if (curr < 0) { |
| 141 | return 0; |
| 142 | } |
| 143 | return curr; |
| 144 | } |
| 145 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 146 | void sk_fclose(FILE* f) { |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 147 | if (f) { |
| 148 | fclose(f); |
| 149 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | } |
| 151 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 152 | bool sk_isdir(const char *path) { |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 153 | struct stat status; |
| 154 | if (0 != stat(path, &status)) { |
Jim Van Verth | 329c5a6 | 2017-11-29 11:42:33 -0500 | [diff] [blame] | 155 | #ifdef SK_BUILD_FOR_IOS |
| 156 | // check the bundle directory if not in default path |
| 157 | SkString bundlePath; |
| 158 | if (ios_get_path_in_bundle(path, &bundlePath)) { |
| 159 | if (0 != stat(bundlePath.c_str(), &status)) { |
| 160 | return false; |
| 161 | } |
| 162 | } |
| 163 | #else |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 164 | return false; |
Jim Van Verth | 329c5a6 | 2017-11-29 11:42:33 -0500 | [diff] [blame] | 165 | #endif |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 166 | } |
scroggo@google.com | 6e72516 | 2012-11-01 16:28:23 +0000 | [diff] [blame] | 167 | return SkToBool(status.st_mode & S_IFDIR); |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 168 | } |
| 169 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 170 | bool sk_mkdir(const char* path) { |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 171 | if (sk_isdir(path)) { |
| 172 | return true; |
| 173 | } |
| 174 | if (sk_exists(path)) { |
| 175 | fprintf(stderr, |
| 176 | "sk_mkdir: path '%s' already exists but is not a directory\n", |
| 177 | path); |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | int retval; |
| 182 | #ifdef _WIN32 |
| 183 | retval = _mkdir(path); |
| 184 | #else |
| 185 | retval = mkdir(path, 0777); |
| 186 | #endif |
Mike Klein | e903449 | 2017-09-16 10:16:09 -0400 | [diff] [blame] | 187 | return 0 == retval; |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 188 | } |