Line data Source code
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 "SkOSFile.h"
9 : #include "SkTypes.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 : #endif
23 :
24 : #ifdef SK_BUILD_FOR_IOS
25 : #import <CoreFoundation/CoreFoundation.h>
26 :
27 : static FILE* ios_open_from_bundle(const char path[], const char* perm) {
28 : // Get a reference to the main bundle
29 : CFBundleRef mainBundle = CFBundleGetMainBundle();
30 :
31 : // Get a reference to the file's URL
32 : CFStringRef pathRef = CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8);
33 : CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, pathRef, NULL, NULL);
34 : CFRelease(pathRef);
35 : if (!imageURL) {
36 : return nullptr;
37 : }
38 :
39 : // Convert the URL reference into a string reference
40 : CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathStyle);
41 : CFRelease(imageURL);
42 :
43 : // Get the system encoding method
44 : CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
45 :
46 : // Convert the string reference into a C string
47 : const char *finalPath = CFStringGetCStringPtr(imagePath, encodingMethod);
48 : FILE* fileHandle = fopen(finalPath, perm);
49 : CFRelease(imagePath);
50 : return fileHandle;
51 : }
52 : #endif
53 :
54 :
55 0 : FILE* sk_fopen(const char path[], SkFILE_Flags flags) {
56 : char perm[4];
57 0 : char* p = perm;
58 :
59 0 : if (flags & kRead_SkFILE_Flag) {
60 0 : *p++ = 'r';
61 : }
62 0 : if (flags & kWrite_SkFILE_Flag) {
63 0 : *p++ = 'w';
64 : }
65 0 : *p++ = 'b';
66 0 : *p = 0;
67 :
68 : //TODO: on Windows fopen is just ASCII or the current code page,
69 : //convert to utf16 and use _wfopen
70 0 : FILE* file = nullptr;
71 : #ifdef SK_BUILD_FOR_IOS
72 : // if read-only, try to open from bundle first
73 : if (kRead_SkFILE_Flag == flags) {
74 : file = ios_open_from_bundle(path, perm);
75 : }
76 : // otherwise just read from the Documents directory (default)
77 : if (!file) {
78 : #endif
79 0 : file = fopen(path, perm);
80 : #ifdef SK_BUILD_FOR_IOS
81 : }
82 : #endif
83 0 : if (nullptr == file && (flags & kWrite_SkFILE_Flag)) {
84 0 : SkDEBUGF(("sk_fopen: fopen(\"%s\", \"%s\") returned NULL (errno:%d): %s\n",
85 0 : path, perm, errno, strerror(errno)));
86 : }
87 0 : return file;
88 : }
89 :
90 0 : size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* f) {
91 0 : SkASSERT(f);
92 0 : return fwrite(buffer, 1, byteCount, f);
93 : }
94 :
95 0 : void sk_fflush(FILE* f) {
96 0 : SkASSERT(f);
97 0 : fflush(f);
98 0 : }
99 :
100 0 : void sk_fsync(FILE* f) {
101 : #if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \
102 : && !defined(_NEWLIB_VERSION)
103 0 : int fd = fileno(f);
104 0 : fsync(fd);
105 : #endif
106 0 : }
107 :
108 0 : size_t sk_ftell(FILE* f) {
109 0 : long curr = ftell(f);
110 0 : if (curr < 0) {
111 0 : return 0;
112 : }
113 0 : return curr;
114 : }
115 :
116 0 : void sk_fclose(FILE* f) {
117 0 : if (f) {
118 0 : fclose(f);
119 : }
120 0 : }
121 :
122 0 : bool sk_isdir(const char *path) {
123 : struct stat status;
124 0 : if (0 != stat(path, &status)) {
125 0 : return false;
126 : }
127 0 : return SkToBool(status.st_mode & S_IFDIR);
128 : }
129 :
130 0 : bool sk_mkdir(const char* path) {
131 0 : if (sk_isdir(path)) {
132 0 : return true;
133 : }
134 0 : if (sk_exists(path)) {
135 : fprintf(stderr,
136 : "sk_mkdir: path '%s' already exists but is not a directory\n",
137 0 : path);
138 0 : return false;
139 : }
140 :
141 : int retval;
142 : #ifdef _WIN32
143 : retval = _mkdir(path);
144 : #else
145 0 : retval = mkdir(path, 0777);
146 : #endif
147 0 : if (0 == retval) {
148 0 : return true;
149 : } else {
150 0 : fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path);
151 0 : return false;
152 : }
153 : }
|