blob: d8541c21c068eb23913bd38ab9e2f9a7e403905b [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>
Hal Canary2d0e1242018-03-01 12:32:18 -050022#include <vector>
23#include "SkUtils.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
Jim Van Verth329c5a62017-11-29 11:42:33 -050027#include "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) {
49 SkUnichar u = SkUTF8_NextUnicharWithError(&ptr, end);
50 if (u < 0) {
51 return nullptr; // malformed UTF-8
52 }
53 n += SkUTF16_FromUnichar(u);
54 }
55 std::vector<uint16_t> wchars(n + 1);
56 uint16_t* out = wchars.data();
57 for (const char* ptr = utf8path; ptr < end;) {
58 out += SkUTF16_FromUnichar(SkUTF8_NextUnicharWithError(&ptr, end), out);
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
halcanaryd76be9c2015-11-20 13:47:49 -080067FILE* sk_fopen(const char path[], SkFILE_Flags flags) {
Hal Canary2d0e1242018-03-01 12:32:18 -050068 char perm[4] = {0, 0, 0, 0};
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 char* p = perm;
70
bungeman@google.com6cab1a42013-05-29 13:43:31 +000071 if (flags & kRead_SkFILE_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 *p++ = 'r';
bungeman@google.com6cab1a42013-05-29 13:43:31 +000073 }
74 if (flags & kWrite_SkFILE_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 *p++ = 'w';
bungeman@google.com6cab1a42013-05-29 13:43:31 +000076 }
Hal Canary2d0e1242018-03-01 12:32:18 -050077 *p = 'b';
mtklein944c2d92016-02-16 08:10:34 -080078
halcanaryd76be9c2015-11-20 13:47:49 -080079 FILE* file = nullptr;
Hal Canary2d0e1242018-03-01 12:32:18 -050080#ifdef _WIN32
81 file = fopen_win(path, perm);
82#else
Jim Van Verth329c5a62017-11-29 11:42:33 -050083 file = fopen(path, perm);
Hal Canary2d0e1242018-03-01 12:32:18 -050084#endif
jvanverth44dcb8a2015-10-02 09:12:05 -070085#ifdef SK_BUILD_FOR_IOS
Jim Van Verth329c5a62017-11-29 11:42:33 -050086 // 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 }
jvanverth44dcb8a2015-10-02 09:12:05 -070092 }
93#endif
Jim Van Verth329c5a62017-11-29 11:42:33 -050094
bungeman0881b952015-09-02 12:41:35 -070095 if (nullptr == file && (flags & kWrite_SkFILE_Flag)) {
Ben Wagnera93a14a2017-08-28 10:34:05 -040096 SkDEBUGF(("sk_fopen: fopen(\"%s\", \"%s\") returned nullptr (errno:%d): %s\n",
bungeman0881b952015-09-02 12:41:35 -070097 path, perm, errno, strerror(errno)));
98 }
99 return file;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100}
101
Leon Scroggins564ad052017-05-23 13:29:14 +0000102size_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
halcanaryd76be9c2015-11-20 13:47:49 -0800120size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 SkASSERT(f);
mtklein944c2d92016-02-16 08:10:34 -0800122 return fwrite(buffer, 1, byteCount, f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123}
124
halcanaryd76be9c2015-11-20 13:47:49 -0800125void sk_fflush(FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 SkASSERT(f);
mtklein944c2d92016-02-16 08:10:34 -0800127 fflush(f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128}
129
caryclark7471fa42015-12-16 13:41:23 -0800130void sk_fsync(FILE* f) {
131#if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \
132 && !defined(_NEWLIB_VERSION)
mtklein944c2d92016-02-16 08:10:34 -0800133 int fd = fileno(f);
134 fsync(fd);
caryclark7471fa42015-12-16 13:41:23 -0800135#endif
136}
137
halcanaryd76be9c2015-11-20 13:47:49 -0800138size_t sk_ftell(FILE* f) {
mtklein944c2d92016-02-16 08:10:34 -0800139 long curr = ftell(f);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000140 if (curr < 0) {
141 return 0;
142 }
143 return curr;
144}
145
halcanaryd76be9c2015-11-20 13:47:49 -0800146void sk_fclose(FILE* f) {
Ben Wagner4d1955c2017-03-10 13:08:15 -0500147 if (f) {
148 fclose(f);
149 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150}
151
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000152bool sk_isdir(const char *path) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000153 struct stat status;
154 if (0 != stat(path, &status)) {
Jim Van Verth329c5a62017-11-29 11:42:33 -0500155#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.come8ebeb12012-10-29 16:42:11 +0000164 return false;
Jim Van Verth329c5a62017-11-29 11:42:33 -0500165#endif
epoger@google.come8ebeb12012-10-29 16:42:11 +0000166 }
scroggo@google.com6e725162012-11-01 16:28:23 +0000167 return SkToBool(status.st_mode & S_IFDIR);
epoger@google.come8ebeb12012-10-29 16:42:11 +0000168}
169
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000170bool sk_mkdir(const char* path) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000171 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 Kleine9034492017-09-16 10:16:09 -0400187 return 0 == retval;
epoger@google.come8ebeb12012-10-29 16:42:11 +0000188}