ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/extfs_macosx.mm
Revision: 1.7
Committed: 2007-01-21T17:04:47Z (17 years, 10 months ago) by asvitkine
Branch: MAIN
Changes since 1.6: +9 -1 lines
Log Message:
Byteswapping fixes for Mac OS X x86

File Contents

# User Rev Content
1 nigel 1.1 /*
2 asvitkine 1.7 * $Id: extfs_macosx.mm,v 1.6 2006/04/30 14:46:15 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 gbeauche 1.6
118 nigel 1.1 /*
119     * Finder info is a little bit harder. We need to use Carbon library calls to access them
120     */
121    
122    
123     /*
124     * Get/set finder info for file/directory specified by full path
125     */
126    
127     struct ext2type {
128     const char *ext;
129     uint32 type;
130     uint32 creator;
131     };
132    
133     static const ext2type e2t_translation[] = {
134     {".Z", FOURCC('Z','I','V','M'), FOURCC('L','Z','I','V')},
135     {".gz", FOURCC('G','z','i','p'), FOURCC('G','z','i','p')},
136     {".hqx", FOURCC('T','E','X','T'), FOURCC('S','I','T','x')},
137     {".bin", FOURCC('T','E','X','T'), FOURCC('S','I','T','x')},
138     {".pdf", FOURCC('P','D','F',' '), FOURCC('C','A','R','O')},
139     {".ps", FOURCC('T','E','X','T'), FOURCC('t','t','x','t')},
140     {".sit", FOURCC('S','I','T','!'), FOURCC('S','I','T','x')},
141     {".tar", FOURCC('T','A','R','F'), FOURCC('T','A','R',' ')},
142     {".uu", FOURCC('T','E','X','T'), FOURCC('S','I','T','x')},
143     {".uue", FOURCC('T','E','X','T'), FOURCC('S','I','T','x')},
144     {".zip", FOURCC('Z','I','P',' '), FOURCC('Z','I','P',' ')},
145     {".8svx", FOURCC('8','S','V','X'), FOURCC('S','N','D','M')},
146     {".aifc", FOURCC('A','I','F','C'), FOURCC('T','V','O','D')},
147     {".aiff", FOURCC('A','I','F','F'), FOURCC('T','V','O','D')},
148     {".au", FOURCC('U','L','A','W'), FOURCC('T','V','O','D')},
149     {".mid", FOURCC('M','I','D','I'), FOURCC('T','V','O','D')},
150     {".midi", FOURCC('M','I','D','I'), FOURCC('T','V','O','D')},
151     {".mp2", FOURCC('M','P','G',' '), FOURCC('T','V','O','D')},
152     {".mp3", FOURCC('M','P','G',' '), FOURCC('T','V','O','D')},
153     {".wav", FOURCC('W','A','V','E'), FOURCC('T','V','O','D')},
154     {".bmp", FOURCC('B','M','P','f'), FOURCC('o','g','l','e')},
155     {".gif", FOURCC('G','I','F','f'), FOURCC('o','g','l','e')},
156     {".lbm", FOURCC('I','L','B','M'), FOURCC('G','K','O','N')},
157     {".ilbm", FOURCC('I','L','B','M'), FOURCC('G','K','O','N')},
158     {".jpg", FOURCC('J','P','E','G'), FOURCC('o','g','l','e')},
159     {".jpeg", FOURCC('J','P','E','G'), FOURCC('o','g','l','e')},
160     {".pict", FOURCC('P','I','C','T'), FOURCC('o','g','l','e')},
161     {".png", FOURCC('P','N','G','f'), FOURCC('o','g','l','e')},
162     {".sgi", FOURCC('.','S','G','I'), FOURCC('o','g','l','e')},
163     {".tga", FOURCC('T','P','I','C'), FOURCC('o','g','l','e')},
164     {".tif", FOURCC('T','I','F','F'), FOURCC('o','g','l','e')},
165     {".tiff", FOURCC('T','I','F','F'), FOURCC('o','g','l','e')},
166     {".htm", FOURCC('T','E','X','T'), FOURCC('M','O','S','S')},
167     {".html", FOURCC('T','E','X','T'), FOURCC('M','O','S','S')},
168     {".txt", FOURCC('T','E','X','T'), FOURCC('t','t','x','t')},
169     {".rtf", FOURCC('T','E','X','T'), FOURCC('M','S','W','D')},
170     {".c", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
171     {".C", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
172     {".cc", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
173     {".cpp", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
174     {".cxx", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
175     {".h", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
176     {".hh", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
177     {".hpp", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
178     {".hxx", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
179     {".s", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
180     {".S", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
181     {".i", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')},
182     {".mpg", FOURCC('M','P','E','G'), FOURCC('T','V','O','D')},
183     {".mpeg", FOURCC('M','P','E','G'), FOURCC('T','V','O','D')},
184     {".mov", FOURCC('M','o','o','V'), FOURCC('T','V','O','D')},
185     {".fli", FOURCC('F','L','I',' '), FOURCC('T','V','O','D')},
186     {".avi", FOURCC('V','f','W',' '), FOURCC('T','V','O','D')},
187     {".qxd", FOURCC('X','D','O','C'), FOURCC('X','P','R','3')},
188     {".hfv", FOURCC('D','D','i','m'), FOURCC('d','d','s','k')},
189     {".dsk", FOURCC('D','D','i','m'), FOURCC('d','d','s','k')},
190     {".img", FOURCC('r','o','h','d'), FOURCC('d','d','s','k')},
191     {NULL, 0, 0} // End marker
192     };
193    
194     // Mac OS X way of doing the above
195    
196     #import <Foundation/NSString.h>
197    
198     extern "C"
199     {
200     NSString *NSHFSTypeOfFile (const NSString *);
201     uint32 NSHFSTypeCodeFromFileType (const NSString *); // Actually returns an OSType
202     }
203    
204     uint32 fileType (const char *cPath)
205     {
206     NSString *copy = [NSString stringWithCString: cPath],
207     *type = NSHFSTypeOfFile(copy);
208    
209     if ( type == nil )
210     {
211     D(NSLog(@"No type for file %s", cPath));
212     return 0; // Should this be '????' or ' ' ?
213     }
214    
215     D(NSLog(@"Got type %@ for %s", type, cPath));
216     return NSHFSTypeCodeFromFileType(type);
217     }
218    
219    
220     void get_finfo(const char *path, uint32 finfo, uint32 fxinfo, bool is_dir)
221     {
222     FSRef fsRef;
223     int32 status;
224    
225     // Set default finder info
226     Mac_memset(finfo, 0, SIZEOF_FInfo);
227     if (fxinfo)
228     Mac_memset(fxinfo, 0, SIZEOF_FXInfo);
229     WriteMacInt16(finfo + fdFlags, DEFAULT_FINDER_FLAGS);
230     WriteMacInt32(finfo + fdLocation, (uint32)-1);
231    
232     status = FSPathMakeRef((const uint8 *)path, &fsRef, NULL);
233     if ( status == noErr )
234     {
235     FSCatalogInfo cInfo;
236 gbeauche 1.5 uint32 AllFinderInfo = kFSCatInfoFinderInfo;
237    
238     if (fxinfo)
239     AllFinderInfo |= kFSCatInfoFinderXInfo;
240 nigel 1.1
241     status = FSGetCatalogInfo(&fsRef, AllFinderInfo, &cInfo, NULL, NULL, NULL);
242     if ( status == noErr )
243     {
244 asvitkine 1.7 // byte-swap them to big endian (network order) if necessary
245     ((FileInfo *)&cInfo.finderInfo)->fileType = htonl(((FileInfo *)&cInfo.finderInfo)->fileType);
246     ((FileInfo *)&cInfo.finderInfo)->fileCreator = htonl(((FileInfo *)&cInfo.finderInfo)->fileCreator);
247     ((FileInfo *)&cInfo.finderInfo)->finderFlags = htons(((FileInfo *)&cInfo.finderInfo)->finderFlags);
248 nigel 1.1 D(printf("get_finfo(%s,...) - Got info of '%16.16s'\n", path, cInfo.finderInfo));
249 gbeauche 1.5 Host2Mac_memcpy(finfo, &cInfo.finderInfo, SIZEOF_FInfo);
250 nigel 1.1 if (fxinfo)
251 gbeauche 1.5 Host2Mac_memcpy(fxinfo, &cInfo.extFinderInfo, SIZEOF_FXInfo);
252 nigel 1.1 return;
253     }
254     else
255     printf("get_finfo(%s,...) failed to get catalog info\n", path);
256     }
257     else
258     printf("get_finfo(%s,...) failed to get FSRef\n", path);
259    
260    
261     // No Finder info file, translate file name extension to MacOS type/creator
262     if (!is_dir) {
263     int path_len = strlen(path);
264     for (int i=0; e2t_translation[i].ext; i++) {
265     int ext_len = strlen(e2t_translation[i].ext);
266     if (path_len < ext_len)
267     continue;
268     if (!strcmp(path + path_len - ext_len, e2t_translation[i].ext)) {
269     WriteMacInt32(finfo + fdType, e2t_translation[i].type);
270     WriteMacInt32(finfo + fdCreator, e2t_translation[i].creator);
271     break;
272     }
273     }
274     }
275    
276    
277     // Use alternate code to get type
278     uint32 type = fileType(path);
279    
280     if ( type )
281     WriteMacInt32(finfo + fdType, type);
282     }
283    
284     void set_finfo(const char *path, uint32 finfo, uint32 fxinfo, bool is_dir)
285     {
286     FSRef fsRef;
287     OSErr status;
288    
289     status = FSPathMakeRef((const uint8 *)path, &fsRef, NULL);
290     if ( status == noErr )
291     {
292     FSCatalogInfo cInfo;
293    
294     status = FSGetCatalogInfo(&fsRef, kFSCatInfoFinderInfo, &cInfo, NULL, NULL, NULL);
295     if ( status == noErr )
296     {
297 gbeauche 1.5 FSCatalogInfoBitmap whichInfo = kFSCatInfoNone;
298     if (finfo) {
299     whichInfo |= kFSCatInfoFinderInfo;
300 gbeauche 1.6 /*
301     FIXME: Some users reported that directories could
302     mysteriously disappear from a MacOS X 10.4 Finder
303     point of view.
304    
305     An alternative is to use the ".finfo/" technique
306     on MacOS X provided that attributes are first
307     initialised from an FSGetCatalogInfo() call.
308     */
309     uint8 oldFlags1, oldFlags2;
310     if (is_dir) {
311     oldFlags1 = cInfo.finderInfo[fdFlags];
312     oldFlags2 = cInfo.finderInfo[fdFlags + 1];
313     }
314 gbeauche 1.5 Mac2Host_memcpy(&cInfo.finderInfo, finfo, SIZEOF_FInfo);
315 gbeauche 1.6 if (is_dir) {
316     cInfo.finderInfo[fdFlags] = oldFlags1;
317     cInfo.finderInfo[fdFlags + 1] = oldFlags2;
318     }
319 gbeauche 1.5 }
320     if (fxinfo) {
321     whichInfo |= kFSCatInfoFinderXInfo;
322     Mac2Host_memcpy(&cInfo.extFinderInfo, fxinfo, SIZEOF_FXInfo);
323     }
324 asvitkine 1.7 // byte-swap them to system byte order from big endian (network order) if necessary
325     ((FileInfo *)&cInfo.finderInfo)->fileType = ntohl(((FileInfo *)&cInfo.finderInfo)->fileType);
326     ((FileInfo *)&cInfo.finderInfo)->fileCreator = ntohl(((FileInfo *)&cInfo.finderInfo)->fileCreator);
327     ((FileInfo *)&cInfo.finderInfo)->finderFlags = ntohs(((FileInfo *)&cInfo.finderInfo)->finderFlags);
328 gbeauche 1.5 FSSetCatalogInfo(&fsRef, whichInfo, &cInfo);
329 nigel 1.1 }
330     }
331     }
332    
333    
334     /*
335     * Resource fork emulation functions
336     */
337    
338     uint32 get_rfork_size(const char *path)
339     {
340     // Open resource file
341     int fd = open_rsrc(path, O_RDONLY);
342     if (fd < 0)
343     return 0;
344    
345     // Get size
346     off_t size = lseek(fd, 0, SEEK_END);
347    
348     // Close file and return size
349     close(fd);
350     return size < 0 ? 0 : size;
351     }
352    
353     int open_rfork(const char *path, int flag)
354     {
355     return open_rsrc(path, flag);
356     }
357    
358     void close_rfork(const char *path, int fd)
359     {
360     close(fd);
361     }
362    
363    
364     /*
365     * Read "length" bytes from file to "buffer",
366     * returns number of bytes read (or -1 on error)
367     */
368    
369     ssize_t extfs_read(int fd, void *buffer, size_t length)
370     {
371     return read(fd, buffer, length);
372     }
373    
374    
375     /*
376     * Write "length" bytes from "buffer" to file,
377     * returns number of bytes written (or -1 on error)
378     */
379    
380     ssize_t extfs_write(int fd, void *buffer, size_t length)
381     {
382     return write(fd, buffer, length);
383     }
384    
385    
386     /*
387     * Remove file/directory (and associated helper files),
388     * returns false on error (and sets errno)
389     */
390    
391     bool extfs_remove(const char *path)
392     {
393     // Remove helpers first, don't complain if this fails
394     char helper_path[MAX_PATH_LENGTH];
395     add_rsrc(path, helper_path);
396     remove(helper_path);
397    
398     // Now remove file or directory (and helper directories in the directory)
399     if (remove(path) < 0) {
400     if (errno == EISDIR || errno == ENOTEMPTY) {
401     return rmdir(path) == 0;
402     } else
403     return false;
404     }
405     return true;
406     }
407    
408    
409     /*
410     * Rename/move file/directory (and associated helper files),
411     * returns false on error (and sets errno)
412     */
413    
414     bool extfs_rename(const char *old_path, const char *new_path)
415     {
416     // Rename helpers first, don't complain if this fails
417     char old_helper_path[MAX_PATH_LENGTH], new_helper_path[MAX_PATH_LENGTH];
418     add_rsrc(old_path, old_helper_path);
419     add_rsrc(new_path, new_helper_path);
420     rename(old_helper_path, new_helper_path);
421    
422     // Now rename file
423     return rename(old_path, new_path) == 0;
424     }