fatfs.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2004 Martin Fuchs
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 
00020  //
00021  // Explorer clone
00022  //
00023  // fatfs.h
00024  //
00025  // Martin Fuchs, 01.02.2004
00026  //
00027 
00028 
00029  /// FAT file system file-entry
00030 struct FATEntry : public Entry
00031 {
00032     FATEntry(Entry* parent, unsigned cluster) : Entry(parent, ET_FAT), _cluster(cluster) {}
00033 
00034 protected:
00035     FATEntry() : Entry(ET_FAT) {}
00036 
00037     virtual bool get_path(PTSTR path, size_t path_count) const;
00038     virtual ShellPath create_absolute_pidl() const;
00039 
00040     DWORD   _cluster;
00041 };
00042 
00043 
00044 struct FATDrive;
00045 
00046  /// FAT file system directory-entry
00047 struct FATDirectory : public FATEntry, public Directory
00048 {
00049     FATDirectory(FATDrive& drive, LPCTSTR root_path);
00050     FATDirectory(FATDrive& drive, Entry* parent, LPCTSTR path, unsigned cluster);
00051     ~FATDirectory();
00052 
00053     virtual void read_directory(int scan_flags=0);
00054     virtual const void* get_next_path_component(const void*) const;
00055     virtual Entry* find_entry(const void*);
00056 
00057 protected:
00058     FATDrive&   _drive;
00059 
00060     struct dirsecz* _secarr;
00061     int     _cur_bufs;
00062     int     _ents;
00063     struct dirent* _dir;
00064     struct Kette* _alloc;
00065 
00066     bool    read_dir();
00067 };
00068 
00069 
00070 #pragma pack(push, 1)
00071 
00072 struct BootSector {
00073     BYTE    jmp[3];
00074     char    OEM[8];
00075     WORD    BytesPerSector;     // dpb.bsec
00076     BYTE    SectorsPerCluster;  // dpb.sclus + 1
00077     WORD    ReservedSectors;    // dpb.ffatsec
00078     BYTE    NumberFATs;
00079     WORD    RootEntries;        // dpb.ndir
00080     WORD    Sectors16;
00081     BYTE    MediaDescr;
00082     WORD    SectorsPerFAT;
00083     WORD    SectorsPerTrack;
00084     WORD    Heads;
00085     DWORD   HiddenSectors;
00086     DWORD   Sectors32;
00087     BYTE    DriveUnit;
00088     WORD    ExtBootFlag;
00089     DWORD   SerialNr;
00090     char    Label[11];
00091     char    FileSystem[8];
00092     BYTE    BootCode[448];
00093     BYTE    BootSignature[2];
00094 };
00095 
00096 struct BootSector32 {
00097     BYTE    jmp[3];
00098     char    OEM[8];
00099     WORD    BytesPerSector;
00100     BYTE    SectorsPerCluster;
00101     WORD    ReservedSectors;
00102     BYTE    NumberFATs;
00103     WORD    reserved1;  // immer 0 für FAT32
00104     WORD    Sectors16;
00105     BYTE    MediaDescr;
00106     WORD    reserved2;  // immer 0 für FAT32
00107     WORD    SectorsPerTrack;
00108     WORD    Heads;
00109     DWORD   HiddenSectors;
00110     DWORD   Sectors32;
00111     DWORD   SectorsPerFAT32;
00112     DWORD   unknown1;
00113     DWORD   RootSectors; // correct?
00114     char    unknown2[6];
00115     char    FileSystem[8];
00116     BYTE    BootCode[448];
00117     BYTE    BootSignature[2];
00118 };
00119 
00120 
00121 struct ftime {
00122     WORD    sec2    : 5;
00123     WORD    min     : 6;
00124     WORD    hour    : 5;
00125 };
00126 
00127 struct fdate {
00128     WORD    day     : 5;
00129     WORD    month   : 4;
00130     WORD    year    : 7;
00131 };
00132 
00133 typedef struct {
00134     unsigned readonly   : 1;
00135     unsigned hidden     : 1;
00136     unsigned system     : 1;
00137     unsigned volume     : 1;
00138     unsigned directory  : 1;
00139     unsigned archived   : 1;
00140     unsigned deleted    : 1;
00141 } fattr;
00142 
00143 typedef union {
00144     char    b;
00145     fattr  a;
00146 } FAT_attribute;
00147 
00148 struct DEntry_E {
00149     char            name[8];
00150     char            ext[3];
00151     char            attr;
00152     char            rsrvd[10];
00153     struct ftime    time;
00154     struct fdate    date;
00155     WORD            fclus;
00156     DWORD           size;
00157 };
00158 
00159 union DEntry {
00160     DEntry_E E;
00161     BYTE B[8+3+1+10+sizeof(struct ftime)+sizeof(struct fdate)+sizeof(WORD)+sizeof(DWORD)];
00162 };
00163 
00164 #pragma pack(pop)
00165 
00166 
00167 #define BufLen  512
00168 
00169 struct Buffer {
00170  BYTE   dat[BufLen];
00171 };
00172 
00173 struct Cache {
00174  BYTE   dat[BufLen];
00175 };
00176 
00177 struct dskrwblk {
00178  DWORD          sec;
00179  WORD           anz;
00180  struct buffer  far *buf;
00181 };
00182 
00183 #define RONLY           0x01
00184 #define HIDDEN          0x02
00185 #define SYSTEM          0x04
00186 #define VOLUME          0x08
00187 #define DIRENT          0x10
00188 #define ARCHIVE         0x20
00189 
00190 #define _A_DELETED      0x40
00191 #define _A_ILLEGAL      0x80
00192 #define IS_LNAME(a) ((a&0xFF)==0x0F)    // "& 0xFF" correct?
00193 
00194 #define FAT_DEL_CHAR    (char)0xe5
00195 
00196 #define AddP(p,s)  {(int&)p += s;}
00197 
00198 struct dirent {
00199     union DEntry  ent[1];
00200 };
00201 
00202 struct dirsecz {
00203     DWORD  s[32];  // 32 only as placeholder
00204 };
00205 
00206 struct Kette {
00207  struct Kette*  Vorw;
00208  struct Kette*  Rueck;
00209  union DEntry*  Ent;
00210 };
00211 
00212 
00213 #define MK_P(ofs)       ((void*) ((size_t)(ofs)))
00214 #define MK_LONG(l,h)    ((DWORD)WORD(l)|((DWORD)WORD(h)<<16))
00215 
00216 #define spoke(ofs,w)    (*((BYTE*)MK_P(ofs)) = (BYTE)(w))
00217 #define wpoke(ofs,w)    (*((WORD*)MK_P(ofs)) = (WORD)(w))
00218 #define dpoke(ofs,w)    (*((DWORD*)MK_P(ofs)) = (DWORD)(w))
00219 #define speek(ofs)      (*((BYTE*)MK_P(ofs)))
00220 #define wpeek(ofs)      (*((WORD*)MK_P(ofs)))
00221 #define dpeek(p)        (*((DWORD*)MK_P(p)))
00222 
00223 
00224  /// FAT drive root entry
00225 struct FATDrive : public FATDirectory
00226 {
00227     FATDrive(LPCTSTR path);
00228 /*
00229     FATDrive(Entry* parent, LPCTSTR path)
00230      :  FATEntry(parent)
00231     {
00232         _path = _tcsdup(path);
00233     }
00234 */
00235     ~FATDrive();
00236 
00237     HANDLE  _hDrive;
00238     BootSector  _boot_sector;
00239     int     _bufl;
00240     int     _bufents;
00241     int     _SClus;
00242 
00243 #define CACHE_SIZE_LOW  32
00244     Cache*  _FATCache;
00245     int     _CacheCount;
00246     DWORD*  _CacheSec;  // numbers of buffered cache sectors
00247     int*    _CacheCnt;  // counters for cache usage
00248     bool*   _CacheDty;  // dirty flags for cache
00249     int     _Caches;
00250     bool    _cache_empty;
00251     int     _read_ahead;
00252 
00253     bool    read_sector(DWORD sec, Buffer* buf, int len);
00254     DWORD   read_FAT(DWORD Clus, bool& ok);
00255 
00256     void    small_cache();
00257     void    reset_cache();
00258     bool    read_cache(DWORD sec, Buffer** bufptr);
00259     int     get_cache_buffer();
00260 };

ROS Explorer Source Code Documentation
generated on 02.01.2013 by doxygen

ROS Explorer Homepage