home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / x / x_files / X-Recover / h / chunks
Text File  |  1996-04-04  |  11KB  |  302 lines

  1. /* chunks.h   -- low level allocation driver for xfiles
  2.  *               which handles the allocation of 'disc'
  3.  *               chunks including extension of the virtual
  4.  *               disc as necessary.
  5.  */
  6.  
  7. #ifndef __chunks_h
  8. #define __chunks_h
  9.  
  10. #include "kernel.h"
  11.  
  12. #define xFiles_TYPE              0xB23       /* for now             */
  13. #define xFiles_SIG               0x4C494658  /* 'XFIL' or something */
  14. #define xFiles_DIRSIG            0x79646E41  /* 'Andy' of course    */
  15. #define xFiles_STRUCTUREVERSION  1
  16. #define xFiles_DIRECTORYVERSION  1
  17. #define xFiles_ALLOCATIONUNIT    1024        /* and why not? */
  18. #define xFiles_WINDOWSIZE        16384       /* size of window buffer */
  19. #define xFiles_NEWCHUNKS         100
  20. #define xFiles_GROWDIRBY         20
  21. #define xFiles_MAXNAME           256
  22. #define xFiles_CHUNKCACHE        10          /* actually 5 seems OK, but... */
  23.  
  24. #ifndef BOOL
  25. #define BOOL int
  26. #ifndef FALSE
  27. #define FALSE (1 == 0)
  28. #endif
  29. #ifndef TRUE
  30. #define TRUE  (!FALSE)
  31. #endif
  32. #endif
  33.  
  34. /* Fundamental disc allocation record. Used ones contain an offset
  35.  * into the image and size. Unused ones are chained together through
  36.  * their offset field (i.e. if the offset field is 1 the next free
  37.  * chunk is number 1). Since chunk zero can never be free a zero in
  38.  * the offset field marks the end of the free chain.
  39.  */
  40. typedef struct
  41. {
  42.    unsigned offset;
  43.    unsigned size;
  44.    unsigned usage;
  45.    unsigned allocSize;
  46.  
  47. } xFiles_chunk;
  48.  
  49. typedef struct
  50. {
  51.    unsigned     cnkNum;
  52.    xFiles_chunk chunk;
  53.    
  54. } xFiles_chunkCacheItem;
  55.  
  56. typedef struct
  57. {
  58.    unsigned sig;
  59.    unsigned hdrSize;                /* size of this structure                      */
  60.    unsigned structureVersion;
  61.    unsigned directoryVersion;
  62.  
  63.    xFiles_chunk   chunkTable;       /* this record is duplicated in the chunkTable */
  64.    unsigned       rootChunk;        /* root directory                              */
  65.  
  66.    unsigned       allocationUnit;   /* allocate space in multiples of this. can be */
  67.                                     /* changed after file creation because it only */
  68.                                     /* influences allocation: the existing image   */
  69.                                     /* is valid regardless of allocationUnit size  */
  70.    unsigned       freeChunk;        /* Head of the free chunk list                 */
  71.  
  72.    unsigned       waste;            /* Amount of wasted space in the image         */                                 
  73.  
  74. } xFiles_header;
  75.  
  76. typedef struct xFiles_listItem
  77. {
  78.    struct xFiles_listItem *prev;
  79.    struct xFiles_listItem *next;
  80.  
  81. } xFiles_listItem;
  82.  
  83. typedef struct
  84. {
  85.    xFiles_listItem *head, *tail;
  86.    
  87. } xFiles_list;
  88.  
  89. /* Flags for an image */
  90.  
  91. enum
  92. {
  93.    xFiles_fNeedTruncate = 0x0001
  94. };
  95.  
  96. /* Memory stuff */
  97.  
  98. typedef struct
  99. {
  100.    xFiles_listItem li;    /* Must be first item */
  101.  
  102.    int fileHandle;        /* OS filehandle associated which this image is open on */
  103.    BOOL readOnly;         /* True if the file is readonly                         */
  104.    BOOL compacting;       /* True during compaction                               */
  105.    int lastCompact;       /* Time of last compaction                              */
  106.  
  107.    xFiles_list openList;  /* List of open files on this image                     */
  108.  
  109.    xFiles_header fileHeader;  /* convenient */
  110.  
  111.    /* Random replacement chunk cache */
  112.  
  113.    xFiles_chunkCacheItem chunkCache[xFiles_CHUNKCACHE];
  114.    int cacheUsed;
  115.  
  116.    unsigned fileSize;     /* cached file size */
  117.  
  118.    unsigned flags;
  119.  
  120. } xFiles_info;
  121.  
  122. typedef struct
  123. {
  124.    unsigned sig;       /* 'Andy' of course! */
  125.    unsigned parent;    /* 0 for root */
  126.    unsigned size, used;
  127.    
  128. } xFiles_dirHeader;
  129.  
  130. /* There's a dirHash for each directory entry. It's not really a hash, just the
  131.  * first four characters of the name (duplicated in the real entry) and an
  132.  * offset to the actual entry.
  133.  */
  134.  
  135. typedef struct
  136. {
  137.    char     nameStart[4];
  138.    unsigned entryPos;
  139.    unsigned node;        /* chunk number where this entry may be found */
  140.    
  141. } xFiles_dirHash;
  142.  
  143. typedef enum
  144. {
  145.    xFiles_meRead   = 0x001,
  146.    xFiles_meWrite  = 0x002,
  147.    xFiles_locked   = 0x008,
  148.    xFiles_youRead  = 0x010,
  149.    xFiles_youWrite = 0x020,
  150.  
  151.    /* Now our private ones */
  152.  
  153.    xFiles_isDir    = 0x100
  154.  
  155. } xFiles_attr;
  156.  
  157. /* Here's the actual directory entry. The name, which can be any length, follows
  158.  * the entry and is NULL terminated. The next entry is at the next word boundary
  159.  */
  160.  
  161. typedef struct
  162. {
  163.    unsigned    load;
  164.    unsigned    exec;
  165.    unsigned    size;        /* can be found from node but this is quicker */
  166.    xFiles_attr attr;
  167.    
  168.    unsigned    nameLen;
  169.  
  170.    /* Name follows */
  171.  
  172. } xFiles_dirEntry;
  173.  
  174. /* Open file information structure */
  175.  
  176. typedef struct xFiles_fileInfo
  177. {
  178.    xFiles_listItem li;    /* Must be first item */
  179.  
  180.    int          fileSwitchHandle;
  181.    xFiles_info *pOwner;
  182.    unsigned     cnkNum;
  183.    unsigned     parentDir;   /* directory chunk */
  184.    int          fileSize;
  185.  
  186. } xFiles_fileInfo;
  187.  
  188. extern void *xFiles_windowBuffer;    /* shared by all and sundry */
  189. extern int   xFiles_windowBufferSize;
  190. extern xFiles_list xFiles_openFiles;
  191. extern void *wsp;
  192.  
  193. unsigned xFiles_chunkAddress(xFiles_info *pInfo, unsigned cnkNum);
  194. unsigned xFiles_roundUp(xFiles_info *pInfo, unsigned pos);
  195. unsigned xFiles_roundDown(xFiles_info *pInfo, unsigned pos);
  196. _kernel_oserror *xFiles_InitCache(unsigned size);
  197. _kernel_oserror *xFiles_FlushFileInfo(xFiles_info *pInfo);
  198. _kernel_oserror *xFiles_buildNewFilesystem(xFiles_info *pInfo);
  199. _kernel_oserror *xFiles_claimBuffer(int notBiggerThan);
  200. _kernel_oserror *xFiles_releaseBuffer(void);
  201. _kernel_oserror *xFiles_getChunkInfo(xFiles_info *pInfo, unsigned cnkNum, xFiles_chunk *pChunk);
  202. _kernel_oserror *xFiles_getLength(xFiles_info *pInfo, unsigned *pLength);
  203. _kernel_oserror *xFiles_makeFreeChunks(xFiles_info *pInfo);
  204. _kernel_oserror *xFiles_moveToEnd(xFiles_info *pInfo, unsigned cnkNum);
  205. _kernel_oserror *xFiles_newChunk(xFiles_info *pInfo, unsigned *pCnkNum);
  206. _kernel_oserror *xFiles_OpenImage(xFiles_info *pInfo);
  207. _kernel_oserror *xFiles_CloseImage(xFiles_info *pInfo);
  208. _kernel_oserror *xFiles_read(xFiles_info *pInfo, void *pBuffer, unsigned pos, unsigned size);
  209. _kernel_oserror *xFiles_setChunkInfo(xFiles_info *pInfo, unsigned cnkNum, const xFiles_chunk *pChunk);
  210. _kernel_oserror *xFiles_setLength(xFiles_info *pInfo, unsigned Length);
  211. _kernel_oserror *xFiles_squashFile(xFiles_info *pInfo, BOOL force);
  212. _kernel_oserror *xFiles_updateHeader(xFiles_info *pInfo);
  213. _kernel_oserror *xFiles_write(xFiles_info *pInfo, void *pBuffer, unsigned pos, unsigned size);
  214. _kernel_oserror *xFiles_erase(xFiles_info *pInfo, unsigned pos, unsigned size);
  215.  
  216. _kernel_oserror *xFiles_freeChunk(xFiles_info *pInfo, unsigned cnkNum);
  217. _kernel_oserror *xFiles_setChunkSize(xFiles_info *pInfo, unsigned cnkNum, unsigned size);
  218. _kernel_oserror *xFiles_moveBlock(xFiles_info *pInfo, unsigned to, unsigned from, int size);
  219. _kernel_oserror *xFiles_midExtend(xFiles_info *pInfo, unsigned cnkNum, int pos, int by);
  220. _kernel_oserror *xFiles_midExtendDirectory(xFiles_info *pInfo, unsigned dirObject, int pos, int by);
  221.  
  222. _kernel_oserror *xFiles_readChunk(xFiles_info *pInfo, void *pBuffer, unsigned cnkNum, unsigned pos, unsigned size);
  223. _kernel_oserror *xFiles_writeAndGrow(xFiles_info *pInfo, void *pBuffer, unsigned cnkNum, unsigned pos, unsigned size);
  224. _kernel_oserror *xFiles_writeChunk(xFiles_info *pInfo, void *pBuffer, unsigned cnkNum, unsigned pos, unsigned size);
  225.  
  226. _kernel_oserror *xFiles_getDirHeader(xFiles_info *pInfo, unsigned dirObject, xFiles_dirHeader *pHdr);
  227. _kernel_oserror *xFiles_setDirHeader(xFiles_info *pInfo, unsigned dirObject, const xFiles_dirHeader *pHdr);
  228. _kernel_oserror *xFiles_cDir(xFiles_info *pInfo, unsigned parent, unsigned *pCnkNum);
  229. _kernel_oserror *xFiles_createDirEntry(xFiles_info *pInfo, unsigned dirObject,
  230.                                        xFiles_dirHash *pDirHash, xFiles_dirEntry *pDirEnt,
  231.                                        const char *pName);
  232. _kernel_oserror *xFiles_makeSpaceInHashTable(xFiles_info *pInfo, unsigned dirObject);
  233. _kernel_oserror *xFiles_relocateHashes(xFiles_info *pInfo, unsigned dirObject, int pos, int by);
  234. _kernel_oserror *xFiles_dirLookup(xFiles_info *pInfo, unsigned dirObject,