ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/AmigaOS/extfs_amiga.cpp
Revision: 1.3
Committed: 1999-11-08T17:00:11Z (25 years ago) by cebix
Branch: MAIN
Changes since 1.2: +25 -0 lines
Log Message:
- fs_delete() will also delete helper files

File Contents

# Content
1 /*
2 * extfs_amiga.cpp - MacOS file system for access native file system access, AmigaOS 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 <exec/types.h>
22 #include <proto/dos.h>
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <dirent.h>
31 #include <errno.h>
32
33 #include "sysdeps.h"
34 #include "extfs.h"
35 #include "extfs_defs.h"
36
37 #define DEBUG 0
38 #include "debug.h"
39
40
41 // Default Finder flags
42 const uint16 DEFAULT_FINDER_FLAGS = kHasBeenInited;
43
44
45 /*
46 * Initialization
47 */
48
49 void extfs_init(void)
50 {
51 }
52
53
54 /*
55 * Deinitialization
56 */
57
58 void extfs_exit(void)
59 {
60 }
61
62
63 /*
64 * Add component to path name
65 */
66
67 void add_path_component(char *path, const char *component)
68 {
69 AddPart(path, (char *)component, MAX_PATH_LENGTH);
70 }
71
72
73 /*
74 * Finder info and resource forks are kept in helper files
75 *
76 * Finder info:
77 * /path/.finf/file
78 * Resource fork:
79 * /path/.rsrc/file
80 */
81
82 // Layout of Finder info helper files (all fields big-endian)
83 struct finf_struct {
84 uint32 type;
85 uint32 creator;
86 uint16 flags;
87 uint16 pad0;
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 = FilePart((char *)src);
96
97 // Copy everything before
98 strncpy(dest, src, last_part-src);
99 dest[last_part-src] = 0;
100
101 // Add additional component
102 AddPart(dest, (char *)add, MAX_PATH_LENGTH);
103
104 // Add last component
105 if (!only_dir)
106 AddPart(dest, (char *)last_part, MAX_PATH_LENGTH);
107 }
108
109 static int create_helper_dir(const char *path, const char *add)
110 {
111 char helper_dir[MAX_PATH_LENGTH];
112 make_helper_path(path, helper_dir, add, true);
113 return mkdir(helper_dir, 0777);
114 }
115
116 static int open_helper(const char *path, const char *add, int flag)
117 {
118 char helper_path[MAX_PATH_LENGTH];
119 make_helper_path(path, helper_path, add);
120
121 if ((flag & O_ACCMODE) == O_RDWR || (flag & O_ACCMODE) == O_WRONLY)
122 flag |= O_CREAT;
123 int fd = open(helper_path, flag, 0666);
124 if (fd < 0) {
125 if (errno == ENOENT && (flag & O_CREAT)) {
126 // One path component was missing, probably the helper
127 // directory. Try to create it and re-open the file.
128 int ret = create_helper_dir(path, add);
129 if (ret < 0)
130 return ret;
131 fd = open(helper_path, flag, 0666);
132 }
133 }
134 return fd;
135 }
136
137 static int open_finf(const char *path, int flag)
138 {
139 return open_helper(path, ".finf/", flag);
140 }
141
142 static int open_rsrc(const char *path, int flag)
143 {
144 return open_helper(path, ".rsrc/", flag);
145 }
146
147
148 /*
149 * Get/set finder type/creator for file specified by full path
150 */
151
152 struct ext2type {
153 const char *ext;
154 uint32 type;
155 uint32 creator;
156 };
157
158 static const ext2type e2t_translation[] = {
159 {".Z", 'ZIVM', 'LZIV'},
160 {".gz", 'Gzip', 'Gzip'},
161 {".hqx", 'TEXT', 'SITx'},
162 {".pdf", 'PDF ', 'CARO'},
163 {".ps", 'TEXT', 'ttxt'},
164 {".sit", 'SIT!', 'SITx'},
165 {".tar", 'TARF', 'TAR '},
166 {".uu", 'TEXT', 'SITx'},
167 {".uue", 'TEXT', 'SITx'},
168 {".zip", 'ZIP ', 'ZIP '},
169 {".8svx", '8SVX', 'SNDM'},
170 {".aifc", 'AIFC', 'TVOD'},
171 {".aiff", 'AIFF', 'TVOD'},
172 {".au", 'ULAW', 'TVOD'},
173 {".mid", 'MIDI', 'TVOD'},
174 {".midi", 'MIDI', 'TVOD'},
175 {".mp2", 'MPG ', 'TVOD'},
176 {".mp3", 'MPG ', 'TVOD'},
177 {".wav", 'WAVE', 'TVOD'},
178 {".bmp", 'BMPf', 'ogle'},
179 {".gif", 'GIFf', 'ogle'},
180 {".lbm", 'ILBM', 'GKON'},
181 {".ilbm", 'ILBM', 'GKON'},
182 {".jpg", 'JPEG', 'ogle'},
183 {".jpeg", 'JPEG', 'ogle'},
184 {".pict", 'PICT', 'ogle'},
185 {".png", 'PNGf', 'ogle'},
186 {".sgi", '.SGI', 'ogle'},
187 {".tga", 'TPIC', 'ogle'},
188 {".tif", 'TIFF', 'ogle'},
189 {".tiff", 'TIFF', 'ogle'},
190 {".html", 'TEXT', 'MOSS'},
191 {".txt", 'TEXT', 'ttxt'},
192 {".rtf", 'TEXT', 'MSWD'},
193 {".c", 'TEXT', 'R*ch'},
194 {".C", 'TEXT', 'R*ch'},
195 {".cc", 'TEXT', 'R*ch'},
196 {".cpp", 'TEXT', 'R*ch'},
197 {".cxx", 'TEXT', 'R*ch'},
198 {".h", 'TEXT', 'R*ch'},
199 {".hh", 'TEXT', 'R*ch'},
200 {".hpp", 'TEXT', 'R*ch'},
201 {".hxx", 'TEXT', 'R*ch'},
202 {".s", 'TEXT', 'R*ch'},
203 {".S", 'TEXT', 'R*ch'},
204 {".i", 'TEXT', 'R*ch'},
205 {".mpg", 'MPEG', 'TVOD'},
206 {".mpeg", 'MPEG', 'TVOD'},
207 {".mov", 'MooV', 'TVOD'},
208 {".fli", 'FLI ', 'TVOD'},
209 {".avi", 'VfW ', 'TVOD'},
210 {NULL, 0, 0} // End marker
211 };
212
213 void get_finder_type(const char *path, uint32 &type, uint32 &creator)
214 {
215 type = 0;
216 creator = 0;
217
218 // Open Finder info file
219 int fd = open_finf(path, O_RDONLY);
220 if (fd >= 0) {
221
222 // Read file
223 finf_struct finf;
224 if (read(fd, &finf, sizeof(finf_struct)) == sizeof(finf_struct)) {
225
226 // Type/creator are in Finder info file, return them
227 type = ntohl(finf.type);
228 creator = ntohl(finf.creator);
229 close(fd);
230 return;
231 }
232 close(fd);
233 }
234
235 // No Finder info file, translate file name extension to MacOS type/creator
236 int path_len = strlen(path);
237 for (int i=0; e2t_translation[i].ext; i++) {
238 int ext_len = strlen(e2t_translation[i].ext);
239 if (path_len < ext_len)
240 continue;
241 if (!strcmp(path + path_len - ext_len, e2t_translation[i].ext)) {
242 type = e2t_translation[i].type;
243 creator = e2t_translation[i].creator;
244 break;
245 }
246 }
247 }
248
249 void set_finder_type(const char *path, uint32 type, uint32 creator)
250 {
251 // Open Finder info file
252 int fd = open_finf(path, O_RDWR);
253 if (fd < 0)
254 return;
255
256 // Read file
257 finf_struct finf = {0, 0, DEFAULT_FINDER_FLAGS, 0};
258 read(fd, &finf, sizeof(finf_struct));
259
260 // Set Finder flags
261 finf.type = htonl(type);
262 finf.creator = htonl(creator);
263
264 // Update file
265 lseek(fd, 0, SEEK_SET);
266 write(fd, &finf, sizeof(finf_struct));
267 close(fd);
268 }
269
270
271 /*
272 * Get/set finder flags for file/dir specified by full path
273 */
274
275 void get_finder_flags(const char *path, uint16 &flags)
276 {
277 flags = DEFAULT_FINDER_FLAGS; // Default
278
279 // Open Finder info file
280 int fd = open_finf(path, O_RDONLY);
281 if (fd < 0)
282 return;
283
284 // Read Finder flags
285 finf_struct finf;
286 if (read(fd, &finf, sizeof(finf_struct)) == sizeof(finf_struct))
287 flags = ntohs(finf.flags);
288
289 // Close file
290 close(fd);
291 }
292
293 void set_finder_flags(const char *path, uint16 flags)
294 {
295 // Open Finder info file
296 int fd = open_finf(path, O_RDWR);
297 if (fd < 0)
298 return;
299
300 // Read file
301 finf_struct finf = {0, 0, DEFAULT_FINDER_FLAGS, 0};
302 read(fd, &finf, sizeof(finf_struct));
303
304 // Set Finder flags
305 finf.flags = htons(flags);
306
307 // Update file
308 lseek(fd, 0, SEEK_SET);
309 write(fd, &finf, sizeof(finf_struct));
310 close(fd);
311 }
312
313
314 /*
315 * Resource fork emulation functions
316 */
317
318 uint32 get_rfork_size(const char *path)
319 {
320 // Open resource file
321 int fd = open_rsrc(path, O_RDONLY);
322 if (fd < 0)
323 return 0;
324
325 // Get size
326 off_t size = lseek(fd, 0, SEEK_END);
327
328 // Close file and return size
329 close(fd);
330 return size < 0 ? 0 : size;
331 }
332
333 int open_rfork(const char *path, int flag)
334 {
335 return open_rsrc(path, flag);
336 }
337
338 void close_rfork(const char *path, int fd)
339 {
340 close(fd);
341 }
342
343
344 /*
345 * Read "length" bytes from file to "buffer",
346 * returns number of bytes read (or 0)
347 */
348
349 size_t extfs_read(int fd, void *buffer, size_t length)
350 {
351 errno = 0;
352 return read(fd, buffer, length);
353 }
354
355
356 /*
357 * Write "length" bytes from "buffer" to file,
358 * returns number of bytes written (or 0)
359 */
360
361 size_t extfs_write(int fd, void *buffer, size_t length)
362 {
363 errno = 0;
364 return write(fd, buffer, length);
365 }
366
367
368 /*
369 * Remove file/directory (and associated helper files),
370 * returns false on error (and sets errno)
371 */
372
373 bool extfs_remove(const char *path)
374 {
375 // Remove helpers first, don't complain if this fails
376 char helper_path[MAX_PATH_LENGTH];
377 make_helper_path(path, helper_path, ".finf/", false);
378 remove(helper_path);
379 make_helper_path(path, helper_path, ".rsrc/", false);
380 remove(helper_path);
381
382 // Now remove file or directory
383 if (remove(path) < 0) {
384 if (errno == EISDIR)
385 return rmdir(path) == 0;
386 else
387 return false;
388 }
389 return true;
390 }
391
392
393 /*
394 * ftruncate() is missing from libnix
395 */
396
397 extern unsigned long *__stdfiledes;
398
399 int ftruncate(int fd, off_t size)
400 {
401 if (SetFileSize(__stdfiledes[fd], size, OFFSET_BEGINNING) < 0)
402 return -1;
403 else
404 return 0;
405 }