File System

2019-06-30  本文已影响0人  白宇的斑马

Commands

File Permissions

Disk Duplication

sudo dd if=FILE of=FILE bs=BYTES count=BLOCKS

if: input file, of: output file, bs: block size(how many bytes), count: copy only BLOCKS input blocks
详见百度百科
eg. dd if=/dev/zero of=test.disk bs=64M count=1
//用zero产生特定大小的空白文件

Create FAT Filesystems

mkfs.vfat -F FAT-size -f NUMFAT -S logical-sector-size -s sectors-per-cluster -R number-of-reserved-sectors DEVICE

eg. mkfs.vfat -F 32 -f 2 -S 512 -s 1 -R 32 test.disk
2 FATs, 512 bytes per sector, 1 sector per cluster, 32 reserved sectors

Check and Repair FAT filesystem

Mount

  1. mkdir ~/rd //creat a mount point
  2. sudo mount -t vfat -o test.disk ~/rd //mount the disk to the mount point 详见http://www.runoob.com/linux/linux-comm-mount.html
  3. sudo umount ~/rd //unmount the disk
  1. lsof ~/rd //列出当前系统打开文件
  2. fuser -vm ~/rd //显示出当前哪个程序在使用磁盘上的某个文件、挂载点、甚至网络端口,并给出程序进程的详细信息.
    显示出哪个process正在occupy device,kill他的PID

Demo

create a new file test.disk to hold the virtual volume by pre-filling the file with data

dd if=/dev/zero of=test.disk bs=64M count=1

give the volume a filesystem, FAT32

mkfs.vfat -F 32 -f 2 -S 512 -s 1 -R 32 test.disk

check the details of the FAT filesystem

dosfsck -v test.disk

to mount the formatted file from the terminal, create a folder to mount it to(~/rd)

mkdir ~/rd
sudo mount -t vfat -o loop test.disk ~/rd
cd ~/rd
ls
sudo touch a
ls

unmount

cd -
sudo umount ~/rd

Viewing with Hex Editor

To analyze the file system, view it with hexasecimal editor, eg. hexedit, xxd in Ubuntu
//after unmount

hexedit test.disk

Three columns in output: Line number in hexadecimal, content in hexadecimal, content in ASCII

Endian-ness in FAT32

Endian-ness is about byte ordering. Two types: big endian, little endian
big endian: 0x0002 = 2 bytes
little endian: 0x0200 = 512bytes
In FAT32, little endian is used, the number of bytes per sector is 512 bytes

Linux File Systems Calls

Directory Related Calls in C Language

/* listdir.c */
#include <stdio.h>
#include <dirent.h>

int main (int argc,char *argv[]) {
    int total = 0;
        DIR *pDir;
        struct dirent *pDirent;

        if (argc < 2) {
            printf ("Missing out directory!\n");
            return -1;
        }
        pDir = opendir (argv[1]);
        if (pDir == NULL) {
            printf ("Cannot open directory '%s'\n", argv[1]);
            return 1;
        }

        while ((pDirent = readdir(pDir)) != NULL) {
            printf ("[%s]\n", pDirent->d_name);
        }
        closedir (pDir);
        return 0;
    }

Overview of FAT32

Accessing FAT using C

C Header of TAF

include <linus/msdos_fs.h>

Header: Boot Sector

Header: Dir Entry

Read the header

举个栗子 create a virtual disk beforehand

FILE *fp = NULL;
    struct fat_boot_sector boot_entry;

    fp = fopen(device_name, "r+");
    if(fp == NULL)
        exit(-1);
    uint32_t numItem = fread(&boot_entry, sizeof(struct fat_boot_sector), 1, fp);
    if(numItem != 1)
        exit(-1);
    //  Bytes per sector. Allowed values include 512, 1024, 2048, and 4096
    uint16_t bps = boot_entry.sector_size[0] + ((uint16_t) boot_entry.sector_size[1] << 8);
    off_t root_entry_offset = ( boot_entry.reserved +
                                boot_entry.fats * boot_entry.fat32.length) * bps;
    uint32_t bpc = bps * boot_entry.sec_per_clus;
    off_t fat_offset = bps * boot_entry.reserved;

    disk_info->fp = fp;
    disk_info->root_entry_offset = root_entry_offset;
    disk_info->bpc = bpc;
    disk_info->bps = bps;
    disk_info->spc = boot_entry.sec_per_clus;
    disk_info->reserved_sectors = boot_entry.reserved;
    disk_info->fat_offset = fat_offset;
    disk_info->num_fats = boot_entry.fats;
    disk_info->fat_size = boot_entry.fat32.length;

8+3 File Name

at most 8 chatacters + '.' + at most 3 characters

define MSDOS_NAME 11

__u8 name[MSDOS_NAME]

Traversing Cluster

Finding Next Cluster

上一篇 下一篇

猜你喜欢

热点阅读