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 
8 #include "include/core/SkTypes.h"
9 #include "src/core/SkOSFile.h"
10 
11 #include <errno.h>
12 #include <stdio.h>
13 #include <sys/stat.h>
14 
15 #ifdef SK_BUILD_FOR_UNIX
16 #include <unistd.h>
17 #endif
18 
19 #ifdef _WIN32
20 #include <direct.h>
21 #include <io.h>
22 #include <vector>
23 #include "src/utils/SkUTF.h"
24 #endif
25 
26 #ifdef SK_BUILD_FOR_IOS
27 #include "src/ports/SkOSFile_ios.h"
28 #endif
29 
30 #ifdef _WIN32
is_ascii(const char * s)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 
fopen_win(const char * utf8path,const char * perm)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) {
49         SkUnichar u = SkUTF::NextUTF8(&ptr, end);
50         if (u < 0) {
51             return nullptr;  // malformed UTF-8
52         }
53         n += SkUTF::ToUTF16(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 += SkUTF::ToUTF16(SkUTF::NextUTF8(&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 
sk_fopen(const char path[],SkFILE_Flags flags)67 FILE* sk_fopen(const char path[], SkFILE_Flags flags) {
68     char    perm[4] = {0, 0, 0, 0};
69     char*   p = perm;
70 
71     if (flags & kRead_SkFILE_Flag) {
72         *p++ = 'r';
73     }
74     if (flags & kWrite_SkFILE_Flag) {
75         SkASSERT(!(flags & kAppend_SkFILE_Flag));
76         *p++ = 'w';
77     } else if (flags & kAppend_SkFILE_Flag) {
78         *p++ = 'a';
79     }
80     *p = 'b';
81 
82     FILE* file = nullptr;
83 #ifdef _WIN32
84     file = fopen_win(path, perm);
85 #else
86     file = fopen(path, perm);
87 #endif
88 #ifdef SK_BUILD_FOR_IOS
89     // if not found in default path and read-only, try to open from bundle
90     if (!file && kRead_SkFILE_Flag == flags) {
91         SkString bundlePath;
92         if (ios_get_path_in_bundle(path, &bundlePath)) {
93             file = fopen(bundlePath.c_str(), perm);
94         }
95     }
96 #endif
97 
98     if (nullptr == file && (flags & kWrite_SkFILE_Flag)) {
99         SkDEBUGF("sk_fopen: fopen(\"%s\", \"%s\") returned nullptr (errno:%d): %s\n",
100                  path, perm, errno, strerror(errno));
101     }
102     return file;
103 }
104 
sk_fgetsize(FILE * f)105 size_t sk_fgetsize(FILE* f) {
106     SkASSERT(f);
107 
108     long curr = ftell(f); // remember where we are
109     if (curr < 0) {
110         return 0;
111     }
112 
113     fseek(f, 0, SEEK_END); // go to the end
114     long size = ftell(f); // record the size
115     if (size < 0) {
116         size = 0;
117     }
118 
119     fseek(f, curr, SEEK_SET); // go back to our prev location
120     return size;
121 }
122 
sk_fwrite(const void * buffer,size_t byteCount,FILE * f)123 size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* f) {
124     SkASSERT(f);
125     return fwrite(buffer, 1, byteCount, f);
126 }
127 
sk_fflush(FILE * f)128 void sk_fflush(FILE* f) {
129     SkASSERT(f);
130     fflush(f);
131 }
132 
sk_fsync(FILE * f)133 void sk_fsync(FILE* f) {
134 #if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \
135         && !defined(_NEWLIB_VERSION) && !defined(__FreeBSD__)
136     int fd = fileno(f);
137     fsync(fd);
138 #endif
139 }
140 
sk_ftell(FILE * f)141 size_t sk_ftell(FILE* f) {
142     long curr = ftell(f);
143     if (curr < 0) {
144         return 0;
145     }
146     return curr;
147 }
148 
sk_fclose(FILE * f)149 void sk_fclose(FILE* f) {
150     if (f) {
151         fclose(f);
152     }
153 }
154 
sk_isdir(const char * path)155 bool sk_isdir(const char *path) {
156     struct stat status;
157     if (0 != stat(path, &status)) {
158 #ifdef SK_BUILD_FOR_IOS
159         // check the bundle directory if not in default path
160         SkString bundlePath;
161         if (ios_get_path_in_bundle(path, &bundlePath)) {
162             if (0 != stat(bundlePath.c_str(), &status)) {
163                 return false;
164             }
165         }
166 #else
167         return false;
168 #endif
169     }
170     return SkToBool(status.st_mode & S_IFDIR);
171 }
172 
sk_mkdir(const char * path)173 bool sk_mkdir(const char* path) {
174     if (sk_isdir(path)) {
175         return true;
176     }
177     if (sk_exists(path)) {
178         fprintf(stderr,
179                 "sk_mkdir: path '%s' already exists but is not a directory\n",
180                 path);
181         return false;
182     }
183 
184     int retval;
185 #ifdef _WIN32
186     retval = _mkdir(path);
187 #else
188     retval = mkdir(path, 0777);
189     if (retval) {
190       perror("mkdir() failed with error: ");
191     }
192 #endif
193     return 0 == retval;
194 }
195