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
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 = 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    ptr = utf8path;
58    while (ptr < end) {
59        out += SkUTF::ToUTF16(SkUTF::NextUTF8(&ptr, end), out);
60    }
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
68FILE* sk_fopen(const char path[], SkFILE_Flags flags) {
69    char    perm[4] = {0, 0, 0, 0};
70    char*   p = perm;
71
72    if (flags & kRead_SkFILE_Flag) {
73        *p++ = 'r';
74    }
75    if (flags & kWrite_SkFILE_Flag) {
76        *p++ = 'w';
77    }
78    *p = 'b';
79
80    FILE* file = nullptr;
81#ifdef _WIN32
82    file = fopen_win(path, perm);
83#else
84    file = fopen(path, perm);
85#endif
86#ifdef SK_BUILD_FOR_IOS
87    // 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        }
93    }
94#endif
95
96    if (nullptr == file && (flags & kWrite_SkFILE_Flag)) {
97        SkDEBUGF("sk_fopen: fopen(\"%s\", \"%s\") returned nullptr (errno:%d): %s\n",
98                 path, perm, errno, strerror(errno));
99    }
100    return file;
101}
102
103size_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
121size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* f) {
122    SkASSERT(f);
123    return fwrite(buffer, 1, byteCount, f);
124}
125
126void sk_fflush(FILE* f) {
127    SkASSERT(f);
128    fflush(f);
129}
130
131void sk_fsync(FILE* f) {
132#if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \
133        && !defined(_NEWLIB_VERSION)
134    int fd = fileno(f);
135    fsync(fd);
136#endif
137}
138
139size_t sk_ftell(FILE* f) {
140    long curr = ftell(f);
141    if (curr < 0) {
142        return 0;
143    }
144    return curr;
145}
146
147void sk_fclose(FILE* f) {
148    if (f) {
149        fclose(f);
150    }
151}
152
153bool sk_isdir(const char *path) {
154    struct stat status;
155    if (0 != stat(path, &status)) {
156#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
165        return false;
166#endif
167    }
168    return SkToBool(status.st_mode & S_IFDIR);
169}
170
171bool sk_mkdir(const char* path) {
172    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);
187    if (retval) {
188      perror("mkdir() failed with error: ");
189    }
190#endif
191    return 0 == retval;
192}
193