1 |
cebix |
1.1 |
/* |
2 |
|
|
* extfs_unix.cpp - MacOS file system for access native file system access, Unix specific stuff |
3 |
|
|
* |
4 |
|
|
* Basilisk II (C) 1997-1999 Christian Bauer |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include <sys/types.h> |
22 |
|
|
#include <sys/stat.h> |
23 |
|
|
#include <stdio.h> |
24 |
|
|
#include <stdlib.h> |
25 |
|
|
#include <unistd.h> |
26 |
|
|
#include <dirent.h> |
27 |
|
|
#include <errno.h> |
28 |
|
|
|
29 |
|
|
#include "sysdeps.h" |
30 |
|
|
#include "extfs.h" |
31 |
|
|
#include "extfs_defs.h" |
32 |
|
|
|
33 |
|
|
#define DEBUG 0 |
34 |
|
|
#include "debug.h" |
35 |
|
|
|
36 |
|
|
|
37 |
|
|
// Default Finder flags |
38 |
|
|
const uint16 DEFAULT_FINDER_FLAGS = kHasBeenInited; |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
/* |
42 |
|
|
* Initialization |
43 |
|
|
*/ |
44 |
|
|
|
45 |
|
|
void extfs_init(void) |
46 |
|
|
{ |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
|
50 |
|
|
/* |
51 |
|
|
* Deinitialization |
52 |
|
|
*/ |
53 |
|
|
|
54 |
|
|
void extfs_exit(void) |
55 |
|
|
{ |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
|
59 |
|
|
/* |
60 |
cebix |
1.3 |
* Add component to path name |
61 |
|
|
*/ |
62 |
|
|
|
63 |
cebix |
1.5 |
void add_path_component(char *path, const char *component) |
64 |
cebix |
1.3 |
{ |
65 |
|
|
int l = strlen(path); |
66 |
cebix |
1.5 |
if (l < MAX_PATH_LENGTH-1 && path[l-1] != '/') { |
67 |
cebix |
1.3 |
path[l] = '/'; |
68 |
|
|
path[l+1] = 0; |
69 |
|
|
} |
70 |
cebix |
1.5 |
strncat(path, component, MAX_PATH_LENGTH-1); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
|
74 |
|
|
/* |
75 |
|
|
* Finder info and resource forks are kept in helper files |
76 |
|
|
* |
77 |
|
|
* Finder info: |
78 |
|
|
* /path/.finf/file |
79 |
|
|
* Resource fork: |
80 |
|
|
* /path/.rsrc/file |
81 |
|
|
*/ |
82 |
|
|
|
83 |
|
|
// Layout of Finder info helper files (all fields big-endian) |
84 |
|
|
struct finf_struct { |
85 |
|
|
uint32 type; |
86 |
|
|
uint32 creator; |
87 |
|
|
uint16 flags; |
88 |
|
|
}; |
89 |
|
|
|
90 |
|
|
static void make_helper_path(const char *src, char *dest, const char *add, bool only_dir = false) |
91 |
|
|
{ |
92 |
|
|
dest[0] = 0; |
93 |
|
|
|
94 |
|
|
// Get pointer to last component of path |
95 |
|
|
const char *last_part = strrchr(src, '/'); |
96 |
|
|
if (last_part) |
97 |
|
|
last_part++; |
98 |
|
|
else |
99 |
|
|
last_part = src; |
100 |
|
|
|
101 |
|
|
// Copy everything before |
102 |
|
|
strncpy(dest, src, last_part-src); |
103 |
|
|
dest[last_part-src] = 0; |
104 |
|
|
|
105 |
|
|
// Add additional component |
106 |
|
|
strncat(dest, add, MAX_PATH_LENGTH-1); |
107 |
|
|
|
108 |
|
|
// Add last component |
109 |
|
|
if (!only_dir) |
110 |
|
|
strncat(dest, last_part, MAX_PATH_LENGTH-1); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
static int create_helper_dir(const char *path, const char *add) |
114 |
|
|
{ |
115 |
|
|
char helper_dir[MAX_PATH_LENGTH]; |
116 |
|
|
make_helper_path(path, helper_dir, add, true); |
117 |
|
|
return mkdir(helper_dir, 0755); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
static int open_helper(const char *path, const char *add, int flag) |
121 |
|
|
{ |
122 |
|
|
char helper_path[MAX_PATH_LENGTH]; |
123 |
|
|
make_helper_path(path, helper_path, add); |
124 |
|
|
|
125 |
|
|
if ((flag & O_RDWR) || (flag && O_WRONLY)) |
126 |
|
|
flag |= O_CREAT; |
127 |
|
|
int fd = open(helper_path, flag, 0644); |
128 |
|
|
if (fd < 0) { |
129 |
|
|
if (errno == ENOENT && (flag & O_CREAT)) { |
130 |
|
|
// One path component was missing, probably the helper |
131 |
|
|
// directory. Try to create it and re-open the file. |
132 |
|
|
int ret = create_helper_dir(path, add); |
133 |
|
|
if (ret < 0) |
134 |
|
|
return ret; |
135 |
|
|
fd = open(helper_path, flag, 0644); |
136 |
|
|
} |
137 |
|
|
} |
138 |
|
|
return fd; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
static int open_finf(const char *path, int flag) |
142 |
|
|
{ |
143 |
|
|
return open_helper(path, ".finf/", flag); |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
static int open_rsrc(const char *path, int flag) |
147 |
|
|
{ |
148 |
|
|
return open_helper(path, ".rsrc/", flag); |
149 |
cebix |
1.3 |
} |
150 |
|
|
|
151 |
|
|
|
152 |
|
|
/* |
153 |
cebix |
1.1 |
* Get/set finder type/creator for file specified by full path |
154 |
|
|
*/ |
155 |
|
|
|
156 |
|
|
struct ext2type { |
157 |
|
|
const char *ext; |
158 |
|
|
uint32 type; |
159 |
|
|
uint32 creator; |
160 |
|
|
}; |
161 |
|
|
|
162 |
|
|
static const ext2type e2t_translation[] = { |
163 |
cebix |
1.2 |
{".Z", 'ZIVM', 'LZIV'}, |
164 |
cebix |
1.1 |
{".gz", 'Gzip', 'Gzip'}, |
165 |
|
|
{".hqx", 'TEXT', 'SITx'}, |
166 |
|
|
{".pdf", 'PDF ', 'CARO'}, |
167 |
|
|
{".ps", 'TEXT', 'ttxt'}, |
168 |
|
|
{".sit", 'SIT!', 'SITx'}, |
169 |
|
|
{".tar", 'TARF', 'TAR '}, |
170 |
|
|
{".uu", 'TEXT', 'SITx'}, |
171 |
|
|
{".uue", 'TEXT', 'SITx'}, |
172 |
|
|
{".zip", 'ZIP ', 'ZIP '}, |
173 |
|
|
{".8svx", '8SVX', 'SNDM'}, |
174 |
|
|
{".aifc", 'AIFC', 'TVOD'}, |
175 |
|
|
{".aiff", 'AIFF', 'TVOD'}, |
176 |
|
|
{".au", 'ULAW', 'TVOD'}, |
177 |
|
|
{".mid", 'MIDI', 'TVOD'}, |
178 |
|
|
{".midi", 'MIDI', 'TVOD'}, |
179 |
|
|
{".mp2", 'MPG ', 'TVOD'}, |
180 |
|
|
{".mp3", 'MPG ', 'TVOD'}, |
181 |
|
|
{".wav", 'WAVE', 'TVOD'}, |
182 |
|
|
{".bmp", 'BMPf', 'ogle'}, |
183 |
|
|
{".gif", 'GIFf', 'ogle'}, |
184 |
|
|
{".lbm", 'ILBM', 'GKON'}, |
185 |
|
|
{".ilbm", 'ILBM', 'GKON'}, |
186 |
|
|
{".jpg", 'JPEG', 'ogle'}, |
187 |
|
|
{".jpeg", 'JPEG', 'ogle'}, |
188 |
|
|
{".pict", 'PICT', 'ogle'}, |
189 |
|
|
{".png", 'PNGf', 'ogle'}, |
190 |
|
|
{".sgi", '.SGI', 'ogle'}, |
191 |
|
|
{".tga", 'TPIC', 'ogle'}, |
192 |
|
|
{".tif", 'TIFF', 'ogle'}, |
193 |
|
|
{".tiff", 'TIFF', 'ogle'}, |
194 |
|
|
{".html", 'TEXT', 'MOSS'}, |
195 |
|
|
{".txt", 'TEXT', 'ttxt'}, |
196 |
|
|
{".rtf", 'TEXT', 'MSWD'}, |
197 |
|
|
{".c", 'TEXT', 'R*ch'}, |
198 |
cebix |
1.2 |
{".C", 'TEXT', 'R*ch'}, |
199 |
cebix |
1.1 |
{".cc", 'TEXT', 'R*ch'}, |
200 |
|
|
{".cpp", 'TEXT', 'R*ch'}, |
201 |
|
|
{".cxx", 'TEXT', 'R*ch'}, |
202 |
|
|
{".h", 'TEXT', 'R*ch'}, |
203 |
|
|
{".hh", 'TEXT', 'R*ch'}, |
204 |
|
|
{".hpp", 'TEXT', 'R*ch'}, |
205 |
|
|
{".hxx", 'TEXT', 'R*ch'}, |
206 |
|
|
{".s", 'TEXT', 'R*ch'}, |
207 |
cebix |
1.2 |
{".S", 'TEXT', 'R*ch'}, |
208 |
cebix |
1.1 |
{".i", 'TEXT', 'R*ch'}, |
209 |
|
|
{".mpg", 'MPEG', 'TVOD'}, |
210 |
|
|
{".mpeg", 'MPEG', 'TVOD'}, |
211 |
|
|
{".mov", 'MooV', 'TVOD'}, |
212 |
|
|
{".fli", 'FLI ', 'TVOD'}, |
213 |
|
|
{".avi", 'VfW ', 'TVOD'}, |
214 |
|
|
{NULL, 0, 0} // End marker |
215 |
|
|
}; |
216 |
|
|
|
217 |
|
|
void get_finder_type(const char *path, uint32 &type, uint32 &creator) |
218 |
|
|
{ |
219 |
|
|
type = 0; |
220 |
|
|
creator = 0; |
221 |
|
|
|
222 |
cebix |
1.5 |
// Open Finder info file |
223 |
|
|
int fd = open_finf(path, O_RDONLY); |
224 |
|
|
if (fd >= 0) { |
225 |
|
|
|
226 |
|
|
// Read file |
227 |
|
|
finf_struct finf; |
228 |
|
|
if (read(fd, &finf, sizeof(finf_struct)) == sizeof(finf_struct)) { |
229 |
|
|
|
230 |
|
|
// Type/creator are in Finder info file, return them |
231 |
|
|
type = ntohl(finf.type); |
232 |
|
|
creator = ntohl(finf.creator); |
233 |
|
|
close(fd); |
234 |
|
|
return; |
235 |
|
|
} |
236 |
|
|
close(fd); |
237 |
|
|
} |
238 |
|
|
|
239 |
|
|
// No Finder info file, translate file name extension to MacOS type/creator |
240 |
cebix |
1.1 |
int path_len = strlen(path); |
241 |
|
|
for (int i=0; e2t_translation[i].ext; i++) { |
242 |
|
|
int ext_len = strlen(e2t_translation[i].ext); |
243 |
|
|
if (path_len < ext_len) |
244 |
|
|
continue; |
245 |
|
|
if (!strcmp(path + path_len - ext_len, e2t_translation[i].ext)) { |
246 |
|
|
type = e2t_translation[i].type; |
247 |
|
|
creator = e2t_translation[i].creator; |
248 |
|
|
break; |
249 |
|
|
} |
250 |
|
|
} |
251 |
|
|
} |
252 |
|
|
|
253 |
|
|
void set_finder_type(const char *path, uint32 type, uint32 creator) |
254 |
|
|
{ |
255 |
cebix |
1.5 |
// Open Finder info file |
256 |
|
|
int fd = open_finf(path, O_RDWR); |
257 |
|
|
if (fd < 0) |
258 |
|
|
return; |
259 |
|
|
|
260 |
|
|
// Read file |
261 |
|
|
finf_struct finf = {0, 0, DEFAULT_FINDER_FLAGS}; |
262 |
|
|
read(fd, &finf, sizeof(finf_struct)); |
263 |
|
|
|
264 |
|
|
// Set Finder flags |
265 |
|
|
finf.type = htonl(type); |
266 |
|
|
finf.creator = htonl(creator); |
267 |
|
|
|
268 |
|
|
// Update file |
269 |
|
|
lseek(fd, 0, SEEK_SET); |
270 |
|
|
write(fd, &finf, sizeof(finf_struct)); |
271 |
|
|
close(fd); |
272 |
cebix |
1.1 |
} |
273 |
|
|
|
274 |
|
|
|
275 |
|
|
/* |
276 |
cebix |
1.5 |
* Get/set finder flags for file/dir specified by full path |
277 |
cebix |
1.1 |
*/ |
278 |
|
|
|
279 |
|
|
void get_finder_flags(const char *path, uint16 &flags) |
280 |
|
|
{ |
281 |
|
|
flags = DEFAULT_FINDER_FLAGS; // Default |
282 |
cebix |
1.5 |
|
283 |
|
|
// Open Finder info file |
284 |
|
|
int fd = open_finf(path, O_RDONLY); |
285 |
|
|
if (fd < 0) |
286 |
|
|
return; |
287 |
|
|
|
288 |
|
|
// Read Finder flags |
289 |
|
|
finf_struct finf; |
290 |
|
|
if (read(fd, &finf, sizeof(finf_struct)) == sizeof(finf_struct)) |
291 |
|
|
flags = ntohs(finf.flags); |
292 |
|
|
|
293 |
|
|
// Close file |
294 |
|
|
close(fd); |
295 |
cebix |
1.1 |
} |
296 |
|
|
|
297 |
|
|
void set_finder_flags(const char *path, uint16 flags) |
298 |
|
|
{ |
299 |
cebix |
1.5 |
// Open Finder info file |
300 |
|
|
int fd = open_finf(path, O_RDWR); |
301 |
|
|
if (fd < 0) |
302 |
|
|
return; |
303 |
|
|
|
304 |
|
|
// Read file |
305 |
|
|
finf_struct finf = {0, 0, DEFAULT_FINDER_FLAGS}; |
306 |
|
|
read(fd, &finf, sizeof(finf_struct)); |
307 |
|
|
|
308 |
|
|
// Set Finder flags |
309 |
|
|
finf.flags = htons(flags); |
310 |
|
|
|
311 |
|
|
// Update file |
312 |
|
|
lseek(fd, 0, SEEK_SET); |
313 |
|
|
write(fd, &finf, sizeof(finf_struct)); |
314 |
|
|
close(fd); |
315 |
cebix |
1.1 |
} |
316 |
|
|
|
317 |
|
|
|
318 |
|
|
/* |
319 |
|
|
* Resource fork emulation functions |
320 |
|
|
*/ |
321 |
|
|
|
322 |
|
|
uint32 get_rfork_size(const char *path) |
323 |
|
|
{ |
324 |
cebix |
1.5 |
// Open resource file |
325 |
|
|
int fd = open_rsrc(path, O_RDONLY); |
326 |
|
|
if (fd < 0) |
327 |
|
|
return 0; |
328 |
|
|
|
329 |
|
|
// Get size |
330 |
|
|
off_t size = lseek(fd, 0, SEEK_END); |
331 |
|
|
|
332 |
|
|
// Close file and return size |
333 |
|
|
close(fd); |
334 |
|
|
return size < 0 ? 0 : size; |
335 |
cebix |
1.1 |
} |
336 |
|
|
|
337 |
|
|
int open_rfork(const char *path, int flag) |
338 |
|
|
{ |
339 |
cebix |
1.5 |
return open_rsrc(path, flag); |
340 |
cebix |
1.1 |
} |
341 |
|
|
|
342 |
|
|
void close_rfork(const char *path, int fd) |
343 |
|
|
{ |
344 |
cebix |
1.5 |
close(fd); |
345 |
cebix |
1.1 |
} |
346 |
|
|
|
347 |
|
|
|
348 |
|
|
/* |
349 |
|
|
* Read "length" bytes from file to "buffer", |
350 |
|
|
* returns number of bytes read (or 0) |
351 |
|
|
*/ |
352 |
|
|
|
353 |
|
|
size_t extfs_read(int fd, void *buffer, size_t length) |
354 |
|
|
{ |
355 |
|
|
errno = 0; |
356 |
|
|
return read(fd, buffer, length); |
357 |
|
|
} |
358 |
|
|
|
359 |
|
|
|
360 |
|
|
/* |
361 |
|
|
* Write "length" bytes from "buffer" to file, |
362 |
|
|
* returns number of bytes written (or 0) |
363 |
|
|
*/ |
364 |
|
|
|
365 |
|
|
size_t extfs_write(int fd, void *buffer, size_t length) |
366 |
|
|
{ |
367 |
|
|
errno = 0; |
368 |
|
|
return write(fd, buffer, length); |
369 |
|
|
} |