How to write FAT mounter in C++?
atifjatt 0 Newbie Poster
plz any one have idea i m just start woking in C++.
p@rse -2 Light Poster
atifjatt 0 Newbie Poster
/*
* FAT.cpp
*
* Created on: Jun 22, 2010
* Author: Vibrant-Text
*/
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <dos.h>
#include <windows.h>
#include<conio.h>
#include"FAT.h"
#include"device.h"
using namespace std;
//=================================
static unsigned char buffer[512];
DWORD unusedSectors;
WORD bytesPerSector, sectorPerCluster, reservedSectorCount;
DWORD firstDataSector, rootCluster, totalClusters;
DWORD startBlock;
BYTE freeClusterCountUpdated;
DWORD CurrentDirStartCluster;
int drive = 1;
//===========================Boot Sector=======================================
BYTE getBootSectorData (void)
{
struct BS_Structure *bpb; //mapping the buffer onto the structure
struct MBRinfo_Structure *mbr;
struct partitionInfo_Structure *partition;
DWORD dataSectors;
unusedSectors = 0;
bpb = (struct BS_Structure *)buffer;
if(bpb->jumpBoot[0]!=0xE9 && bpb->jumpBoot[0]!=0xEB) //check if it is boot sector
{
mbr = (struct MBRinfo_Structure *) buffer; //if it is not boot sector, it must be MBR
if(mbr->signature != 0xaa55) return 1; //if it is not even MBR then it's not FAT32
partition = (struct partitionInfo_Structure *)(mbr->partitionData);//first partition
unusedSectors = partition->firstSector; //the unused sectors, hidden to the FAT
// partition->firstSector;
//read the bpb sector
bpb = (struct BS_Structure *)buffer;
if(bpb->jumpBoot[0]!=0xE9 && bpb->jumpBoot[0]!=0xEB) return 1;
}
bytesPerSector = bpb->bytesPerSector;
sectorPerCluster = bpb->sectorPerCluster;
reservedSectorCount = bpb->reservedSectorCount;
rootCluster = bpb->rootCluster;//+ (sector / sectorPerCluster) +1;
firstDataSector = bpb->hiddenSectors + reservedSectorCount + (bpb->numberofFATs * bpb->FATsize_F32);
dataSectors = bpb->totalSectors_F32 - bpb->reservedSectorCount - ( bpb->numberofFATs * bpb->FATsize_F32);
totalClusters = dataSectors / sectorPerCluster;
CurrentDirStartCluster = bpb->rootCluster;
}
//================== Get sector# from cluster================================
unsigned long fatClusterToSector(unsigned long cluster)
{
return ((cluster-2) * sectorPerCluster) + firstDataSector;
}
//======================Cluster Size=========
unsigned int fatClusterSize(void)
{
// return the number of sectors in a disk cluster
return sectorPerCluster;
}
//========================First Sector==========================================
DWORD getFirstSector(DWORD clusterNumber)
{
return (((clusterNumber - 2) * sectorPerCluster) + firstDataSector);
}
//=====================================================================================
unsigned char fatGetDirEntry(unsigned short entry)
{
DWORD sector;
struct rootdir *de = 0; // avoid compiler warning by initializing
struct winentry *we;
unsigned char haveLongNameEntry;
unsigned char gotEntry;
unsigned short b;
int i,index;
char *fnbPtr;
WORD entrycount = 0;
// read dir data
sector = fatClusterToSector(CurrentDirStartCluster);
haveLongNameEntry = 0;
gotEntry = 0;
index = 16; // crank it up
//while(entrycount < entry)
while(1)
{
if(index == 16) // time for next sector ?
{
FatFsInfo.devdisk.ReadSector(sector++, 1, SectorBuffer);
de = (struct rootdir *) SectorBuffer;
index = 0;
}
// check the status of this directory entry slot
if(de->deName[0] == 0x00)
{
// slot is empty and this is the end of directory
gotEntry = 0;
break;
}
else if(de->deName[0] == 0xE5)
{
// this is an empty slot
// do nothing and move to the next one
}
else
{
// this is a valid and occupied entry
// is it a part of a long file/dir name?
if(de->deAttributes == ATTR_LONG_FILENAME)
{
// we have a long name entry
// cast this directory entry as a "windows" (LFN: LongFileName) entry
we = (struct winentry *) de;
b = WIN_ENTRY_CHARS*( (we->weCnt-1) & 0x0f); // index into string
fnbPtr = &FileNameBuffer[b];
for (i=0;i<5;i++) *fnbPtr++ = we->wePart1[i*2]; // copy first part
for (i=0;i<6;i++) *fnbPtr++ = we->wePart2[i*2]; // second part
for (i=0;i<2;i++) *fnbPtr++ = we->wePart3[i*2]; // and third part
if (we->weCnt & WIN_LAST) *fnbPtr = 0; // in case dirnamelength is multiple of 13, add termination
if ((we->weCnt & 0x0f) == 1) haveLongNameEntry = 1; // flag that we have a complete long name entry set
}
else
{
// we have a short name entry
// check if this is the short name entry corresponding
// to the end of a multi-part long name entry
if(haveLongNameEntry)
{
// a long entry name has been collected
if(entrycount == entry)
{
// desired entry has been found, break out
gotEntry = 1;
break;
}
// otherwise
haveLongNameEntry = 0; // clear long name flag
entrycount++; // increment entry counter
}
else
{
// entry is a short name (8.3 format) without a
// corresponding multi-part long name entry
fnbPtr = FileNameBuffer;
for (i=0;i<8;i++) *fnbPtr++ = de->deName[i]; // copy name
*fnbPtr++ = '.'; // insert '.'
for (i=0;i<3;i++) *fnbPtr++ = de->deExtension[i]; // copy extension
*fnbPtr = 0; // null-terminate
if(entrycount == entry)
{
// desired entry has been found, break out
gotEntry = 1;
break;
}
// otherwise
entrycount++; // increment entry counter
}
}
// next directory entry
de++;
// next index
index++;
}
// we have a file/dir to return
// store file/dir starting cluster (start of data)
FileInfo.StartCluster = (unsigned long) ((unsigned long)de->clusterNumber_High << 16) + de->firstClusterAddress;
// store file/dir size
// (note: size field for subdirectory entries is always zero)
FileInfo.Size = de->sizeofFile;
// store file/dir attributes
FileInfo.Attr = de->fileAttributes;
// store file/dir creation time
FileInfo.CreateTime = de->createDate[0] | de->createDate[1]<<8;
// store file/dir creation date
FileInfo.CreateTime = de->createDate[0] | de->createDate[1]<<8;
return gotEntry;
}
atifjatt 0 Newbie Poster
i hav also problem to using device.h header file.i am working in eclipse C++
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.