You can even start with FAT32 LBA.quadrant wrote:I plan on using an SD card as my "disk" (i.e. persistent memory). What should I keep in mind when designing a simple file system with regards to sending read and write requests to the SD card?
You can format the disk or a partition, create big files with an ID string at the start, and record the position of your files. Since they are defragmented, you just need to know up to which point you have written them.
You can also create empty files and write a string with the sector number of the file at the start of the sector block, so that you can see if in fact you created an unfragmented file in your new test partition.
create_big_file.c
Code: Select all
#include <stdio.h>
//NOTE: These two libraries seem to need to be together
// to avoid an error message that says
// "warning: incompatible implicit declaration of built-in
// declaration of built-in function 'memset'":
//
// This warning occurs with gcc.
///
#include <stdlib.h>
#include <string.h>
//This is a macro to easily access the first command line
//as the file name the program should use as a big file
//to create:
///
#define creatFileName argv[1]
#define sectors512Count argv[2]
//(bytes_per_sect*sects)-zero_addressing
///
#define creatFileSize____512MB 512*1048576-1
#define creatFileSize____20GB0 39070080
#define creatFileSize____875GBbytes 939524096000
#define creatFileSize____875GB4ksects 229376000
//for completing aligned/full file bytes
int main(int argc, char *argv[])
{
//Here we just declare the variables
//to handle this simple big file:
///
FILE *fHandle;
const char *fName = "hard_disk.img";
// long numsects=39070080;
long int numsects=creatFileSize____875GB4ksects;
void *buff;
unsigned long sectnum=0;
if(argc<3)
{
printf("ERROR: Please specify a file name and a number of 512-byte sectors\r\n");
}
//Let's open (and create or overwrite)
//the specified file name:
///
if(!(fHandle=fopen(creatFileName, "wb")))
{
printf("ERROR: Couldn't open %s\r\n", creatFileName);
return -1;
}
if(!(abs(numsects=atol(sectors512Count))))
{
printf("ERROR: Sector count (%ld) must be greater than 0\r\n", numsects);
}
// fseek(fHandle, creatFileSize____20GB0, SEEK_SET);
// fwrite("", 1, 1, fHandle);
// fwrite("", 1, 39070080, fHandle);
buff=malloc(512);
do
{
memset(
buff, //Buffer to zero out
0, //(int)(unsigned char) value to write (0 in this case)
512 //Number of bytes to write
);
ltoa(sectnum, buff, 10);
fwrite(buff, 1, 512, fHandle);
sectnum++;
}
while(--numsects);
free(buff);
fclose(fHandle);
return 0;
}