home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / disk-man / mtools-3.000 / mtools-3 / mtools-3.0 / directory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-03  |  4.0 KB  |  163 lines

  1. #include "sysincludes.h"
  2. #include "msdos.h"
  3. #include "stream.h"
  4. #include "mtools.h"
  5. #include "file.h"
  6. #include "vfat.h"
  7. #include "fs.h"
  8.  
  9. /* #define DEBUG */
  10.  
  11. /*
  12.  * Read a directory entry into caller supplied buffer
  13.  */
  14. struct directory *dir_read(Stream_t *Dir,
  15.                struct directory *dir, 
  16.                int num,
  17.                struct vfat_state *v)
  18. {
  19.     if(force_read(Dir, (char *) dir, num * MDIR_SIZE, MDIR_SIZE) !=
  20.        MDIR_SIZE)
  21.         return NULL;
  22.  
  23.     if (v && (dir->attr == 0x0f)) {
  24.         struct vfat_subentry *vse;
  25.         unsigned char id, last_flag;
  26.         char *c;
  27.  
  28.         vse = (struct vfat_subentry *) dir;
  29.         id = vse->id & VSE_MASK;
  30.         last_flag = (vse->id & VSE_LAST);
  31.         if (id > MAX_VFAT_SUBENTRIES) {
  32.             fprintf(stderr, "dir_read: invalid VSE ID %d at %d.\n",
  33.                 id, num);
  34.             return dir;
  35.         }
  36.  
  37. /* 950819: This code enforced finding the VSEs in order.  Well, Win95
  38.  * likes to write them in *reverse* order for some bizarre reason!  So
  39.  * we pretty much have to tolerate them coming in any possible order.
  40.  * So skip this check, we'll do without it (What does this do, Alain?).
  41.  *
  42.  * 950820: Totally rearranged code to tolerate any order but to warn if
  43.  * they are not in reverse order like Win95 uses.
  44.  *
  45.  * 950909: Tolerate any order. We recognize new chains by mismatching
  46.  * checksums. In the event that the checksums match, new entries silently
  47.  * overwrite old entries of the same id. This should accept all valid
  48.  * entries, but may fail to reject invalid entries in some rare cases.
  49.  */
  50.  
  51.         /* bad checksum, begin new chain */
  52.         if(v->sum != vse->sum) {
  53.             clear_vfat(v);
  54.             v->sum = vse->sum;
  55.         }
  56.  
  57.         if(v->status & (1 << (id-1)))
  58.             fprintf(stderr,
  59.                 "dir_read: duplicate VSE %d\n", vse->id);
  60.             
  61.         v->status |= 1 << (id-1);
  62.         if(last_flag)
  63.             v->subentries = id;
  64.             
  65. #ifdef DEBUG
  66.         if (id > v->subentries)
  67.             /* simple test to detect entries preceding
  68.              * the "last" entry (really the first) */
  69.             fprintf(stderr,
  70.                 "dir_read: new VSE %d sans LAST flag\n",
  71.                 vse->id);
  72. #endif
  73.  
  74.         c = &(v->name[VSE_NAMELEN * (id-1)]);
  75.         c += unicode_read(vse->text1, c, VSE1SIZE);
  76.         c += unicode_read(vse->text2, c, VSE2SIZE);
  77.         c += unicode_read(vse->text3, c, VSE3SIZE);
  78. #ifdef DEBUG
  79. printf("Read VSE %d at %d, subentries=%d, = (%13s).\n",
  80. id,num,v->subentries,&(v->name[VSE_NAMELEN * (id-1)]));
  81. #endif
  82.         
  83.         if (last_flag)
  84.             *c = '\0';    /* Null terminate long name */
  85.     }
  86.     return dir;
  87. }
  88.  
  89. /*
  90.  * Make a subdirectory grow in length.  Only subdirectories (not root)
  91.  * may grow.  Returns a 0 on success, 1 on failure (disk full), or -1
  92.  * on error.
  93.  */
  94.  
  95. int dir_grow(Stream_t *Dir, Stream_t *Stream, int size)
  96. {
  97.     DeclareThis(FsPublic_t);
  98.     int ret;
  99.     int buflen;
  100.     char *buffer;
  101.     
  102.     
  103.     buflen = This->cluster_size * This->sector_size;
  104.  
  105.     if(! (buffer=malloc(buflen)) ){
  106.         perror("dir_grow: malloc");
  107.         return -1;
  108.     }
  109.         
  110.     memset((char *) buffer, '\0', buflen);
  111.     ret = force_write(Dir, buffer, size * MDIR_SIZE, buflen);
  112.     if(ret < buflen)
  113.         return -1;
  114.     return 0;
  115. }
  116.  
  117.  
  118. void dir_write(Stream_t *Dir, int num, struct directory *dir)
  119. {
  120.     force_write(Dir, (char *) dir, num * MDIR_SIZE, MDIR_SIZE);
  121. }
  122.  
  123.  
  124. /*
  125.  * Make a directory entry.  Builds a directory entry based on the
  126.  * name, attribute, starting cluster number, and size.  Returns a pointer
  127.  * to a static directory structure.
  128.  */
  129.  
  130. struct directory *mk_entry(char *filename, char attr,
  131.                unsigned int fat, long size, long date,
  132.                struct directory *ndir)
  133. {
  134.     int i;
  135.     struct tm *now;
  136.     time_t date2 = date;
  137.     unsigned char hour, min_hi, min_low, sec;
  138.     unsigned char year, month_hi, month_low, day;
  139.  
  140.     now = localtime(&date2);
  141.     strncpy(ndir->name, filename, 8);
  142.     strncpy(ndir->ext, filename + 8, 3);
  143.     ndir->attr = attr;
  144.     for (i = 0; i < 10; i++)
  145.         ndir->reserved[i] = '\0';
  146.     hour = now->tm_hour << 3;
  147.     min_hi = now->tm_min >> 3;
  148.     min_low = now->tm_min << 5;
  149.     sec = now->tm_sec / 2;
  150.     ndir->time[1] = hour + min_hi;
  151.     ndir->time[0] = min_low + sec;
  152.     year = (now->tm_year - 80) << 1;
  153.     month_hi = (now->tm_mon + 1) >> 3;
  154.     month_low = (now->tm_mon + 1) << 5;
  155.     day = now->tm_mday;
  156.     ndir->date[1] = year + month_hi;
  157.     ndir->date[0] = month_low + day;
  158.  
  159.     set_word(ndir->start, fat);
  160.     set_dword(ndir->size, size);
  161.     return ndir;
  162. }
  163.