This is my operating system class design.

scanner.h

 #include<string>
 using namespace std;
 #pragma once
 struct Scanner
 {
     int si;
     ];
     Scanner(char ss[]){
         int i;
         ; ss[i]; i++)s[i] = ss[i];
         s[i] = ;
         si = ;
         while (s[si] == ' ')si++;
     }
     bool hasNext(){
         ;
     }
     string next(){
         string ans = "";
         while (s[si] != ' '&&s[si])ans += s[si++];
         while (s[si] == ' ')si++;
         return ans;
     }
 };
  

path.h

#include"disk.h"
#include<string>
#include<string.h>
using namespace std;
#pragma once
struct Path{
    Item a[];
    int ai;
    Path(){
        ai = ;
        a[].block = ;
        a[].isFile = false;
        strcpy(a[].name, "root");
    }
    void push(Item it){
        a[ai++] = it;
    }
    Item pop(){
        ){
            Item it;
            it.block = -;
            return it;
        }
        return a[--ai];
    }
    Item peek(){
        ];
    }
    string gets(){
        string s = "";
        extern string str(char*);
        ; i < ai; i++){
            s +=string(a[i].name)+"\\";
        }
        return s;
    }
    void home(){
        ai = ;
    }
};

disk.h

#pragma once
//组长块
struct Leader{
    short blockCnt;
    short nextLeader;
    ];
};
struct Dir
{
    short blockCnt;
    short itemCnt;
    ];
};
struct Item{
    ];
    short block;
    bool isFile;
    ];
};

struct Block{
    ];
};

main.cpp

#include<iostream>
#include<math.h>
#include<string>
#include<string.h>
#include"scanner.h"
#include"disk.h"
#include"path.h"
using namespace std;

const char disk[] = "disk.txt";
;
;
;
;
;
Leader super;

string replace(string src, string olds, string news){
    string ans = "";
    ; i < src.length();){
        int j;
        ; j < olds.length() && i + j < src.length(); j++){
            if (src[j + i] != olds[j]){
                break;
            }
        }
        if (j == olds.length()){
            ans += news;
            i += j;
        }
        else{
            ans += src[i];
            i++;
        }
    }
    return ans;
}
void readBlock(short x, void *block){
    FILE*file = fopen(disk, "rb");
    fseek(file, x*BlockCapacity, );
    fread(block, , file);
    fclose(file);
}
void writeBlock(short x, void* block){
    FILE *file = fopen(disk, "rb+");
    fseek(file, x*BlockCapacity, );
    fwrite(block, , file);
    fclose(file);
}

Item getItem(]){
    Dir dir; readBlock(folder, &dir);
    Item it; it.block = -;
    ;
    ; i < dir.blockCnt; i++){
        Item a[];
        readBlock(dir.data[i], a);
        ; j <  && k++ < dir.itemCnt; j++){
            ){
                it = a[j];
                return it;
            }
        }
    }
    return it;
}

]){
    Item it = getItem(x, name);
    )return false;
    else return true;
}
void format(){
    //删除旧磁盘,创建新磁盘,设置新磁盘长度
    remove(disk);
    FILE*file = fopen(disk, "w");//write会覆盖,没有文件就创建文件
    fseek(file, BlockCapacity*BlockCount - , );
    char c = 'a';
    fwrite(&c, , , file);
    fclose(file);

    //第0块
    ] = "root";
    ] = "haha";
    Block b0;
    strcpy(b0.a, userName);
    strcpy(b0.a + , password);
    b0.a[] = ;//0个用户
    writeBlock(, &b0);

    //初始化所有组长块
    int leader = BlockCount - BlocksPerGroup;
    ; leader -= BlocksPerGroup){
        Leader l;
        l.blockCnt = BlocksPerGroup - ;
        l.nextLeader = leader + BlocksPerGroup;
        ; i < l.blockCnt; i++){
            l.member[i] = i +  + leader;
        }
        writeBlock(leader, &l);
    }
    //初始化超级块
    leader += BlocksPerGroup;
    Leader super;
    super.blockCnt = leader - ;
    super.nextLeader = leader;
    ; i < super.blockCnt; i++)
        super.member[i] =  + i;
    writeBlock(, &super);

    //根目录
    Dir root;
    root.blockCnt = ;
    root.itemCnt = ;
    writeBlock(, &root);
}
int allocBlock(){
    ){
        int ans = super.nextLeader;
        ;
        readBlock(ans, &super);
        return ans;
    }
    else{
        return super.member[--super.blockCnt];
    }
}
void freeBlock(short x){
    ){
        writeBlock(x, &super);
        super.blockCnt = ;
        super.nextLeader = x;
    }
    else{
        super.member[super.blockCnt++] = x;
    }
}
void readFile(short x, char s[]){
    Dir dir;
    readBlock(x, &dir);
    ;
    ; i < dir.blockCnt; i++){
        Block data;
        readBlock(dir.data[i], &data);
        ; j < BlockCapacity&&k < dir.itemCnt; j++)
            s[k++] = data.a[j];
    }
    s[k] = ;
}
void writeFile(short x, char s[]){
    Dir dir;
    readBlock(x, &dir);
     + strlen(s);
    int blockCnt = ceil(1.0*itemCnt / BlockCapacity);
    if (blockCnt>dir.blockCnt){
        for (int i = dir.blockCnt; i < blockCnt; i++){
            dir.data[i] = allocBlock();
        }
    }
    else if (blockCnt < dir.blockCnt){
        for (int i = blockCnt; i < dir.blockCnt; i++){
            freeBlock(dir.data[i]);
        }
    }
    dir.itemCnt = itemCnt;
    dir.blockCnt = blockCnt;
    ;
    ; i < dir.blockCnt; i++){
        Block data;
        ; j < BlockCapacity&&k < dir.itemCnt; j++)
            data.a[j] = s[k++];
        writeBlock(dir.data[i], &data);
    }
    writeBlock(x, &dir);
}
int addItem(short x, const char *name, bool isFile){
    Dir dir;
    readBlock(x, &dir);
    if (exist(x, name)){
        cout << "此文件夹中已经包含同名文件或文件夹" << endl;
        ;
    }
    ;
    ){
        dir.blockCnt++;
        dir.data[dir.blockCnt - ] = allocBlock();
    }
    Item t[];
    readBlock(dir.data[dir.blockCnt - ], t);
    strcpy(t[pos].name, name);
    t[pos].isFile = isFile;
    t[pos].block = allocBlock();
    writeBlock(dir.data[dir.blockCnt - ], t);

    Dir it; it.blockCnt = ; it.itemCnt = ;
    writeBlock(t[pos].block, &it);

    dir.itemCnt++;
    writeBlock(x, &dir);
    return t[pos].block;
}
void deleteFile(short x){
    Dir dir;
    readBlock(x, &dir);
    ; i < dir.blockCnt; i++){
        freeBlock(dir.data[i]);
    }
    freeBlock(x);
}
void deleteFolder(short x){
    Dir dir;
    readBlock(x, &dir);
    ;
    ; i < dir.blockCnt; i++){
        Item a[];
        readBlock(dir.data[i], a);
        ; j <  && k++ < dir.itemCnt; j++){
            if (a[j].isFile){
                deleteFile(a[j].block);
            }
            else {
                deleteFolder(a[j].block);
            }
        }
        freeBlock(dir.data[i]);
    }
    freeBlock(x);
}
]){
    Dir dir;
    readBlock(folder, &dir);
    ;
    ; i < dir.blockCnt; i++){
        Item a[];
        readBlock(dir.data[i], a);
        ; j <  && k++ < dir.itemCnt; j++){
            ){
                if (a[j].isFile){
                    deleteFile(a[j].block);
                }
                else{
                    deleteFolder(a[j].block);
                }
                Item last[];
                readBlock(dir.data[dir.blockCnt - ], last);
                a[j] = last[(dir.itemCnt - ) % ];
                writeBlock(dir.data[i], a);
                 == ){
                    dir.blockCnt--;
                    freeBlock(dir.data[dir.blockCnt]);
                }
                dir.itemCnt--;
                writeBlock(folder, &dir);
                return;
            }
        }
    }
}
void start(){
    FILE*file = fopen(disk, "r");
    ){
        cout << "磁盘不存在,将要进行新建磁盘并格式化" << endl;
        format();
    }
    readBlock(, &super);
}
void exit(){
    writeBlock(, &super);
}
void debugGroup(){
    writeBlock(, &super);
    ;
    ; leader < BlockCount&&k++ < ;){
        Leader l;
        readBlock(leader, &l);
        cout << "leader " << leader << " " << l.blockCnt << ":";
        ; i < l.blockCnt; i++){
            cout << l.member[i] << " ";
        }
        cout << endl;
        leader = l.nextLeader;
    }
}
void ls(short x){
    Dir dir;
    readBlock(x, &dir);
    ;
    ;
    char table[len];
    ; i < len; i++)table[i] = '-';
    table[len - ] = ;
    cout << table << endl;
    ; i < dir.blockCnt; i++){
        Item a[];
        readBlock(dir.data[i], a);
        ; j <  && k++ < dir.itemCnt; j++){
            printf("| %-10s | %-10s |\n", a[j].name, a[j].isFile ? "文件" : "文件夹");
        }
    }
    cout << table << endl;
}
int filelen(FILE*file){
    fseek(file, , );
    return ftell(file);
}
//a little difficult
void tree(short x, const char*name, bool isFile,string prefix, string &treeString){
    treeString += prefix + name + "\n";
    if (isFile)return;
    string temp =replace(prefix,"┣", "┃");
    temp = replace(temp,"━", "  ");
    temp = replace(temp,"┗", "  "); // 一定要注意,一个这个符号是两个空格
    Dir dir; readBlock(x, &dir);
    ;
    ; i < dir.blockCnt; i++){
        Item a[]; readBlock(dir.data[i], a);
        ; j <  && k++ < dir.itemCnt; j++){
            if (k == dir.itemCnt)
                tree(a[j].block,a[j].name,a[j].isFile,temp + "┗━", treeString);
            else
            tree(a[j].block, a[j].name,a[j].isFile ,temp + "┣━", treeString);
        }
    }
}
][] = {
    "newfile  fileName", "创建新文件",
    "newfolder folderName ", "创建新文件夹",
    "cd 文件夹名 ", "进入子文件夹",
    "show 文件名", "显示子文件内容",
    "ls", "显示当前目录下的全部子项",
    "tree", "显示以当前目录为根的树形结构",
    "home ", "返回根目录",
    "back ", "返回上级目录",
    "del  文件名或者文件夹名 ", "删除当前目录下的文件或文件夹",
    "writefile 文件名  文本文件名", "写入文件",
    "format", "格式化磁盘",
    "exit", "退出系统",
    "help", "帮助",
    "debug", "显示磁盘使用情况",
    "#"
};
void help(){
    ];
    ; i < ; i++)table[i] = '-';
    table[] = ;
    ; strcmp(cmds[i], ){
        )printf("\t\t%s\n", table);
        printf(]);
        printf("\t\t%s\n", table);
    }
}
void run(){
    ];
    Path p;
    cout << "请您一定要注意,退出时要使用exit指令退出,不要关闭控制台" << endl;
    cout << "因为程序需要用exit()做一些善后工作" << endl;
    cout << "请使用help命令查看帮助" << endl;
    while (cout << p.gets() << ">" , gets(cmd)){
        Scanner s(cmd);
        string one = s.next();
        if (one == "")continue;
        else if (one == "format"){
            cout << "格式化将会清空全部文件及文件夹" << endl;
            cout << "您真的要清空吗?(Y/N)" << endl;
            gets(cmd); Scanner ss = Scanner(cmd);
            string c = ss.next();
            if (c == "y" || c == "Y")
                format(), start(),p.home(), cout << "格式化成功" << endl;
            else cout << "取消格式化" << endl;
            start();
        }
        else if (one == "help"){
            cout << "您可以使用以下指令" << endl;
            help();
        }
        else if (one == "ls"){
            cout << p.gets() << endl;
            ls(p.peek().block);
        }
        else if (one == "home"){
            p.home();
        }
        else if (one == "back"){
            p.pop();
        }
        else if (one == "exit"){
            break;
        }
        else if (one == "tree"){
            Item it = p.peek();
            string tr = "";
            cout << "---------------------------" << endl;
            tree(it.block, it.name, , "", tr);
            cout << "---------------------------" << endl;
            cout << tr << endl;
        }
        else if (one == "del"){
            if (s.hasNext() == false){
                cout << "命令格式错误,缺少文件名" << endl;
                continue;
            }
            string file = s.next();
            const char *name = file.c_str();
             || !exist(p.peek().block, name)){
                cout << "不存在这样的文件" << endl;
                continue;
            }
            Item it = getItem(p.peek().block, name);
            deleteItem(p.peek().block, name);
        }
        else if (one == "newfile" || one == "newfolder"){
            if (s.hasNext() == false){
                cout << "命令格式错误,缺少文件名" << endl;
                continue;
            }
            string file = s.next();
            const char *name = file.c_str();
            ){
                cout << "文件名太长" << endl;
                continue;
            }
            if (exist(p.peek().block, name)){
                cout << "已经存在名为" << file << "的文件了" << endl;
                continue;
            }
            bool isFile = (one == "newfile");
            addItem(p.peek().block, name, isFile);
        }
        else if (one == "cd"){
            if (s.hasNext() == false){
                cout << "命令格式错误" << endl;
                continue;
            }
            string son = s.next();
            const char* name = son.c_str();
             || !exist(p.peek().block, name)){
                cout << "不存在这样的文件" << endl;
                continue;
            }
            Item it = getItem(p.peek().block, name);
            if (it.isFile){
                cout << "不能进入文件,请输入文件夹" << endl;
                continue;
            }
            p.push(it);
        }
        else if (one == "show"){
            if (s.hasNext() == false){
                cout << "命令格式错误" << endl;
                continue;
            }
            string son = s.next();
            const char* name = son.c_str();
             || exist(p.peek().block, name) == ){
                cout << "不存在这样的文件" << endl;
                continue;
            }
            Item it = getItem(p.peek().block, name);
            ){
                cout << son << "不是文件,而是文件夹" << endl;
                continue;
            }
            ];
            readFile(it.block, content);
            cout << content << endl;
        }
        else if (one == "del"){
            if (s.hasNext() == false){
                cout << "命令格式错误" << endl;
                continue;
            }
            string son = s.next();
            const char*name = son.c_str();
             || exist(p.peek().block, name) == ){
                cout << "不存在这样的文件" << endl;
                continue;
            }
            Item it = getItem(p.peek().block, name);
            deleteItem(p.peek().block, it.name);
        }
        else if (one == "writefile"){
            if (s.hasNext() == false){
                cout << "命令格式错误" << endl;
                continue;
            }
            string son = s.next();
            const char*name = son.c_str();
             || exist(p.peek().block, name) == ){
                cout << "不存在这样的文件" << endl;
                continue;
            }
            Item it = getItem(p.peek().block, name);
            ){
                cout << "不能往文件夹中写内容" << endl;
                continue;
            }
            if (s.hasNext() == false){
                cout << "命令格式错误" << endl;
                continue;
            }
            string fin = s.next();
            FILE*ff = fopen(fin.c_str(), "r");
            ){
                cout << "不存在名为" << fin << "的子文件" << endl;
                continue;
            }
            int len = filelen(ff);
             - ){
                cout << fin << "文件太长" << endl;
                continue;
            }
            ];
            fseek(ff, , );
            fread(content, len, , ff);
            content[len] = ;
            writeFile(it.block, content);
            fclose(ff);
            delete content;
        }
        else if (one == "debug"){
            cout << "------------" << endl;
            debugGroup();
            cout << endl;
        }
        else {
            cout << "没有这样的命令,请输入help查看可用命令" << endl;
        }
        //这句话本来该省略,之所以加在这里是为了防止不用exit命令退出,那样会破坏系统.
        writeBlock(, &super);
    }
}
int main(){
    start();
    run();
    exit();
    ;
}

Simple File System的更多相关文章

  1. File System Implementation 文件系统设计实现

    先来扯淡吧,上一篇文章说到要补习的第二篇文章介绍文件系统的,现在就来写吧.其实这些技术都已经是很久以前的了,但是不管怎么样,是基础,慢慢来学习吧.有种直接上Spark源码的冲动.. 1. 这篇博客具体 ...

  2. HDFS分布式文件系统(The Hadoop Distributed File System)

    The Hadoop Distributed File System (HDFS) is designed to store very large data sets reliably, and to ...

  3. Low-overhead enhancement of reliability of journaled file system using solid state storage and de-duplication

    A mechanism is provided in a data processing system for reliable asynchronous solid-state device bas ...

  4. HDFS relaxes a few POSIX requirements to enable streaming access to file system data

    https://hadoop.apache.org/docs/r2.7.2/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html Introduction [ ...

  5. File System Programming---(三)

    Accessing Files and Directories Before you can open a file, you first have to locate it in the file ...

  6. File System Programming --- (二)

    File System Basics The file systems in OS X and iOS handle the persistent storage of data files, app ...

  7. About the iOS File System

    两个维度: 1)是否给用户使用: 2)是否持久存储. During installation of a new app, the installer creates a number of conta ...

  8. PatentTips – EMC Virtual File System

    BACKGROUND OF THE INVENTION 1. Field of the Invention The present invention generally relates to net ...

  9. File System Design Case Studies

    SRC=http://www.cs.rutgers.edu/~pxk/416/notes/13-fs-studies.html Paul Krzyzanowski April 24, 2014 Int ...

随机推荐

  1. linux基础-基本命令的讲解(1-7单元)

    基本命令的讲解 主要内容介绍 1.LINUX操作系统安装及初始化配置(熟悉):2.LINUX操作系统目录组成结构及文件级增删改查操作(重点):3.LINUX操作系统用户.权限管理(重点):4.开源软件 ...

  2. 非常不错的点餐系统应用ios源码完整版

    该源码是一款非常不错的点餐系统应用,应用源码齐全,运行起来非常不错,基本实现了点餐的一些常用的功能,而且界面设计地也很不错,是一个不错的ios应用学习的例子,喜欢的朋友可以下载学习看看,更多ios源码 ...

  3. SQL--实现分页查询

          在查询数据中,对于某些数据量过大,为了减少页面上单页的加载时间,我们常常会选择分页查询,分页查询有很多方法,下面主要介绍两种分页方法.   一. 通过主键来实现分页: 1.数据库背景. P ...

  4. TestNG之注解的生命周期

    有必要介绍一下TestNG注解的生命周期,先看一下官网支持的注解有 @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeGroups@AfterGro ...

  5. Visual Studio 代码折叠快捷键(摘要)

    代码编辑器的展开和折叠代码确实很方便和实用.以下是展开代码和折叠代码所用到的快捷键,很常用: Ctrl + M + O: 折叠所有方法 Ctrl + M + M: 折叠或者展开当前方法 Ctrl +  ...

  6. 拓扑排序 POJ2367Genealogical tree[topo-sort]

    ---恢复内容开始--- Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4875   A ...

  7. 基于jquery的消息提示框toastr.js

    //消息提示全局配置 toastr.options = { "closeButton": false,//是否配置关闭按钮 "debug": false,//是 ...

  8. Maven私服Nexus3.x环境构建操作记录

    Maven介绍Apache Maven是一个创新的软件项目管理和综合工具.Maven提供了一个基于项目对象模型(POM)文件的新概念来管理项目的构建,可以从一个中心资料片管理项目构建,报告和文件.Ma ...

  9. Spring的反射机制和依赖注入

    我们知道,Spring中大量使用了反射机制,那么究竟是什么地方使用了呢? spring的一大核心概念是注入, 但是,这存在的一个前提就是类是由spring管理起来的. 反射是根据className生成 ...

  10. js,onblur后下一个控件获取焦点判断、html当前活跃控件、jquery版本查看、jquery查看浏览器版本、setTimeout&setInterval

    需求: input控件在失去焦点后直接做验证,验证通不过的话,显示相应错误.但是如果失去焦点后点击的下个控件是比较特殊的控件(比如,退出系统),那么不执行验证操作,直接退出系统(防止在系统退出前,还显 ...