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> |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 22 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 24 | #ifdef SK_BUILD_FOR_IOS |
| 25 | #import <CoreFoundation/CoreFoundation.h> |
| 26 | |
| 27 | static FILE* ios_open_from_bundle(const char path[], const char* perm) { |
| 28 | // Get a reference to the main bundle |
| 29 | CFBundleRef mainBundle = CFBundleGetMainBundle(); |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 30 | |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 31 | // Get a reference to the file's URL |
| 32 | CFStringRef pathRef = CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8); |
| 33 | CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, pathRef, NULL, NULL); |
sdefresne | 388127f | 2016-09-20 08:53:14 -0700 | [diff] [blame] | 34 | CFRelease(pathRef); |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 35 | if (!imageURL) { |
| 36 | return nullptr; |
| 37 | } |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 38 | |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 39 | // Convert the URL reference into a string reference |
| 40 | CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathStyle); |
sdefresne | 67ba29c | 2016-09-21 06:51:33 -0700 | [diff] [blame] | 41 | CFRelease(imageURL); |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 42 | |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 43 | // Get the system encoding method |
| 44 | CFStringEncoding encodingMethod = CFStringGetSystemEncoding(); |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 45 | |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 46 | // Convert the string reference into a C string |
| 47 | const char *finalPath = CFStringGetCStringPtr(imagePath, encodingMethod); |
sdefresne | 67ba29c | 2016-09-21 06:51:33 -0700 | [diff] [blame] | 48 | FILE* fileHandle = fopen(finalPath, perm); |
| 49 | CFRelease(imagePath); |
| 50 | return fileHandle; |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 51 | } |
| 52 | #endif |
| 53 | |
| 54 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 55 | FILE* sk_fopen(const char path[], SkFILE_Flags flags) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | char perm[4]; |
| 57 | char* p = perm; |
| 58 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 59 | if (flags & kRead_SkFILE_Flag) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | *p++ = 'r'; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 61 | } |
| 62 | if (flags & kWrite_SkFILE_Flag) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | *p++ = 'w'; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 64 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | *p++ = 'b'; |
| 66 | *p = 0; |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 67 | |
commit-bot@chromium.org | 9711e44 | 2013-04-24 20:03:00 +0000 | [diff] [blame] | 68 | //TODO: on Windows fopen is just ASCII or the current code page, |
| 69 | //convert to utf16 and use _wfopen |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 70 | FILE* file = nullptr; |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 71 | #ifdef SK_BUILD_FOR_IOS |
| 72 | // if read-only, try to open from bundle first |
| 73 | if (kRead_SkFILE_Flag == flags) { |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 74 | file = ios_open_from_bundle(path, perm); |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 75 | } |
| 76 | // otherwise just read from the Documents directory (default) |
| 77 | if (!file) { |
| 78 | #endif |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 79 | file = fopen(path, perm); |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 80 | #ifdef SK_BUILD_FOR_IOS |
| 81 | } |
| 82 | #endif |
bungeman | 0881b95 | 2015-09-02 12:41:35 -0700 | [diff] [blame] | 83 | if (nullptr == file && (flags & kWrite_SkFILE_Flag)) { |
| 84 | SkDEBUGF(("sk_fopen: fopen(\"%s\", \"%s\") returned NULL (errno:%d): %s\n", |
| 85 | path, perm, errno, strerror(errno))); |
| 86 | } |
| 87 | return file; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | } |
| 89 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 90 | char* sk_fgets(char* str, int size, FILE* f) { |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 91 | return fgets(str, size, (FILE *)f); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 92 | } |
| 93 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 94 | int sk_feof(FILE *f) { |
humper@google.com | d386b0e | 2013-01-14 22:01:14 +0000 | [diff] [blame] | 95 | // no :: namespace qualifier because it breaks android |
| 96 | return feof((FILE *)f); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 97 | } |
| 98 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 99 | size_t sk_fgetsize(FILE* f) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | SkASSERT(f); |
| 101 | |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 102 | long curr = ftell(f); // remember where we are |
vandebo@chromium.org | 6390c72 | 2012-03-28 21:03:22 +0000 | [diff] [blame] | 103 | if (curr < 0) { |
| 104 | return 0; |
| 105 | } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 106 | |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 107 | fseek(f, 0, SEEK_END); // go to the end |
| 108 | long size = ftell(f); // record the size |
vandebo@chromium.org | 6390c72 | 2012-03-28 21:03:22 +0000 | [diff] [blame] | 109 | if (size < 0) { |
| 110 | size = 0; |
| 111 | } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 112 | |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 113 | fseek(f, curr, SEEK_SET); // go back to our prev location |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | return size; |
| 115 | } |
| 116 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 117 | bool sk_frewind(FILE* f) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | SkASSERT(f); |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 119 | ::rewind(f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 123 | size_t sk_fread(void* buffer, size_t byteCount, FILE* f) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | SkASSERT(f); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 125 | if (buffer == nullptr) { |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 126 | size_t curr = ftell(f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | if ((long)curr == -1) { |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 128 | SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f, feof(f), ferror(f))); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | return 0; |
| 130 | } |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 131 | int err = fseek(f, (long)byteCount, SEEK_CUR); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | if (err != 0) { |
| 133 | SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d returned:%d\n", |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 134 | byteCount, curr, feof(f), ferror(f), err)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | return byteCount; |
| 138 | } |
| 139 | else |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 140 | return fread(buffer, 1, byteCount, f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | } |
| 142 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 143 | 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] | 144 | SkASSERT(f); |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 145 | return fwrite(buffer, 1, byteCount, f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | } |
| 147 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 148 | void sk_fflush(FILE* f) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | SkASSERT(f); |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 150 | fflush(f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | } |
| 152 | |
caryclark | 7471fa4 | 2015-12-16 13:41:23 -0800 | [diff] [blame] | 153 | void sk_fsync(FILE* f) { |
| 154 | #if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \ |
| 155 | && !defined(_NEWLIB_VERSION) |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 156 | int fd = fileno(f); |
| 157 | fsync(fd); |
caryclark | 7471fa4 | 2015-12-16 13:41:23 -0800 | [diff] [blame] | 158 | #endif |
| 159 | } |
| 160 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 161 | bool sk_fseek(FILE* f, size_t byteCount) { |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 162 | int err = fseek(f, (long)byteCount, SEEK_SET); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 163 | return err == 0; |
| 164 | } |
| 165 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 166 | bool sk_fmove(FILE* f, long byteCount) { |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 167 | int err = fseek(f, byteCount, SEEK_CUR); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 168 | return err == 0; |
| 169 | } |
| 170 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 171 | size_t sk_ftell(FILE* f) { |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 172 | long curr = ftell(f); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 173 | if (curr < 0) { |
| 174 | return 0; |
| 175 | } |
| 176 | return curr; |
| 177 | } |
| 178 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 179 | void sk_fclose(FILE* f) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 180 | SkASSERT(f); |
mtklein | 944c2d9 | 2016-02-16 08:10:34 -0800 | [diff] [blame] | 181 | fclose(f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | } |
| 183 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 184 | bool sk_isdir(const char *path) { |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 185 | struct stat status; |
| 186 | if (0 != stat(path, &status)) { |
| 187 | return false; |
| 188 | } |
scroggo@google.com | 6e72516 | 2012-11-01 16:28:23 +0000 | [diff] [blame] | 189 | return SkToBool(status.st_mode & S_IFDIR); |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 190 | } |
| 191 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 192 | bool sk_mkdir(const char* path) { |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 193 | if (sk_isdir(path)) { |
| 194 | return true; |
| 195 | } |
| 196 | if (sk_exists(path)) { |
| 197 | fprintf(stderr, |
| 198 | "sk_mkdir: path '%s' already exists but is not a directory\n", |
| 199 | path); |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | int retval; |
| 204 | #ifdef _WIN32 |
| 205 | retval = _mkdir(path); |
| 206 | #else |
| 207 | retval = mkdir(path, 0777); |
| 208 | #endif |
| 209 | if (0 == retval) { |
| 210 | return true; |
| 211 | } else { |
| 212 | fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path); |
| 213 | return false; |
| 214 | } |
| 215 | } |