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