-
Notifications
You must be signed in to change notification settings - Fork 0
/
fat12_file_system.h
87 lines (76 loc) · 2.83 KB
/
fat12_file_system.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef FAT12_FILE_SYSTEM_H
#define FAT12_FILE_SYSTEM_H
#include <cstdint>
#include <string>
#include <fstream>
using namespace std;
// Constants
#define MAX_BLOCKS 4096
#define BLOCK_SIZE_512 512
#define BLOCK_SIZE_1024 1024
// SuperBlock structure
typedef struct SuperBlock {
uint32_t totalBlocks;
uint32_t freeBlocks;
uint32_t blockSize;
uint32_t rootDirectory;
uint32_t firstDataBlock;
} SuperBlock;
typedef uint16_t FAT12Entry;
#define FAT_FREE 0x0000
#define FAT_END 0xFFFF
// File attributes
typedef struct file_attributes {
uint8_t read_permission : 1;
uint8_t write_permission : 1;
uint8_t is_directory : 1;
uint8_t is_protected : 1;
uint8_t filename_exceeds : 1;
uint8_t reserved : 3;
} file_attributes;
// File time structure
typedef struct file_time {
uint8_t hours : 5;
uint8_t minutes : 6;
uint8_t seconds : 5;
} file_time;
// File date structure
typedef struct file_date {
uint8_t day : 5;
uint8_t month : 4;
uint16_t year : 7;
} file_date;
// Directory entry structure
typedef struct DirectoryEntry {
char filename[8]; // 64 bits for the filename (8 characters with 8 bits each)
char extension[3]; // File extension
file_attributes attributes; // File attributes
char reserved[18]; // Extended filename (if it exceeds 8 characters)
file_time last_modificaton_time; // Time of last modification
file_date last_modification_date; // Date of last modification
file_time creation_time; // Time of creation
file_date creation_date; // Date of creation
uint16_t first_block_number; // The first block number of the file
uint32_t file_size; // Size of the file in bytes
char password[16]; // Password for the file
} DirectoryEntry;
// Function prototypes
void initializeSuperBlock(SuperBlock &superBlock, uint32_t blockSize);
void writeSuperBlock(std::ofstream &file, SuperBlock &superBlock);
void writeSuperBlock(std::fstream &file, SuperBlock &superBlock);
void readSuperBlock(std::ifstream &file, SuperBlock &superBlock);
void readSuperBlock(std::fstream &file, SuperBlock &superBlock);
void initializeFreeBlocks(uint8_t *free_blocks);
void writeFreeBlocks(std::ofstream &file, uint8_t *free_blocks);
void writeFreeBlocks(std::fstream &file, uint8_t *free_blocks);
void readFreeBlocks(std::ifstream &file, uint8_t *free_blocks);
void readFreeBlocks(std::fstream &file, uint8_t *free_blocks);
void initializeFAT12(FAT12Entry *fat);
void writeFAT12(std::ofstream &file, FAT12Entry *fat);
void writeFAT12(std::fstream &file, FAT12Entry *fat);
void readFAT12(std::ifstream &file, FAT12Entry *fat);
void readFAT12(std::fstream &file, FAT12Entry *fat);
uint32_t readFAT12Entry(const FAT12Entry *fat, uint32_t currentBlock);
vector<DirectoryEntry> readDirectoryEntries(fstream &file, uint32_t block);
int findFreeBlock(uint8_t *free_blocks);
#endif // FAT12_FILE_SYSTEM_H