Debian Codegraph @

android/app/jni/src/fileutils.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "fileutils.h"
21 #ifdef _WIN32
22 #include "utfutils.h"
23 #include <windows.h>
24 #include <shlobj.h>
25 #include <shlwapi.h>
26 #else
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <sys/stat.h>
31 #endif
32
33 FILE* ps_fopen(const char* path, const char* mode) {
34 #ifdef _WIN32
35 return _wfopen(utf8To16(path).c_str(),utf8To16(mode).c_str());
36 #else
37 return fopen(path,mode);
38 #endif
39 }
40
41 // TODO: copy in case of failure
42 bool moveFiles(const char* src, const char* dest) {
43 #ifdef _WIN32
44 return MoveFileW(utf8To16(src).c_str(),utf8To16(dest).c_str());
45 #else
46 if (rename(src,dest)==-1) {
47 return false;
48 }
49 return true;
50 #endif
51 }
52
53 bool deleteFile(const char* path) {
54 #ifdef _WIN32
55 return DeleteFileW(utf8To16(path).c_str());
56 #else
57 return (unlink(path)==0);
58 #endif
59 }
60
61 int fileExists(const char* path) {
62 #ifdef _WIN32
63 if (PathFileExistsW(utf8To16(path).c_str())) return 1;
64 // which errors could PathFileExists possibly throw?
65 switch (GetLastError()) {
66 case ERROR_FILE_EXISTS:
67 return 1;
68 break;
69 case ERROR_FILE_NOT_FOUND:
70 case ERROR_PATH_NOT_FOUND:
71 case ERROR_INVALID_DRIVE:
72 case ERROR_DEV_NOT_EXIST:
73 case ERROR_NETNAME_DELETED:
74 case ERROR_BAD_NET_NAME:
75 return 0;
76 break;
77 }
78 return -1;
79 #else
80 if (access(path,F_OK)==0) return 1;
81 if (errno==ENOENT) return 0;
82 return -1;
83 #endif
84 }
85
86 bool dirExists(const char* what) {
87 #ifdef _WIN32
88 WString ws=utf8To16(what);
89 return (PathIsDirectoryW(ws.c_str())!=FALSE);
90 #else
91 struct stat st;
92 if (stat(what,&st)<0) return false;
93 return (st.st_mode&S_IFDIR);
94 #endif
95 }
96
97 bool makeDir(const char* path) {
98 #ifdef _WIN32
99 return (SHCreateDirectoryExW(NULL,utf8To16(path).c_str(),NULL)==ERROR_SUCCESS);
100 #else
101 return (mkdir(path,0755)==0);
102 #endif
103 }
104
105 int touchFile(const char* path) {
106 #ifdef _WIN32
107 HANDLE h=CreateFileW(utf8To16(path).c_str(),GENERIC_WRITE,FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_TEMPORARY,NULL);
108 if (h==INVALID_HANDLE_VALUE) {
109 switch (GetLastError()) {
110 case ERROR_FILE_EXISTS:
111 return -EEXIST;
112 break;
113 }
114 return -EPERM;
115 }
116 if (CloseHandle(h)==0) {
117 return -EBADF;
118 }
119 return 0;
120 #else
121 int fd=open(path,O_CREAT|O_WRONLY|O_TRUNC|O_EXCL,0666);
122 if (fd<0) return -errno;
123 close(fd);
124 return 0;
125 #endif
126 }