1/**************************************************************************/
2/* file_access_unix.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "file_access_unix.h"
32
33#if defined(UNIX_ENABLED)
34
35#include "core/os/os.h"
36#include "core/string/print_string.h"
37
38#include <errno.h>
39#include <fcntl.h>
40#include <sys/stat.h>
41#include <sys/types.h>
42
43#if defined(UNIX_ENABLED)
44#include <unistd.h>
45#endif
46
47#ifdef MSVC
48#define S_ISREG(m) ((m)&_S_IFREG)
49#include <io.h>
50#endif
51#ifndef S_ISREG
52#define S_ISREG(m) ((m)&S_IFREG)
53#endif
54
55void FileAccessUnix::check_errors() const {
56 ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
57
58 if (feof(f)) {
59 last_error = ERR_FILE_EOF;
60 }
61}
62
63Error FileAccessUnix::open_internal(const String &p_path, int p_mode_flags) {
64 _close();
65
66 path_src = p_path;
67 path = fix_path(p_path);
68 //printf("opening %s, %i\n", path.utf8().get_data(), Memory::get_static_mem_usage());
69
70 ERR_FAIL_COND_V_MSG(f, ERR_ALREADY_IN_USE, "File is already in use.");
71 const char *mode_string;
72
73 if (p_mode_flags == READ) {
74 mode_string = "rb";
75 } else if (p_mode_flags == WRITE) {
76 mode_string = "wb";
77 } else if (p_mode_flags == READ_WRITE) {
78 mode_string = "rb+";
79 } else if (p_mode_flags == WRITE_READ) {
80 mode_string = "wb+";
81 } else {
82 return ERR_INVALID_PARAMETER;
83 }
84
85 /* pretty much every implementation that uses fopen as primary
86 backend (unix-compatible mostly) supports utf8 encoding */
87
88 //printf("opening %s as %s\n", p_path.utf8().get_data(), path.utf8().get_data());
89 struct stat st = {};
90 int err = stat(path.utf8().get_data(), &st);
91 if (!err) {
92 switch (st.st_mode & S_IFMT) {
93 case S_IFLNK:
94 case S_IFREG:
95 break;
96 default:
97 return ERR_FILE_CANT_OPEN;
98 }
99 }
100
101 if (is_backup_save_enabled() && (p_mode_flags == WRITE)) {
102 save_path = path;
103 // Create a temporary file in the same directory as the target file.
104 path = path + "-XXXXXX";
105 CharString cs = path.utf8();
106 int fd = mkstemp(cs.ptrw());
107 if (fd == -1) {
108 last_error = ERR_FILE_CANT_OPEN;
109 return last_error;
110 }
111 fchmod(fd, 0666);
112 path = String::utf8(cs.ptr());
113
114 f = fdopen(fd, mode_string);
115 if (f == nullptr) {
116 // Delete temp file and close descriptor if open failed.
117 ::unlink(cs.ptr());
118 ::close(fd);
119 last_error = ERR_FILE_CANT_OPEN;
120 return last_error;
121 }
122 } else {
123 f = fopen(path.utf8().get_data(), mode_string);
124 }
125
126 if (f == nullptr) {
127 switch (errno) {
128 case ENOENT: {
129 last_error = ERR_FILE_NOT_FOUND;
130 } break;
131 default: {
132 last_error = ERR_FILE_CANT_OPEN;
133 } break;
134 }
135 return last_error;
136 }
137
138 // Set close on exec to avoid leaking it to subprocesses.
139 int fd = fileno(f);
140
141 if (fd != -1) {
142 int opts = fcntl(fd, F_GETFD);
143 fcntl(fd, F_SETFD, opts | FD_CLOEXEC);
144 }
145
146 last_error = OK;
147 flags = p_mode_flags;
148 return OK;
149}
150
151void FileAccessUnix::_close() {
152 if (!f) {
153 return;
154 }
155
156 fclose(f);
157 f = nullptr;
158
159 if (close_notification_func) {
160 close_notification_func(path, flags);
161 }
162
163 if (!save_path.is_empty()) {
164 int rename_error = rename(path.utf8().get_data(), save_path.utf8().get_data());
165
166 if (rename_error && close_fail_notify) {
167 close_fail_notify(save_path);
168 }
169
170 save_path = "";
171 ERR_FAIL_COND(rename_error != 0);
172 }
173}
174
175bool FileAccessUnix::is_open() const {
176 return (f != nullptr);
177}
178
179String FileAccessUnix::get_path() const {
180 return path_src;
181}
182
183String FileAccessUnix::get_path_absolute() const {
184 return path;
185}
186
187void FileAccessUnix::seek(uint64_t p_position) {
188 ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
189
190 last_error = OK;
191 if (fseeko(f, p_position, SEEK_SET)) {
192 check_errors();
193 }
194}
195
196void FileAccessUnix::seek_end(int64_t p_position) {
197 ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
198
199 if (fseeko(f, p_position, SEEK_END)) {
200 check_errors();
201 }
202}
203
204uint64_t FileAccessUnix::get_position() const {
205 ERR_FAIL_NULL_V_MSG(f, 0, "File must be opened before use.");
206
207 int64_t pos = ftello(f);
208 if (pos < 0) {
209 check_errors();
210 ERR_FAIL_V(0);
211 }
212 return pos;
213}
214
215uint64_t FileAccessUnix::get_length() const {
216 ERR_FAIL_NULL_V_MSG(f, 0, "File must be opened before use.");
217
218 int64_t pos = ftello(f);
219 ERR_FAIL_COND_V(pos < 0, 0);
220 ERR_FAIL_COND_V(fseeko(f, 0, SEEK_END), 0);
221 int64_t size = ftello(f);
222 ERR_FAIL_COND_V(size < 0, 0);
223 ERR_FAIL_COND_V(fseeko(f, pos, SEEK_SET), 0);
224
225 return size;
226}
227
228bool FileAccessUnix::eof_reached() const {
229 return last_error == ERR_FILE_EOF;
230}
231
232uint8_t FileAccessUnix::get_8() const {
233 ERR_FAIL_NULL_V_MSG(f, 0, "File must be opened before use.");
234 uint8_t b;
235 if (fread(&b, 1, 1, f) == 0) {
236 check_errors();
237 b = '\0';
238 }
239 return b;
240}
241
242uint64_t FileAccessUnix::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
243 ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
244 ERR_FAIL_NULL_V_MSG(f, -1, "File must be opened before use.");
245
246 uint64_t read = fread(p_dst, 1, p_length, f);
247 check_errors();
248 return read;
249}
250
251Error FileAccessUnix::get_error() const {
252 return last_error;
253}
254
255void FileAccessUnix::flush() {
256 ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
257 fflush(f);
258}
259
260void FileAccessUnix::store_8(uint8_t p_dest) {
261 ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
262 ERR_FAIL_COND(fwrite(&p_dest, 1, 1, f) != 1);
263}
264
265void FileAccessUnix::store_buffer(const uint8_t *p_src, uint64_t p_length) {
266 ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
267 ERR_FAIL_COND(!p_src && p_length > 0);
268 ERR_FAIL_COND(fwrite(p_src, 1, p_length, f) != p_length);
269}
270
271bool FileAccessUnix::file_exists(const String &p_path) {
272 int err;
273 struct stat st = {};
274 String filename = fix_path(p_path);
275
276 // Does the name exist at all?
277 err = stat(filename.utf8().get_data(), &st);
278 if (err) {
279 return false;
280 }
281
282#ifdef UNIX_ENABLED
283 // See if we have access to the file
284 if (access(filename.utf8().get_data(), F_OK)) {
285 return false;
286 }
287#else
288 if (_access(filename.utf8().get_data(), 4) == -1) {
289 return false;
290 }
291#endif
292
293 // See if this is a regular file
294 switch (st.st_mode & S_IFMT) {
295 case S_IFLNK:
296 case S_IFREG:
297 return true;
298 default:
299 return false;
300 }
301}
302
303uint64_t FileAccessUnix::_get_modified_time(const String &p_file) {
304 String file = fix_path(p_file);
305 struct stat status = {};
306 int err = stat(file.utf8().get_data(), &status);
307
308 if (!err) {
309 return status.st_mtime;
310 } else {
311 print_verbose("Failed to get modified time for: " + p_file + "");
312 return 0;
313 }
314}
315
316BitField<FileAccess::UnixPermissionFlags> FileAccessUnix::_get_unix_permissions(const String &p_file) {
317 String file = fix_path(p_file);
318 struct stat status = {};
319 int err = stat(file.utf8().get_data(), &status);
320
321 if (!err) {
322 return status.st_mode & 0xFFF; //only permissions
323 } else {
324 ERR_FAIL_V_MSG(0, "Failed to get unix permissions for: " + p_file + ".");
325 }
326}
327
328Error FileAccessUnix::_set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions) {
329 String file = fix_path(p_file);
330
331 int err = chmod(file.utf8().get_data(), p_permissions);
332 if (!err) {
333 return OK;
334 }
335
336 return FAILED;
337}
338
339bool FileAccessUnix::_get_hidden_attribute(const String &p_file) {
340#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
341 String file = fix_path(p_file);
342
343 struct stat st = {};
344 int err = stat(file.utf8().get_data(), &st);
345 ERR_FAIL_COND_V_MSG(err, false, "Failed to get attributes for: " + p_file);
346
347 return (st.st_flags & UF_HIDDEN);
348#else
349 return false;
350#endif
351}
352
353Error FileAccessUnix::_set_hidden_attribute(const String &p_file, bool p_hidden) {
354#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
355 String file = fix_path(p_file);
356
357 struct stat st = {};
358 int err = stat(file.utf8().get_data(), &st);
359 ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to get attributes for: " + p_file);
360
361 if (p_hidden) {
362 err = chflags(file.utf8().get_data(), st.st_flags | UF_HIDDEN);
363 } else {
364 err = chflags(file.utf8().get_data(), st.st_flags & ~UF_HIDDEN);
365 }
366 ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to set attributes for: " + p_file);
367 return OK;
368#else
369 return ERR_UNAVAILABLE;
370#endif
371}
372
373bool FileAccessUnix::_get_read_only_attribute(const String &p_file) {
374#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
375 String file = fix_path(p_file);
376
377 struct stat st = {};
378 int err = stat(file.utf8().get_data(), &st);
379 ERR_FAIL_COND_V_MSG(err, false, "Failed to get attributes for: " + p_file);
380
381 return st.st_flags & UF_IMMUTABLE;
382#else
383 return false;
384#endif
385}
386
387Error FileAccessUnix::_set_read_only_attribute(const String &p_file, bool p_ro) {
388#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
389 String file = fix_path(p_file);
390
391 struct stat st = {};
392 int err = stat(file.utf8().get_data(), &st);
393 ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to get attributes for: " + p_file);
394
395 if (p_ro) {
396 err = chflags(file.utf8().get_data(), st.st_flags | UF_IMMUTABLE);
397 } else {
398 err = chflags(file.utf8().get_data(), st.st_flags & ~UF_IMMUTABLE);
399 }
400 ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to set attributes for: " + p_file);
401 return OK;
402#else
403 return ERR_UNAVAILABLE;
404#endif
405}
406
407void FileAccessUnix::close() {
408 _close();
409}
410
411CloseNotificationFunc FileAccessUnix::close_notification_func = nullptr;
412
413FileAccessUnix::~FileAccessUnix() {
414 _close();
415}
416
417#endif // UNIX_ENABLED
418