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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,902评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,037评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,978评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,867评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,763评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,104评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,565评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,236评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,379评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,313评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,363评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,034评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,637评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,719评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,952评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,371评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,948评论 2 341

推荐阅读更多精彩内容

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