ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/extfs_macosx.mm
Revision: 1.5
Committed: 2006-04-04T07:08:12Z (18 years, 8 months ago) by gbeauche
Branch: MAIN
Changes since 1.4: +18 -8 lines
Log Message:
cleanups

File Contents

# User Rev Content
1 nigel 1.1 /*
2 gbeauche 1.5 * $Id: extfs_macosx.mm,v 1.4 2005/01/30 21:42:13 gbeauche Exp $
3 nigel 1.1 *
4     * extfs_macosx.mm - Access Mac OS X Finder and resource information (using Carbon calls).
5     * Based on:
6     *
7     * extfs_unix.cpp - MacOS file system for access native file system access, Unix specific stuff
8     *
9 gbeauche 1.4 * Basilisk II (C) 1997-2005 Christian Bauer
10 nigel 1.1 *
11     * This program is free software; you can redistribute it and/or modify
12     * it under the terms of the GNU General Public License as published by
13     * the Free Software Foundation; either version 2 of the License, or
14     * (at your option) any later version.
15     *
16     * This program is distributed in the hope that it will be useful,
17     * but WITHOUT ANY WARRANTY; without even the implied warranty of
18     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19     * GNU General Public License for more details.
20     *
21     * You should have received a copy of the GNU General Public License
22     * along with this program; if not, write to the Free Software
23     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24     */
25    
26     #include <sys/types.h>
27     #include <sys/stat.h>
28     #include <stdio.h>
29     #include <stdlib.h>
30     #include <unistd.h>
31     #include <dirent.h>
32     #include <errno.h>
33    
34     #include "sysdeps.h"
35     #include "extfs_macosx.h"
36     #include "extfs.h"
37    
38     #define DEBUG 0
39     #include "debug.h"
40    
41    
42     // Default Finder flags
43     const uint16 DEFAULT_FINDER_FLAGS = kHasBeenInited;
44    
45    
46     /*
47     * Initialization
48     */
49    
50     void extfs_init(void)
51     {
52     }
53    
54    
55     /*
56     * Deinitialization
57     */
58    
59     void extfs_exit(void)
60     {
61     }
62    
63    
64     /*
65     * Add component to path name
66     */
67    
68     void add_path_component(char *path, const char *component)
69     {
70     int l = strlen(path);
71     if (l < MAX_PATH_LENGTH-1 && path[l-1] != '/') {
72     path[l] = '/';
73     path[l+1] = 0;
74     }
75     strncat(path, component, MAX_PATH_LENGTH-1);
76     }
77    
78    
79     /*
80 nigel 1.2 * Add /rsrc to path name. Note that the 'correct' way to do this is to
81     * append '/..namedfork/rsrc', but I use this short form to save chars.
82 nigel 1.1 */
83    
84     void add_rsrc(const char *path, char *dest)
85     {
86     int l = strlen(path);
87    
88     if ( l > MAX_PATH_LENGTH-5 ) // If there is no room to add "rsrc\0"
89     return;
90    
91     *dest = '\0';
92     strncat(dest, path, l);
93    
94     if (l < MAX_PATH_LENGTH-1 && path[l-1] != '/') {
95     dest[l] = '/';
96     dest[l+1] = 0;
97     }
98     strcat(dest, "rsrc");
99     }
100    
101    
102     /*
103     * Resource forks on Mac OS X are kept on a UFS volume as /path/file/rsrc
104     *
105     * On HFS volumes, there is of course the data and rsrc fork, but the Darwin
106 gbeauche 1.5 * filesystem layer presents the resource fork using the above pseudo path
107 nigel 1.1 */
108    
109     static int open_rsrc(const char *path, int flag)
110     {
111     char path_rsrc[MAX_PATH_LENGTH];
112    
113     add_rsrc(path, path_rsrc);
114     return open(path_rsrc, flag);
115     }
116    
117     /*
118     * Finder info is a little bit harder. We need to use Carbon library calls to access them
119     */
120    
121    
122     /*
123     * Get/set finder info for file/directory specified by full path
124     */
125    
126     struct ext2type {
127     const char *ext;
128     uint32 type;
129     uint32 creator;
130     };
131    
132     static const ext2type e2t_translation[] = {
133     {".Z", FOURCC('Z','I','V','M'), FOURCC('L','Z','I','V')},
134     {".gz", FOURCC('G','z','i','p'), FOURCC('G','z','i','p')},
135     {".hqx", FOURCC('T','E','X','T'), FOURCC('S','I','T','x')},
136     {".bin", FOURCC('T','E','X','T'), FOURCC('S','I','T','x')},
137     {".pdf", FOURCC('P','D','F',' '), FOURCC('C','A','R','O')},
138     {".ps", FOURCC('T','E','X','T'), FOURCC('t','t','x','t')},
139     {".sit", FOURCC('S','I','T','!'), FOURCC('S','I','T','x')},
140     {".tar", FOURCC('T','A','R','F'), FOURCC('T','A','R',' ')},
141     {".uu", FOURCC('T','E','X','T'), FOURCC('S','I','T','x')},
142     {".uue", FOURCC('T','E','X','T'), FOURCC('S','I','T','x')},
143     {".zip", FOURCC('Z','I','P',' '), FOURCC('Z','I','P',' ')},
144     {".8svx", FOURCC('8','S','V','X'), FOURCC('S','N','D','M')},
145     {".aifc", FOURCC('A','I','F','C'), FOURCC('T','V','O','D')},
146     {".aiff", FOURCC('A','I','F','F'), FOURCC('T','V','O','D')},
147     {".au", FOURCC('U','L','A','W'), FOURCC('T','V','O','D')},
148     {".mid", FOURCC('M','I','D','I'), FOURCC('T','V','O','D')},
149     {".midi", FOURCC('M','I','D','I'), FOURCC('T','V','O','D')},
150     {".mp2", FOURCC('M','P','G',' '), FOURCC('T','V','O','D')},
151     {".mp3", FOURCC('M','P','G',' '), FOURCC('T','V','O','D')},
152     {".wav", FOURCC('W','A','V','E'), FOURCC('T','V','O','D')},
153     {".bmp", FOURCC('B','M','P','f'), FOURCC('o','g','l','e')},
154     {".gif", FOURCC('G','I','F','f'), FOURCC('o','g','l','e')},
155     {".lbm", FOURCC('I','L','B','M'), FOURCC('G','K','O','N')},
156     {".ilbm", FOURCC('I','L','B','M'), FOURCC('G','K','O','N')},
157     {".jpg", FOURCC('J','P','E','G'), FOURCC('o','g','l','e')},
158     {".jpeg", FOURCC('J','P','E','G'), FOURCC('o','g','l','e')},
159     {".pict", FOURCC('P','I','C','T'), FOURCC('o','g','l','e')},
160     {".png", FOURCC('P','N','G','f'), FOURCC('o','g','l','e')},
161     {".sgi", FOURCC('.','S','G','I'), FOURCC('o','g','l','e')},
162     {".tga", FOURCC('T','P','I','C'), FOURCC('o','g','l','e')},
163     {".tif", FOURCC('T','I','F','F'), FOURCC('o','g','l','e')},
164     {".tiff", FOURCC('T','I','F','F'), FOURCC('o','g','l','e')},
165     {".htm", FOURCC('T','E','X','T'), FOURCC('M','O','S','S')},
166     {".html", FOURCC('T','E','X','T'), FOURCC('M','O','S','S')},
167     {".txt", FOURCC('T','E','X','T'), FOURCC('t','t','x','t')},
168     {".rtf", FOURCC('T','E','X','T'), FOURCC('M','S','W','D')},
169     {".c", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
170     {".C", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
171     {".cc", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
172     {".cpp", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
173     {".cxx", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
174     {".h", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
175     {".hh", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
176     {".hpp", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
177     {".hxx", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
178     {".s", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
179     {".S", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
180     {".i", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
181     {".mpg", FOURCC('M','P','E','G'), FOURCC('T','V','O','D')},
182     {".mpeg", FOURCC('M','P','E','G'), FOURCC('T','V','O','D')},
183     {".mov", FOURCC('M','o','o','V'), FOURCC('T','V','O','D')},
184     {".fli", FOURCC('F','L','I',' '), FOURCC('T','V','O','D')},
185     {".avi", FOURCC('V','f','W',' '), FOURCC('T','V','O','D')},
186     {".qxd", FOURCC('X','D','O','C'), FOURCC('X','P','R','3')},
187     {".hfv", FOURCC('D','D','i','m'), FOURCC('d','d','s','k')},
188     {".dsk", FOURCC('D','D','i','m'), FOURCC('d','d','s','k')},
189     {".img", FOURCC('r','o','h','d'), FOURCC('d','d','s','k')},
190     {NULL, 0, 0} // End marker
191     };
192    
193     // Mac OS X way of doing the above
194    
195     #import <Foundation/NSString.h>
196    
197     extern "C"
198     {
199     NSString *NSHFSTypeOfFile (const NSString *);
200     uint32 NSHFSTypeCodeFromFileType (const NSString *); // Actually returns an OSType
201     }
202    
203     uint32 fileType (const char *cPath)
204     {
205     NSString *copy = [NSString stringWithCString: cPath],
206     *type = NSHFSTypeOfFile(copy);
207    
208     if ( type == nil )
209     {
210     D(NSLog(@"No type for file %s", cPath));
211     return 0; // Should this be '????' or ' ' ?
212     }
213    
214     D(NSLog(@"Got type %@ for %s", type, cPath));
215     return NSHFSTypeCodeFromFileType(type);
216     }
217    
218    
219     void get_finfo(const char *path, uint32 finfo, uint32 fxinfo, bool is_dir)
220     {
221     FSRef fsRef;
222     int32 status;
223    
224     // Set default finder info
225     Mac_memset(finfo, 0, SIZEOF_FInfo);
226     if (fxinfo)
227     Mac_memset(fxinfo, 0, SIZEOF_FXInfo);
228     WriteMacInt16(finfo + fdFlags, DEFAULT_FINDER_FLAGS);
229     WriteMacInt32(finfo + fdLocation, (uint32)-1);
230    
231     status = FSPathMakeRef((const uint8 *)path, &fsRef, NULL);
232     if ( status == noErr )
233     {
234     FSCatalogInfo cInfo;
235 gbeauche 1.5 uint32 AllFinderInfo = kFSCatInfoFinderInfo;
236    
237     if (fxinfo)
238     AllFinderInfo |= kFSCatInfoFinderXInfo;
239 nigel 1.1
240     status = FSGetCatalogInfo(&fsRef, AllFinderInfo, &cInfo, NULL, NULL, NULL);
241     if ( status == noErr )
242     {
243     D(printf("get_finfo(%s,...) - Got info of '%16.16s'\n", path, cInfo.finderInfo));
244 gbeauche 1.5 Host2Mac_memcpy(finfo, &cInfo.finderInfo, SIZEOF_FInfo);
245 nigel 1.1 if (fxinfo)
246 gbeauche 1.5 Host2Mac_memcpy(fxinfo, &cInfo.extFinderInfo, SIZEOF_FXInfo);
247 nigel 1.1 return;
248     }
249     else
250     printf("get_finfo(%s,...) failed to get catalog info\n", path);
251     }
252     else
253     printf("get_finfo(%s,...) failed to get FSRef\n", path);
254    
255    
256     // No Finder info file, translate file name extension to MacOS type/creator
257     if (!is_dir) {
258     int path_len = strlen(path);
259     for (int i=0; e2t_translation[i].ext; i++) {
260     int ext_len = strlen(e2t_translation[i].ext);
261     if (path_len < ext_len)
262     continue;
263     if (!strcmp(path + path_len - ext_len, e2t_translation[i].ext)) {
264     WriteMacInt32(finfo + fdType, e2t_translation[i].type);
265     WriteMacInt32(finfo + fdCreator, e2t_translation[i].creator);
266     break;
267     }
268     }
269     }
270    
271    
272     // Use alternate code to get type
273     uint32 type = fileType(path);
274    
275     if ( type )
276     WriteMacInt32(finfo + fdType, type);
277     }
278    
279     void set_finfo(const char *path, uint32 finfo, uint32 fxinfo, bool is_dir)
280     {
281     FSRef fsRef;
282     OSErr status;
283    
284     status = FSPathMakeRef((const uint8 *)path, &fsRef, NULL);
285     if ( status == noErr )
286     {
287     FSCatalogInfo cInfo;
288    
289     status = FSGetCatalogInfo(&fsRef, kFSCatInfoFinderInfo, &cInfo, NULL, NULL, NULL);
290     if ( status == noErr )
291     {
292 gbeauche 1.5 FSCatalogInfoBitmap whichInfo = kFSCatInfoNone;
293     if (finfo) {
294     whichInfo |= kFSCatInfoFinderInfo;
295     Mac2Host_memcpy(&cInfo.finderInfo, finfo, SIZEOF_FInfo);
296     }
297     if (fxinfo) {
298     whichInfo |= kFSCatInfoFinderXInfo;
299     Mac2Host_memcpy(&cInfo.extFinderInfo, fxinfo, SIZEOF_FXInfo);
300     }
301     FSSetCatalogInfo(&fsRef, whichInfo, &cInfo);
302 nigel 1.1 }
303     }
304     }
305    
306    
307     /*
308     * Resource fork emulation functions
309     */
310    
311     uint32 get_rfork_size(const char *path)
312     {
313     // Open resource file
314     int fd = open_rsrc(path, O_RDONLY);
315     if (fd < 0)
316     return 0;
317    
318     // Get size
319     off_t size = lseek(fd, 0, SEEK_END);
320    
321     // Close file and return size
322     close(fd);
323     return size < 0 ? 0 : size;
324     }
325    
326     int open_rfork(const char *path, int flag)
327     {
328     return open_rsrc(path, flag);
329     }
330    
331     void close_rfork(const char *path, int fd)
332     {
333     close(fd);
334     }
335    
336    
337     /*
338     * Read "length" bytes from file to "buffer",
339     * returns number of bytes read (or -1 on error)
340     */
341    
342     ssize_t extfs_read(int fd, void *buffer, size_t length)
343     {
344     return read(fd, buffer, length);
345     }
346    
347    
348     /*
349     * Write "length" bytes from "buffer" to file,
350     * returns number of bytes written (or -1 on error)
351     */
352    
353     ssize_t extfs_write(int fd, void *buffer, size_t length)
354     {
355     return write(fd, buffer, length);
356     }
357    
358    
359     /*
360     * Remove file/directory (and associated helper files),
361     * returns false on error (and sets errno)
362     */
363    
364     bool extfs_remove(const char *path)
365     {
366     // Remove helpers first, don't complain if this fails
367     char helper_path[MAX_PATH_LENGTH];
368     add_rsrc(path, helper_path);
369     remove(helper_path);
370    
371     // Now remove file or directory (and helper directories in the directory)
372     if (remove(path) < 0) {
373     if (errno == EISDIR || errno == ENOTEMPTY) {
374     return rmdir(path) == 0;
375     } else
376     return false;
377     }
378     return true;
379     }
380    
381    
382     /*
383     * Rename/move file/directory (and associated helper files),
384     * returns false on error (and sets errno)
385     */
386    
387     bool extfs_rename(const char *old_path, const char *new_path)
388     {
389     // Rename helpers first, don't complain if this fails
390     char old_helper_path[MAX_PATH_LENGTH], new_helper_path[MAX_PATH_LENGTH];
391     add_rsrc(old_path, old_helper_path);
392     add_rsrc(new_path, new_helper_path);
393     rename(old_helper_path, new_helper_path);
394    
395     // Now rename file
396     return rename(old_path, new_path) == 0;
397     }