ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/extfs_unix.cpp
Revision: 1.13
Committed: 2000-07-21T18:01:07Z (24 years, 4 months ago) by cebix
Branch: MAIN
Changes since 1.12: +41 -92 lines
Log Message:
- extfs: replaced get/set_finder_*() functions by get/set_finfo(), helper
  files now store complete FInfo/FXInfo

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * extfs_unix.cpp - MacOS file system for access native file system access, Unix specific stuff
3     *
4 cebix 1.12 * Basilisk II (C) 1997-2000 Christian Bauer
5 cebix 1.1 *
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 cebix 1.13 *
82     * The .finf files store a FInfo/DInfo, followed by a FXInfo/DXInfo
83     * (16+16 bytes)
84 cebix 1.5 */
85    
86     static void make_helper_path(const char *src, char *dest, const char *add, bool only_dir = false)
87     {
88     dest[0] = 0;
89    
90     // Get pointer to last component of path
91     const char *last_part = strrchr(src, '/');
92     if (last_part)
93     last_part++;
94     else
95     last_part = 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     strncat(dest, add, MAX_PATH_LENGTH-1);
103    
104     // Add last component
105     if (!only_dir)
106     strncat(dest, last_part, MAX_PATH_LENGTH-1);
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 cebix 1.13 if (helper_dir[strlen(helper_dir) - 1] == '/') // Remove trailing "/"
114     helper_dir[strlen(helper_dir) - 1] = 0;
115 cebix 1.6 return mkdir(helper_dir, 0777);
116 cebix 1.5 }
117    
118     static int open_helper(const char *path, const char *add, int flag)
119     {
120     char helper_path[MAX_PATH_LENGTH];
121     make_helper_path(path, helper_path, add);
122    
123 cebix 1.7 if ((flag & O_ACCMODE) == O_RDWR || (flag & O_ACCMODE) == O_WRONLY)
124 cebix 1.5 flag |= O_CREAT;
125 cebix 1.6 int fd = open(helper_path, flag, 0666);
126 cebix 1.5 if (fd < 0) {
127     if (errno == ENOENT && (flag & O_CREAT)) {
128     // One path component was missing, probably the helper
129     // directory. Try to create it and re-open the file.
130     int ret = create_helper_dir(path, add);
131     if (ret < 0)
132     return ret;
133 cebix 1.6 fd = open(helper_path, flag, 0666);
134 cebix 1.5 }
135     }
136     return fd;
137     }
138    
139     static int open_finf(const char *path, int flag)
140     {
141     return open_helper(path, ".finf/", flag);
142     }
143    
144     static int open_rsrc(const char *path, int flag)
145     {
146     return open_helper(path, ".rsrc/", flag);
147 cebix 1.3 }
148    
149    
150     /*
151 cebix 1.13 * Get/set finder info for file/directory specified by full path
152 cebix 1.1 */
153    
154     struct ext2type {
155     const char *ext;
156     uint32 type;
157     uint32 creator;
158     };
159    
160     static const ext2type e2t_translation[] = {
161 cebix 1.2 {".Z", 'ZIVM', 'LZIV'},
162 cebix 1.1 {".gz", 'Gzip', 'Gzip'},
163     {".hqx", 'TEXT', 'SITx'},
164     {".pdf", 'PDF ', 'CARO'},
165     {".ps", 'TEXT', 'ttxt'},
166     {".sit", 'SIT!', 'SITx'},
167     {".tar", 'TARF', 'TAR '},
168     {".uu", 'TEXT', 'SITx'},
169     {".uue", 'TEXT', 'SITx'},
170     {".zip", 'ZIP ', 'ZIP '},
171     {".8svx", '8SVX', 'SNDM'},
172     {".aifc", 'AIFC', 'TVOD'},
173     {".aiff", 'AIFF', 'TVOD'},
174     {".au", 'ULAW', 'TVOD'},
175     {".mid", 'MIDI', 'TVOD'},
176     {".midi", 'MIDI', 'TVOD'},
177     {".mp2", 'MPG ', 'TVOD'},
178     {".mp3", 'MPG ', 'TVOD'},
179     {".wav", 'WAVE', 'TVOD'},
180     {".bmp", 'BMPf', 'ogle'},
181     {".gif", 'GIFf', 'ogle'},
182     {".lbm", 'ILBM', 'GKON'},
183     {".ilbm", 'ILBM', 'GKON'},
184     {".jpg", 'JPEG', 'ogle'},
185     {".jpeg", 'JPEG', 'ogle'},
186     {".pict", 'PICT', 'ogle'},
187     {".png", 'PNGf', 'ogle'},
188     {".sgi", '.SGI', 'ogle'},
189     {".tga", 'TPIC', 'ogle'},
190     {".tif", 'TIFF', 'ogle'},
191     {".tiff", 'TIFF', 'ogle'},
192 cebix 1.13 {".htm", 'TEXT', 'MOSS'},
193 cebix 1.1 {".html", 'TEXT', 'MOSS'},
194     {".txt", 'TEXT', 'ttxt'},
195     {".rtf", 'TEXT', 'MSWD'},
196     {".c", 'TEXT', 'R*ch'},
197 cebix 1.2 {".C", 'TEXT', 'R*ch'},
198 cebix 1.1 {".cc", 'TEXT', 'R*ch'},
199     {".cpp", 'TEXT', 'R*ch'},
200     {".cxx", 'TEXT', 'R*ch'},
201     {".h", 'TEXT', 'R*ch'},
202     {".hh", 'TEXT', 'R*ch'},
203     {".hpp", 'TEXT', 'R*ch'},
204     {".hxx", 'TEXT', 'R*ch'},
205     {".s", 'TEXT', 'R*ch'},
206 cebix 1.2 {".S", 'TEXT', 'R*ch'},
207 cebix 1.1 {".i", 'TEXT', 'R*ch'},
208     {".mpg", 'MPEG', 'TVOD'},
209     {".mpeg", 'MPEG', 'TVOD'},
210     {".mov", 'MooV', 'TVOD'},
211     {".fli", 'FLI ', 'TVOD'},
212     {".avi", 'VfW ', 'TVOD'},
213 cebix 1.13 {".qxd", 'XDOC', 'XPR3'},
214     {".hfv", 'DDim', 'ddsk'},
215     {".dsk", 'DDim', 'ddsk'},
216     {".img", 'rohd', 'ddsk'},
217 cebix 1.1 {NULL, 0, 0} // End marker
218     };
219    
220 cebix 1.13 void get_finfo(const char *path, uint32 finfo, uint32 fxinfo)
221 cebix 1.1 {
222 cebix 1.13 // Set default finder info
223     Mac_memset(finfo, 0, SIZEOF_FInfo);
224     if (fxinfo)
225     Mac_memset(fxinfo, 0, SIZEOF_FXInfo);
226     WriteMacInt16(finfo + fdFlags, DEFAULT_FINDER_FLAGS);
227     WriteMacInt32(finfo + fdLocation, (uint32)-1);
228 cebix 1.1
229 cebix 1.13 // Read Finder info file
230 cebix 1.5 int fd = open_finf(path, O_RDONLY);
231     if (fd >= 0) {
232 cebix 1.13 ssize_t actual = read(fd, Mac2HostAddr(finfo), SIZEOF_FInfo);
233     if (fxinfo)
234     actual += read(fd, Mac2HostAddr(fxinfo), SIZEOF_FXInfo);
235     close(fd);
236     if (actual >= SIZEOF_FInfo)
237 cebix 1.5 return;
238     }
239    
240     // No Finder info file, translate file name extension to MacOS type/creator
241 cebix 1.13 struct stat st;
242     if (stat(path, &st) == 0 && !S_ISDIR(st.st_mode)) {
243     int path_len = strlen(path);
244     for (int i=0; e2t_translation[i].ext; i++) {
245     int ext_len = strlen(e2t_translation[i].ext);
246     if (path_len < ext_len)
247     continue;
248     if (!strcmp(path + path_len - ext_len, e2t_translation[i].ext)) {
249     WriteMacInt32(finfo + fdType, e2t_translation[i].type);
250     WriteMacInt32(finfo + fdCreator, e2t_translation[i].creator);
251     break;
252     }
253 cebix 1.1 }
254     }
255     }
256    
257 cebix 1.13 void set_finfo(const char *path, uint32 finfo, uint32 fxinfo)
258 cebix 1.1 {
259 cebix 1.5 // Open Finder info file
260     int fd = open_finf(path, O_RDWR);
261     if (fd < 0)
262     return;
263    
264 cebix 1.13 // Write file
265     write(fd, Mac2HostAddr(finfo), SIZEOF_FInfo);
266     if (fxinfo)
267     write(fd, Mac2HostAddr(fxinfo), SIZEOF_FXInfo);
268 cebix 1.5 close(fd);
269 cebix 1.1 }
270    
271    
272     /*
273     * Resource fork emulation functions
274     */
275    
276     uint32 get_rfork_size(const char *path)
277     {
278 cebix 1.5 // Open resource file
279     int fd = open_rsrc(path, O_RDONLY);
280     if (fd < 0)
281     return 0;
282    
283     // Get size
284     off_t size = lseek(fd, 0, SEEK_END);
285    
286     // Close file and return size
287     close(fd);
288     return size < 0 ? 0 : size;
289 cebix 1.1 }
290    
291     int open_rfork(const char *path, int flag)
292     {
293 cebix 1.5 return open_rsrc(path, flag);
294 cebix 1.1 }
295    
296     void close_rfork(const char *path, int fd)
297     {
298 cebix 1.5 close(fd);
299 cebix 1.1 }
300    
301    
302     /*
303     * Read "length" bytes from file to "buffer",
304 cebix 1.10 * returns number of bytes read (or -1 on error)
305 cebix 1.1 */
306    
307 cebix 1.10 ssize_t extfs_read(int fd, void *buffer, size_t length)
308 cebix 1.1 {
309     return read(fd, buffer, length);
310     }
311    
312    
313     /*
314     * Write "length" bytes from "buffer" to file,
315 cebix 1.10 * returns number of bytes written (or -1 on error)
316 cebix 1.1 */
317    
318 cebix 1.10 ssize_t extfs_write(int fd, void *buffer, size_t length)
319 cebix 1.1 {
320     return write(fd, buffer, length);
321     }
322 cebix 1.8
323    
324     /*
325     * Remove file/directory (and associated helper files),
326     * returns false on error (and sets errno)
327     */
328    
329     bool extfs_remove(const char *path)
330     {
331     // Remove helpers first, don't complain if this fails
332     char helper_path[MAX_PATH_LENGTH];
333     make_helper_path(path, helper_path, ".finf/", false);
334     remove(helper_path);
335     make_helper_path(path, helper_path, ".rsrc/", false);
336     remove(helper_path);
337    
338 cebix 1.9 // Now remove file or directory (and helper directories in the directory)
339 cebix 1.8 if (remove(path) < 0) {
340 cebix 1.9 if (errno == EISDIR || errno == ENOTEMPTY) {
341     helper_path[0] = 0;
342     strncpy(helper_path, path, MAX_PATH_LENGTH-1);
343     add_path_component(helper_path, ".finf");
344     rmdir(helper_path);
345     helper_path[0] = 0;
346     strncpy(helper_path, path, MAX_PATH_LENGTH-1);
347     add_path_component(helper_path, ".rsrc");
348     rmdir(helper_path);
349 cebix 1.8 return rmdir(path) == 0;
350 cebix 1.9 } else
351 cebix 1.8 return false;
352     }
353     return true;
354 cebix 1.9 }
355    
356    
357     /*
358     * Rename/move file/directory (and associated helper files),
359     * returns false on error (and sets errno)
360     */
361    
362     bool extfs_rename(const char *old_path, const char *new_path)
363     {
364     // Rename helpers first, don't complain if this fails
365     char old_helper_path[MAX_PATH_LENGTH], new_helper_path[MAX_PATH_LENGTH];
366     make_helper_path(old_path, old_helper_path, ".finf/", false);
367     make_helper_path(new_path, new_helper_path, ".finf/", false);
368     create_helper_dir(new_path, ".finf/");
369     rename(old_helper_path, new_helper_path);
370     make_helper_path(old_path, old_helper_path, ".rsrc/", false);
371     make_helper_path(new_path, new_helper_path, ".rsrc/", false);
372     create_helper_dir(new_path, ".rsrc/");
373     rename(old_helper_path, new_helper_path);
374    
375     // Now rename file
376     return rename(old_path, new_path) == 0;
377 cebix 1.8 }