directory.c

Go to the documentation of this file.
00001 #include "sysincludes.h"
00002 #include "msdos.h"
00003 #include "stream.h"
00004 #include "mtools.h"
00005 #include "file.h"
00006 #include "fs.h"
00007 
00008 /* #define DEBUG */
00009 
00010 /*
00011  * Read a directory entry into caller supplied buffer
00012  */
00013 struct directory *dir_read(direntry_t *entry, int *error)
00014 {
00015         int n;
00016         *error = 0;
00017         if((n=force_read(entry->Dir, (char *) (&entry->dir), 
00018                          (mt_off_t) entry->entry * MDIR_SIZE, 
00019                          MDIR_SIZE)) != MDIR_SIZE) {
00020                 if (n < 0) {
00021                         *error = -1;
00022                 }
00023                 return NULL;
00024         }
00025         return &entry->dir;
00026 }
00027 
00028 /*
00029  * Make a subdirectory grow in length.  Only subdirectories (not root)
00030  * may grow.  Returns a 0 on success, 1 on failure (disk full), or -1
00031  * on error.
00032  */
00033 
00034 int dir_grow(Stream_t *Dir, int size)
00035 {
00036         Stream_t *Stream = GetFs(Dir);
00037         DeclareThis(FsPublic_t);
00038         int ret;
00039         int buflen;
00040         char *buffer;
00041         
00042         if (!getfreeMinClusters(Dir, 1))
00043                 return -1;
00044 
00045         buflen = This->cluster_size * This->sector_size;
00046 
00047         if(! (buffer=malloc(buflen)) ){
00048                 perror("dir_grow: malloc");
00049                 return -1;
00050         }
00051                 
00052         memset((char *) buffer, '\0', buflen);
00053         ret = force_write(Dir, buffer, (mt_off_t) size * MDIR_SIZE, buflen);
00054         free(buffer);
00055         if(ret < buflen)
00056                 return -1;
00057         return 0;
00058 }
00059 
00060 
00061 void low_level_dir_write(direntry_t *entry)
00062 {
00063         force_write(entry->Dir, 
00064                     (char *) (&entry->dir), 
00065                     (mt_off_t) entry->entry * MDIR_SIZE, MDIR_SIZE);
00066 }
00067 
00068 
00069 /*
00070  * Make a directory entry.  Builds a directory entry based on the
00071  * name, attribute, starting cluster number, and size.  Returns a pointer
00072  * to a static directory structure.
00073  */
00074 
00075 struct directory *mk_entry(const char *filename, char attr,
00076                            unsigned int fat, size_t size, time_t date,
00077                            struct directory *ndir)
00078 {
00079         struct tm *now;
00080         time_t date2 = date;
00081         unsigned char hour, min_hi, min_low, sec;
00082         unsigned char year, month_hi, month_low, day;
00083 
00084         now = localtime(&date2);
00085         strncpy(ndir->name, filename, 8);
00086         strncpy(ndir->ext, filename + 8, 3);
00087         ndir->attr = attr;
00088         ndir->ctime_ms = 0;
00089         hour = now->tm_hour << 3;
00090         min_hi = now->tm_min >> 3;
00091         min_low = now->tm_min << 5;
00092         sec = now->tm_sec / 2;
00093         ndir->ctime[1] = ndir->time[1] = hour + min_hi;
00094         ndir->ctime[0] = ndir->time[0] = min_low + sec;
00095         year = (now->tm_year - 80) << 1;
00096         month_hi = (now->tm_mon + 1) >> 3;
00097         month_low = (now->tm_mon + 1) << 5;
00098         day = now->tm_mday;
00099         ndir -> adate[1] = ndir->cdate[1] = ndir->date[1] = year + month_hi;
00100         ndir -> adate[0] = ndir->cdate[0] = ndir->date[0] = month_low + day;
00101 
00102         set_word(ndir->start, fat & 0xffff);
00103         set_word(ndir->startHi, fat >> 16);
00104         set_dword(ndir->size, size);
00105         return ndir;
00106 }

Generated on Fri Apr 14 22:56:55 2006 for minix by  doxygen 1.4.6