File System

Commands

File Permissions

  • ls -l <filename>: 查file mode, owner, group, file size, last modified, filename
  • rwx: read 4, write 2, execute 1
  • chmod 744 <filename>:更改权限
  • 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 <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

  • 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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 书名是《好好学习》副标题:个人知识管理精进指南。副标题包含两个要点:个人知识管理和精进指南,这对读者其实提出了两点...
    读书会崔森阅读 170评论 0 1
  • Day2 日更的第三次重启,哈哈 没事,重头再来。 001想要找房子,最好一个月内找到房并确定下来,不然,你会身心...
    囧囧雕塑家阅读 285评论 0 5
  • 昨天,儿子不小心把我放在柜子上的零钱罐打碎了,在我捡拾散落一地的硬币的时候,看到了躺在地上的那两枚5元韩币。 那还...
    梅香树影阅读 704评论 4 15
  • 近来(包括现在在公交车上)总能听到或看到,有些人或网友在抱怨活着很辛苦,活的不幸福,最近工作压力大,房贷,孩奴,...
    喵妹纸阅读 266评论 0 0
  • 妇女节、女神节…… 总之,今天,是个让全世界,都充满女性意识的日子。 而我的脑子里,却突然跳出一个人——玛格丽特....
    无来有阅读 374评论 0 1