blob: e79d87fc895eed6e68325a3cf698cfe238eda5cd [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
25#import <CoreFoundation/CoreFoundation.h>
26
27static FILE* ios_open_from_bundle(const char path[], const char* perm) {
28 // Get a reference to the main bundle
29 CFBundleRef mainBundle = CFBundleGetMainBundle();
mtklein944c2d92016-02-16 08:10:34 -080030
jvanverth44dcb8a2015-10-02 09:12:05 -070031 // Get a reference to the file's URL
32 CFStringRef pathRef = CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8);
33 CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, pathRef, NULL, NULL);
sdefresne388127f2016-09-20 08:53:14 -070034 CFRelease(pathRef);
jvanverth44dcb8a2015-10-02 09:12:05 -070035 if (!imageURL) {
36 return nullptr;
37 }
mtklein944c2d92016-02-16 08:10:34 -080038
jvanverth44dcb8a2015-10-02 09:12:05 -070039 // Convert the URL reference into a string reference
40 CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathStyle);
sdefresne67ba29c2016-09-21 06:51:33 -070041 CFRelease(imageURL);
mtklein944c2d92016-02-16 08:10:34 -080042
jvanverth44dcb8a2015-10-02 09:12:05 -070043 // Get the system encoding method
44 CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
mtklein944c2d92016-02-16 08:10:34 -080045
jvanverth44dcb8a2015-10-02 09:12:05 -070046 // Convert the string reference into a C string
47 const char *finalPath = CFStringGetCStringPtr(imagePath, encodingMethod);
sdefresne67ba29c2016-09-21 06:51:33 -070048 FILE* fileHandle = fopen(finalPath, perm);
49 CFRelease(imagePath);
50 return fileHandle;
jvanverth44dcb8a2015-10-02 09:12:05 -070051}
52#endif
53
54
halcanaryd76be9c2015-11-20 13:47:49 -080055FILE* sk_fopen(const char path[], SkFILE_Flags flags) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 char perm[4];
57 char* p = perm;
58
bungeman@google.com6cab1a42013-05-29 13:43:31 +000059 if (flags & kRead_SkFILE_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 *p++ = 'r';
bungeman@google.com6cab1a42013-05-29 13:43:31 +000061 }
62 if (flags & kWrite_SkFILE_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 *p++ = 'w';
bungeman@google.com6cab1a42013-05-29 13:43:31 +000064 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 *p++ = 'b';
66 *p = 0;
mtklein944c2d92016-02-16 08:10:34 -080067
commit-bot@chromium.org9711e442013-04-24 20:03:00 +000068 //TODO: on Windows fopen is just ASCII or the current code page,
69 //convert to utf16 and use _wfopen
halcanaryd76be9c2015-11-20 13:47:49 -080070 FILE* file = nullptr;
jvanverth44dcb8a2015-10-02 09:12:05 -070071#ifdef SK_BUILD_FOR_IOS
72 // if read-only, try to open from bundle first
73 if (kRead_SkFILE_Flag == flags) {
halcanaryd76be9c2015-11-20 13:47:49 -080074 file = ios_open_from_bundle(path, perm);
jvanverth44dcb8a2015-10-02 09:12:05 -070075 }
76 // otherwise just read from the Documents directory (default)
77 if (!file) {
78#endif
mtklein944c2d92016-02-16 08:10:34 -080079 file = fopen(path, perm);
jvanverth44dcb8a2015-10-02 09:12:05 -070080#ifdef SK_BUILD_FOR_IOS
81 }
82#endif
bungeman0881b952015-09-02 12:41:35 -070083 if (nullptr == file && (flags & kWrite_SkFILE_Flag)) {
84 SkDEBUGF(("sk_fopen: fopen(\"%s\", \"%s\") returned NULL (errno:%d): %s\n",
85 path, perm, errno, strerror(errno)));
86 }
87 return file;
reed@android.com8a1c16f2008-12-17 15:59:43 +000088}
89
halcanaryd76be9c2015-11-20 13:47:49 -080090size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 SkASSERT(f);
mtklein944c2d92016-02-16 08:10:34 -080092 return fwrite(buffer, 1, byteCount, f);
reed@android.com8a1c16f2008-12-17 15:59:43 +000093}
94
halcanaryd76be9c2015-11-20 13:47:49 -080095void sk_fflush(FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 SkASSERT(f);
mtklein944c2d92016-02-16 08:10:34 -080097 fflush(f);
reed@android.com8a1c16f2008-12-17 15:59:43 +000098}
99
caryclark7471fa42015-12-16 13:41:23 -0800100void sk_fsync(FILE* f) {
101#if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \
102 && !defined(_NEWLIB_VERSION)
mtklein944c2d92016-02-16 08:10:34 -0800103 int fd = fileno(f);
104 fsync(fd);
caryclark7471fa42015-12-16 13:41:23 -0800105#endif
106}
107
halcanaryd76be9c2015-11-20 13:47:49 -0800108size_t sk_ftell(FILE* f) {
mtklein944c2d92016-02-16 08:10:34 -0800109 long curr = ftell(f);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000110 if (curr < 0) {
111 return 0;
112 }
113 return curr;
114}
115
halcanaryd76be9c2015-11-20 13:47:49 -0800116void sk_fclose(FILE* f) {
Ben Wagner4d1955c2017-03-10 13:08:15 -0500117 if (f) {
118 fclose(f);
119 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120}
121
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000122bool sk_isdir(const char *path) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000123 struct stat status;
124 if (0 != stat(path, &status)) {
125 return false;
126 }
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
147 if (0 == retval) {
148 return true;
149 } else {
150 fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path);
151 return false;
152 }
153}