dosfstools  4.2
About: dosfstools are utilities to create, check and label (MS-DOS) FAT filesystems.
  Fossies Dox: dosfstools-4.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

fat.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "common.h"
#include "fsck.fat.h"
#include "io.h"
#include "boot.h"
#include "check.h"
#include "fat.h"
Include dependency graph for fat.c:

Go to the source code of this file.

Functions

void get_fat (FAT_ENTRY *entry, void *fat, uint32_t cluster, DOS_FS *fs)
 
void release_fat (DOS_FS *fs)
 
static void fix_first_cluster (DOS_FS *fs, void *first_cluster)
 
void read_fat (DOS_FS *fs, int mode)
 
void set_fat (DOS_FS *fs, uint32_t cluster, int32_t new)
 
int bad_cluster (DOS_FS *fs, uint32_t cluster)
 
uint32_t next_cluster (DOS_FS *fs, uint32_t cluster)
 
off_t cluster_start (DOS_FS *fs, uint32_t cluster)
 
void set_owner (DOS_FS *fs, uint32_t cluster, DOS_FILE *owner)
 
DOS_FILEget_owner (DOS_FS *fs, uint32_t cluster)
 
void fix_bad (DOS_FS *fs)
 
void reclaim_free (DOS_FS *fs)
 
static void tag_free (DOS_FS *fs, DOS_FILE *owner, uint32_t *num_refs, uint32_t start_cluster)
 
void reclaim_file (DOS_FS *fs)
 
uint32_t update_free (DOS_FS *fs)
 

Function Documentation

◆ bad_cluster()

int bad_cluster ( DOS_FS fs,
uint32_t  cluster 
)

Definition at line 337 of file fat.c.

References DOS_FS::fat, FAT_IS_BAD, get_fat(), and FAT_ENTRY::value.

Referenced by check_file(), and test_file().

◆ cluster_start()

off_t cluster_start ( DOS_FS fs,
uint32_t  cluster 
)

◆ fix_bad()

void fix_bad ( DOS_FS fs)

◆ fix_first_cluster()

static void fix_first_cluster ( DOS_FS fs,
void *  first_cluster 
)
static

Definition at line 90 of file fat.c.

References DOS_FS::fat_bits, FAT_EXTD, fs_read(), and boot_sector::media.

Referenced by read_fat().

◆ get_fat()

void get_fat ( FAT_ENTRY entry,
void *  fat,
uint32_t  cluster,
DOS_FS fs 
)

Fetch the FAT entry for a specified cluster.

Parameters
[out]entryCluster to which cluster of interest is linked
[in]fatFAT table for the partition
[in]clusterCluster of interest
[in]fsInformation from the FAT boot sectors (bits per FAT entry)

Definition at line 48 of file fat.c.

References DOS_FS::data_clusters, die(), fat, DOS_FS::fat_bits, FAT_ENTRY::reserved, and FAT_ENTRY::value.

Referenced by alloc_rootdir_entry(), bad_cluster(), check_dirty_bits(), check_file(), fix_bad(), next_cluster(), read_fat(), reclaim_file(), reclaim_free(), set_fat(), tag_free(), undelete(), and update_free().

◆ get_owner()

DOS_FILE* get_owner ( DOS_FS fs,
uint32_t  cluster 
)

◆ next_cluster()

uint32_t next_cluster ( DOS_FS fs,
uint32_t  cluster 
)

Get the cluster to which the specified cluster is linked. If the linked cluster is marked bad, abort.

Parameters
[in]fsInformation about the filesystem
[in]clusterCluster to follow
Returns
-1 'cluster' is at the end of the chain
Other values Next cluster in this chain

Definition at line 355 of file fat.c.

References die(), DOS_FS::fat, FAT_IS_BAD, FAT_IS_EOF, get_fat(), and FAT_ENTRY::value.

Referenced by alloc_rootdir_entry(), check_file(), find_volume_de(), reclaim_file(), scan_dir(), scan_free_entry(), tag_free(), test_file(), and truncate_file().

◆ read_fat()

void read_fat ( DOS_FS fs,
int  mode 
)

Build a bookkeeping structure from the partition's FAT table. If the partition has multiple FATs and they don't agree, try to pick a winner, and queue a command to overwrite the loser. One error that is fixed here is a cluster that links to something out of range.

Parameters
[in,out]fsInformation about the filesystem
[in]mode0 - read-only, 1 - read-write (no repair), 2 - repair

Definition at line 114 of file fat.c.

References alloc(), DOS_FS::cluster_owner, DOS_FS::data_clusters, die(), DOS_FS::fat, DOS_FS::fat_bits, FAT_EXTD, FAT_MIN_BAD, DOS_FS::fat_size, DOS_FS::fat_start, fat_table, fix_first_cluster(), fs_read(), fs_write(), get_choice(), get_fat(), DOS_FS::nfats, release_fat(), set_fat(), and FAT_ENTRY::value.

Referenced by handle_label(), and main().

◆ reclaim_file()

void reclaim_file ( DOS_FS fs)

Recover orphan chains to files, handling any cycles or cross-links.

Parameters
[in,out]fsInformation about the filesystem

Definition at line 502 of file fat.c.

References alloc(), alloc_rootdir_entry(), DOS_FS::cluster_size, DOS_FS::data_clusters, die(), DOS_FS::fat, DOS_FS::fat_bits, FAT_IS_BAD, fs_write(), get_fat(), get_owner(), next_cluster(), set_fat(), DIR_ENT::size, DIR_ENT::start, DIR_ENT::starthi, tag_free(), FAT_ENTRY::value, and verbose.

Referenced by main().

◆ reclaim_free()

void reclaim_free ( DOS_FS fs)

◆ release_fat()

void release_fat ( DOS_FS fs)

Definition at line 80 of file fat.c.

References DOS_FS::cluster_owner, and DOS_FS::fat.

Referenced by handle_label(), main(), and read_fat().

◆ set_fat()

void set_fat ( DOS_FS fs,
uint32_t  cluster,
int32_t  new 
)

Update the FAT entry for a specified cluster (i.e., change the cluster it links to). Queue a command to write out this change.

Parameters
[in,out]fsInformation about the filesystem
[in]clusterCluster to change
[in]newCluster to link to Special values: 0 == free cluster -1 == end-of-chain -2 == bad cluster

Definition at line 268 of file fat.c.

References DOS_FS::data_clusters, die(), DOS_FS::fat, FAT_BAD, DOS_FS::fat_bits, FAT_EOF, DOS_FS::fat_size, DOS_FS::fat_start, fs_write(), get_fat(), DOS_FS::nfats, FAT_ENTRY::reserved, and FAT_ENTRY::value.

Referenced by alloc_rootdir_entry(), check_file(), fix_bad(), read_fat(), reclaim_file(), reclaim_free(), tag_free(), test_file(), truncate_file(), and undelete().

◆ set_owner()

void set_owner ( DOS_FS fs,
uint32_t  cluster,
DOS_FILE owner 
)

Update internal bookkeeping to show that the specified cluster belongs to the specified dentry.

Parameters
[in,out]fsInformation about the filesystem
[in]clusterCluster being assigned
[in]ownerInformation on dentry that owns this cluster (may be NULL)

Definition at line 383 of file fat.c.

References DOS_FS::cluster_owner, and die().

Referenced by alloc_rootdir_entry(), check_file(), tag_free(), and test_file().

◆ tag_free()

static void tag_free ( DOS_FS fs,
DOS_FILE owner,
uint32_t *  num_refs,
uint32_t  start_cluster 
)
static

Assign the specified owner to all orphan chains (except cycles). Break cross-links between orphan chains.

Parameters
[in,out]fsInformation about the filesystem
[in]ownerdentry to be assigned ownership of orphans
[in,out]num_refsFor each orphan cluster [index], how many clusters link to it.
[in]start_clusterWhere to start scanning for orphans

Definition at line 454 of file fat.c.

References DOS_FS::data_clusters, DOS_FS::fat, FAT_IS_BAD, get_fat(), get_owner(), next_cluster(), set_fat(), set_owner(), and FAT_ENTRY::value.

Referenced by reclaim_file().

◆ update_free()

uint32_t update_free ( DOS_FS fs)