home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff236.lzh / DiskHandler / handler.h < prev    next >
C/C++ Source or Header  |  1989-08-09  |  17KB  |  451 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /* |_o_o|\\ Copyright (c) 1987 The Software Distillery.  All Rights Reserved */
  3. /* |. o.| || This program may not be distributed without the permission of   */
  4. /* | .  | || the authors:                                          BBS:      */
  5. /* | o  | ||   John Toebes     Dave Baker     John Mainwaring                */
  6. /* |  . |//                                                                  */
  7. /* ======                                                                    */
  8. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  9.  
  10. #include <exec/types.h>
  11. #include <exec/nodes.h>
  12. #include <exec/lists.h>
  13. #include <exec/ports.h>
  14. #include <exec/libraries.h>
  15. #include <exec/devices.h>
  16. #include <exec/io.h>
  17. #include <exec/memory.h>
  18. #include <exec/interrupts.h>
  19. #include <devices/console.h>
  20. #include <devices/timer.h>
  21. #include <devices/trackdisk.h>
  22. #include <intuition/intuition.h>
  23. #include <libraries/dos.h>
  24. #include <libraries/dosextens.h>
  25. #include <libraries/filehandler.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #ifdef MANX
  29. #define U_ARGS(a) ()    /* No support for prototypes - oh well */
  30. #else
  31. #define U_ARGS(a) a     /* prototype checking to ensure all is well */
  32. #include <proto/exec.h>
  33. #include <proto/dos.h>
  34. #endif
  35.  
  36. /* Unfortunately the 4.1 memcmp doesn't seem to work correctly so just */
  37. /* tell the compiler not to use the builtin function                   */
  38. #ifdef memcmp
  39. #undef memcmp
  40. int memcmp(char *, char *, int);
  41. #endif
  42.  
  43. #ifndef min
  44. #define min(a,b) ((a)<(b)?(a):(b))
  45. #endif
  46.  
  47. #undef GLOBAL
  48. /* my version of BADDR() has no problems with casting */
  49. #undef  BADDR
  50. #define BADDR(x)        ((APTR)((long)x << 2))
  51. #define MKBADDR(x)      ((BPTR)((long)x >> 2))
  52.  
  53. #define ACTION_FIND_WRITE       1004L
  54. #define ACTION_FIND_INPUT       1005L /* please refer to DOS Tech. Ref. */
  55. #define ACTION_FIND_OUTPUT      1006L
  56. #define ACTION_END              1007L
  57. #define ACTION_SEEK             1008L
  58. #ifndef ACTION_MORE_CACHE
  59. #define ACTION_MORE_CACHE       18L
  60. #endif
  61. #ifndef ACTION_FLUSH
  62. #define ACTION_FLUSH            27L
  63. #endif
  64. #define ACTION_SET_FILE_DATE    34L
  65. #define ACTION_SET_RAW_MODE     994L
  66.  
  67. #define DOS_FALSE        0L
  68. #define DOS_TRUE        -1L           /* BCPL "TRUE" */
  69.  
  70. typedef long KEY;
  71. typedef int (*fptr)();
  72.  
  73. /* handler support routines */
  74.  
  75. #define BUFSIZE  488
  76. #define BLOCKSIZE 488
  77.  
  78. /* We define this because the AmigaDos one is so deficient in types         */
  79. struct EFileHandle {
  80.    struct EFileHandle *efh_Next; /* Next efh in system.                  */
  81.    long efh_CurPos;              /* Current position in block               */
  82.    KEY efh_CurKey;               /* Key of current block                    */
  83.    long efh_CurBlock;            /* Sequence number of current block        */
  84.    struct FileLock *efh_Lock;    /* Lock of file to be accessed             */
  85.    long efh_CurExtBlock;         /* Current extension block number          */
  86.    KEY  efh_ExtBlockKey;         /* Key of current extension block          */
  87.    long efh_Protect;             /* Current protection attributes           */
  88.    };
  89.  
  90. typedef struct EFileHandle *EFH;
  91.  
  92. /* We define this because the AmigaDos one is so deficient in types         */
  93. struct XFileHandle {
  94.    struct Message *fh_Link;      /* Not used - for user to link on his list */
  95.    struct MsgPort *fh_Port;      /* Reply port for packet                   */
  96.    struct MsgPort *fh_Type;      /* Process ID of handler process           */
  97.    char *fh_Buf;                 /* Buffer for file I/O                     */
  98.    char *fh_Pos;                 /* Current position in buffer              */
  99.    char *fh_End;                 /* Pointer to end of buffer                */
  100.    fptr fh_Funcs;                /* Function to call to fill buffer         */
  101.    fptr fh_Func2;                /* Function to call to empty buffer        */
  102.    fptr fh_Func3;                /* Function to call when file is closed    */
  103.    EFH fh_Args;  /* For Internal Use - Root of file         */
  104.    LONG fh_Arg2;                 /* For Internal Use - Block of Current pos */
  105.    };
  106.  
  107. #define FileHandle XFileHandle
  108.  
  109. #define BLOCKSPERENTRY 72
  110. #define HASHTABLESIZE 72
  111. #define COMMENTSIZE   23
  112. #define NAMESIZE      16*4
  113. #define VOLNAMESIZE   13*4
  114. #define BITMAPSIZE    26
  115.  
  116. struct DirBlock
  117.    {
  118.    long  db_Type;
  119.    long  db_OwnKey;
  120.    long  db_HighSeq;             /* Highest Sequence number */
  121.    long  db_HashTableSize;
  122.    long  db_FirstData;
  123.    long  db_CheckSum;
  124.    long  db_HashTable[HASHTABLESIZE];
  125.    long  db_Spare1;               /* Start of bitmap pages on root */
  126.    long  db_Spare2;
  127.    long  db_Protect;             /* Protection bits */
  128.    long  db_Size;
  129.    long  db_Comment[COMMENTSIZE];
  130.    long  db_Days;
  131.    long  db_Minutes;
  132.    long  db_Ticks;
  133.    char  db_Name[NAMESIZE];
  134.    long  db_HashChain;
  135.    long  db_Parent;
  136.    long  db_Extension;
  137.    long  db_SecondaryType;
  138.    };
  139.  
  140. struct FileBlock
  141.    {
  142.    long  fb_Type;
  143.    long  fb_OwnKey;
  144.    long  fb_HighSeq;
  145.    long  fb_DataBlockCount;
  146.    long  fb_FirstData;
  147.    long  fb_CheckSum;
  148.    long  fb_DataBlock[BLOCKSPERENTRY];
  149.    long  fb_Spare1;
  150.    long  fb_Spare2;
  151.    long  fb_Protect;
  152.    long  fb_Size;
  153.    long  fb_Comment[COMMENTSIZE];
  154.    long  fb_Days;
  155.    long  fb_Minutes;
  156.    long  fb_Ticks;
  157.    char  fb_Name[NAMESIZE];
  158.    long  fb_HashChain;
  159.    long  fb_Parent;
  160.    long  fb_Extension;
  161.    long  fb_SecondaryType;
  162.    };
  163.  
  164. struct RootBlock
  165.    {
  166.    long  rb_Type;
  167.    long  rb_OwnKey;              /* Always Zero */
  168.    long  rb_HighSeq;             /* Highest Sequence number */
  169.    long  rb_HashTableSize;
  170.    long  rb_fill1;
  171.    long  rb_CheckSum;
  172.    long  rb_HashTable[HASHTABLESIZE];
  173.    long  rb_BitMapFlag;
  174.    long  rb_BitMapPages[BITMAPSIZE];
  175.    long  rb_AltDays;
  176.    long  rb_AltMinutes;
  177.    long  rb_AltTicks;
  178.    char  rb_Name[VOLNAMESIZE];
  179.    long  rb_CreateDays;
  180.    long  rb_CreateMinutes;
  181.    long  rb_CreateTicks;
  182.    long  rb_HashChain;
  183.    long  rb_Parent;
  184.    long  rb_Extension;
  185.    long  rb_SecondaryType;
  186.    };
  187.  
  188. struct DataBlock
  189.    {
  190.    long  db_Type;
  191.    long  db_Header;
  192.    long  db_SeqNum;
  193.    long  db_DataSize;
  194.    long  db_NextData;
  195.    long  db_CheckSum;
  196.    long  db_Data[122];
  197.    };
  198.  
  199. /* Primary types that appear as first longword in a block */
  200. #define T_SHORT       2    /* Primary type - Root, UserDir, FileHeader blocks */
  201. #define T_LONG        4    /* Unused - indicates long files */
  202. #define T_DATA        8    /* Primary type for Data blocks */
  203. #define T_LIST       16    /* Primary type for file list blocks */
  204. #define T_DELETED     1    /* Bit to indicate a deleted block */
  205.  
  206. /* Secondary types that appear as the last longword in a block */
  207. #define ST_ROOT       1    /* Secondary type for root block */
  208. #define ST_USERDIR    2    /* Secondary type for a user directory */
  209. #define ST_FILE      -3    /* Secondary type for a file */
  210. #define ST_BITMAP    99    /* Bitmap type for checksum */
  211.  
  212. #define OLDFILE      8
  213. #define NEWFILE      16
  214.  
  215. #ifdef DEBUG
  216. /* Debugging routines */
  217. void myputbstr U_ARGS((char *,));
  218. void myputlstr U_ARGS((char *,));
  219. void myprintf U_ARGS((char *,));
  220. void xwrite U_ARGS((char *, int));
  221. int sprintf U_ARGS((char *, char *,));
  222. #define BUG(a) myprintf a;
  223. #define BUGBSTR(a,b) myputbstr(a,b);
  224. #define BUGLSTR(a,b,c) myputlstr(a,b,c);
  225. #else
  226. #define BUG(a)
  227. #define BUGBSTR(a,b)
  228. #define BUGLSTR(a,b,c)
  229. #endif
  230.  
  231. extern struct DosLibrary *DOSBase;
  232. #define alloc(a,b) DosAllocMem(a,b)
  233. #define free(p) DosFreeMem(p)
  234.  
  235. typedef struct TimerPacket {
  236.    struct timerequest tm_req;
  237.    struct DosPacket   tm_pkt;
  238. };
  239.  
  240. typedef struct DskChngPacket {
  241.    struct IOExtTD     dc_req;
  242.    struct Interrupt   dc_int;
  243. }; 
  244.  
  245. typedef struct DiskEnvironment {
  246.    ULONG  de_sizeblock;
  247.    ULONG  de_blkspertrk;
  248.    ULONG  de_reservedblks;
  249.    ULONG  de_numblks;
  250.    ULONG  de_lowcyl;
  251.    ULONG  de_uppercyl;
  252.    ULONG  de_numbufs;
  253.    ULONG  de_membuftype;
  254. };
  255.  
  256. typedef struct BlockBuffer{
  257.    char   *bb_blk;     /* ptr to data buffer associated  */ 
  258.    UBYTE  bb_used;     /* used flag                      */
  259.    UBYTE  bb_priority; /* blk priority                   */
  260.    UBYTE  bb_dirty;    /* dirty minded blk               */          
  261.    ULONG  bb_key;      /* key associated with this block */
  262. };
  263.  
  264. typedef struct Bitmap {
  265.    SHORT bm_blocks;                  /* number of disk blocks required      */
  266.    SHORT bm_dirty;                   /* bitmap modification flag            */
  267.    ULONG *bm_bitmap;                 /* pointer to bitmap in memory         */
  268. };
  269.  
  270. typedef struct global
  271.    {
  272.    struct DosPacket     *pkt;        /* the packet we are processing        */
  273.    struct TimerPacket   *timerpkt;   /* pkt used for timer request          */
  274.    struct timerequest   *systimepkt; /* pkt for getting the current time    */
  275.    struct DskChngPacket *dskchngpkt; /* pkt used for diskchange             */
  276.    struct DeviceNode    *node;       /* our device node                     */
  277.    struct DeviceList    *volume;     /* currently mounted volume            */
  278.    struct Process       *self;       /* my process                          */
  279.    struct MsgPort       *port;       /* our message port                    */
  280.    struct DiskEnvironment dskenv;    /* stuff need for the device           */
  281.    struct BlockBuffer   *blkbuf;     /* ptr to allocated blks for buffers   */
  282.    struct Bitmap        bitmap;      /* ptr to bitmap data                  */
  283.    struct IOExtTD       *devreq;     /* device io request pointer           */
  284.    struct MsgPort       *devport;    /* msg port for device io              */
  285.    EFH    AllHandles;                /* Chain of all known file handles     */
  286.    char   *devname;                  /* pointer to device name              */
  287.    long   ErrorCount;                /* Error count for current volume      */
  288.    long   DiskChange;                /* Current disk change number          */
  289.    long   Root;                      /* Root block on current volume        */
  290.    KEY    prevkey;                   /* Previous key in a search            */
  291.    int    run;                       /* flag to continue running            */
  292.    int    reply;                     /* flag to inhibit replying to message */
  293.    long   diskstatus;                /* Status of disk in the drive         */
  294.    long   diskstate;                 /* Status of disk in the drive         */
  295.    long   unitnum;                   /* Unit number of drive                */
  296.    long   changedisk;                /* Flag to indicate a disk change      */
  297.    UBYTE  deviotimeout;              /* device io timeout for r/w           */  
  298.    /* These are the fields of the autorequest structure we want to create   */
  299.    struct IntuiText line1;           /* Top Line of requester               */
  300.    struct IntuiText line2;           /* Second Line of requester            */
  301.    struct IntuiText line3;           /* Bottom Line of requester            */
  302.    struct IntuiText retrytxt;        /* Retry information                   */
  303.    struct IntuiText canceltxt;       /* CANCEL information                  */
  304.    char   buf3[16];                  /* Buffer to hold unit information     */
  305.    }* GLOBAL;
  306.  
  307. /* Routines in buff.c called directly from io.c */
  308. int initbuf  U_ARGS((GLOBAL, int, int));
  309. void termbuf U_ARGS((GLOBAL, EFH));
  310.  
  311. /* Bitmap.c */
  312. int  AllocBitMap   U_ARGS((GLOBAL));
  313. int  FreeBitMap    U_ARGS((GLOBAL));
  314. int  CountBlocks   U_ARGS((GLOBAL));
  315. void DoCheckSum    U_ARGS((GLOBAL,char *));
  316. void WriteBitMap   U_ARGS((GLOBAL));
  317. void SetBlock      U_ARGS((GLOBAL,KEY));
  318. void FreeBlock     U_ARGS((GLOBAL, KEY));
  319. KEY  AllocateBlock U_ARGS((GLOBAL, KEY, int /* type */));
  320.  
  321. /* file.c */
  322. void ActDelete        U_ARGS((GLOBAL, struct DosPacket *));
  323. void ActRename        U_ARGS((GLOBAL, struct DosPacket *));
  324. void ActSetComment    U_ARGS((GLOBAL, struct DosPacket *));
  325. void ActSetProtection U_ARGS((GLOBAL, struct DosPacket *));
  326.  
  327. /* io.c */
  328. void ActFindwrite U_ARGS((GLOBAL, struct DosPacket *));
  329. void ActFindin    U_ARGS((GLOBAL, struct DosPacket *));
  330. void ActFindout   U_ARGS((GLOBAL, struct DosPacket *));
  331. void ActEnd       U_ARGS((GLOBAL, struct DosPacket *));
  332. void ActRead      U_ARGS((GLOBAL, struct DosPacket *));
  333. void ActWrite     U_ARGS((GLOBAL, struct DosPacket *));
  334. void ActSeek      U_ARGS((GLOBAL, struct DosPacket *));
  335. int  unsafe       U_ARGS((GLOBAL, EFH, int));
  336.  
  337. /* dir.c */
  338. void ActCreateDir U_ARGS((GLOBAL, struct DosPacket *));
  339. void ActExamine   U_ARGS((GLOBAL, struct DosPacket *));
  340. void ActExNext    U_ARGS((GLOBAL, struct DosPacket *));
  341. void ActParent    U_ARGS((GLOBAL, struct DosPacket *));
  342.  
  343. /* databuff.c */
  344. char *GetBlock     U_ARGS((GLOBAL, KEY));
  345. char *ModBlock     U_ARGS((GLOBAL, KEY));
  346. int  FindBuffer    U_ARGS((GLOBAL,KEY, long));
  347. int  AllocBlkBuffs U_ARGS((GLOBAL, long));
  348. void FreeBlkBuffs  U_ARGS((GLOBAL));
  349. void FlushOne      U_ARGS((GLOBAL,long));
  350. void FlushBuffers  U_ARGS((GLOBAL,KEY));
  351.  
  352. /* Subs.c */
  353. struct DosPacket   *taskwait     U_ARGS((GLOBAL));
  354. void initdebug    U_ARGS((void));
  355. void retpkt       U_ARGS((GLOBAL, struct DosPacket *));
  356. char *DosAllocMem U_ARGS((GLOBAL, long));
  357. void DosFreeMem   U_ARGS((char *));
  358.  
  359. /* Disk.c */
  360. int  AddDskChngInt U_ARGS((GLOBAL));
  361. void RemDskChngInt U_ARGS((GLOBAL));
  362. int  Motor      U_ARGS((GLOBAL, int));
  363. int  ResetDrive U_ARGS((GLOBAL));
  364. int  ReadPhy    U_ARGS((GLOBAL, char *, KEY));
  365. int  WritePhy   U_ARGS((GLOBAL, char *, KEY));
  366. void demanddisk U_ARGS((GLOBAL));
  367.  
  368. /* mount.c */
  369. void DisMount U_ARGS((GLOBAL));
  370. void Mount    U_ARGS((GLOBAL));
  371.  
  372. /* lock.c */
  373. long GetKey     U_ARGS((struct global *,struct FileLock *));
  374. struct FileLock *CreateLock U_ARGS((GLOBAL, KEY, long));
  375. void freelock   U_ARGS((GLOBAL, struct FileLock *));
  376. void ActLock    U_ARGS((GLOBAL, struct DosPacket *));
  377. void ActDupLock U_ARGS((GLOBAL, struct DosPacket *));
  378. void ActUnLock  U_ARGS((GLOBAL, struct DosPacket *));
  379.  
  380. /* Process.c */
  381. void ActDie     U_ARGS((GLOBAL, struct DosPacket *));
  382. void ActInhibit U_ARGS((GLOBAL, struct DosPacket *));
  383. void ActFlush   U_ARGS((GLOBAL, struct DosPacket *));
  384. void ActTimer   U_ARGS((GLOBAL, struct DosPacket *));
  385.  
  386. /* volume.c */
  387. void ActCurentVol  U_ARGS((GLOBAL, struct DosPacket *));
  388. void ActRenameDisk U_ARGS((GLOBAL, struct DosPacket *));
  389. void ActDiskInfo   U_ARGS((GLOBAL, struct DosPacket *));
  390. void ActInfo       U_ARGS((GLOBAL, struct DosPacket *));
  391. void GetVolInfo    U_ARGS((GLOBAL, struct InfoData *));
  392.  
  393. /* device.c */
  394. int GetDevice  U_ARGS((GLOBAL, struct FileSysStartupMsg *));
  395. int InitDevice U_ARGS((GLOBAL));
  396. int TermDevice U_ARGS((GLOBAL));
  397.  
  398. /* inhibit.c */
  399. int inhibit  U_ARGS((struct MsgPort *, long));
  400. long sendpkt U_ARGS((struct MsgPort *, long, long*, long));
  401.  
  402. /* timer.c */
  403. int  OpenTimer     U_ARGS((GLOBAL));
  404. void PostTimerReq  U_ARGS((GLOBAL));
  405. void GetDateStamp  U_ARGS((GLOBAL, struct DateStamp *));
  406. void MakeDateStamp U_ARGS((struct timeval *, struct DateStamp *));
  407.  
  408. /* dos1_2.c */
  409. int  IsSameName      U_ARGS((char *, char *, int));
  410. KEY  NextKey         U_ARGS((GLOBAL, KEY, struct FileInfoBlock *, KEY));
  411. KEY  ParentKey       U_ARGS((GLOBAL, KEY));
  412. int  hash            U_ARGS((char *, int));
  413. int  parse           U_ARGS((char **, int *, char *));
  414. long GetProtect      U_ARGS((GLOBAL, KEY));
  415. int  SetProtect      U_ARGS((GLOBAL, KEY, long /*Protection*/));
  416. int  GetType         U_ARGS((GLOBAL, KEY));
  417. int  SetCommentStr   U_ARGS((GLOBAL, KEY, char *));
  418. int  GetInfo         U_ARGS((GLOBAL, KEY, struct FileInfoBlock *));
  419. KEY  FindDir         U_ARGS((GLOBAL, KEY, char **, int *));
  420. KEY  FindEntry       U_ARGS((GLOBAL, KEY, char *, int));
  421. KEY  MakeEntry       U_ARGS((GLOBAL, KEY, char *, int, int));
  422. KEY  LocateEntry     U_ARGS((GLOBAL, KEY, char * /* Name */));
  423. KEY  CreateEntry     U_ARGS((GLOBAL, KEY, char * /* Name */, 
  424.                                      int /* Mode */, int /* Type */));
  425. int  FreeDataChain   U_ARGS((GLOBAL, KEY));
  426. KEY  SeekDataChain   U_ARGS((GLOBAL, EFH, long));
  427. KEY  AppendDataChain U_ARGS((GLOBAL, EFH, long));
  428. long GetFileSize     U_ARGS((GLOBAL, EFH));
  429. int  UpdateFile      U_ARGS((GLOBAL, EFH, long, long));
  430. void GETDATA         U_ARGS((char *, long, char *, long));
  431. void PUTDATA         U_ARGS((char *, long, char *, long));
  432. KEY  BlockKey        U_ARGS((GLOBAL, EFH, long, int));
  433. void SetDateInfo     U_ARGS((GLOBAL, struct DirBlock *));
  434. KEY  GetParent       U_ARGS((GLOBAL, KEY));
  435. int  GetExtInfo      U_ARGS((GLOBAL, KEY, struct ExtInfoBlock *));
  436. int  SetExtInfo      U_ARGS((GLOBAL, KEY, struct ExtInfoBlock *));
  437. int  FreeDirEntry    U_ARGS((GLOBAL, KEY));
  438. int  RenameDisk      U_ARGS((GLOBAL, char *));
  439. int  DeleteEntry     U_ARGS((GLOBAL, KEY, char *));
  440. int  RenameEntry     U_ARGS((GLOBAL, KEY, KEY, char *, char *));
  441. KEY  LinkDir         U_ARGS((GLOBAL, KEY, KEY, int));
  442. /* Directory/File level maintainence */
  443. KEY  MakeEntry       U_ARGS((GLOBAL,KEY, char *, int, int));
  444. KEY  NextDataBlock   U_ARGS((GLOBAL, KEY));
  445.  
  446. /* Requester routine */
  447. int  request         U_ARGS((GLOBAL, int, char *));
  448. #define REQ_MUST    0
  449. #define REQ_ERROR   1
  450. #define REQ_GENERAL 2
  451.