blob: fe2b74911e4e98c4bcbe5ea42c2f32e56d3886a6 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
9#include "src/core/SkOSFile.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>
Hal Canary2d0e1242018-03-01 12:32:18 -050022#include <vector>
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/utils/SkUTF.h"
epoger@google.come8ebeb12012-10-29 16:42:11 +000024#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
jvanverth44dcb8a2015-10-02 09:12:05 -070026#ifdef SK_BUILD_FOR_IOS
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/ports/SkOSFile_ios.h"
jvanverth44dcb8a2015-10-02 09:12:05 -070028#endif
29
Hal Canary2d0e1242018-03-01 12:32:18 -050030#ifdef _WIN32
31static 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
40static 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 Canaryf107a2f2018-07-25 16:52:48 -040049 SkUnichar u = SkUTF::NextUTF8(&ptr, end);
Hal Canary2d0e1242018-03-01 12:32:18 -050050 if (u < 0) {
51 return nullptr; // malformed UTF-8
52 }
Hal Canaryf107a2f2018-07-25 16:52:48 -040053 n += SkUTF::ToUTF16(u);
Hal Canary2d0e1242018-03-01 12:32:18 -050054 }
55 std::vector<uint16_t> wchars(n + 1);
56 uint16_t* out = wchars.data();
John Stiles13c9f662021-08-16 12:16:29 -040057 ptr = utf8path;
58 while (ptr < end) {
Hal Canaryf107a2f2018-07-25 16:52:48 -040059 out += SkUTF::ToUTF16(SkUTF::NextUTF8(&ptr, end), out);
Hal Canary2d0e1242018-03-01 12:32:18 -050060 }
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
halcanaryd76be9c2015-11-20 13:47:49 -080068FILE* sk_fopen(const char path[], SkFILE_Flags flags) {
Hal Canary2d0e1242018-03-01 12:32:18 -050069 char perm[4] = {0, 0, 0, 0};
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 char* p = perm;
71
bungeman@google.com6cab1a42013-05-29 13:43:31 +000072 if (flags & kRead_SkFILE_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 *p++ = 'r';
bungeman@google.com6cab1a42013-05-29 13:43:31 +000074 }
75 if (flags & kWrite_SkFILE_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 *p++ = 'w';
bungeman@google.com6cab1a42013-05-29 13:43:31 +000077 }
Hal Canary2d0e1242018-03-01 12:32:18 -050078 *p = 'b';
mtklein944c2d92016-02-16 08:10:34 -080079
halcanaryd76be9c2015-11-20 13:47:49 -080080 FILE* file = nullptr;
Hal Canary2d0e1242018-03-01 12:32:18 -050081#ifdef _WIN32
82 file = fopen_win(path, perm);
83#else
Jim Van Verth329c5a62017-11-29 11:42:33 -050084 file = fopen(path, perm);
Hal Canary2d0e1242018-03-01 12:32:18 -050085#endif
jvanverth44dcb8a2015-10-02 09:12:05 -070086#ifdef SK_BUILD_FOR_IOS
Jim Van Verth329c5a62017-11-29 11:42:33 -050087 // 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 }
jvanverth44dcb8a2015-10-02 09:12:05 -070093 }
94#endif
Jim Van Verth329c5a62017-11-29 11:42:33 -050095
bungeman0881b952015-09-02 12:41:35 -070096 if (nullptr == file && (flags & kWrite_SkFILE_Flag)) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -040097 SkDEBUGF("sk_fopen: fopen(\"%s\", \"%s\") returned nullptr (errno:%d): %s\n",
98 path, perm, errno, strerror(errno));
bungeman0881b952015-09-02 12:41:35 -070099 }
100 return file;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101}
102
Leon Scroggins564ad052017-05-23 13:29:14 +0000103size_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
halcanaryd76be9c2015-11-20 13:47:49 -0800121size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 SkASSERT(f);
mtklein944c2d92016-02-16 08:10:34 -0800123 return fwrite(buffer, 1, byteCount, f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124}
125
halcanaryd76be9c2015-11-20 13:47:49 -0800126void sk_fflush(FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 SkASSERT(f);
mtklein944c2d92016-02-16 08:10:34 -0800128 fflush(f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129}
130
caryclark7471fa42015-12-16 13:41:23 -0800131void sk_fsync(FILE* f) {
132#if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \
133 && !defined(_NEWLIB_VERSION)
mtklein944c2d92016-02-16 08:10:34 -0800134 int fd = fileno(f);
135 fsync(fd);
caryclark7471fa42015-12-16 13:41:23 -0800136#endif
137}
138
halcanaryd76be9c2015-11-20 13:47:49 -0800139size_t sk_ftell(FILE* f) {
mtklein944c2d92016-02-16 08:10:34 -0800140 long curr = ftell(f);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000141 if (curr < 0) {
142 return 0;
143 }
144 return curr;
145}
146
halcanaryd76be9c2015-11-20 13:47:49 -0800147void sk_fclose(FILE* f) {
Ben Wagner4d1955c2017-03-10 13:08:15 -0500148 if (f) {
149 fclose(f);
150 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151}
152
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000153bool sk_isdir(const char *path) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000154 struct stat status;
155 if (0 != stat(path, &status)) {
Jim Van Verth329c5a62017-11-29 11:42:33 -0500156#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.come8ebeb12012-10-29 16:42:11 +0000165 return false;
Jim Van Verth329c5a62017-11-29 11:42:33 -0500166#endif
epoger@google.come8ebeb12012-10-29 16:42:11 +0000167 }
scroggo@google.com6e725162012-11-01 16:28:23 +0000168 return SkToBool(status.st_mode & S_IFDIR);
epoger@google.come8ebeb12012-10-29 16:42:11 +0000169}
170
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000171bool sk_mkdir(const char* path) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000172 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 Denniston0cfcd1c2020-03-05 10:38:17 -0500187 if (retval) {
188 perror("mkdir() failed with error: ");
189 }
epoger@google.come8ebeb12012-10-29 16:42:11 +0000190#endif
Mike Kleine9034492017-09-16 10:16:09 -0400191 return 0 == retval;
epoger@google.come8ebeb12012-10-29 16:42:11 +0000192}