My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Functions | Variables
fat32.c File Reference
#include <sw_types.h>
#include <fat32.h>
#include <sw_mem_functions.h>
#include <sw_buddy.h>
#include <sw_debug.h>
#include <sw_string_functions.h>

Functions

int mount_file_system (u8 *dev)
 Mount the FAT file system and retrieve its basic information. More...
 
bool chk_fat32_file_system (u8 *file_sys_type)
 Verify the type of FAT file system. More...
 
void cal_region_offsets (boot_sector *bs, u8 *dev)
 Function to calculate all the region offsets. More...
 
int file_open (const char *file_path, int flags)
 Function to open a specific file mentioned in the path based on the incoming modes. More...
 
bool get_dir_entry (char *fname, dir_entry **entry, u8 *strt, u32 strt_clus, bool is_lfn)
 Function to check whether a given file name exists in the file system and if so retrieve the corresponding short file name directory entry. More...
 
int get_long_file_name (char *fname, u8 **dir_strt, int *count)
 Function to retrieve the long file name given its end of the long file name entry. More...
 
int lfn_entry_to_name (lfn_entry *entry, char *lfname, int *index)
 Function to get the long file name correspoding to a directory slot. More...
 
int retrieve_file_info (file_info *finfo, dir_entry *entry, u8 mode, u8 *dir_offset, const char *path)
 Function to assign a file descriptor and necessary information to a file structure inorder to pursue subsequent operations. More...
 
int file_read (int fd, char *buffer, int bytes_to_read)
 Function to read the contents of a file. More...
 
int file_write (int fd, const char *buffer, int bytes_to_write)
 Function to write into the given file with the contents given. More...
 
int file_close (int fd)
 Close the opened file based on the file descriptor and free the file structure pointer. More...
 
int create_file (char *lfname, char *shrt_name, u32 clus, dir_entry **entry)
 Function to create a file if the file doesnt exists and the create flag is set. More...
 
void fill_lng_fname_entries (char *fname, u8 *entry, u8 chk_sum, int count)
 Function to put the long file name entries into the respective allocated region. More...
 
int file_seek (int fd, int offset, int whence)
 Function to seek the opened file pointer to the desired location so that subsequent read and write operations are performed from the new value. More...
 
int file_rename (const char *old, const char *new)
 Function to rename a file/directory. More...
 
int create_dir (const char *path)
 Function to create a directory. More...
 
int open_dir (const char *path)
 Function to open a directory. More...
 
int list_dir (const char *path)
 Function to list the directory contents. More...
 
file_statread_dir (int fd)
 
u32 allocate_cluster ()
 Function to find an unused cluster from the FAT table. More...
 
u8 * cluster_to_memory_addr (u32 cluster)
 Function to get the corresponding physical address from the given cluster. More...
 
u32 get_fat_table_entry (u32 cluster)
 Function to retrieve the next cluster value in a cluster chain from the FAT table. More...
 
void write_fat_table (u32 cluster, u32 value)
 Function to write into the FAT table after assigning an empty cluster to a file. More...
 
void convert_to_uppercase (char *file_name)
 Function to convert the incoming short file name to its corresponding upper case letters. More...
 
int find_depth (const char *file_path)
 Function to calculate the number of iterations needed to reach the file from the given starting path. More...
 
int get_seek_off (file_info *file_ptr, u32 strt_cluster, int offset)
 Function to move the current cluster and its offset value to the value specified in the offset from the starting cluster provided. More...
 
void get_short_file_name (char *lfname, char *sfname, char seq_num)
 Function to extract the short file name given its long file name. More...
 
int chk_file_lock (const char *path)
 
u8 calc_check_sum (u8 *dir_entry)
 Function to calculate the checksum for the long file name based on its short name entry. More...
 
int cal_req_dir_cnt (int file_name_len)
 
int file_truncate (const char *tr_pf, int tr_lgh)
 file_truncate - function cause the regular file named by path to be truncated to a size of precisely length bytes More...
 
int file_remove (const char *fl_name)
 file_remove - Deletes the file whose name is specified in filename More...
 
int file_unlink (const char *ul_name)
 file_unlink - removes the file from the file system More...
 

Variables

u8 * bs_sector
 
u8 * fs_infosector
 
u8 * backup_boot_sector
 
u8 * first_fat
 
u8 * second_fat
 
u8 * root_directory
 
u8 * data
 
u16 sec_size
 
u32 cluster_size
 
u16 root_dir_size
 
u16 rt_dir_entry_cnt
 
u32 rt_dir_strt_clus
 
u32 nxt_free_cluster
 
u8 * dir_file_offset
 
int fdes
 
file_infofile_head
 

Function Documentation

u32 allocate_cluster ( )

Function to find an unused cluster from the FAT table.

Returns
Newly allocated cluster for the file
void cal_region_offsets ( boot_sector bs,
u8 *  dev 
)

Function to calculate all the region offsets.

Parameters
bsPointer to the boot sector structure which holds the information of the boot sector of the file system
devPointer to the start of the file system
int cal_req_dir_cnt ( int  file_name_len)
Parameters
file_name_len
Returns
u8 calc_check_sum ( u8 *  dir_entry)

Function to calculate the checksum for the long file name based on its short name entry.

Parameters
dir_entryPointer to the start of the short file name corresponding to a long file name
Returns
checksum value of the short file name
bool chk_fat32_file_system ( u8 *  file_sys_type)

Verify the type of FAT file system.

Parameters
file_sys_typePointer to the location where the type of file system is stored
Returns
true:indicates FAT32 file system false:indicates not FAT32 file system
int chk_file_lock ( const char *  path)
Parameters
path
Returns
u8* cluster_to_memory_addr ( u32  cluster)

Function to get the corresponding physical address from the given cluster.

Parameters
clusterCluster value for which the address is to be obtained
Returns
Memory address of the incoming cluster
void convert_to_uppercase ( char *  file_name)

Function to convert the incoming short file name to its corresponding upper case letters.

Parameters
file_nameFile name whose values are to be converted
int create_dir ( const char *  path)

Function to create a directory.

Parameters
pathPointer to the start of the path which represents the directory hierarchy
Returns
On successful creation:File descriptor of the directory -1:If the creation failed or if it already exists
int create_file ( char *  lfname,
char *  shrt_name,
u32  clus,
dir_entry **  entry 
)

Function to create a file if the file doesnt exists and the create flag is set.

Parameters
lfnamePointer to the start of the long file. NULL in case of short file name creation
shrt_namePointer to the start of the short file name. In case of long file names the corresponding short file name is found out.
clusStarting cluster of the directory in which the file is going to be created
entryPointer to a pointer of the directory entry structure in which the short file name directory entry content is to be stored
Returns
On successful creation returns zero
int file_close ( int  fd)

Close the opened file based on the file descriptor and free the file structure pointer.

Parameters
fdFile descriptor of the file which needs to be closed
Returns
Zero on successful closing of the file
int file_open ( const char *  file_path,
int  flags 
)

Function to open a specific file mentioned in the path based on the incoming modes.

Parameters
file_pathPointer to the location of the file path string
flagsFlags indicating the modes in which the file is to be opened
Returns
Non-zero:File descriptor of the opened file Zero :File open is unsuccessful
int file_read ( int  fd,
char *  buffer,
int  bytes_to_read 
)

Function to read the contents of a file.

Parameters
fdFile descriptor of the file in which read operation needs to be performed
bufferPointer to the start of the buffer where the contents read are stored
bytes_to_readNumber of bytes to be read from the file
Returns
Number of bytes successfully read
int file_remove ( const char *  fl_name)

file_remove - Deletes the file whose name is specified in filename

Parameters
fl_name- string containing the name of the file to be deleted
Returns
- if the file is successfully deleted, a zero value is returned. On failure, a non-zero value is returned
int file_rename ( const char *  old,
const char *  new 
)

Function to rename a file/directory.

Parameters
oldConstant pointer to the start of the path where the file which needs to be renamed is located
newConstant pointer to the start of the path where the new name of the file/directory is situated
Returns
zero on successful renaming -1 if the new name already exists
int file_seek ( int  fd,
int  offset,
int  whence 
)

Function to seek the opened file pointer to the desired location so that subsequent read and write operations are performed from the new value.

Parameters
fdFile descriptor of the file in which the seek operation needs to be performed
offsetOffset bytes indicating the number of bytes which needs to be moved
whenceFlag denoting the position from which the seek operation needs to be performed
Returns
Number of bytes traversed from the beginning of the file to the current position
int file_truncate ( const char *  tr_pf,
int  tr_lgh 
)

file_truncate - function cause the regular file named by path to be truncated to a size of precisely length bytes

Parameters
tr_pf- truncates the file whose name is specified in filename
tr_lgh- The desired truncate size of the file in bytes
Returns
- If the file contents is successfully truncated, a zero value is returned.On failure, non-Zero is returned
int file_unlink ( const char *  ul_name)

file_unlink - removes the file from the file system

file_remove - Deletes the file whose name is specified in filename

Parameters
ul_name- string containing the name of the file to be deleted
Returns
- if the file is successfully deleted, a zero value is returned. On failure, a non-zero value is returned
int file_write ( int  fd,
const char *  buffer,
int  bytes_to_write 
)

Function to write into the given file with the contents given.

Parameters
fdFile descriptor of the file
bufferPointer to the start of the contents which needs to be written into the file
bytes_to_writeIndicates the number of bytes which needs to be written into the file
Returns
Integer representing number of bytes successfully written into the file
void fill_lng_fname_entries ( char *  fname,
u8 *  entry,
u8  chk_sum,
int  count 
)

Function to put the long file name entries into the respective allocated region.

Parameters
fnamePointer to the start of the long file name
entryStarting memory location to the short file name directory entry
chk_sumCalculated check sum of the short file name corresponding to the long file name
countCounter indicating the number of 32 byte entries needed for the whole file name to get accommodated
int find_depth ( const char *  file_path)

Function to calculate the number of iterations needed to reach the file from the given starting path.

Parameters
file_pathPointer to the starting of the file path
Returns
Count of directories which resides before the file/directory from the given path
bool get_dir_entry ( char *  fname,
dir_entry **  entry,
u8 *  strt,
u32  strt_clus,
bool  is_lfn 
)

Function to check whether a given file name exists in the file system and if so retrieve the corresponding short file name directory entry.

Parameters
fnamePointer to the start of the file name which needs to be searched
entryPointer to a pointer of the directory entry structure in which the short file name directory entry content is to be stored
strtPointer to the start of the region where the file/directory needs to be searched
strt_clusStarting cluster of the directory in which the file is assumed to be present
is_lfnBoolean value which indicates whether the file is short or long file
Returns
true: if the file/dir is present false:if it is not present in the respected path
u32 get_fat_table_entry ( u32  cluster)

Function to retrieve the next cluster value in a cluster chain from the FAT table.

Parameters
clusterCluster whose next cluster value is to be obtained
Returns
Corresponding FAT table entry for the given cluster
int get_long_file_name ( char *  fname,
u8 **  dir_strt,
int *  count 
)

Function to retrieve the long file name given its end of the long file name entry.

Parameters
fnamePointer to a location where the retrieved long file name starts
dir_strtPointer to the memory location where the last component of the long file name resides
countCount representing the number of directory entries needed for the long file name
Returns
Length of the long file name
int get_seek_off ( file_info file_ptr,
u32  strt_cluster,
int  offset 
)

Function to move the current cluster and its offset value to the value specified in the offset from the starting cluster provided.

Parameters
file_ptrFile structure pointer pointing to the opened file
strt_clusterStarting cluster value from which the seek operation has to be performed
offsetOffset bytes for the seek operation
Returns
Number of bytes traversed from the beginning of the file
void get_short_file_name ( char *  lfname,
char *  sfname,
char  seq_num 
)

Function to extract the short file name given its long file name.

Parameters
lfname: Pointer to the start of the long file name string
sfname: Pointer to the start of the short file name in which this function extracts and stored the short file name value
seq_num: sequence number
int lfn_entry_to_name ( lfn_entry entry,
char *  lfname,
int *  index 
)

Function to get the long file name correspoding to a directory slot.

Parameters
entryPointer to the slot where a long file component resides
lfnamePointer to the start of the long file name
indexPointer to the offset of the long file name where the retrieved name from the given slot is going to be stored
Returns
0:if the file name doesnt end in the given slot 1:if the file name ends in the given slot
int list_dir ( const char *  path)

Function to list the directory contents.

Parameters
pathPointer to the start of the path representing the directory hierarchy
Returns
Number of directories/files present in the given directory
int mount_file_system ( u8 *  dev)

Mount the FAT file system and retrieve its basic information.

Parameters
devPointer to the start of the file system image
Returns
0:File system is FAT32 and the mount process is successful -1:Mount is unsuccessful
int open_dir ( const char *  path)

Function to open a directory.

Parameters
pathPointer to the start of the path representing the directory hierarchy
Returns
file descriptor assigned to the directory
file_stat* read_dir ( int  fd)
Parameters
fd
Returns
int retrieve_file_info ( file_info finfo,
dir_entry entry,
u8  mode,
u8 *  dir_offset,
const char *  path 
)

Function to assign a file descriptor and necessary information to a file structure inorder to pursue subsequent operations.

Parameters
finfo: Pointer to a file structure where the file information is stored
entry: Pointer to the directory entry of the file/directory
mode: Mode in which the file is requested to be opened
dir_offset: directory offset
path: path
Returns
returns : File descriptor of the file
void write_fat_table ( u32  cluster,
u32  value 
)

Function to write into the FAT table after assigning an empty cluster to a file.

Parameters
clusterCluster number for which the entry has to be made
valueNext cluster value of the cluster number provided

Variable Documentation

u8* backup_boot_sector
u8* bs_sector
u32 cluster_size
u8* data
u8* dir_file_offset
int fdes
file_info* file_head
u8* first_fat
u8* fs_infosector
u32 nxt_free_cluster
u16 root_dir_size
u8* root_directory
u16 rt_dir_entry_cnt
u32 rt_dir_strt_clus
u16 sec_size
u8* second_fat