blob: 7cdc549df6b7dc10e0c2aa731978aa53f3c38ae0 [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
Jim Van Verth329c5a62017-11-29 11:42:33 -050025#include "SkOSFile_ios.h"
jvanverth44dcb8a2015-10-02 09:12:05 -070026#endif
27
halcanaryd76be9c2015-11-20 13:47:49 -080028FILE* sk_fopen(const char path[], SkFILE_Flags flags) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 char perm[4];
30 char* p = perm;
31
bungeman@google.com6cab1a42013-05-29 13:43:31 +000032 if (flags & kRead_SkFILE_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 *p++ = 'r';
bungeman@google.com6cab1a42013-05-29 13:43:31 +000034 }
35 if (flags & kWrite_SkFILE_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 *p++ = 'w';
bungeman@google.com6cab1a42013-05-29 13:43:31 +000037 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 *p++ = 'b';
39 *p = 0;
mtklein944c2d92016-02-16 08:10:34 -080040
commit-bot@chromium.org9711e442013-04-24 20:03:00 +000041 //TODO: on Windows fopen is just ASCII or the current code page,
42 //convert to utf16 and use _wfopen
halcanaryd76be9c2015-11-20 13:47:49 -080043 FILE* file = nullptr;
Jim Van Verth329c5a62017-11-29 11:42:33 -050044 file = fopen(path, perm);
jvanverth44dcb8a2015-10-02 09:12:05 -070045#ifdef SK_BUILD_FOR_IOS
Jim Van Verth329c5a62017-11-29 11:42:33 -050046 // if not found in default path and read-only, try to open from bundle
47 if (!file && kRead_SkFILE_Flag == flags) {
48 SkString bundlePath;
49 if (ios_get_path_in_bundle(path, &bundlePath)) {
50 file = fopen(bundlePath.c_str(), perm);
51 }
jvanverth44dcb8a2015-10-02 09:12:05 -070052 }
53#endif
Jim Van Verth329c5a62017-11-29 11:42:33 -050054
bungeman0881b952015-09-02 12:41:35 -070055 if (nullptr == file && (flags & kWrite_SkFILE_Flag)) {
Ben Wagnera93a14a2017-08-28 10:34:05 -040056 SkDEBUGF(("sk_fopen: fopen(\"%s\", \"%s\") returned nullptr (errno:%d): %s\n",
bungeman0881b952015-09-02 12:41:35 -070057 path, perm, errno, strerror(errno)));
58 }
59 return file;
reed@android.com8a1c16f2008-12-17 15:59:43 +000060}
61
Leon Scroggins564ad052017-05-23 13:29:14 +000062size_t sk_fgetsize(FILE* f) {
63 SkASSERT(f);
64
65 long curr = ftell(f); // remember where we are
66 if (curr < 0) {
67 return 0;
68 }
69
70 fseek(f, 0, SEEK_END); // go to the end
71 long size = ftell(f); // record the size
72 if (size < 0) {
73 size = 0;
74 }
75
76 fseek(f, curr, SEEK_SET); // go back to our prev location
77 return size;
78}
79
halcanaryd76be9c2015-11-20 13:47:49 -080080size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 SkASSERT(f);
mtklein944c2d92016-02-16 08:10:34 -080082 return fwrite(buffer, 1, byteCount, f);
reed@android.com8a1c16f2008-12-17 15:59:43 +000083}
84
halcanaryd76be9c2015-11-20 13:47:49 -080085void sk_fflush(FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 SkASSERT(f);
mtklein944c2d92016-02-16 08:10:34 -080087 fflush(f);
reed@android.com8a1c16f2008-12-17 15:59:43 +000088}
89
caryclark7471fa42015-12-16 13:41:23 -080090void sk_fsync(FILE* f) {
91#if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \
92 && !defined(_NEWLIB_VERSION)
mtklein944c2d92016-02-16 08:10:34 -080093 int fd = fileno(f);
94 fsync(fd);
caryclark7471fa42015-12-16 13:41:23 -080095#endif
96}
97
halcanaryd76be9c2015-11-20 13:47:49 -080098size_t sk_ftell(FILE* f) {
mtklein944c2d92016-02-16 08:10:34 -080099 long curr = ftell(f);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000100 if (curr < 0) {
101 return 0;
102 }
103 return curr;
104}
105
halcanaryd76be9c2015-11-20 13:47:49 -0800106void sk_fclose(FILE* f) {
Ben Wagner4d1955c2017-03-10 13:08:15 -0500107 if (f) {
108 fclose(f);
109 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110}
111
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000112bool sk_isdir(const char *path) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000113 struct stat status;
114 if (0 != stat(path, &status)) {
Jim Van Verth329c5a62017-11-29 11:42:33 -0500115#ifdef SK_BUILD_FOR_IOS
116 // check the bundle directory if not in default path
117 SkString bundlePath;
118 if (ios_get_path_in_bundle(path, &bundlePath)) {
119 if (0 != stat(bundlePath.c_str(), &status)) {
120 return false;
121 }
122 }
123#else
epoger@google.come8ebeb12012-10-29 16:42:11 +0000124 return false;
Jim Van Verth329c5a62017-11-29 11:42:33 -0500125#endif
epoger@google.come8ebeb12012-10-29 16:42:11 +0000126 }
scroggo@google.com6e725162012-11-01 16:28:23 +0000127 return SkToBool(status.st_mode & S_IFDIR);
epoger@google.come8ebeb12012-10-29 16:42:11 +0000128}
129
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000130bool sk_mkdir(const char* path) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000131 if (sk_isdir(path)) {
132 return true;
133 }
134 if (sk_exists(path)) {
135 fprintf(stderr,
136 "sk_mkdir: path '%s' already exists but is not a directory\n",
137 path);
138 return false;
139 }
140
141 int retval;
142#ifdef _WIN32
143 retval = _mkdir(path);
144#else
145 retval = mkdir(path, 0777);
146#endif
Mike Kleine9034492017-09-16 10:16:09 -0400147 return 0 == retval;
epoger@google.come8ebeb12012-10-29 16:42:11 +0000148}