ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/extfs.cpp
(Generate patch)

Comparing BasiliskII/src/extfs.cpp (file contents):
Revision 1.1 by cebix, 1999-10-19T17:41:14Z vs.
Revision 1.18 by cebix, 2000-07-14T21:29:08Z

# Line 1 | Line 1
1   /*
2 < *  extfs.cpp - MacOS file system for access native file system access
2 > *  extfs.cpp - MacOS file system for native file system access
3   *
4 < *  Basilisk II (C) 1997-1999 Christian Bauer
4 > *  Basilisk II (C) 1997-2000 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
# Line 19 | Line 19
19   */
20  
21   /*
22 < TODO:
23 < LockRng
24 < UnlockRng
25 < (CatSearch)
26 < (MakeFSSpec)
27 < (GetVolMountInfoSize)
28 < (GetVolMountInfo)
29 < (GetForeignPrivs)
30 < (SetForeignPrivs)
31 < */
22 > *  SEE ALSO
23 > *    Guide to the File System Manager (from FSM 1.2 SDK)
24 > *
25 > *  TODO
26 > *    LockRng
27 > *    UnlockRng
28 > *    (CatSearch)
29 > *    (MakeFSSpec)
30 > *    (GetVolMountInfoSize)
31 > *    (GetVolMountInfo)
32 > *    (GetForeignPrivs)
33 > *    (SetForeignPrivs)
34 > */
35 >
36 > #include "sysdeps.h"
37  
38   #include <sys/types.h>
39   #include <sys/stat.h>
40 + #include <string.h>
41   #include <stdio.h>
42   #include <stdlib.h>
43 + #include <fcntl.h>
44 + #include <errno.h>
45 +
46 + #ifndef WIN32
47   #include <unistd.h>
48   #include <dirent.h>
49 < #include <errno.h>
49 > #endif
50  
41 #include "sysdeps.h"
51   #include "cpu_emulation.h"
52   #include "macos_util.h"
53   #include "emul_op.h"
# Line 49 | Line 58 | UnlockRng
58   #include "extfs.h"
59   #include "extfs_defs.h"
60  
61 + #ifdef WIN32
62 + # include "posix_emu.h"
63 + #endif
64 +
65   #define DEBUG 0
66   #include "debug.h"
67  
# Line 59 | Line 72 | enum {
72          fsHFSProcStub = 6,
73          fsDrvStatus = 12,                               // Drive Status record
74          fsFSD = 42,                                             // File system descriptor
75 <        fsPB = 238,                                             // IOParam (for mounting)
75 >        fsPB = 238,                                             // IOParam (for mounting and renaming)
76          fsVMI = 288,                                    // VoumeMountInfoHeader (for mounting)
77          fsParseRec = 296,                               // ParsePathRec struct
78          fsReturn = 306,                                 // Area for return data of 68k routines
79          fsAllocateVCB = 562,                    // UTAllocateVCB(uint16 *sysVCBLength{a0}, uint32 *vcb{a1})
80          fsAddNewVCB = 578,                              // UTAddNewVCB(int drive_number{d0}, int16 *vRefNum{a1}, uint32 vcb{a1})
81          fsDetermineVol = 594,                   // UTDetermineVol(uint32 pb{a0}, int16 *status{a1}, int16 *more_matches{a2}, int16 *vRefNum{a3}, uint32 *vcb{a4})
82 <        fsResolveWDCB = 614,                    // UTResolveWDCB(int16 vRefNum{d0}, uint32 *wdcb{a0})
82 >        fsResolveWDCB = 614,                    // UTResolveWDCB(uint32 procID{d0}, int16 index{d1}, int16 vRefNum{d0}, uint32 *wdcb{a0})
83          fsGetDefaultVol = 632,                  // UTGetDefaultVol(uint32 wdpb{a0})
84          fsGetPathComponentName = 644,   // UTGetPathComponentName(uint32 rec{a0})
85          fsParsePathname = 656,                  // UTParsePathname(uint32 *start{a0}, uint32 name{a1})
# Line 105 | Line 118 | const uint32 ROOT_PARENT_ID = 1;
118   // File system stack size
119   const int STACK_SIZE = 0x10000;
120  
121 + // Allocation block and clump size as reported to MacOS (these are of course
122 + // not the real values and have no meaning on the host OS)
123 + const int ALBLK_SIZE = 0x4000;
124 + const int CLUMP_SIZE = 0x4000;
125 +
126   // Drive number of our pseudo-drive
127   static int drive_number;
128  
# Line 195 | Line 213 | static FSItem *find_fsitem(const char *n
213   *  Get full path (->full_path) for given FSItem
214   */
215  
198 const int MAX_PATH_LENGTH = 1024;
216   static char full_path[MAX_PATH_LENGTH];
217  
218 < static void add_path_component(const char *s)
218 > static void add_path_comp(const char *s)
219   {
220 <        int l = strlen(full_path);
204 <        if (l < MAX_PATH_LENGTH-1 && full_path[l-1] != '/') {
205 <                full_path[l] = '/';
206 <                full_path[l+1] = 0;
207 <        }
208 <        strncat(full_path, s, MAX_PATH_LENGTH-1);
220 >        add_path_component(full_path, s);
221   }
222  
223   static void get_path_for_fsitem(FSItem *p)
224   {
225 <        if (p->id == ROOT_ID) {
225 >        if (p->id == ROOT_PARENT_ID) {
226 >                full_path[0] = 0;
227 >        } else if (p->id == ROOT_ID) {
228                  strncpy(full_path, RootPath, MAX_PATH_LENGTH-1);
229                  full_path[MAX_PATH_LENGTH-1] = 0;
230          } else {
231                  get_path_for_fsitem(p->parent);
232 <                add_path_component(p->name);
232 >                add_path_comp(p->name);
233 >        }
234 > }
235 >
236 >
237 > /*
238 > *  Exchange parent CNIDs in all FSItems
239 > */
240 >
241 > static void swap_parent_ids(uint32 parent1, uint32 parent2)
242 > {
243 >        FSItem *p = first_fs_item;
244 >        while (p) {
245 >                if (p->parent_id == parent1)
246 >                        p->parent_id = parent2;
247 >                else if (p->parent_id == parent2)
248 >                        p->parent_id = parent1;
249 >                p = p->next;
250          }
251   }
252  
# Line 238 | Line 269 | static void cstr2pstr(char *dst, const c
269          *dst++ = strlen(src);
270          char c;
271          while ((c = *src++) != 0) {
272 +                // Note: we are converting host ':' characters to Mac '/' characters here
273 +                // '/' is not a path separator as this function is only used on object names
274                  if (c == ':')
275                          c = '/';
276                  *dst++ = c;
# Line 250 | Line 283 | static void pstr2cstr(char *dst, const c
283          int size = *src++;
284          while (size--) {
285                  char c = *src++;
286 +                // Note: we are converting Mac '/' characters to host ':' characters here
287 +                // '/' is not a path separator as this function is only used on object names
288                  if (c == '/')
289                          c = ':';
290                  *dst++ = c;
# Line 262 | Line 297 | static void strn2cstr(char *dst, const c
297   {
298          while (size--) {
299                  char c = *src++;
300 +                // Note: we are converting Mac '/' characters to host ':' characters here
301 +                // '/' is not a path separator as this function is only used on object names
302                  if (c == '/')
303                          c = ':';
304                  *dst++ = c;
# Line 319 | Line 356 | void ExtFSInit(void)
356          cstr2pstr(FS_NAME, GetString(STR_EXTFS_NAME));
357          cstr2pstr(VOLUME_NAME, GetString(STR_EXTFS_VOLUME_NAME));
358  
359 <        // Create root FSItem
359 >        // Create root's parent FSItem
360          FSItem *p = new FSItem;
361          first_fs_item = last_fs_item = p;
362          p->next = NULL;
363 +        p->id = ROOT_PARENT_ID;
364 +        p->parent_id = 0;
365 +        p->parent = NULL;
366 +        p->name[0] = 0;
367 +
368 +        // Create root FSItem
369 +        p = new FSItem;
370 +        last_fs_item->next = p;
371 +        p->next = NULL;
372 +        last_fs_item = p;
373          p->id = ROOT_ID;
374          p->parent_id = ROOT_PARENT_ID;
375 <        p->parent = NULL;
375 >        p->parent = first_fs_item;
376          strncpy(p->name, GetString(STR_EXTFS_VOLUME_NAME), 32);
377 +        p->name[31] = 0;
378  
379          // Find path for root
380          if ((RootPath = PrefsFindString("extfs")) != NULL) {
# Line 376 | Line 424 | void InstallExtFS(void)
424          r.d[0] = gestaltFSAttr;
425          Execute68kTrap(0xa1ad, &r);     // Gestalt()
426          D(bug("FSAttr %ld, %08lx\n", r.d[0], r.a[0]));
427 <        if ((r.d[0] & 0xffff) || !(r.a[0] & (1 << gestaltHasFileSystemManager)))
427 >        if ((r.d[0] & 0xffff) || !(r.a[0] & (1 << gestaltHasFileSystemManager))) {
428 >                printf("WARNING: No FSM present, disabling ExtFS\n");
429                  return;
430 +        }
431  
432          // Yes, version >=1.2?
433          r.d[0] = gestaltFSMVersion;
434          Execute68kTrap(0xa1ad, &r);     // Gestalt()
435          D(bug("FSMVersion %ld, %08lx\n", r.d[0], r.a[0]));
436 <        if ((r.d[0] & 0xffff) || (r.a[0] < 0x0120))
436 >        if ((r.d[0] & 0xffff) || (r.a[0] < 0x0120)) {
437 >                printf("WARNING: FSM <1.2 found, disabling ExtFS\n");
438                  return;
439 +        }
440  
441          D(bug("FSM present\n"));
442  
# Line 420 | Line 472 | void InstallExtFS(void)
472          WriteMacInt16(p, 0x7006); p+= 2;        // UTAllocateVCB
473          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
474          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
475 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
475 >        WriteMacInt16(p, M68K_RTS); p+= 2;
476          if (p - fs_data != fsAddNewVCB)
477                  goto fsdat_error;
478          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 430 | Line 482 | void InstallExtFS(void)
482          WriteMacInt16(p, 0x7007); p+= 2;        // UTAddNewVCB
483          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
484          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
485 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
485 >        WriteMacInt16(p, M68K_RTS); p+= 2;
486          if (p - fs_data != fsDetermineVol)
487                  goto fsdat_error;
488          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 442 | Line 494 | void InstallExtFS(void)
494          WriteMacInt16(p, 0x701d); p+= 2;        // UTDetermineVol
495          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
496          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
497 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
497 >        WriteMacInt16(p, M68K_RTS); p+= 2;
498          if (p - fs_data != fsResolveWDCB)
499                  goto fsdat_error;
500          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
501 <        WriteMacInt16(p, 0x42a7); p+= 2;        // clr.l -(sp)
502 <        WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
503 <        WriteMacInt16(p, 0x3f00); p+= 2;        // move.w d0,-(sp)
501 >        WriteMacInt16(p, 0x2f00); p+= 2;        // move.l d0,-(sp)
502 >        WriteMacInt16(p, 0x3f01); p+= 2;        // move.w d1,-(sp)
503 >        WriteMacInt16(p, 0x3f02); p+= 2;        // move.w d2,-(sp)
504          WriteMacInt16(p, 0x2f08); p+= 2;        // move.l a0,-(sp)
505          WriteMacInt16(p, 0x700e); p+= 2;        // UTResolveWDCB
506          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
507          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
508 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
508 >        WriteMacInt16(p, M68K_RTS); p+= 2;
509          if (p - fs_data != fsGetDefaultVol)
510                  goto fsdat_error;
511          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 461 | Line 513 | void InstallExtFS(void)
513          WriteMacInt16(p, 0x7012); p+= 2;        // UTGetDefaultVol
514          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
515          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
516 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
516 >        WriteMacInt16(p, M68K_RTS); p+= 2;
517          if (p - fs_data != fsGetPathComponentName)
518                  goto fsdat_error;
519          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 469 | Line 521 | void InstallExtFS(void)
521          WriteMacInt16(p, 0x701c); p+= 2;        // UTGetPathComponentName
522          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
523          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
524 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
524 >        WriteMacInt16(p, M68K_RTS); p+= 2;
525          if (p - fs_data != fsParsePathname)
526                  goto fsdat_error;
527          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 478 | Line 530 | void InstallExtFS(void)
530          WriteMacInt16(p, 0x701b); p+= 2;        // UTParsePathname
531          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
532          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
533 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
533 >        WriteMacInt16(p, M68K_RTS); p+= 2;
534          if (p - fs_data != fsDisposeVCB)
535                  goto fsdat_error;
536          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 486 | Line 538 | void InstallExtFS(void)
538          WriteMacInt16(p, 0x7008); p+= 2;        // UTDisposeVCB
539          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
540          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
541 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
541 >        WriteMacInt16(p, M68K_RTS); p+= 2;
542          if (p - fs_data != fsCheckWDRefNum)
543                  goto fsdat_error;
544          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 494 | Line 546 | void InstallExtFS(void)
546          WriteMacInt16(p, 0x7013); p+= 2;        // UTCheckWDRefNum
547          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
548          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
549 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
549 >        WriteMacInt16(p, M68K_RTS); p+= 2;
550          if (p - fs_data != fsSetDefaultVol)
551                  goto fsdat_error;
552          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 504 | Line 556 | void InstallExtFS(void)
556          WriteMacInt16(p, 0x7011); p+= 2;        // UTSetDefaultVol
557          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
558          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
559 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
559 >        WriteMacInt16(p, M68K_RTS); p+= 2;
560          if (p - fs_data != fsAllocateFCB)
561                  goto fsdat_error;
562          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 513 | Line 565 | void InstallExtFS(void)
565          WriteMacInt16(p, 0x7000); p+= 2;        // UTAllocateFCB
566          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
567          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
568 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
568 >        WriteMacInt16(p, M68K_RTS); p+= 2;
569          if (p - fs_data != fsReleaseFCB)
570                  goto fsdat_error;
571          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 521 | Line 573 | void InstallExtFS(void)
573          WriteMacInt16(p, 0x7001); p+= 2;        // UTReleaseFCB
574          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
575          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
576 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
576 >        WriteMacInt16(p, M68K_RTS); p+= 2;
577          if (p - fs_data != fsIndexFCB)
578                  goto fsdat_error;
579          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 531 | Line 583 | void InstallExtFS(void)
583          WriteMacInt16(p, 0x7004); p+= 2;        // UTIndexFCB
584          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
585          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
586 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
586 >        WriteMacInt16(p, M68K_RTS); p+= 2;
587          if (p - fs_data != fsResolveFCB)
588                  goto fsdat_error;
589          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 540 | Line 592 | void InstallExtFS(void)
592          WriteMacInt16(p, 0x7005); p+= 2;        // UTResolveFCB
593          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
594          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
595 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
595 >        WriteMacInt16(p, M68K_RTS); p+= 2;
596          if (p - fs_data != fsAdjustEOF)
597                  goto fsdat_error;
598          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 548 | Line 600 | void InstallExtFS(void)
600          WriteMacInt16(p, 0x7010); p+= 2;        // UTAdjustEOF
601          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
602          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
603 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
603 >        WriteMacInt16(p, M68K_RTS); p+= 2;
604          if (p - fs_data != fsAllocateWDCB)
605                  goto fsdat_error;
606          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 556 | Line 608 | void InstallExtFS(void)
608          WriteMacInt16(p, 0x700c); p+= 2;        // UTAllocateWDCB
609          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
610          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
611 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
611 >        WriteMacInt16(p, M68K_RTS); p+= 2;
612          if (p - fs_data != fsReleaseWDCB)
613                  goto fsdat_error;
614          WriteMacInt16(p, 0x4267); p+= 2;        // clr.w -(sp)
# Line 564 | Line 616 | void InstallExtFS(void)
616          WriteMacInt16(p, 0x700d); p+= 2;        // UTReleaseWDCB
617          WriteMacInt16(p, 0xa824); p+= 2;        // FSMgr
618          WriteMacInt16(p, 0x301f); p+= 2;        // move.w (sp)+,d0
619 <        WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2;
619 >        WriteMacInt16(p, M68K_RTS); p+= 2;
620          if (p - fs_data != SIZEOF_fsdat)
621                  goto fsdat_error;
622  
# Line 588 | Line 640 | void InstallExtFS(void)
640          WriteMacInt16(fs_data + fsFSD + fsdLength, SIZEOF_FSDRec);
641          WriteMacInt16(fs_data + fsFSD + fsdVersion, fsdVersion1);
642          WriteMacInt16(fs_data + fsFSD + fileSystemFSID, MY_FSID);
643 <        memcpy(Mac2HostAddr(fs_data + fsFSD + fileSystemName), FS_NAME, 32);
643 >        Host2Mac_memcpy(fs_data + fsFSD + fileSystemName, FS_NAME, 32);
644          WriteMacInt32(fs_data + fsFSD + fileSystemCommProc, fs_data + fsCommProcStub);
645          WriteMacInt32(fs_data + fsFSD + fsdHFSCI + compInterfProc, fs_data + fsHFSProcStub);
646          WriteMacInt32(fs_data + fsFSD + fsdHFSCI + stackTop, fs_stack + STACK_SIZE);
# Line 642 | Line 694 | int16 ExtFSComm(uint16 message, uint32 p
694  
695                  case ffsGetIconMessage: {               // Get disk/drive icon
696                          if (ReadMacInt8(paramBlock + iconType) == kLargeIcon && ReadMacInt32(paramBlock + requestSize) >= sizeof(ExtFSIcon)) {
697 <                                memcpy(Mac2HostAddr(ReadMacInt32(paramBlock + iconBufferPtr)), ExtFSIcon, sizeof(ExtFSIcon));
697 >                                Host2Mac_memcpy(ReadMacInt32(paramBlock + iconBufferPtr), ExtFSIcon, sizeof(ExtFSIcon));
698                                  WriteMacInt32(paramBlock + actualSize, sizeof(ExtFSIcon));
699                                  return noErr;
700                          } else
# Line 679 | Line 731 | static int16 get_current_dir(uint32 pb,
731          int16 result;
732  
733          // Determine volume
734 < //      D(bug("  determining volume\n"));
734 >        D(bug("  determining volume, dirID %d\n", dirID));
735          r.a[0] = pb;
736          r.a[1] = fs_data + fsReturn;
737          r.a[2] = fs_data + fsReturn + 2;
# Line 697 | Line 749 | static int16 get_current_dir(uint32 pb,
749          int16 more_matches = ReadMacInt16(fs_data + fsReturn + 2);
750          int16 vRefNum = ReadMacInt16(fs_data + fsReturn + 4);
751          uint32 vcb = ReadMacInt32(fs_data + fsReturn + 6);
752 < //      D(bug("  UTDetermineVol() returned %d, status %d\n", r.d[0], status));
753 <        result = r.d[0] & 0xffff;
752 >        D(bug("  UTDetermineVol() returned %d, status %d\n", r.d[0], status));
753 >        result = (int16)(r.d[0] & 0xffff);
754  
755          if (result == noErr) {
756                  switch (status) {
# Line 716 | Line 768 | static int16 get_current_dir(uint32 pb,
768                                          current_dir = dirID;
769                                  else {
770                                          D(bug("  resolving WDCB\n"));
771 <                                        r.d[0] = ReadMacInt16(pb + ioVRefNum);
771 >                                        r.d[0] = 0;
772 >                                        r.d[1] = 0;
773 >                                        r.d[2] = ReadMacInt16(pb + ioVRefNum);
774                                          r.a[0] = fs_data + fsReturn;
775                                          Execute68k(fs_data + fsResolveWDCB, &r);
776                                          uint32 wdcb = ReadMacInt32(fs_data + fsReturn);
777                                          D(bug("  UTResolveWDCB() returned %d, dirID %d\n", r.d[0], ReadMacInt32(wdcb + wdDirID)));
778 <                                        result = r.d[0] & 0xffff;
778 >                                        result = (int16)(r.d[0] & 0xffff);
779                                          if (result == noErr)
780                                                  current_dir = ReadMacInt32(wdcb + wdDirID);
781                                  }
# Line 737 | Line 791 | static int16 get_current_dir(uint32 pb,
791                                          r.a[0] = wdpb;
792                                          Execute68k(fs_data + fsGetDefaultVol, &r);
793                                          D(bug("  UTGetDefaultVol() returned %d, dirID %d\n", r.d[0], ReadMacInt32(wdpb + ioWDDirID)));
794 <                                        result = r.d[0] & 0xffff;
794 >                                        result = (int16)(r.d[0] & 0xffff);
795                                          if (result == noErr)
796                                                  current_dir = ReadMacInt32(wdpb + ioWDDirID);
797                                  }
# Line 763 | Line 817 | static int16 get_path_component_name(uin
817          r.a[0] = rec;
818          Execute68k(fs_data + fsGetPathComponentName, &r);
819   //      D(bug("  UTGetPathComponentName returned %d\n", r.d[0]));
820 <        return r.d[0] & 0xffff;
820 >        return (int16)(r.d[0] & 0xffff);
821   }
822  
823  
# Line 780 | Line 834 | static int16 get_item_and_path(uint32 pb
834          uint32 current_dir;
835          if ((result = get_current_dir(pb, dirID, current_dir, no_vol_name)) != noErr)
836                  return result;
837 +        D(bug("  current dir %08x\n", current_dir));
838          FSItem *p = find_fsitem_by_id(current_dir);
839          if (p == NULL)
840                  return dirNFErr;
# Line 793 | Line 848 | static int16 get_item_and_path(uint32 pb
848          WriteMacInt8(parseRec + ppFoundDelimiter, false);
849  
850          // Get length of volume name
851 < //      D(bug("  parsing pathname\n"));
851 >        D(bug("  parsing pathname\n"));
852          r.a[0] = parseRec + ppStartOffset;
853          r.a[1] = ReadMacInt32(parseRec + ppNamePtr);
854          Execute68k(fs_data + fsParsePathname, &r);
855 < //      D(bug("  UTParsePathname() returned %d, startOffset %d\n", r.d[0], ReadMacInt16(parseRec + ppStartOffset)));
856 <        result = r.d[0] & 0xffff;
855 >        D(bug("  UTParsePathname() returned %d, startOffset %d\n", r.d[0], ReadMacInt16(parseRec + ppStartOffset)));
856 >        result = (int16)(r.d[0] & 0xffff);
857          if (result == noErr) {
858  
859                  // Check for leading delimiter of the partial pathname
# Line 932 | Line 987 | static int16 fs_volume_mount(uint32 pb)
987          uint32 vcb = ReadMacInt32(fs_data + fsReturn + 2);
988          D(bug("  UTAllocateVCB() returned %d, vcb %08lx, size %d\n", r.d[0], vcb, sysVCBLength));
989          if (r.d[0] & 0xffff)
990 <                return r.d[0];
990 >                return (int16)r.d[0];
991  
992          // Init VCB
993          WriteMacInt16(vcb + vcbSigWord, 0x4244);
994 < #ifdef __BEOS__
994 > #if defined(__BEOS__) || defined(WIN32)
995          WriteMacInt32(vcb + vcbCrDate, root_stat.st_crtime + TIME_OFFSET);
996   #else
997          WriteMacInt32(vcb + vcbCrDate, 0);
# Line 946 | Line 1001 | static int16 fs_volume_mount(uint32 pb)
1001          WriteMacInt16(vcb + vcbNmFls, 1);                       //!!
1002          WriteMacInt16(vcb + vcbNmRtDirs, 1);            //!!
1003          WriteMacInt16(vcb + vcbNmAlBlks, 0xffff);       //!!
1004 <        WriteMacInt32(vcb + vcbAlBlkSiz, 1024);
1005 <        WriteMacInt32(vcb + vcbClpSiz, 1024);
1004 >        WriteMacInt32(vcb + vcbAlBlkSiz, AL_BLK_SIZE);
1005 >        WriteMacInt32(vcb + vcbClpSiz, CLUMPSIZE);
1006          WriteMacInt32(vcb + vcbNxtCNID, next_cnid);
1007          WriteMacInt16(vcb + vcbFreeBks, 0xffff);        //!!
1008 <        memcpy(Mac2HostAddr(vcb + vcbVN), VOLUME_NAME, 28);
1008 >        Host2Mac_memcpy(vcb + vcbVN, VOLUME_NAME, 28);
1009          WriteMacInt16(vcb + vcbFSID, MY_FSID);
1010          WriteMacInt32(vcb + vcbFilCnt, 1);                      //!!
1011          WriteMacInt32(vcb + vcbDirCnt, 1);                      //!!
# Line 961 | Line 1016 | static int16 fs_volume_mount(uint32 pb)
1016          r.a[0] = fs_data + fsReturn;
1017          r.a[1] = vcb;
1018          Execute68k(fs_data + fsAddNewVCB, &r);
1019 <        int16 vRefNum = ReadMacInt32(fs_data + fsReturn);
1019 >        int16 vRefNum = (int16)ReadMacInt32(fs_data + fsReturn);
1020          D(bug("  UTAddNewVCB() returned %d, vRefNum %d\n", r.d[0], vRefNum));
1021          if (r.d[0] & 0xffff)
1022 <                return r.d[0];
1022 >                return (int16)r.d[0];
1023  
1024          // Post diskInsertEvent
1025          D(bug("  posting diskInsertEvent\n"));
# Line 988 | Line 1043 | static int16 fs_unmount_vol(uint32 vcb)
1043          r.a[0] = vcb;
1044          Execute68k(fs_data + fsDisposeVCB, &r);
1045          D(bug("  UTDisposeVCB() returned %d\n", r.d[0]));
1046 <        return r.d[0];
1046 >        return (int16)r.d[0];
1047   }
1048  
1049   // Get information about a volume (HVolumeParam)
# Line 999 | Line 1054 | static int16 fs_get_vol_info(uint32 pb,
1054          // Fill in struct
1055          if (ReadMacInt32(pb + ioNamePtr))
1056                  pstrcpy((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), VOLUME_NAME);
1057 < #ifdef __BEOS__
1057 > #if defined(__BEOS__) || defined(WIN32)
1058          WriteMacInt32(pb + ioVCrDate, root_stat.st_crtime + TIME_OFFSET);
1059   #else
1060          WriteMacInt32(pb + ioVCrDate, 0);
# Line 1010 | Line 1065 | static int16 fs_get_vol_info(uint32 pb,
1065          WriteMacInt16(pb + ioVBitMap, 0);
1066          WriteMacInt16(pb + ioAllocPtr, 0);
1067          WriteMacInt16(pb + ioVNmAlBlks, 0xffff);        //!!
1068 <        WriteMacInt32(pb + ioVAlBlkSiz, 1024);
1069 <        WriteMacInt32(pb + ioVClpSiz, 1024);
1068 >        WriteMacInt32(pb + ioVAlBlkSiz, AL_BLK_SIZE);
1069 >        WriteMacInt32(pb + ioVClpSiz, CLUMP_SIZE);
1070          WriteMacInt16(pb + ioAlBlSt, 0);
1071          WriteMacInt32(pb + ioVNxtCNID, next_cnid);
1072          WriteMacInt16(pb + ioVFrBlk, 0xffff);           //!!
# Line 1024 | Line 1079 | static int16 fs_get_vol_info(uint32 pb,
1079                  WriteMacInt32(pb + ioVWrCnt, 0);
1080                  WriteMacInt32(pb + ioVFilCnt, 1);                       //!!
1081                  WriteMacInt32(pb + ioVDirCnt, 1);                       //!!
1082 <                memset(Mac2HostAddr(pb + ioVFndrInfo), 0, 32);
1082 >                Mac_memset(pb + ioVFndrInfo, 0, 32);
1083          }
1084          return noErr;
1085   }
# Line 1044 | Line 1099 | static int16 fs_get_vol_parms(uint32 pb)
1099   //      D(bug(" fs_get_vol_parms(%08lx)\n", pb));
1100  
1101          // Return parameter block
1047        uint8 vol[SIZEOF_GetVolParmsInfoBuffer];
1048        WriteMacInt16((uint32)vol + vMVersion, 2);
1049        WriteMacInt32((uint32)vol + vMAttrib, kNoMiniFndr | kNoVNEdit | kNoLclSync | kTrshOffLine | kNoSwitchTo | kNoBootBlks | kNoSysDir | kHasExtFSVol);
1050        WriteMacInt32((uint32)vol + vMLocalHand, 0);
1051        WriteMacInt32((uint32)vol + vMServerAdr, 0);
1052        WriteMacInt32((uint32)vol + vMVolumeGrade, 0);
1053        WriteMacInt16((uint32)vol + vMForeignPrivID, 0);
1102          uint32 actual = ReadMacInt32(pb + ioReqCount);
1103 <        if (actual > sizeof(vol))
1104 <                actual = sizeof(vol);
1057 <        memcpy(Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), vol, actual);
1103 >        if (actual > SIZEOF_GetVolParmsInfoBuffer)
1104 >                actual = SIZEOF_GetVolParmsInfoBuffer;
1105          WriteMacInt32(pb + ioActCount, actual);
1106 +        uint32 p = ReadMacInt32(pb + ioBuffer);
1107 +        if (actual > vMVersion) WriteMacInt16(p + vMVersion, 2);
1108 +        if (actual > vMAttrib) WriteMacInt32(p + vMAttrib, kNoMiniFndr | kNoVNEdit | kNoLclSync | kTrshOffLine | kNoSwitchTo | kNoBootBlks | kNoSysDir | kHasExtFSVol);
1109 +        if (actual > vMLocalHand) WriteMacInt32(p + vMLocalHand, 0);
1110 +        if (actual > vMServerAdr) WriteMacInt32(p + vMServerAdr, 0);
1111 +        if (actual > vMVolumeGrade) WriteMacInt32(p + vMVolumeGrade, 0);
1112 +        if (actual > vMForeignPrivID) WriteMacInt16(p + vMForeignPrivID, 0);
1113          return noErr;
1114   }
1115  
# Line 1070 | Line 1124 | static int16 fs_get_vol(uint32 pb)
1124          r.a[0] = pb;
1125          Execute68k(fs_data + fsGetDefaultVol, &r);
1126          D(bug("  UTGetDefaultVol() returned %d\n", r.d[0]));
1127 <        return r.d[0];
1127 >        return (int16)r.d[0];
1128   }
1129  
1130   // Set default volume (WDParam)
1131   static int16 fs_set_vol(uint32 pb, bool hfs, uint32 vcb)
1132   {
1133 <        D(bug(" fs_set_vol(%08lx), vRefNum %d, name %#s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt32(pb + ioWDDirID)));
1133 >        D(bug(" fs_set_vol(%08lx), vRefNum %d, name %.31s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt32(pb + ioWDDirID)));
1134          M68kRegisters r;
1135  
1136          // Determine parameters
# Line 1126 | Line 1180 | static int16 fs_set_vol(uint32 pb, bool
1180          r.d[2] = refNum;
1181          Execute68k(fs_data + fsSetDefaultVol, &r);
1182          D(bug("  UTSetDefaultVol() returned %d\n", r.d[0]));
1183 <        return r.d[0];
1183 >        return (int16)r.d[0];
1184   }
1185  
1186   // Query file attributes (HFileParam)
1187   static int16 fs_get_file_info(uint32 pb, bool hfs, uint32 dirID)
1188   {
1189 <        D(bug(" fs_get_file_info(%08lx), vRefNum %d, name %#s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt16(pb + ioFDirIndex), dirID));
1189 >        D(bug(" fs_get_file_info(%08lx), vRefNum %d, name %.31s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt16(pb + ioFDirIndex), dirID));
1190  
1191          FSItem *fs_item;
1192          int16 dir_index = ReadMacInt16(pb + ioFDirIndex);
1193 <        if (dir_index == 0) {   // Query item specified by ioDirID and ioNamePtr
1193 >        if (dir_index <= 0) {           // Query item specified by ioDirID and ioNamePtr
1194  
1195                  // Find FSItem for given file
1196                  int16 result = get_item_and_path(pb, dirID, fs_item);
# Line 1168 | Line 1222 | read_next_de:
1222                                  return fnfErr;
1223                          }
1224                          if (de->d_name[0] == '.')
1225 <                                goto read_next_de;      // Suppress name beginning with '.' (MacOS could interpret these as driver names)
1225 >                                goto read_next_de;      // Suppress names beginning with '.' (MacOS could interpret these as driver names)
1226                          //!! suppress directories
1227                  }
1228 <                add_path_component(de->d_name);
1228 >                add_path_comp(de->d_name);
1229  
1230                  // Get FSItem for queried item
1231                  fs_item = find_fsitem(de->d_name, p);
# Line 1192 | Line 1246 | read_next_de:
1246          WriteMacInt8(pb + ioFlAttrib, access(full_path, W_OK) == 0 ? 0 : faLocked);
1247          WriteMacInt32(pb + ioDirID, fs_item->id);
1248  
1249 < #ifdef __BEOS__
1249 > #if defined(__BEOS__) || defined(WIN32)
1250          WriteMacInt32(pb + ioFlCrDat, st.st_crtime + TIME_OFFSET);
1251   #else
1252          WriteMacInt32(pb + ioFlCrDat, 0);
1253   #endif
1254          WriteMacInt32(pb + ioFlMdDat, st.st_mtime + TIME_OFFSET);
1255  
1256 <        memset(Mac2HostAddr(pb + ioFlFndrInfo), 0, SIZEOF_FInfo);
1256 >        Mac_memset(pb + ioFlFndrInfo, 0, SIZEOF_FInfo);
1257          uint32 type, creator;   // pb may point to kernel space, but stack is switched
1258          get_finder_type(full_path, type, creator);
1259          WriteMacInt32(pb + ioFlFndrInfo + fdType, type);
# Line 1210 | Line 1264 | read_next_de:
1264  
1265          WriteMacInt16(pb + ioFlStBlk, 0);
1266          WriteMacInt32(pb + ioFlLgLen, st.st_size);
1267 <        WriteMacInt32(pb + ioFlPyLen, (st.st_size + 1023) & ~1023);
1267 >        WriteMacInt32(pb + ioFlPyLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1);
1268          WriteMacInt16(pb + ioFlRStBlk, 0);
1269          uint32 rf_size = get_rfork_size(full_path);
1270          WriteMacInt32(pb + ioFlRLgLen, rf_size);
1271 <        WriteMacInt32(pb + ioFlRPyLen, (rf_size + 1023) & ~1023);
1271 >        WriteMacInt32(pb + ioFlRPyLen, (rf_size | (AL_BLK_SIZE - 1)) + 1);
1272  
1273          if (hfs) {
1274                  WriteMacInt32(pb + ioFlBkDat, 0);
1275 <                memset(Mac2HostAddr(pb + ioFlXFndrInfo), 0, SIZEOF_FXInfo);
1275 >                Mac_memset(pb + ioFlXFndrInfo, 0, SIZEOF_FXInfo);
1276                  WriteMacInt32(pb + ioFlParID, fs_item->parent_id);
1277                  WriteMacInt32(pb + ioFlClpSiz, 0);
1278          }
# Line 1228 | Line 1282 | read_next_de:
1282   // Set file attributes (HFileParam)
1283   static int16 fs_set_file_info(uint32 pb, bool hfs, uint32 dirID)
1284   {
1285 <        D(bug(" fs_set_file_info(%08lx), vRefNum %d, name %#s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt16(pb + ioFDirIndex), dirID));
1285 >        D(bug(" fs_set_file_info(%08lx), vRefNum %d, name %.31s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt16(pb + ioFDirIndex), dirID));
1286  
1287          // Find FSItem for given file/dir
1288          FSItem *fs_item;
# Line 1253 | Line 1307 | static int16 fs_set_file_info(uint32 pb,
1307   // Query file/directory attributes
1308   static int16 fs_get_cat_info(uint32 pb)
1309   {
1310 <        D(bug(" fs_get_cat_info(%08lx), vRefNum %d, name %#s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt16(pb + ioFDirIndex), ReadMacInt32(pb + ioDirID)));
1310 >        D(bug(" fs_get_cat_info(%08lx), vRefNum %d, name %.31s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt16(pb + ioFDirIndex), ReadMacInt32(pb + ioDirID)));
1311  
1312          FSItem *fs_item;
1313          int16 dir_index = ReadMacInt16(pb + ioFDirIndex);
1314 <        if (dir_index == -1) {          // Query directory specified by ioDirID
1314 >        if (dir_index < 0) {                    // Query directory specified by ioDirID
1315  
1316                  // Find FSItem for directory
1317                  fs_item = find_fsitem_by_id(ReadMacInt32(pb + ioDrDirID));
# Line 1297 | Line 1351 | read_next_de:
1351                                  return fnfErr;
1352                          }
1353                          if (de->d_name[0] == '.')
1354 <                                goto read_next_de;      // Suppress name beginning with '.' (MacOS could interpret these as driver names)
1354 >                                goto read_next_de;      // Suppress names beginning with '.' (MacOS could interpret these as driver names)
1355                  }
1356 <                add_path_component(de->d_name);
1356 >                add_path_comp(de->d_name);
1357  
1358                  // Get FSItem for queried item
1359                  fs_item = find_fsitem(de->d_name, p);
# Line 1322 | Line 1376 | read_next_de:
1376          WriteMacInt8(pb + ioACUser, 0);
1377          WriteMacInt32(pb + ioDirID, fs_item->id);
1378          WriteMacInt32(pb + ioFlParID, fs_item->parent_id);
1379 < #ifdef __BEOS__
1379 > #if defined(__BEOS__) || defined(WIN32)
1380          WriteMacInt32(pb + ioFlCrDat, st.st_crtime + TIME_OFFSET);
1381   #else
1382          WriteMacInt32(pb + ioFlCrDat, 0);
# Line 1333 | Line 1387 | read_next_de:
1387                  fs_item->mtime = mtime;
1388                  cached = false;
1389          }
1390 <        WriteMacInt32(pb + ioFlMdDat, mtime);
1390 >        WriteMacInt32(pb + ioFlMdDat, mtime + TIME_OFFSET);
1391          WriteMacInt32(pb + ioFlBkDat, 0);
1392          if (S_ISDIR(st.st_mode)) {
1393 <                memset(Mac2HostAddr(pb + ioDrUsrWds), 0, SIZEOF_DInfo);
1394 <                memset(Mac2HostAddr(pb + ioDrFndrInfo), 0, SIZEOF_DXInfo);
1393 >                Mac_memset(pb + ioDrUsrWds, 0, SIZEOF_DInfo);
1394 >                Mac_memset(pb + ioDrFndrInfo, 0, SIZEOF_DXInfo);
1395                  uint16 fflags;  // pb may point to kernel space, but stack is switched
1396                  get_finder_flags(full_path, fflags);
1397                  WriteMacInt16(pb + ioDrUsrWds + frFlags, fflags);
# Line 1355 | Line 1409 | read_next_de:
1409                                          de = readdir(d);
1410                                          if (de == NULL)
1411                                                  break;
1412 +                                        if (de->d_name[0] == '.')
1413 +                                                continue;       // Suppress names beginning with '.'
1414                                          count++;
1415                                  }
1416                                  closedir(d);
# Line 1363 | Line 1419 | read_next_de:
1419                  }
1420                  WriteMacInt16(pb + ioDrNmFls, count);
1421          } else {
1422 <                memset(Mac2HostAddr(pb + ioFlFndrInfo), 0, SIZEOF_FInfo);
1423 <                memset(Mac2HostAddr(pb + ioFlXFndrInfo), 0, SIZEOF_FXInfo);
1422 >                Mac_memset(pb + ioFlFndrInfo, 0, SIZEOF_FInfo);
1423 >                Mac_memset(pb + ioFlXFndrInfo, 0, SIZEOF_FXInfo);
1424                  uint32 type, creator;   // pb may point to kernel space, but stack is switched
1425                  get_finder_type(full_path, type, creator);
1426                  WriteMacInt32(pb + ioFlFndrInfo + fdType, type);
# Line 1374 | Line 1430 | read_next_de:
1430                  WriteMacInt16(pb + ioFlFndrInfo + fdFlags, fflags);
1431                  WriteMacInt16(pb + ioFlStBlk, 0);
1432                  WriteMacInt32(pb + ioFlLgLen, st.st_size);
1433 <                WriteMacInt32(pb + ioFlPyLen, (st.st_size + 1023) & ~1023);
1433 >                WriteMacInt32(pb + ioFlPyLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1);
1434                  WriteMacInt16(pb + ioFlRStBlk, 0);
1435                  uint32 rf_size = get_rfork_size(full_path);
1436                  WriteMacInt32(pb + ioFlRLgLen, rf_size);
1437 <                WriteMacInt32(pb + ioFlRPyLen, (rf_size + 1023) & ~1023);
1437 >                WriteMacInt32(pb + ioFlRPyLen, (rf_size | (AL_BLK_SIZE - 1)) + 1);
1438                  WriteMacInt32(pb + ioFlClpSiz, 0);
1439          }
1440          return noErr;
# Line 1387 | Line 1443 | read_next_de:
1443   // Set file/directory attributes
1444   static int16 fs_set_cat_info(uint32 pb)
1445   {
1446 <        D(bug(" fs_set_cat_info(%08lx), vRefNum %d, name %#s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt16(pb + ioFDirIndex), ReadMacInt32(pb + ioDirID)));
1446 >        D(bug(" fs_set_cat_info(%08lx), vRefNum %d, name %.31s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt16(pb + ioFDirIndex), ReadMacInt32(pb + ioDirID)));
1447  
1448          // Find FSItem for given file/dir
1449          FSItem *fs_item;
# Line 1401 | Line 1457 | static int16 fs_set_cat_info(uint32 pb)
1457                  return errno2oserr();
1458  
1459          // Set attributes
1460 <        if (S_ISDIR(st.st_mode))
1460 >        if (S_ISDIR(st.st_mode)) {
1461                  set_finder_flags(full_path, ReadMacInt16(pb + ioDrUsrWds + frFlags));
1462 <        else {
1462 >        } else {
1463                  set_finder_type(full_path, ReadMacInt32(pb + ioFlFndrInfo + fdType), ReadMacInt32(pb + ioFlFndrInfo + fdCreator));
1464                  set_finder_flags(full_path, ReadMacInt16(pb + ioFlFndrInfo + fdFlags));
1465          }
# Line 1414 | Line 1470 | static int16 fs_set_cat_info(uint32 pb)
1470   // Open file
1471   static int16 fs_open(uint32 pb, uint32 dirID, uint32 vcb, bool resource_fork)
1472   {
1473 <        D(bug(" fs_open(%08lx), %s, vRefNum %d, name %#s, dirID %d, perm %d\n", pb, resource_fork ? "rsrc" : "data", ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), dirID, ReadMacInt8(pb + ioPermssn)));
1473 >        D(bug(" fs_open(%08lx), %s, vRefNum %d, name %.31s, dirID %d, perm %d\n", pb, resource_fork ? "rsrc" : "data", ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), dirID, ReadMacInt8(pb + ioPermssn)));
1474          M68kRegisters r;
1475  
1476          // Find FSItem for given file
# Line 1453 | Line 1509 | static int16 fs_open(uint32 pb, uint32 d
1509                  if (access(full_path, F_OK))
1510                          return fnfErr;
1511                  fd = open_rfork(full_path, flag);
1512 <                if (fd > 0) {
1513 <                        if (fstat(fd, &st) < 0)
1512 >                if (fd >= 0) {
1513 >                        if (fstat(fd, &st) < 0) {
1514 >                                close(fd);
1515                                  return errno2oserr();
1516 +                        }
1517                  } else {        // Resource fork not supported, silently ignore it ("pseudo" resource fork)
1518                          st.st_size = 0;
1519                          st.st_mode = 0;
# Line 1464 | Line 1522 | static int16 fs_open(uint32 pb, uint32 d
1522                  fd = open(full_path, flag);
1523                  if (fd < 0)
1524                          return errno2oserr();
1525 <                if (fstat(fd, &st) < 0)
1525 >                if (fstat(fd, &st) < 0) {
1526 >                        close(fd);
1527                          return errno2oserr();
1528 +                }
1529          }
1530  
1531          // File open, allocate FCB
# Line 1477 | Line 1537 | static int16 fs_open(uint32 pb, uint32 d
1537          D(bug("  UTAllocateFCB() returned %d, fRefNum %d, fcb %08lx\n", r.d[0], ReadMacInt16(pb + ioRefNum), fcb));
1538          if (r.d[0] & 0xffff) {
1539                  close(fd);
1540 <                return r.d[0];
1540 >                return (int16)r.d[0];
1541          }
1542  
1543          // Initialize FCB, fd is stored in fcbCatPos
1544          WriteMacInt32(fcb + fcbFlNm, fs_item->id);
1545          WriteMacInt8(fcb + fcbFlags, ((flag == O_WRONLY || flag == O_RDWR) ? fcbWriteMask : 0) | (resource_fork ? fcbResourceMask : 0) | (write_ok ? 0 : fcbFileLockedMask));
1546          WriteMacInt32(fcb + fcbEOF, st.st_size);
1547 <        WriteMacInt32(fcb + fcbPLen, (st.st_size + 1023) & ~1023);
1547 >        WriteMacInt32(fcb + fcbPLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1);
1548          WriteMacInt32(fcb + fcbCrPs, 0);
1549          WriteMacInt32(fcb + fcbVPtr, vcb);
1550 <        WriteMacInt32(fcb + fcbClmpSize, 1024);
1551 <        uint32 type, creator;   // fcb may point to kernel space, but stack is switched
1550 >        WriteMacInt32(fcb + fcbClmpSize, CLUMP_SIZE);
1551 >        uint32 type, creator;   // BeOS: fcb may point to kernel space, but stack is switched
1552          get_finder_type(full_path, type, creator);
1553          WriteMacInt32(fcb + fcbFType, type);
1554          WriteMacInt32(fcb + fcbCatPos, fd);
# Line 1527 | Line 1587 | static int16 fs_close(uint32 pb)
1587          r.d[0] = ReadMacInt16(pb + ioRefNum);
1588          Execute68k(fs_data + fsReleaseFCB, &r);
1589          D(bug("  UTReleaseFCB() returned %d\n", r.d[0]));
1590 <        return r.d[0];
1590 >        return (int16)r.d[0];
1591   }
1592  
1593   // Query information about FCB (FCBPBRec)
# Line 1555 | Line 1615 | static int16 fs_get_fcb_info(uint32 pb,
1615                          fcb = ReadMacInt32(fs_data + fsReturn);
1616                          D(bug("  UTIndexFCB() returned %d, fcb %p\n", r.d[0], fcb));
1617                          if (r.d[0] & 0xffff)
1618 <                                return r.d[0];
1618 >                                return (int16)r.d[0];
1619                  }
1620          }
1621          if (fcb == 0)
# Line 1603 | Line 1663 | static int16 fs_get_eof(uint32 pb)
1663  
1664          // Adjust FCBs
1665          WriteMacInt32(fcb + fcbEOF, st.st_size);
1666 <        WriteMacInt32(fcb + fcbPLen, (st.st_size + 1023) & ~1023);
1666 >        WriteMacInt32(fcb + fcbPLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1);
1667          WriteMacInt32(pb + ioMisc, st.st_size);
1668          D(bug("  adjusting FCBs\n"));
1669          r.d[0] = ReadMacInt16(pb + ioRefNum);
# Line 1638 | Line 1698 | static int16 fs_set_eof(uint32 pb)
1698  
1699          // Adjust FCBs
1700          WriteMacInt32(fcb + fcbEOF, size);
1701 <        WriteMacInt32(fcb + fcbPLen, (size + 1023) & ~1023);
1701 >        WriteMacInt32(fcb + fcbPLen, (size | (AL_BLK_SIZE - 1)) + 1);
1702          D(bug("  adjusting FCBs\n"));
1703          r.d[0] = ReadMacInt16(pb + ioRefNum);
1704          Execute68k(fs_data + fsAdjustEOF, &r);
# Line 1701 | Line 1761 | static int16 fs_set_fpos(uint32 pb)
1761                          if (lseek(fd, ReadMacInt32(pb + ioPosOffset), SEEK_SET) < 0)
1762                                  return posErr;
1763                          break;
1764 +                case fsFromLEOF:
1765 +                        if (lseek(fd, (int32)ReadMacInt32(pb + ioPosOffset), SEEK_END) < 0)
1766 +                                return posErr;
1767 +                        break;
1768                  case fsFromMark:
1769 <                        if (lseek(fd, ReadMacInt32(pb + ioPosOffset), SEEK_CUR) < 0)
1769 >                        if (lseek(fd, (int32)ReadMacInt32(pb + ioPosOffset), SEEK_CUR) < 0)
1770                                  return posErr;
1771 +                        break;
1772                  default:
1773                          break;
1774          }
# Line 1718 | Line 1783 | static int16 fs_read(uint32 pb)
1783   {
1784          D(bug(" fs_read(%08lx), refNum %d, buffer %p, count %d, posMode %d, posOffset %d\n", pb, ReadMacInt16(pb + ioRefNum), ReadMacInt32(pb + ioBuffer), ReadMacInt32(pb + ioReqCount), ReadMacInt16(pb + ioPosMode), ReadMacInt32(pb + ioPosOffset)));
1785  
1786 +        // Check parameters
1787 +        if ((int32)ReadMacInt32(pb + ioReqCount) < 0)
1788 +                return paramErr;
1789 +
1790          // Find FCB and fd for file
1791          uint32 fcb = find_fcb(ReadMacInt16(pb + ioRefNum));
1792          if (fcb == 0)
# Line 1749 | Line 1818 | static int16 fs_read(uint32 pb)
1818          }
1819  
1820          // Read
1821 <        size_t actual = extfs_read(fd, Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), ReadMacInt32(pb + ioReqCount));
1821 >        ssize_t actual = extfs_read(fd, Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), ReadMacInt32(pb + ioReqCount));
1822 >        int16 read_err = errno2oserr();
1823          D(bug("  actual %d\n", actual));
1824 <        WriteMacInt32(pb + ioActCount, actual);
1824 >        WriteMacInt32(pb + ioActCount, actual >= 0 ? actual : 0);
1825          uint32 pos = lseek(fd, 0, SEEK_CUR);
1826          WriteMacInt32(fcb + fcbCrPs, pos);
1827          WriteMacInt32(pb + ioPosOffset, pos);
1828          if (actual != ReadMacInt32(pb + ioReqCount))
1829 <                if (errno)
1760 <                        return errno2oserr();
1761 <                else
1762 <                        return eofErr;
1829 >                return actual < 0 ? read_err : eofErr;
1830          else
1831                  return noErr;
1832   }
# Line 1769 | Line 1836 | static int16 fs_write(uint32 pb)
1836   {
1837          D(bug(" fs_write(%08lx), refNum %d, buffer %p, count %d, posMode %d, posOffset %d\n", pb, ReadMacInt16(pb + ioRefNum), ReadMacInt32(pb + ioBuffer), ReadMacInt32(pb + ioReqCount), ReadMacInt16(pb + ioPosMode), ReadMacInt32(pb + ioPosOffset)));
1838  
1839 +        // Check parameters
1840 +        if ((int32)ReadMacInt32(pb + ioReqCount) < 0)
1841 +                return paramErr;
1842 +
1843          // Find FCB and fd for file
1844          uint32 fcb = find_fcb(ReadMacInt16(pb + ioRefNum));
1845          if (fcb == 0)
# Line 1800 | Line 1871 | static int16 fs_write(uint32 pb)
1871          }
1872  
1873          // Write
1874 <        size_t actual = extfs_write(fd, Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), ReadMacInt32(pb + ioReqCount));
1874 >        ssize_t actual = extfs_write(fd, Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), ReadMacInt32(pb + ioReqCount));
1875 >        int16 write_err = errno2oserr();
1876          D(bug("  actual %d\n", actual));
1877 <        WriteMacInt32(pb + ioActCount, actual);
1877 >        WriteMacInt32(pb + ioActCount, actual >= 0 ? actual : 0);
1878          uint32 pos = lseek(fd, 0, SEEK_CUR);
1879          WriteMacInt32(fcb + fcbCrPs, pos);
1880          WriteMacInt32(pb + ioPosOffset, pos);
1881          if (actual != ReadMacInt32(pb + ioReqCount))
1882 <                return errno2oserr();
1882 >                return write_err;
1883          else
1884                  return noErr;
1885   }
# Line 1815 | Line 1887 | static int16 fs_write(uint32 pb)
1887   // Create file
1888   static int16 fs_create(uint32 pb, uint32 dirID)
1889   {
1890 <        D(bug(" fs_create(%08lx), vRefNum %d, name %#s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), dirID));
1890 >        D(bug(" fs_create(%08lx), vRefNum %d, name %.31s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), dirID));
1891  
1892          // Find FSItem for given file
1893          FSItem *fs_item;
# Line 1828 | Line 1900 | static int16 fs_create(uint32 pb, uint32
1900                  return dupFNErr;
1901  
1902          // Create file
1903 <        int fd = creat(full_path, 0664);
1903 >        int fd = creat(full_path, 0666);
1904          if (fd < 0)
1905                  return errno2oserr();
1906          else {
# Line 1840 | Line 1912 | static int16 fs_create(uint32 pb, uint32
1912   // Create directory
1913   static int16 fs_dir_create(uint32 pb)
1914   {
1915 <        D(bug(" fs_dir_create(%08lx), vRefNum %d, name %#s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt32(pb + ioDirID)));
1915 >        D(bug(" fs_dir_create(%08lx), vRefNum %d, name %.31s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt32(pb + ioDirID)));
1916  
1917          // Find FSItem for given directory
1918          FSItem *fs_item;
# Line 1853 | Line 1925 | static int16 fs_dir_create(uint32 pb)
1925                  return dupFNErr;
1926  
1927          // Create directory
1928 <        if (mkdir(full_path, 0775) < 0)
1928 >        if (mkdir(full_path, 0777) < 0)
1929                  return errno2oserr();
1930          else {
1931                  WriteMacInt32(pb + ioDirID, fs_item->id);
# Line 1864 | Line 1936 | static int16 fs_dir_create(uint32 pb)
1936   // Delete file/directory
1937   static int16 fs_delete(uint32 pb, uint32 dirID)
1938   {
1939 <        D(bug(" fs_delete(%08lx), vRefNum %d, name %#s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), dirID));
1939 >        D(bug(" fs_delete(%08lx), vRefNum %d, name %.31s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), dirID));
1940  
1941          // Find FSItem for given file/dir
1942          FSItem *fs_item;
# Line 1873 | Line 1945 | static int16 fs_delete(uint32 pb, uint32
1945                  return result;
1946  
1947          // Delete file
1948 <        if (remove(full_path) < 0) {
1949 <                int16 err = errno2oserr();
1950 <                if (errno == EISDIR) {  // Workaround for BeOS bug
1879 <                        if (rmdir(full_path) < 0)
1880 <                                return errno2oserr();
1881 <                        else
1882 <                                return noErr;
1883 <                } else
1884 <                        return err;
1885 <        } else
1948 >        if (!extfs_remove(full_path))
1949 >                return errno2oserr();
1950 >        else
1951                  return noErr;
1952   }
1953  
1954   // Rename file/directory
1955   static int16 fs_rename(uint32 pb, uint32 dirID)
1956   {
1957 <        D(bug(" fs_rename(%08lx), vRefNum %d, name %#s, dirID %d, new name %#s\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), dirID, Mac2HostAddr(ReadMacInt32(pb + ioMisc))));
1957 >        D(bug(" fs_rename(%08lx), vRefNum %d, name %.31s, dirID %d, new name %.31s\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), dirID, Mac2HostAddr(ReadMacInt32(pb + ioMisc) + 1)));
1958  
1959          // Find path of given file/dir
1960          FSItem *fs_item;
# Line 1902 | Line 1967 | static int16 fs_rename(uint32 pb, uint32
1967          strcpy(old_path, full_path);
1968  
1969          // Find path for new name
1970 <        uint8 new_pb[SIZEOF_IOParam];
1971 <        memcpy(new_pb, Mac2HostAddr(pb), SIZEOF_IOParam);
1907 <        WriteMacInt32((uint32)new_pb + ioNamePtr, ReadMacInt32(pb + ioMisc));
1970 >        Mac2Mac_memcpy(fs_data + fsPB, pb, SIZEOF_IOParam);
1971 >        WriteMacInt32(fs_data + fsPB + ioNamePtr, ReadMacInt32(pb + ioMisc));
1972          FSItem *new_item;
1973 <        result = get_item_and_path((uint32)new_pb, dirID, new_item);
1973 >        result = get_item_and_path(fs_data + fsPB, dirID, new_item);
1974          if (result != noErr)
1975                  return result;
1976  
# Line 1916 | Line 1980 | static int16 fs_rename(uint32 pb, uint32
1980  
1981          // Rename item
1982          D(bug("  renaming %s -> %s\n", old_path, full_path));
1983 <        if (rename(old_path, full_path) < 0)
1983 >        if (!extfs_rename(old_path, full_path))
1984                  return errno2oserr();
1985          else {
1986                  // The ID of the old file/dir has to stay the same, so we swap the IDs of the FSItems
1987 +                swap_parent_ids(fs_item->id, new_item->id);
1988                  uint32 t = fs_item->id;
1989                  fs_item->id = new_item->id;
1990                  new_item->id = t;
# Line 1930 | Line 1995 | static int16 fs_rename(uint32 pb, uint32
1995   // Move file/directory (CMovePBRec)
1996   static int16 fs_cat_move(uint32 pb)
1997   {
1998 <        D(bug(" fs_cat_move(%08lx), vRefNum %d, name %#s, dirID %d, new name %#s, new dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt32(pb + ioDirID), Mac2HostAddr(ReadMacInt32(pb + ioNewName)), ReadMacInt32(pb + ioNewDirID)));
1998 >        D(bug(" fs_cat_move(%08lx), vRefNum %d, name %.31s, dirID %d, new name %.31s, new dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt32(pb + ioDirID), Mac2HostAddr(ReadMacInt32(pb + ioNewName) + 1), ReadMacInt32(pb + ioNewDirID)));
1999  
2000          // Find path of given file/dir
2001          FSItem *fs_item;
# Line 1943 | Line 2008 | static int16 fs_cat_move(uint32 pb)
2008          strcpy(old_path, full_path);
2009  
2010          // Find path for new directory
2011 <        uint8 new_pb[SIZEOF_IOParam];
2012 <        memcpy(new_pb, Mac2HostAddr(pb), SIZEOF_IOParam);
1948 <        WriteMacInt32((uint32)new_pb + ioNamePtr, ReadMacInt32(pb + ioNewName));
2011 >        Mac2Mac_memcpy(fs_data + fsPB, pb, SIZEOF_IOParam);
2012 >        WriteMacInt32(fs_data + fsPB + ioNamePtr, ReadMacInt32(pb + ioNewName));
2013          FSItem *new_dir_item;
2014 <        result = get_item_and_path((uint32)new_pb, ReadMacInt32(pb + ioNewDirID), new_dir_item);
2014 >        result = get_item_and_path(fs_data + fsPB, ReadMacInt32(pb + ioNewDirID), new_dir_item);
2015          if (result != noErr)
2016                  return result;
2017  
2018          // Append old file/dir name
2019 <        add_path_component(fs_item->name);
2019 >        add_path_comp(fs_item->name);
2020  
2021          // Does the new name already exist?
2022          if (access(full_path, F_OK) == 0)
# Line 1960 | Line 2024 | static int16 fs_cat_move(uint32 pb)
2024  
2025          // Move item
2026          D(bug("  moving %s -> %s\n", old_path, full_path));
2027 <        if (rename(old_path, full_path) < 0)
2027 >        if (!extfs_rename(old_path, full_path))
2028                  return errno2oserr();
2029          else {
2030                  // The ID of the old file/dir has to stay the same, so we swap the IDs of the FSItems
2031                  FSItem *new_item = find_fsitem(fs_item->name, new_dir_item);
2032                  if (new_item) {
2033 +                        swap_parent_ids(fs_item->id, new_item->id);
2034                          uint32 t = fs_item->id;
2035                          fs_item->id = new_item->id;
2036                          new_item->id = t;
# Line 1977 | Line 2042 | static int16 fs_cat_move(uint32 pb)
2042   // Open working directory (WDParam)
2043   static int16 fs_open_wd(uint32 pb)
2044   {
2045 <        D(bug(" fs_open_wd(%08lx), vRefNum %d, name %#s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt32(pb + ioWDDirID)));
2045 >        D(bug(" fs_open_wd(%08lx), vRefNum %d, name %.31s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt32(pb + ioWDDirID)));
2046          M68kRegisters r;
2047  
2048          // Allocate WDCB
2049          D(bug("  allocating WDCB\n"));
2050          r.a[0] = pb;
2051          Execute68k(fs_data + fsAllocateWDCB, &r);
2052 <        D(bug("  UTAllocateWDCB returned %d\n", r.d[0]));
2053 <        return r.d[0];
2052 >        D(bug("  UTAllocateWDCB returned %d, refNum is %d\n", r.d[0], ReadMacInt16(pb + ioVRefNum)));
2053 >        return (int16)r.d[0];
2054   }
2055  
2056   // Close working directory (WDParam)
# Line 1999 | Line 2064 | static int16 fs_close_wd(uint32 pb)
2064          r.d[0] = ReadMacInt16(pb + ioVRefNum);
2065          Execute68k(fs_data + fsReleaseWDCB, &r);
2066          D(bug("  UTReleaseWDCB returned %d\n", r.d[0]));
2067 <        return r.d[0];
2067 >        return (int16)r.d[0];
2068   }
2069  
2070   // Query information about working directory (WDParam)
# Line 2013 | Line 2078 | static int16 fs_get_wd_info(uint32 pb, u
2078                  WriteMacInt32(pb + ioWDProcID, 0);
2079                  WriteMacInt16(pb + ioWDVRefNum, ReadMacInt16(vcb + vcbVRefNum));
2080                  if (ReadMacInt32(pb + ioNamePtr))
2081 <                        memcpy(Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), Mac2HostAddr(vcb + vcbVN), 28);
2081 >                        Mac2Mac_memcpy(ReadMacInt32(pb + ioNamePtr), vcb + vcbVN, 28);
2082                  WriteMacInt32(pb + ioWDDirID, ROOT_ID);
2083                  return noErr;
2084          }
# Line 2028 | Line 2093 | static int16 fs_get_wd_info(uint32 pb, u
2093          uint32 wdcb = ReadMacInt32(fs_data + fsReturn);
2094          D(bug("  UTResolveWDCB() returned %d, dirID %d\n", r.d[0], ReadMacInt32(wdcb + wdDirID)));
2095          if (r.d[0] & 0xffff)
2096 <                return r.d[0];
2096 >                return (int16)r.d[0];
2097  
2098          // Return information
2099 <        WriteMacInt16(pb + ioWDProcID, ReadMacInt32(wdcb + wdProcID));
2099 >        WriteMacInt32(pb + ioWDProcID, ReadMacInt32(wdcb + wdProcID));
2100          WriteMacInt16(pb + ioWDVRefNum, ReadMacInt16(ReadMacInt32(wdcb + wdVCBPtr) + vcbVRefNum));
2101          if (ReadMacInt32(pb + ioNamePtr))
2102 <                memcpy(Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), Mac2HostAddr(ReadMacInt32(wdcb + wdVCBPtr) + vcbVN), 28);
2102 >                Mac2Mac_memcpy(ReadMacInt32(pb + ioNamePtr), ReadMacInt32(wdcb + wdVCBPtr) + vcbVN, 28);
2103          WriteMacInt32(pb + ioWDDirID, ReadMacInt32(wdcb + wdDirID));
2104          return noErr;
2105   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines