File System

Commands

File Permissions

  • ls -l : 查file mode, owner, group, file size, last modified, filename
  • rwx: read 4, write 2, execute 1
  • chmod 744 :更改权限
  • unmask 111: 将默认权限改为777-111=666

Disk Duplication

  • dd: low-level copying. As disks are like normal files, with dd can create a virtual disk
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: initialize a blank disk
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

  • dosfcsk: check the details of the FAT filesystem
    eg. dosfsck -v test.disk

Mount

  • mount: mounts a storage or a file system, make is accessible and attach it to an existing directory structure. The attached directory is called mount point.
    unmount after using, or not fully synchonized, cause loss of data
    Steps:
  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
  • Device busy problem: process occopies the device s.t cannot unmount it
    Method:
  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

  • file descriptor: STDIN_FILENO 0, STDOUT_FILENO 1, STDERR_FILENO 2
  • three system file tables: open file table->inode table

Directory Related Calls in C Language

  • mkdir(const char* pathname, mode): create a directory
  • rmdir(const char* pathname): delete a directory
  • opendir(const char* pathname): open a directory
  • readdir(DIR* dp): read a directory
  • closedir(DIR* dp): close a directory
  • DIR structure
  • dirent :ino_t d_ino(file serial number), char d_name[](name of entry)
    举个例子: simulate ls
/* listdir.c */
#include 
#include 

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

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

  • In Dir Entry header:

define MSDOS_NAME 11

__u8 name[MSDOS_NAME]

  • File deletion and File name
    To delete, mark the first character of the file name with 0xe5

Traversing Cluster

Finding Next Cluster

你可能感兴趣的:(File System)