Jump to content

Accessing FAT using C

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
$AP

$AP

    Newbie

  • Members
  • PipPip
  • 24 posts
I need to write a code in C which will be forming a B TREE using the address of certain files from a pen-drive for example...

Now, problem lies in opening the file and assigning it to a file pointer manually when number of files become very large ( say, 1000) ..

In that case, is it possible to access one file manually and others automatically using some mechanism (say, from FAT/NTFS whatever be the file system of the prn drive) and assign all the files to an array of file pointers?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I would expect that having around 1000 files open at once would cause issues. Why do you need them open simultaneously?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
$AP

$AP

    Newbie

  • Members
  • PipPip
  • 24 posts
Actually, i want to store the addresses of the files in a B TREE format.... addresses of 1000 files for example.... can u suggest anything??

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I'm pretty rusty on my C. However, something about this feels very odd. If the files are not open, they could change during the lifetime of your program. What is the purpose of storing them in a B Tree?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
This makes no sense to me either, and I've written filesystem drivers for FAT12/16/32...

If you have block access to the device, you can read/write to it just like a file, either using /dev/sd* or the internal device name on windows. I'll save you some time,

typedef struct fat_extBS_32
{
	//extended fat32 stuff
	unsigned int		table_size_32;
	unsigned short		extended_flags;
	unsigned short		fat_version;
	unsigned int		root_cluster;
	unsigned short		fat_info;
	unsigned short		backup_BS_sector;
	unsigned char 		reserved_0[12];
	unsigned char		drive_number;
	unsigned char 		reserved_1;
	unsigned char		boot_signature;
	unsigned int 		volume_id;
	unsigned char		volume_label[11];
	unsigned char		fat_type_label[8];

}__attribute__((packed)) fat_extBS_32_t;

typedef struct fat_extBS_16
{
	//extended fat12 and fat16 stuff
	unsigned char		bios_drive_num;
	unsigned char		reserved1;
	unsigned char		boot_signature;
	unsigned int		volume_id;
	unsigned char		volume_label[11];
	unsigned char		fat_type_label[8];
	
}__attribute__((packed)) fat_extBS_16_t;

typedef struct fat_BS
{
	unsigned char 		bootjmp[3];
	unsigned char 		oem_name[8];
	unsigned short 	    bytes_per_sector;
	unsigned char		sectors_per_cluster;
	unsigned short		reserved_sector_count;
	unsigned char		table_count;
	unsigned short		root_entry_count;
	unsigned short		total_sectors_16;
	unsigned char		media_type;
	unsigned short		table_size_16;
	unsigned short		sectors_per_track;
	unsigned short		head_side_count;
	unsigned int 		hidden_sector_count;
	unsigned int 		total_sectors_32;
}__attribute__((packed)) fat_BS_t;

typedef union fat_dir
{
    struct
    {
        unsigned char dos_name[8];
        unsigned char dos_ext[3];
        unsigned char attributes;
        unsigned char reserved;
        unsigned char fine_create_time;
        unsigned short create_time;
        unsigned short create_date;
        unsigned short last_access;
        union
        {
            unsigned short ea_index;
            unsigned short high32;
        };
        unsigned short last_modified_time;
        unsigned short last_modified_date;
        union
        {
            unsigned short first_cluster;
            unsigned short low32;
        };
        unsigned int file_size;
    }__attribute__((packed)) std;
    
    struct
    {
        unsigned char seq_number;
        unsigned char name1[10];
        unsigned char attributes;
        unsigned char reserved;
        unsigned char checksum;
        unsigned char name2[12];
        unsigned short first_cluster;
        unsigned char name3[4];
    }__attribute__((packed)) lfn;
    
}__attribute__((packed)) fat_dir_t;