The project VHD is going to implement basic functions to handle files in Virtual FAT32 disk.
- OS: OS X Yosemite Version 10.10.3
- language: C
- compiler: gcc
- clone the repo
git clone git@github.com:Travelinglight/VHD.git
- use Windows to generate a .vhd file
- the format of the file should be FunAT32
- the filesize should not be too large; 20MB is recommended
- the number of bytes per block is recommended to be 512
- the .vhd file should be put into the repo
- compile VHD.c
gcc -g -o VHD VHD.c
- run the executable file, and follow the instructions:
./VHD
type "help" for instructions list
- list out the file list, with detailed infomation
- remove a file from the VHD
- copy a file from the VHD to the outside
- move a file from outside into the VHD
- FILE* fp ---- file point for .vhd file
- word offSet ---- the offset of fp
- byte buff_block[32768] ---- the buff of a block
- byte buff_byte ---- the buff of a byte
- hWord FAT[32768] ---- the FAT store in memory
- hWord nByte ---- the number of bytes within a block
- hWord nBlock ---- the number of blocks within a cluster
- hWord nCluster ---- the number of clusters in data area of the vhd
- hWord nFAT ---- the number of FATs in the vhd
- hWord nBFAT ---- the number of blocks within a FAT
- hWord nFile ---- the maximum number of files that could be recorded in the root directory
- file* fIndex[512] ---- the file index maintained in memory, corresponding to the root directory
- init
- read parameters from boot block
- read the whole FAT
- read root directory and initialize the fIndex array
- ls: list file infomation
- list the file id, file name, attribute, timestamp, and file size for each file in the fIndex
- cp: copy a file from vhd to outside
- generate the filename of the outside file, and open it
- read data area according to the FAT, and write to the destination file block by block
- rm: remove a file from VHD
- clear the FAT chain (reset those half_word in FAT describing the clusters of the file to 0)
- put 0xE5 to the first byte of the filename in root directory
- free and clear the file struct in the fIndex array
- mv: move a file from outside into the VHD
- parse the filename and file_extention
- get the file size
- find and clear enough clusters that are not used, and record them in an array
- obtain other necessary attributes describing a file
- modify root directory with the attributes obtained before
- modify FAT according to the clusters array found out before
- read binary data from source file, and write them into the clusters found before, byte by byte
- readBlock: to read a block from .vhd file into buff_block
- readByte: to read and return a byte from .vhd file
- readHWord: to read and return a half_word from .vhd file. This function calls:
- readByte;
- readWord: to read and return a word from .vhd file. This function calls:
- readHWord;
- readFAT: to read the while FAT from .vhd file into FAT
- raedFileName: to read the filename from .vhd file, format it and handle special cases. This function calls:
- readByte;
- readFileExt: to read the file extention from .vhd file and format it. This function calls:
- readByte;
- readAttr: to read the attribute of a file from .vhd file. This function calls:
- readByte;
- readFileTimeStamp: to read the time and date of the file from root directory in .vhd file. This function calls:
- readHWord;
- readFileStart: to read the id of start cluster of the file from .vhd file. This function calls:
- readHWord;
- readFileSize: to read the file size from the root directory in .vhd file. This function calls:
- readHWord;
- readRD: to read the info of all files. This function calls:
- readFileExt;
- readFileAttr;
- readFileTimeStamp;
- readFileStart;
- readFileSize;
- printFileName: to print the filename in normal way
- printFileAttr: to print the file attribute in binary format
- printFileTime: to print the time of the file vividly
- printFileSize: to print the file size in Bytes
- cpCluster: to copy a whole cluster to destination field, which is called by function cp;
- cvt2upper: to convert a string (e.g. filename) to uppercase string
- getTime: to get the current time and transform it and store it in a struct
- writeTime: to write the time and date into the root directory in .vhd file
- After a .txt file is written into .vhd file, there would be an extra half_word (0x0AFF) appended to the original file.
- The file of 2 bytes would become 512 bytes after being written into and extracted out from VHD.
- Some constants are used in the program, so the program may not be universal to all FAT32 VHDs.