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.12 by cebix, 1999-11-08T17:00:04Z vs.
Revision 1.25 by cebix, 2001-03-31T14:31:58Z

# 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-2001 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 40 | Line 40
40   #include <string.h>
41   #include <stdio.h>
42   #include <stdlib.h>
43 #include <unistd.h>
43   #include <fcntl.h>
45 #include <dirent.h>
44   #include <errno.h>
45  
46 + #ifndef WIN32
47 + #include <unistd.h>
48 + #include <dirent.h>
49 + #endif
50 +
51   #include "cpu_emulation.h"
49 #include "macos_util.h"
52   #include "emul_op.h"
53   #include "main.h"
54   #include "disk.h"
# Line 55 | Line 57
57   #include "extfs.h"
58   #include "extfs_defs.h"
59  
60 + #ifdef WIN32
61 + # include "posix_emu.h"
62 + #endif
63 +
64   #define DEBUG 0
65   #include "debug.h"
66  
# Line 65 | Line 71 | enum {
71          fsHFSProcStub = 6,
72          fsDrvStatus = 12,                               // Drive Status record
73          fsFSD = 42,                                             // File system descriptor
74 <        fsPB = 238,                                             // IOParam (for mounting and renaming)
74 >        fsPB = 238,                                             // IOParam (for mounting and renaming), also used for temporary storage
75          fsVMI = 288,                                    // VoumeMountInfoHeader (for mounting)
76          fsParseRec = 296,                               // ParsePathRec struct
77          fsReturn = 306,                                 // Area for return data of 68k routines
# Line 101 | Line 107 | static bool ready = false;
107   static struct stat root_stat;
108  
109   // File system ID/media type
110 < const int16 MY_FSID = 'ba';
111 < const uint32 MY_MEDIA_TYPE = 'basi';
110 > const int16 MY_FSID = EMULATOR_ID_2;
111 > const uint32 MY_MEDIA_TYPE = EMULATOR_ID_4;
112  
113   // CNID of root and root's parent
114   const uint32 ROOT_ID = 2;
# Line 111 | Line 117 | const uint32 ROOT_PARENT_ID = 1;
117   // File system stack size
118   const int STACK_SIZE = 0x10000;
119  
120 + // Allocation block and clump size as reported to MacOS (these are of course
121 + // not the real values and have no meaning on the host OS)
122 + const int AL_BLK_SIZE = 0x4000;
123 + const int CLUMP_SIZE = 0x4000;
124 +
125   // Drive number of our pseudo-drive
126   static int drive_number;
127  
# Line 223 | Line 234 | static void get_path_for_fsitem(FSItem *
234  
235  
236   /*
237 + *  Exchange parent CNIDs in all FSItems
238 + */
239 +
240 + static void swap_parent_ids(uint32 parent1, uint32 parent2)
241 + {
242 +        FSItem *p = first_fs_item;
243 +        while (p) {
244 +                if (p->parent_id == parent1)
245 +                        p->parent_id = parent2;
246 +                else if (p->parent_id == parent2)
247 +                        p->parent_id = parent1;
248 +                p = p->next;
249 +        }
250 + }
251 +
252 +
253 + /*
254   *  String handling functions
255   */
256  
# Line 240 | Line 268 | static void cstr2pstr(char *dst, const c
268          *dst++ = strlen(src);
269          char c;
270          while ((c = *src++) != 0) {
271 +                // Note: we are converting host ':' characters to Mac '/' characters here
272 +                // '/' is not a path separator as this function is only used on object names
273                  if (c == ':')
274                          c = '/';
275                  *dst++ = c;
276          }
277   }
278  
249 // Convert pascal string to C string
250 static void pstr2cstr(char *dst, const char *src)
251 {
252        int size = *src++;
253        while (size--) {
254                char c = *src++;
255                if (c == '/')
256                        c = ':';
257                *dst++ = c;
258        }
259        *dst = 0;
260 }
261
279   // Convert string (no length byte) to C string, length given separately
280   static void strn2cstr(char *dst, const char *src, int size)
281   {
282          while (size--) {
283                  char c = *src++;
284 +                // Note: we are converting Mac '/' characters to host ':' characters here
285 +                // '/' is not a path separator as this function is only used on object names
286                  if (c == '/')
287                          c = ':';
288                  *dst++ = c;
# Line 339 | Line 358 | void ExtFSInit(void)
358          p->parent_id = ROOT_PARENT_ID;
359          p->parent = first_fs_item;
360          strncpy(p->name, GetString(STR_EXTFS_VOLUME_NAME), 32);
361 +        p->name[31] = 0;
362  
363          // Find path for root
364          if ((RootPath = PrefsFindString("extfs")) != NULL) {
# Line 387 | Line 407 | void InstallExtFS(void)
407          // FSM present?
408          r.d[0] = gestaltFSAttr;
409          Execute68kTrap(0xa1ad, &r);     // Gestalt()
410 <        D(bug("FSAttr %ld, %08lx\n", r.d[0], r.a[0]));
410 >        D(bug("FSAttr %d, %08x\n", r.d[0], r.a[0]));
411          if ((r.d[0] & 0xffff) || !(r.a[0] & (1 << gestaltHasFileSystemManager))) {
412                  printf("WARNING: No FSM present, disabling ExtFS\n");
413                  return;
# Line 396 | Line 416 | void InstallExtFS(void)
416          // Yes, version >=1.2?
417          r.d[0] = gestaltFSMVersion;
418          Execute68kTrap(0xa1ad, &r);     // Gestalt()
419 <        D(bug("FSMVersion %ld, %08lx\n", r.d[0], r.a[0]));
419 >        D(bug("FSMVersion %d, %08x\n", r.d[0], r.a[0]));
420          if ((r.d[0] & 0xffff) || (r.a[0] < 0x0120)) {
421                  printf("WARNING: FSM <1.2 found, disabling ExtFS\n");
422                  return;
# Line 666 | Line 686 | int16 ExtFSComm(uint16 message, uint32 p
686                  }
687  
688                  case ffsIDDiskMessage: {                // Check if volume is handled by our FS
689 <                        if (ReadMacInt16(paramBlock + ioVRefNum) == drive_number)
689 >                        if ((int16)ReadMacInt16(paramBlock + ioVRefNum) == drive_number)
690                                  return noErr;
691                          else
692                                  return extFSErr;
# Line 714 | Line 734 | static int16 get_current_dir(uint32 pb,
734          int16 vRefNum = ReadMacInt16(fs_data + fsReturn + 4);
735          uint32 vcb = ReadMacInt32(fs_data + fsReturn + 6);
736          D(bug("  UTDetermineVol() returned %d, status %d\n", r.d[0], status));
737 <        result = r.d[0] & 0xffff;
737 >        result = (int16)(r.d[0] & 0xffff);
738  
739          if (result == noErr) {
740                  switch (status) {
# Line 739 | Line 759 | static int16 get_current_dir(uint32 pb,
759                                          Execute68k(fs_data + fsResolveWDCB, &r);
760                                          uint32 wdcb = ReadMacInt32(fs_data + fsReturn);
761                                          D(bug("  UTResolveWDCB() returned %d, dirID %d\n", r.d[0], ReadMacInt32(wdcb + wdDirID)));
762 <                                        result = r.d[0] & 0xffff;
762 >                                        result = (int16)(r.d[0] & 0xffff);
763                                          if (result == noErr)
764                                                  current_dir = ReadMacInt32(wdcb + wdDirID);
765                                  }
# Line 755 | Line 775 | static int16 get_current_dir(uint32 pb,
775                                          r.a[0] = wdpb;
776                                          Execute68k(fs_data + fsGetDefaultVol, &r);
777                                          D(bug("  UTGetDefaultVol() returned %d, dirID %d\n", r.d[0], ReadMacInt32(wdpb + ioWDDirID)));
778 <                                        result = r.d[0] & 0xffff;
778 >                                        result = (int16)(r.d[0] & 0xffff);
779                                          if (result == noErr)
780                                                  current_dir = ReadMacInt32(wdpb + ioWDDirID);
781                                  }
# Line 781 | Line 801 | static int16 get_path_component_name(uin
801          r.a[0] = rec;
802          Execute68k(fs_data + fsGetPathComponentName, &r);
803   //      D(bug("  UTGetPathComponentName returned %d\n", r.d[0]));
804 <        return r.d[0] & 0xffff;
804 >        return (int16)(r.d[0] & 0xffff);
805   }
806  
807  
# Line 817 | Line 837 | static int16 get_item_and_path(uint32 pb
837          r.a[1] = ReadMacInt32(parseRec + ppNamePtr);
838          Execute68k(fs_data + fsParsePathname, &r);
839          D(bug("  UTParsePathname() returned %d, startOffset %d\n", r.d[0], ReadMacInt16(parseRec + ppStartOffset)));
840 <        result = r.d[0] & 0xffff;
840 >        result = (int16)(r.d[0] & 0xffff);
841          if (result == noErr) {
842  
843                  // Check for leading delimiter of the partial pathname
# Line 930 | Line 950 | static uint32 find_fcb(int16 refNum)
950   static int16 fs_mount_vol(uint32 pb)
951   {
952          D(bug(" fs_mount_vol(%08lx), vRefNum %d\n", pb, ReadMacInt16(pb + ioVRefNum)));
953 <        if (ReadMacInt16(pb + ioVRefNum) == drive_number)
953 >        if ((int16)ReadMacInt16(pb + ioVRefNum) == drive_number)
954                  return noErr;
955          else
956                  return extFSErr;
# Line 951 | Line 971 | static int16 fs_volume_mount(uint32 pb)
971          uint32 vcb = ReadMacInt32(fs_data + fsReturn + 2);
972          D(bug("  UTAllocateVCB() returned %d, vcb %08lx, size %d\n", r.d[0], vcb, sysVCBLength));
973          if (r.d[0] & 0xffff)
974 <                return r.d[0];
974 >                return (int16)r.d[0];
975  
976          // Init VCB
977          WriteMacInt16(vcb + vcbSigWord, 0x4244);
978 < #ifdef __BEOS__
978 > #if defined(__BEOS__) || defined(WIN32)
979          WriteMacInt32(vcb + vcbCrDate, root_stat.st_crtime + TIME_OFFSET);
980   #else
981          WriteMacInt32(vcb + vcbCrDate, 0);
# Line 965 | Line 985 | static int16 fs_volume_mount(uint32 pb)
985          WriteMacInt16(vcb + vcbNmFls, 1);                       //!!
986          WriteMacInt16(vcb + vcbNmRtDirs, 1);            //!!
987          WriteMacInt16(vcb + vcbNmAlBlks, 0xffff);       //!!
988 <        WriteMacInt32(vcb + vcbAlBlkSiz, 1024);
989 <        WriteMacInt32(vcb + vcbClpSiz, 1024);
988 >        WriteMacInt32(vcb + vcbAlBlkSiz, AL_BLK_SIZE);
989 >        WriteMacInt32(vcb + vcbClpSiz, CLUMP_SIZE);
990          WriteMacInt32(vcb + vcbNxtCNID, next_cnid);
991          WriteMacInt16(vcb + vcbFreeBks, 0xffff);        //!!
992          Host2Mac_memcpy(vcb + vcbVN, VOLUME_NAME, 28);
# Line 980 | Line 1000 | static int16 fs_volume_mount(uint32 pb)
1000          r.a[0] = fs_data + fsReturn;
1001          r.a[1] = vcb;
1002          Execute68k(fs_data + fsAddNewVCB, &r);
1003 <        int16 vRefNum = ReadMacInt32(fs_data + fsReturn);
1003 >        int16 vRefNum = (int16)ReadMacInt32(fs_data + fsReturn);
1004          D(bug("  UTAddNewVCB() returned %d, vRefNum %d\n", r.d[0], vRefNum));
1005          if (r.d[0] & 0xffff)
1006 <                return r.d[0];
1006 >                return (int16)r.d[0];
1007  
1008          // Post diskInsertEvent
1009          D(bug("  posting diskInsertEvent\n"));
# Line 1007 | Line 1027 | static int16 fs_unmount_vol(uint32 vcb)
1027          r.a[0] = vcb;
1028          Execute68k(fs_data + fsDisposeVCB, &r);
1029          D(bug("  UTDisposeVCB() returned %d\n", r.d[0]));
1030 <        return r.d[0];
1030 >        return (int16)r.d[0];
1031   }
1032  
1033   // Get information about a volume (HVolumeParam)
# Line 1018 | Line 1038 | static int16 fs_get_vol_info(uint32 pb,
1038          // Fill in struct
1039          if (ReadMacInt32(pb + ioNamePtr))
1040                  pstrcpy((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), VOLUME_NAME);
1041 < #ifdef __BEOS__
1041 > #if defined(__BEOS__) || defined(WIN32)
1042          WriteMacInt32(pb + ioVCrDate, root_stat.st_crtime + TIME_OFFSET);
1043   #else
1044          WriteMacInt32(pb + ioVCrDate, 0);
# Line 1029 | Line 1049 | static int16 fs_get_vol_info(uint32 pb,
1049          WriteMacInt16(pb + ioVBitMap, 0);
1050          WriteMacInt16(pb + ioAllocPtr, 0);
1051          WriteMacInt16(pb + ioVNmAlBlks, 0xffff);        //!!
1052 <        WriteMacInt32(pb + ioVAlBlkSiz, 1024);
1053 <        WriteMacInt32(pb + ioVClpSiz, 1024);
1052 >        WriteMacInt32(pb + ioVAlBlkSiz, AL_BLK_SIZE);
1053 >        WriteMacInt32(pb + ioVClpSiz, CLUMP_SIZE);
1054          WriteMacInt16(pb + ioAlBlSt, 0);
1055          WriteMacInt32(pb + ioVNxtCNID, next_cnid);
1056          WriteMacInt16(pb + ioVFrBlk, 0xffff);           //!!
# Line 1063 | Line 1083 | static int16 fs_get_vol_parms(uint32 pb)
1083   //      D(bug(" fs_get_vol_parms(%08lx)\n", pb));
1084  
1085          // Return parameter block
1066        uint8 vol[SIZEOF_GetVolParmsInfoBuffer];
1067        WriteMacInt16((uint32)vol + vMVersion, 2);
1068        WriteMacInt32((uint32)vol + vMAttrib, kNoMiniFndr | kNoVNEdit | kNoLclSync | kTrshOffLine | kNoSwitchTo | kNoBootBlks | kNoSysDir | kHasExtFSVol);
1069        WriteMacInt32((uint32)vol + vMLocalHand, 0);
1070        WriteMacInt32((uint32)vol + vMServerAdr, 0);
1071        WriteMacInt32((uint32)vol + vMVolumeGrade, 0);
1072        WriteMacInt16((uint32)vol + vMForeignPrivID, 0);
1086          uint32 actual = ReadMacInt32(pb + ioReqCount);
1087 <        if (actual > sizeof(vol))
1088 <                actual = sizeof(vol);
1076 <        Host2Mac_memcpy(ReadMacInt32(pb + ioBuffer), vol, actual);
1087 >        if (actual > SIZEOF_GetVolParmsInfoBuffer)
1088 >                actual = SIZEOF_GetVolParmsInfoBuffer;
1089          WriteMacInt32(pb + ioActCount, actual);
1090 +        uint32 p = ReadMacInt32(pb + ioBuffer);
1091 +        if (actual > vMVersion) WriteMacInt16(p + vMVersion, 2);
1092 +        if (actual > vMAttrib) WriteMacInt32(p + vMAttrib, kNoMiniFndr | kNoVNEdit | kNoLclSync | kTrshOffLine | kNoSwitchTo | kNoBootBlks | kNoSysDir | kHasExtFSVol);
1093 +        if (actual > vMLocalHand) WriteMacInt32(p + vMLocalHand, 0);
1094 +        if (actual > vMServerAdr) WriteMacInt32(p + vMServerAdr, 0);
1095 +        if (actual > vMVolumeGrade) WriteMacInt32(p + vMVolumeGrade, 0);
1096 +        if (actual > vMForeignPrivID) WriteMacInt16(p + vMForeignPrivID, 0);
1097          return noErr;
1098   }
1099  
# Line 1089 | Line 1108 | static int16 fs_get_vol(uint32 pb)
1108          r.a[0] = pb;
1109          Execute68k(fs_data + fsGetDefaultVol, &r);
1110          D(bug("  UTGetDefaultVol() returned %d\n", r.d[0]));
1111 <        return r.d[0];
1111 >        return (int16)r.d[0];
1112   }
1113  
1114   // Set default volume (WDParam)
# Line 1145 | Line 1164 | static int16 fs_set_vol(uint32 pb, bool
1164          r.d[2] = refNum;
1165          Execute68k(fs_data + fsSetDefaultVol, &r);
1166          D(bug("  UTSetDefaultVol() returned %d\n", r.d[0]));
1167 <        return r.d[0];
1167 >        return (int16)r.d[0];
1168   }
1169  
1170   // Query file attributes (HFileParam)
# Line 1211 | Line 1230 | read_next_de:
1230          WriteMacInt8(pb + ioFlAttrib, access(full_path, W_OK) == 0 ? 0 : faLocked);
1231          WriteMacInt32(pb + ioDirID, fs_item->id);
1232  
1233 < #ifdef __BEOS__
1233 > #if defined(__BEOS__) || defined(WIN32)
1234          WriteMacInt32(pb + ioFlCrDat, st.st_crtime + TIME_OFFSET);
1235   #else
1236          WriteMacInt32(pb + ioFlCrDat, 0);
1237   #endif
1238          WriteMacInt32(pb + ioFlMdDat, st.st_mtime + TIME_OFFSET);
1239  
1240 <        Mac_memset(pb + ioFlFndrInfo, 0, SIZEOF_FInfo);
1222 <        uint32 type, creator;   // pb may point to kernel space, but stack is switched
1223 <        get_finder_type(full_path, type, creator);
1224 <        WriteMacInt32(pb + ioFlFndrInfo + fdType, type);
1225 <        WriteMacInt32(pb + ioFlFndrInfo + fdCreator, creator);
1226 <        uint16 fflags;
1227 <        get_finder_flags(full_path, fflags);
1228 <        WriteMacInt16(pb + ioFlFndrInfo + fdFlags, fflags);
1240 >        get_finfo(full_path, pb + ioFlFndrInfo, hfs ? pb + ioFlXFndrInfo : 0, false);
1241  
1242          WriteMacInt16(pb + ioFlStBlk, 0);
1243          WriteMacInt32(pb + ioFlLgLen, st.st_size);
1244 <        WriteMacInt32(pb + ioFlPyLen, (st.st_size + 1023) & ~1023);
1244 >        WriteMacInt32(pb + ioFlPyLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1);
1245          WriteMacInt16(pb + ioFlRStBlk, 0);
1246          uint32 rf_size = get_rfork_size(full_path);
1247          WriteMacInt32(pb + ioFlRLgLen, rf_size);
1248 <        WriteMacInt32(pb + ioFlRPyLen, (rf_size + 1023) & ~1023);
1248 >        WriteMacInt32(pb + ioFlRPyLen, (rf_size | (AL_BLK_SIZE - 1)) + 1);
1249  
1250          if (hfs) {
1251                  WriteMacInt32(pb + ioFlBkDat, 0);
1240                Mac_memset(pb + ioFlXFndrInfo, 0, SIZEOF_FXInfo);
1252                  WriteMacInt32(pb + ioFlParID, fs_item->parent_id);
1253                  WriteMacInt32(pb + ioFlClpSiz, 0);
1254          }
# Line 1262 | Line 1273 | static int16 fs_set_file_info(uint32 pb,
1273          if (S_ISDIR(st.st_mode))
1274                  return fnfErr;
1275  
1276 <        // Set attributes
1277 <        set_finder_type(full_path, ReadMacInt32(pb + ioFlFndrInfo + fdType), ReadMacInt32(pb + ioFlFndrInfo + fdCreator));
1278 <        set_finder_flags(full_path, ReadMacInt16(pb + ioFlFndrInfo + fdFlags));
1276 >        // Set Finder info
1277 >        set_finfo(full_path, pb + ioFlFndrInfo, hfs ? pb + ioFlXFndrInfo : 0, false);
1278 >
1279          //!! times
1280          return noErr;
1281   }
# Line 1341 | Line 1352 | read_next_de:
1352          WriteMacInt8(pb + ioACUser, 0);
1353          WriteMacInt32(pb + ioDirID, fs_item->id);
1354          WriteMacInt32(pb + ioFlParID, fs_item->parent_id);
1355 < #ifdef __BEOS__
1355 > #if defined(__BEOS__) || defined(WIN32)
1356          WriteMacInt32(pb + ioFlCrDat, st.st_crtime + TIME_OFFSET);
1357   #else
1358          WriteMacInt32(pb + ioFlCrDat, 0);
# Line 1354 | Line 1365 | read_next_de:
1365          }
1366          WriteMacInt32(pb + ioFlMdDat, mtime + TIME_OFFSET);
1367          WriteMacInt32(pb + ioFlBkDat, 0);
1368 +
1369 +        get_finfo(full_path, pb + ioFlFndrInfo, pb + ioFlXFndrInfo, S_ISDIR(st.st_mode));
1370 +
1371          if (S_ISDIR(st.st_mode)) {
1358                Mac_memset(pb + ioDrUsrWds, 0, SIZEOF_DInfo);
1359                Mac_memset(pb + ioDrFndrInfo, 0, SIZEOF_DXInfo);
1360                uint16 fflags;  // pb may point to kernel space, but stack is switched
1361                get_finder_flags(full_path, fflags);
1362                WriteMacInt16(pb + ioDrUsrWds + frFlags, fflags);
1372  
1373                  // Determine number of files in directory (cached)
1374                  int count;
# Line 1384 | Line 1393 | read_next_de:
1393                  }
1394                  WriteMacInt16(pb + ioDrNmFls, count);
1395          } else {
1387                Mac_memset(pb + ioFlFndrInfo, 0, SIZEOF_FInfo);
1388                Mac_memset(pb + ioFlXFndrInfo, 0, SIZEOF_FXInfo);
1389                uint32 type, creator;   // pb may point to kernel space, but stack is switched
1390                get_finder_type(full_path, type, creator);
1391                WriteMacInt32(pb + ioFlFndrInfo + fdType, type);
1392                WriteMacInt32(pb + ioFlFndrInfo + fdCreator, creator);
1393                uint16 fflags;
1394                get_finder_flags(full_path, fflags);
1395                WriteMacInt16(pb + ioFlFndrInfo + fdFlags, fflags);
1396                  WriteMacInt16(pb + ioFlStBlk, 0);
1397                  WriteMacInt32(pb + ioFlLgLen, st.st_size);
1398 <                WriteMacInt32(pb + ioFlPyLen, (st.st_size + 1023) & ~1023);
1398 >                WriteMacInt32(pb + ioFlPyLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1);
1399                  WriteMacInt16(pb + ioFlRStBlk, 0);
1400                  uint32 rf_size = get_rfork_size(full_path);
1401                  WriteMacInt32(pb + ioFlRLgLen, rf_size);
1402 <                WriteMacInt32(pb + ioFlRPyLen, (rf_size + 1023) & ~1023);
1402 >                WriteMacInt32(pb + ioFlRPyLen, (rf_size | (AL_BLK_SIZE - 1)) + 1);
1403                  WriteMacInt32(pb + ioFlClpSiz, 0);
1404          }
1405          return noErr;
# Line 1421 | Line 1421 | static int16 fs_set_cat_info(uint32 pb)
1421          if (stat(full_path, &st) < 0)
1422                  return errno2oserr();
1423  
1424 <        // Set attributes
1425 <        if (S_ISDIR(st.st_mode))
1426 <                set_finder_flags(full_path, ReadMacInt16(pb + ioDrUsrWds + frFlags));
1427 <        else {
1428 <                set_finder_type(full_path, ReadMacInt32(pb + ioFlFndrInfo + fdType), ReadMacInt32(pb + ioFlFndrInfo + fdCreator));
1429 <                set_finder_flags(full_path, ReadMacInt16(pb + ioFlFndrInfo + fdFlags));
1430 <        }
1424 >        // Set Finder info
1425 >        set_finfo(full_path, pb + ioFlFndrInfo, pb + ioFlXFndrInfo, S_ISDIR(st.st_mode));
1426 >
1427          //!! times
1428          return noErr;
1429   }
# Line 1474 | Line 1470 | static int16 fs_open(uint32 pb, uint32 d
1470                  if (access(full_path, F_OK))
1471                          return fnfErr;
1472                  fd = open_rfork(full_path, flag);
1473 <                if (fd > 0) {
1474 <                        if (fstat(fd, &st) < 0)
1473 >                if (fd >= 0) {
1474 >                        if (fstat(fd, &st) < 0) {
1475 >                                close(fd);
1476                                  return errno2oserr();
1477 +                        }
1478                  } else {        // Resource fork not supported, silently ignore it ("pseudo" resource fork)
1479                          st.st_size = 0;
1480                          st.st_mode = 0;
# Line 1485 | Line 1483 | static int16 fs_open(uint32 pb, uint32 d
1483                  fd = open(full_path, flag);
1484                  if (fd < 0)
1485                          return errno2oserr();
1486 <                if (fstat(fd, &st) < 0)
1486 >                if (fstat(fd, &st) < 0) {
1487 >                        close(fd);
1488                          return errno2oserr();
1489 +                }
1490          }
1491  
1492          // File open, allocate FCB
# Line 1498 | Line 1498 | static int16 fs_open(uint32 pb, uint32 d
1498          D(bug("  UTAllocateFCB() returned %d, fRefNum %d, fcb %08lx\n", r.d[0], ReadMacInt16(pb + ioRefNum), fcb));
1499          if (r.d[0] & 0xffff) {
1500                  close(fd);
1501 <                return r.d[0];
1501 >                return (int16)r.d[0];
1502          }
1503  
1504          // Initialize FCB, fd is stored in fcbCatPos
1505          WriteMacInt32(fcb + fcbFlNm, fs_item->id);
1506          WriteMacInt8(fcb + fcbFlags, ((flag == O_WRONLY || flag == O_RDWR) ? fcbWriteMask : 0) | (resource_fork ? fcbResourceMask : 0) | (write_ok ? 0 : fcbFileLockedMask));
1507          WriteMacInt32(fcb + fcbEOF, st.st_size);
1508 <        WriteMacInt32(fcb + fcbPLen, (st.st_size + 1023) & ~1023);
1508 >        WriteMacInt32(fcb + fcbPLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1);
1509          WriteMacInt32(fcb + fcbCrPs, 0);
1510          WriteMacInt32(fcb + fcbVPtr, vcb);
1511 <        WriteMacInt32(fcb + fcbClmpSize, 1024);
1512 <        uint32 type, creator;   // fcb may point to kernel space, but stack is switched
1513 <        get_finder_type(full_path, type, creator);
1514 <        WriteMacInt32(fcb + fcbFType, type);
1511 >        WriteMacInt32(fcb + fcbClmpSize, CLUMP_SIZE);
1512 >
1513 >        get_finfo(full_path, fs_data + fsPB, 0, false);
1514 >        WriteMacInt32(fcb + fcbFType, ReadMacInt32(fs_data + fsPB + fdType));
1515 >
1516          WriteMacInt32(fcb + fcbCatPos, fd);
1517          WriteMacInt32(fcb + fcbDirID, fs_item->parent_id);
1518          cstr2pstr((char *)Mac2HostAddr(fcb + fcbCName), fs_item->name);
# Line 1548 | Line 1549 | static int16 fs_close(uint32 pb)
1549          r.d[0] = ReadMacInt16(pb + ioRefNum);
1550          Execute68k(fs_data + fsReleaseFCB, &r);
1551          D(bug("  UTReleaseFCB() returned %d\n", r.d[0]));
1552 <        return r.d[0];
1552 >        return (int16)r.d[0];
1553   }
1554  
1555   // Query information about FCB (FCBPBRec)
# Line 1567 | Line 1568 | static int16 fs_get_fcb_info(uint32 pb,
1568  
1569                  // Find FCB by index
1570                  WriteMacInt16(pb + ioRefNum, 0);
1571 <                for (int i=0; i<ReadMacInt16(pb + ioFCBIndx); i++) {
1571 >                for (int i=0; i<(int)ReadMacInt16(pb + ioFCBIndx); i++) {
1572                          D(bug("  indexing FCBs\n"));
1573                          r.a[0] = vcb;
1574                          r.a[1] = pb + ioRefNum;
# Line 1576 | Line 1577 | static int16 fs_get_fcb_info(uint32 pb,
1577                          fcb = ReadMacInt32(fs_data + fsReturn);
1578                          D(bug("  UTIndexFCB() returned %d, fcb %p\n", r.d[0], fcb));
1579                          if (r.d[0] & 0xffff)
1580 <                                return r.d[0];
1580 >                                return (int16)r.d[0];
1581                  }
1582          }
1583          if (fcb == 0)
# Line 1624 | Line 1625 | static int16 fs_get_eof(uint32 pb)
1625  
1626          // Adjust FCBs
1627          WriteMacInt32(fcb + fcbEOF, st.st_size);
1628 <        WriteMacInt32(fcb + fcbPLen, (st.st_size + 1023) & ~1023);
1628 >        WriteMacInt32(fcb + fcbPLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1);
1629          WriteMacInt32(pb + ioMisc, st.st_size);
1630          D(bug("  adjusting FCBs\n"));
1631          r.d[0] = ReadMacInt16(pb + ioRefNum);
# Line 1659 | Line 1660 | static int16 fs_set_eof(uint32 pb)
1660  
1661          // Adjust FCBs
1662          WriteMacInt32(fcb + fcbEOF, size);
1663 <        WriteMacInt32(fcb + fcbPLen, (size + 1023) & ~1023);
1663 >        WriteMacInt32(fcb + fcbPLen, (size | (AL_BLK_SIZE - 1)) + 1);
1664          D(bug("  adjusting FCBs\n"));
1665          r.d[0] = ReadMacInt16(pb + ioRefNum);
1666          Execute68k(fs_data + fsAdjustEOF, &r);
# Line 1744 | Line 1745 | static int16 fs_read(uint32 pb)
1745   {
1746          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)));
1747  
1748 +        // Check parameters
1749 +        if ((int32)ReadMacInt32(pb + ioReqCount) < 0)
1750 +                return paramErr;
1751 +
1752          // Find FCB and fd for file
1753          uint32 fcb = find_fcb(ReadMacInt16(pb + ioRefNum));
1754          if (fcb == 0)
# Line 1775 | Line 1780 | static int16 fs_read(uint32 pb)
1780          }
1781  
1782          // Read
1783 <        size_t actual = extfs_read(fd, Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), ReadMacInt32(pb + ioReqCount));
1783 >        ssize_t actual = extfs_read(fd, Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), ReadMacInt32(pb + ioReqCount));
1784          int16 read_err = errno2oserr();
1785          D(bug("  actual %d\n", actual));
1786 <        WriteMacInt32(pb + ioActCount, actual);
1786 >        WriteMacInt32(pb + ioActCount, actual >= 0 ? actual : 0);
1787          uint32 pos = lseek(fd, 0, SEEK_CUR);
1788          WriteMacInt32(fcb + fcbCrPs, pos);
1789          WriteMacInt32(pb + ioPosOffset, pos);
1790 <        if (actual != ReadMacInt32(pb + ioReqCount))
1791 <                return read_err ? read_err : eofErr;
1790 >        if (actual != (ssize_t)ReadMacInt32(pb + ioReqCount))
1791 >                return actual < 0 ? read_err : eofErr;
1792          else
1793                  return noErr;
1794   }
# Line 1793 | Line 1798 | static int16 fs_write(uint32 pb)
1798   {
1799          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)));
1800  
1801 +        // Check parameters
1802 +        if ((int32)ReadMacInt32(pb + ioReqCount) < 0)
1803 +                return paramErr;
1804 +
1805          // Find FCB and fd for file
1806          uint32 fcb = find_fcb(ReadMacInt16(pb + ioRefNum));
1807          if (fcb == 0)
# Line 1824 | Line 1833 | static int16 fs_write(uint32 pb)
1833          }
1834  
1835          // Write
1836 <        size_t actual = extfs_write(fd, Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), ReadMacInt32(pb + ioReqCount));
1836 >        ssize_t actual = extfs_write(fd, Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), ReadMacInt32(pb + ioReqCount));
1837          int16 write_err = errno2oserr();
1838          D(bug("  actual %d\n", actual));
1839 <        WriteMacInt32(pb + ioActCount, actual);
1839 >        WriteMacInt32(pb + ioActCount, actual >= 0 ? actual : 0);
1840          uint32 pos = lseek(fd, 0, SEEK_CUR);
1841          WriteMacInt32(fcb + fcbCrPs, pos);
1842          WriteMacInt32(pb + ioPosOffset, pos);
1843 <        if (actual != ReadMacInt32(pb + ioReqCount))
1843 >        if (actual != (ssize_t)ReadMacInt32(pb + ioReqCount))
1844                  return write_err;
1845          else
1846                  return noErr;
# Line 1933 | Line 1942 | static int16 fs_rename(uint32 pb, uint32
1942  
1943          // Rename item
1944          D(bug("  renaming %s -> %s\n", old_path, full_path));
1945 <        if (rename(old_path, full_path) < 0)
1945 >        if (!extfs_rename(old_path, full_path))
1946                  return errno2oserr();
1947          else {
1948                  // The ID of the old file/dir has to stay the same, so we swap the IDs of the FSItems
1949 +                swap_parent_ids(fs_item->id, new_item->id);
1950                  uint32 t = fs_item->id;
1951                  fs_item->id = new_item->id;
1952                  new_item->id = t;
# Line 1976 | Line 1986 | static int16 fs_cat_move(uint32 pb)
1986  
1987          // Move item
1988          D(bug("  moving %s -> %s\n", old_path, full_path));
1989 <        if (rename(old_path, full_path) < 0)
1989 >        if (!extfs_rename(old_path, full_path))
1990                  return errno2oserr();
1991          else {
1992                  // The ID of the old file/dir has to stay the same, so we swap the IDs of the FSItems
1993                  FSItem *new_item = find_fsitem(fs_item->name, new_dir_item);
1994                  if (new_item) {
1995 +                        swap_parent_ids(fs_item->id, new_item->id);
1996                          uint32 t = fs_item->id;
1997                          fs_item->id = new_item->id;
1998                          new_item->id = t;
# Line 2001 | Line 2012 | static int16 fs_open_wd(uint32 pb)
2012          r.a[0] = pb;
2013          Execute68k(fs_data + fsAllocateWDCB, &r);
2014          D(bug("  UTAllocateWDCB returned %d, refNum is %d\n", r.d[0], ReadMacInt16(pb + ioVRefNum)));
2015 <        return r.d[0];
2015 >        return (int16)r.d[0];
2016   }
2017  
2018   // Close working directory (WDParam)
# Line 2015 | Line 2026 | static int16 fs_close_wd(uint32 pb)
2026          r.d[0] = ReadMacInt16(pb + ioVRefNum);
2027          Execute68k(fs_data + fsReleaseWDCB, &r);
2028          D(bug("  UTReleaseWDCB returned %d\n", r.d[0]));
2029 <        return r.d[0];
2029 >        return (int16)r.d[0];
2030   }
2031  
2032   // Query information about working directory (WDParam)
# Line 2044 | Line 2055 | static int16 fs_get_wd_info(uint32 pb, u
2055          uint32 wdcb = ReadMacInt32(fs_data + fsReturn);
2056          D(bug("  UTResolveWDCB() returned %d, dirID %d\n", r.d[0], ReadMacInt32(wdcb + wdDirID)));
2057          if (r.d[0] & 0xffff)
2058 <                return r.d[0];
2058 >                return (int16)r.d[0];
2059  
2060          // Return information
2061          WriteMacInt32(pb + ioWDProcID, ReadMacInt32(wdcb + wdProcID));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines