blob: f0082838045bff04ee2a4d0635558e43004a6225 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.com8a1c16f2008-12-17 15:59:43 +00008#include "SkOSFile.h"
bungemanf20488b2015-07-29 11:49:40 -07009#include "SkTypes.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include <errno.h>
epoger@google.come8ebeb12012-10-29 16:42:11 +000012#include <stdio.h>
13#include <sys/stat.h>
epoger@google.come8ebeb12012-10-29 16:42:11 +000014
abarth6fc8ff02016-07-15 15:15:15 -070015#ifdef SK_BUILD_FOR_UNIX
16#include <unistd.h>
17#endif
18
epoger@google.come8ebeb12012-10-29 16:42:11 +000019#ifdef _WIN32
20#include <direct.h>
21#include <io.h>
epoger@google.come8ebeb12012-10-29 16:42:11 +000022#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
jvanverth44dcb8a2015-10-02 09:12:05 -070024#ifdef SK_BUILD_FOR_IOS
25#import <CoreFoundation/CoreFoundation.h>
26
27static FILE* ios_open_from_bundle(const char path[], const char* perm) {
28 // Get a reference to the main bundle
29 CFBundleRef mainBundle = CFBundleGetMainBundle();
mtklein944c2d92016-02-16 08:10:34 -080030
jvanverth44dcb8a2015-10-02 09:12:05 -070031 // Get a reference to the file's URL
32 CFStringRef pathRef = CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8);
33 CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, pathRef, NULL, NULL);
34 if (!imageURL) {
35 return nullptr;
36 }
mtklein944c2d92016-02-16 08:10:34 -080037
jvanverth44dcb8a2015-10-02 09:12:05 -070038 // Convert the URL reference into a string reference
39 CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathStyle);
mtklein944c2d92016-02-16 08:10:34 -080040
jvanverth44dcb8a2015-10-02 09:12:05 -070041 // Get the system encoding method
42 CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
mtklein944c2d92016-02-16 08:10:34 -080043
jvanverth44dcb8a2015-10-02 09:12:05 -070044 // Convert the string reference into a C string
45 const char *finalPath = CFStringGetCStringPtr(imagePath, encodingMethod);
mtklein944c2d92016-02-16 08:10:34 -080046
jvanverth44dcb8a2015-10-02 09:12:05 -070047 return fopen(finalPath, perm);
48}
49#endif
50
51
halcanaryd76be9c2015-11-20 13:47:49 -080052FILE* sk_fopen(const char path[], SkFILE_Flags flags) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 char perm[4];
54 char* p = perm;
55
bungeman@google.com6cab1a42013-05-29 13:43:31 +000056 if (flags & kRead_SkFILE_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 *p++ = 'r';
bungeman@google.com6cab1a42013-05-29 13:43:31 +000058 }
59 if (flags & kWrite_SkFILE_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 *p++ = 'w';
bungeman@google.com6cab1a42013-05-29 13:43:31 +000061 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 *p++ = 'b';
63 *p = 0;
mtklein944c2d92016-02-16 08:10:34 -080064
commit-bot@chromium.org9711e442013-04-24 20:03:00 +000065 //TODO: on Windows fopen is just ASCII or the current code page,
66 //convert to utf16 and use _wfopen
halcanaryd76be9c2015-11-20 13:47:49 -080067 FILE* file = nullptr;
jvanverth44dcb8a2015-10-02 09:12:05 -070068#ifdef SK_BUILD_FOR_IOS
69 // if read-only, try to open from bundle first
70 if (kRead_SkFILE_Flag == flags) {
halcanaryd76be9c2015-11-20 13:47:49 -080071 file = ios_open_from_bundle(path, perm);
jvanverth44dcb8a2015-10-02 09:12:05 -070072 }
73 // otherwise just read from the Documents directory (default)
74 if (!file) {
75#endif
mtklein944c2d92016-02-16 08:10:34 -080076 file = fopen(path, perm);
jvanverth44dcb8a2015-10-02 09:12:05 -070077#ifdef SK_BUILD_FOR_IOS
78 }
79#endif
bungeman0881b952015-09-02 12:41:35 -070080 if (nullptr == file && (flags & kWrite_SkFILE_Flag)) {
81 SkDEBUGF(("sk_fopen: fopen(\"%s\", \"%s\") returned NULL (errno:%d): %s\n",
82 path, perm, errno, strerror(errno)));
83 }
84 return file;
reed@android.com8a1c16f2008-12-17 15:59:43 +000085}
86
halcanaryd76be9c2015-11-20 13:47:49 -080087char* sk_fgets(char* str, int size, FILE* f) {
mtklein944c2d92016-02-16 08:10:34 -080088 return fgets(str, size, (FILE *)f);
humper@google.com7af56be2013-01-14 18:49:19 +000089}
90
halcanaryd76be9c2015-11-20 13:47:49 -080091int sk_feof(FILE *f) {
humper@google.comd386b0e2013-01-14 22:01:14 +000092 // no :: namespace qualifier because it breaks android
93 return feof((FILE *)f);
humper@google.com7af56be2013-01-14 18:49:19 +000094}
95
halcanaryd76be9c2015-11-20 13:47:49 -080096size_t sk_fgetsize(FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 SkASSERT(f);
98
mtklein944c2d92016-02-16 08:10:34 -080099 long curr = ftell(f); // remember where we are
vandebo@chromium.org6390c722012-03-28 21:03:22 +0000100 if (curr < 0) {
101 return 0;
102 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000103
mtklein944c2d92016-02-16 08:10:34 -0800104 fseek(f, 0, SEEK_END); // go to the end
105 long size = ftell(f); // record the size
vandebo@chromium.org6390c722012-03-28 21:03:22 +0000106 if (size < 0) {
107 size = 0;
108 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000109
mtklein944c2d92016-02-16 08:10:34 -0800110 fseek(f, curr, SEEK_SET); // go back to our prev location
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 return size;
112}
113
halcanaryd76be9c2015-11-20 13:47:49 -0800114bool sk_frewind(FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 SkASSERT(f);
halcanaryd76be9c2015-11-20 13:47:49 -0800116 ::rewind(f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 return true;
118}
119
halcanaryd76be9c2015-11-20 13:47:49 -0800120size_t sk_fread(void* buffer, size_t byteCount, FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 SkASSERT(f);
halcanary96fcdcc2015-08-27 07:41:13 -0700122 if (buffer == nullptr) {
mtklein944c2d92016-02-16 08:10:34 -0800123 size_t curr = ftell(f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 if ((long)curr == -1) {
halcanaryd76be9c2015-11-20 13:47:49 -0800125 SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f, feof(f), ferror(f)));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 return 0;
127 }
mtklein944c2d92016-02-16 08:10:34 -0800128 int err = fseek(f, (long)byteCount, SEEK_CUR);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 if (err != 0) {
130 SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d returned:%d\n",
halcanaryd76be9c2015-11-20 13:47:49 -0800131 byteCount, curr, feof(f), ferror(f), err));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 return 0;
133 }
134 return byteCount;
135 }
136 else
mtklein944c2d92016-02-16 08:10:34 -0800137 return fread(buffer, 1, byteCount, f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138}
139
halcanaryd76be9c2015-11-20 13:47:49 -0800140size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 SkASSERT(f);
mtklein944c2d92016-02-16 08:10:34 -0800142 return fwrite(buffer, 1, byteCount, f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143}
144
halcanaryd76be9c2015-11-20 13:47:49 -0800145void sk_fflush(FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 SkASSERT(f);
mtklein944c2d92016-02-16 08:10:34 -0800147 fflush(f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148}
149
caryclark7471fa42015-12-16 13:41:23 -0800150void sk_fsync(FILE* f) {
151#if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \
152 && !defined(_NEWLIB_VERSION)
mtklein944c2d92016-02-16 08:10:34 -0800153 int fd = fileno(f);
154 fsync(fd);
caryclark7471fa42015-12-16 13:41:23 -0800155#endif
156}
157
halcanaryd76be9c2015-11-20 13:47:49 -0800158bool sk_fseek(FILE* f, size_t byteCount) {
mtklein944c2d92016-02-16 08:10:34 -0800159 int err = fseek(f, (long)byteCount, SEEK_SET);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000160 return err == 0;
161}
162
halcanaryd76be9c2015-11-20 13:47:49 -0800163bool sk_fmove(FILE* f, long byteCount) {
mtklein944c2d92016-02-16 08:10:34 -0800164 int err = fseek(f, byteCount, SEEK_CUR);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000165 return err == 0;
166}
167
halcanaryd76be9c2015-11-20 13:47:49 -0800168size_t sk_ftell(FILE* f) {
mtklein944c2d92016-02-16 08:10:34 -0800169 long curr = ftell(f);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000170 if (curr < 0) {
171 return 0;
172 }
173 return curr;
174}
175
halcanaryd76be9c2015-11-20 13:47:49 -0800176void sk_fclose(FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 SkASSERT(f);
mtklein944c2d92016-02-16 08:10:34 -0800178 fclose(f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179}
180
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000181bool sk_isdir(const char *path) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000182 struct stat status;
183 if (0 != stat(path, &status)) {
184 return false;
185 }
scroggo@google.com6e725162012-11-01 16:28:23 +0000186 return SkToBool(status.st_mode & S_IFDIR);
epoger@google.come8ebeb12012-10-29 16:42:11 +0000187}
188
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000189bool sk_mkdir(const char* path) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000190 if (sk_isdir(path)) {
191 return true;
192 }
193 if (sk_exists(path)) {
194 fprintf(stderr,
195 "sk_mkdir: path '%s' already exists but is not a directory\n",
196 path);
197 return false;
198 }
199
200 int retval;
201#ifdef _WIN32
202 retval = _mkdir(path);
203#else
204 retval = mkdir(path, 0777);
205#endif
206 if (0 == retval) {
207 return true;
208 } else {
209 fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path);
210 return false;
211 }
212}