首页 > 其他分享 >操作系统实训_基于多级目录的文件管理系统

操作系统实训_基于多级目录的文件管理系统

时间:2024-02-21 17:13:39浏览次数:18  
标签:tmp cnt 操作系统 int ptminfo 多级 tm 实训 目录

title: 操作系统实训_基于多级目录的文件管理系统
author: fxy
date: 2022-07-07 19:18:00
tags: 实训
category: 实训
readmore: true

description: SDUT操作系统实训,基于多级目录的文件管理系统,c++

1、实训报告:github实训报告

2、代码:

//基于多级文件目录的文件管理系统
#include<bits/stdc++.h>
#include <windows.h>
#include <stdio.h>
#include <ctime>
#include <cstdio>
#define endl '\n'
using namespace std;

const int N = 1e4 + 10;
const int M = 110;
struct file{
    string name;
    vector<string> vet; //当前文件中存储的内容
    string where;//用来记录当前文件的位置
    int year,month,day,hour,minute,seconds;
    int _size;
}b[N];
struct directory{
    string name;
    vector< pair<string,int> > hold;//记录当前文件存储的文件,pair表示当前的存储是文件还是目录
    map<string,int> is_file; //当前目录下是否存在某个名字的文件
    map<string,int> is_directory; //当前目录下是否存在某个目录
    string where;//用来记录当前目录的位置
    string pre;//上一个文件,用来回到上一个目录
    int year,month,day,hour,minute,seconds;
    int _size;
}a[N];
vector< pair<string,int> > fid[N];//存储某个名字所有出现的位置,如果second = 0,表示已经被删除
map<string,int > file_directory;//找到某个文件或者目录存储的位置
int cnt;//表示每个名字对应的位置
int cnt1, cnt2;//表示存储的文件和目录的位置
int pos = 0;//表示当前所在目录的坐标
char place[N];//表示当前位置
//1表示目录,0表示文件

void manage();

//改变字符显示颜色
void COLOR_PRINT(const char* s, int color){
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
    printf(s);
    SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}


void get_systime(int tmp_stt, int tmp_cnt){ //tmp_cnt表示要进行操作的文件或目录的位置
    time_t rawtime;
    struct tm *ptminfo;
    time(&rawtime);
    ptminfo = localtime(&rawtime);
    if(tmp_stt == 1){ //为1表示要对目录时间信息进行更新
        a[tmp_cnt].year = ptminfo->tm_year + 1900;
        a[tmp_cnt].month = ptminfo->tm_mon + 1;
        a[tmp_cnt].day = ptminfo->tm_mday;
        a[tmp_cnt].hour = ptminfo->tm_hour;
        a[tmp_cnt].minute = ptminfo->tm_min;
        a[tmp_cnt].seconds = ptminfo->tm_sec;
    }
    else{ //为0表示要对文件时间信息进行更新
        b[tmp_cnt].year = ptminfo->tm_year + 1900;
        b[tmp_cnt].month = ptminfo->tm_mon + 1;
        b[tmp_cnt].day = ptminfo->tm_mday;
        b[tmp_cnt].hour = ptminfo->tm_hour;
        b[tmp_cnt].minute = ptminfo->tm_min;
        b[tmp_cnt].seconds = ptminfo->tm_sec;
    }
}

//该函数用来获取目录和文件的大小
int get_size(int id, int tmp_cnt){//tmp_cnt为目录或文件所在存储位置
    int sum = 0;
    if(id == 1){ //获取目录的大小
        sum = a[tmp_cnt].is_file.size() + a[tmp_cnt].is_directory.size();
    }
    else{ //获取文件的大小
        for(int i = 0; i < b[tmp_cnt].vet.size(); i++){
            sum += b[tmp_cnt].vet[i].size();
        }
    }
    return sum;
}

//实现目录打开功能
void open(string poss){ //poss表示要打开的目录的名字
    if(poss == ".."){ //linux命令行,cd ..表示返回上一级目录
        if(pos >= 1) pos --;
        else{
            cout <<"wrong: wrong return!" << endl;
        }
    }
    else{//判断当前目录中是否含有要打开的目录文件
        if(a[pos].is_directory.find(poss) == a[pos].is_directory.end()){
            cout <<"-bash: cd: " << poss <<" : Not a directory" << endl;
        }
        else{
            pos = a[pos].is_directory[poss];
        }
    }
    manage();
}

//该函数用来实现新建目录功能
void mkdir(string poss){ //poss表示所要新建的目录的名字
    if(a[pos].is_directory.find(poss) != a[pos].is_directory.end()){
        cout <<"warning: duplicate directories!"<< endl;
    }
    else{
        cnt1 ++;
        string tmp_pos = a[pos].where;
        tmp_pos += "/";
        tmp_pos += a[pos].name;
        if(file_directory.find(poss) == file_directory.end()){
            cnt ++;
            file_directory[poss] = cnt;
            fid[cnt].push_back({tmp_pos,1});
        }
        else{
            int x = file_directory[poss];
            fid[cnt].push_back({tmp_pos,1});
        }
        a[cnt1].name = poss;
        a[cnt1].where = tmp_pos;
        get_systime(1,cnt1); //获取新建目录时的系统时间
        a[pos].hold.push_back({poss,1});
        a[pos].is_directory[poss] = cnt1;
        a[cnt1].pre = a[pos].name;
    }
    manage();
}
//该函数用来实现新建文件功能
void touch(string name, int stt){ //name表示所要新建的文件名
    string tmp_pos = a[pos].where;
    tmp_pos += "/";
    tmp_pos += a[pos].name;
    if(file_directory.find(name) == file_directory.end()){//查找和判断
        cnt ++;
        file_directory[name] = cnt;
        fid[cnt].push_back({tmp_pos,1});
    }
    else{
        int x = file_directory[name];
        fid[x].push_back({tmp_pos,1});
    }
    if(a[pos].is_file.find(name) != a[pos].is_file.end()){
        cout <<"warning: duplicate files!" << endl;
        manage();
    }
    cnt2 ++;//更新相应数据结构
    b[cnt2].where =tmp_pos;
    b[cnt2].name = name;
    get_systime(0,cnt2); //获取新建文件时的系统时间
    a[pos].hold.push_back({name,0});
    a[pos].is_file[name] = cnt2;
    if(stt == 1)
        manage();
    else return;
}

void echo(string st){//st表示想要写到文件中的信息
    string s;
    s.clear();
    string postion;
    postion.clear();
    int i = 0;
    for(i = 1; i < st.size(); i++){
        if(st[i] == '"'){
            break;
        }
        s += st[i];
    }
    for(i = i + 2; i < st.size(); i++){
        postion += st[i];
    }
    if(a[pos].is_file.find(postion) == a[pos].is_file.end()){
            //查找所想要写入的文件是否存在,不存在进行创建文件和写入
        if(a[pos].is_directory.find(postion) == a[pos].is_directory.end()){
            touch(postion, 0);
            b[cnt2].vet.push_back(s);
            cout << b[cnt2]._size << endl;
        }
        else{
            cout <<"wrong: Illegal naming!" << endl;
        }
    }
    else{ //存在的话,直接查询文件位置,然后进行信息写入即可
        int x = a[pos].is_file[postion];
        b[x].vet.push_back(s);
    }
    manage();
}
//该函数实现正向读取文件内容
void cat(string poss){ //poss表示
    if(a[pos].is_file.find(poss) == a[pos].is_file.end()){ //如果找不到文件就报错
        cout <<"cat " << poss <<": No such file or directory" << endl;
    }
    else{//找到文件直接正向读取文件信息即可
        int x = a[pos].is_file[poss];
        for(int i = 0; i < b[x].vet.size(); i++){
            cout << b[x].vet[i] << endl;
        }
    }
    manage();
}
//该函数实现反向读取文件内容
void tac(string poss){
    if(a[pos].is_file.find(poss) == a[pos].is_file.end()){
        cout <<"cat " << poss <<": No such file or directory" << endl;
    }
    else{
        int x = a[pos].is_file[poss];
        for(int i = b[x].vet.size() - 1; i >= 0; i--){
            cout << b[x].vet[i] << endl;
        }
    }
    manage();
}

void show(string order){ //order表示展示命令
    int tmp_tot = 0;
    if(order == "all"){ //如果展示命令为all,则展示粗略包含信息
        for(int i = 0; i < a[pos].hold.size(); i++){
            string st = a[pos].hold[i].first;
            char tmp[M];
            memset(tmp, 0, sizeof tmp);
            for(int j = 0; j < st.size(); j++){
                tmp[j] = st[j];
            }
            if(a[pos].hold[i].second == 0){
                COLOR_PRINT(tmp,7);
                tmp_tot ++;
                cout <<" ";
            }
            else{
               COLOR_PRINT(tmp,1);
               tmp_tot ++;
               cout <<" ";
            }
        }
        if(tmp_tot == 0) cout <<"warning:is null";
        cout << endl;
    }
    else{ //如果为-l,则展示详细信息,包括建立时间,大小和名称
        for(int i = 0; i < a[pos].hold.size(); i++){
            string st = a[pos].hold[i].first;
            char tmp[M];
            memset(tmp, 0, sizeof tmp);
            for(int j = 0; j < st.size(); j++){
                tmp[j] = st[j];
            }
            int tmp_pos;
            if(a[pos].hold[i].second == 1){
                if(a[pos].is_directory.find(st) != a[pos].is_directory.end()) tmp_pos = a[pos].is_directory[st];
                else continue;
                if(tmp_tot == 0){ //输出目录的相关信息
                    printf("create time\t\tsize\tname\n");
                }
                printf("%02d-%02d-%02d %02d:%02d:%02d\t",a[tmp_pos].year,a[tmp_pos].month, a[tmp_pos].day,
                a[tmp_pos].hour, a[tmp_pos].minute,a[tmp_pos].seconds);
                cout << get_size(1,tmp_pos)<<'\t';
                COLOR_PRINT(tmp,7);
                tmp_tot ++;
                cout << endl;
            }
            else{ //输出文件的相关信息
               if(a[pos].is_file.find(st) != a[pos].is_file.end()) tmp_pos = a[pos].is_file[st];
               else continue;
               if(tmp_tot == 0){
                    printf("create time\t\tsize\tname\n");
                }
               printf("%02d-%02d-%02d %02d:%02d:%02d\t",b[tmp_pos].year,b[tmp_pos].month, b[tmp_pos].day,
                b[tmp_pos].hour, b[tmp_pos].minute,b[tmp_pos].seconds);
                cout <<  get_size(0,tmp_pos)<<'\t';
               COLOR_PRINT(tmp,1);
               tmp_tot ++;
                cout << endl;
            }
        }
        if(tmp_tot == 0) cout <<"warning:is null" << endl;
    }
    manage();
}

void find_where(string name){ //寻找名称为name的文件和目录
    int tmp_tot = 0;
    if(file_directory.find(name) == file_directory.end()){ //如果找不到,输出提示信息
        cout << "can't find!" << endl;
    }
    else{//找到了,遍历进行输出
        int x = file_directory[name];
        for(int i = 0; i < fid[x].size(); i++){
            if(fid[x][i].second == 1){
                cout << fid[x][i].first << endl;
                tmp_tot ++;
            }
        }
        //找不到,输出错误提示信息
        if(tmp_tot == 0) cout <<"can't find!" << endl;
    }
    manage();
}

void manage(){
    string order1,order2;
    COLOR_PRINT("/home/user@LAPTOP-MFH2B9V0:",2);
    string tmp_st = a[pos].where;
    tmp_st +="/";
    tmp_st += a[pos].name;
    memset(place, 0, sizeof place);
    for(int i = 0; i < tmp_st.size(); i++){
        place[i] = tmp_st[i];
    }
    COLOR_PRINT(place,1);
    cout << "$ ";
    cin >> order1 >> order2;
    if(order1 == "mkdir"){ //新建目录指令
        mkdir(order2);
    }
    else if(order1 == "touch"){//新建文件指令
        touch(order2, 1);
    }
    else if(order1 == "cd"){//打开目录/返回上一级目录
        open(order2);
    }
    else if(order1 == "echo"){//写入信息到文件中去
        echo(order2);
    }
    else if(order1 == "cat"){//正向读取文件
        cat(order2);
    }
    else if(order1 == "tac"){//反向读取文件
        tac(order2);
    }
    else if(order1 == "ls"){//展示目录中所含的文件、子目录信息
        show(order2);
    }
    else if(order1 == "find"){//按名查找
        find_where(order2);
    }
    else{
        cout <<"command '" << order1 <<"' not found" << endl; //指令不存在报错
    }
    manage();
}

signed main(){
    //一些初始化操作
    cout <<"Welcome to OS_work Page (Design Based on Linux)" << endl;
    cout <<endl;
    a[0].name = "user";
    file_directory["~/home"] = 0;
    a[0].where = "~/home";
    place[0] = '~';
    manage();
    return 0;
}

标签:tmp,cnt,操作系统,int,ptminfo,多级,tm,实训,目录
From: https://www.cnblogs.com/N-lim/p/18025696

相关文章

  • Cobbler 无人值守安装操作系统
    术语PXE(prebootexecuteenvironment,预启动执行环境)是由Intel公司设计的协议,它可以使计算机通过网络启动。批量装机软件介绍Redhat系主要有两种Kickstart和Cobbler。Kickstart是一种无人值守的安装方式。它的工作原理是在安装过程中记录人工干预填写的各种参数,并生成一......
  • 操作系统和应用的关系
    操作系统与应用程序的关系是什么全网内容智能分析根据全网内容智能分析,为你整理以下内容:操作系统与应用程序的关系是:操作系统是应用程序运行的平台。操作系统负责管理计算机硬件和软件资源,为应用程序提供运行环境和支持。应用程序基于操作系统上具有特定功能的程序,程序中所......
  • 统信操作系统下数据库管理利器
    PL/SQL是一款荷兰公司开发的数据库管理软件,尽管只支持Oracle一种数据库,但是在这一种数据库的支持上深度耕耘了30年,做到了Oracle管理的极致,从而拥有量海量的用户。当然,随着时间的推移,PL/SQL也出现了一些不足:1.不支持Linux原生平台,更不支持ARM芯片;2.必须安装Oracle客户端;3.仅仅支持O......
  • 面试官:如何实现多级缓存?
    对于高并发系统来说,有三个重要的机制来保障其高效运行,它们分别是:缓存、限流和熔断。而缓存是排在最前面也是高并发系统之所以高效运行的关键手段,那么问题来了:缓存只使用Redis就够了吗?1.冗余设计理念当然不是,不要把所有鸡蛋放到一个篮子里,成熟的系统在关键功能实现时一定会考......
  • 第九章——操作系统和应用的关系
    操作系统和应用的关系——程序员是通过利用操作系统提供的功能来编写应用的。操控系统的原型就是具有加载和运行功能的监控程序,后来基本输入输出的程序也被追加到了监控系统中,这就是初期的操作系统。操作系统本身不是单独的程序而是多个程序的集合体。操作系统的诞生减轻了程序员......
  • 视频生成领域的发展概述:从多级扩散到LLM
    2023年是语言模型(llm)和图像生成技术激增的一年,但是视频生成受到的关注相对较少。今年刚到2月份,OpenAI就发布了一个惊人的视频生成模型Sora。虽然它的架构没有披露,但是通过总结现有的视频生成领域可能能对Sora的构架有所理解。在这篇文章中,我们将整理视频生成在最近几年是发展......
  • 第九章:操作系统和应用的关系
    在《程序是怎样跑起来》的第九章中,作者深入探讨了操作系统和应用程序之间的关系。这一章节对于理解计算机系统中软件层次结构以及它们如何相互作用至关重要。以下是我对这一章节的读后感:首先,本章可能从操作系统的基本概念开始,介绍了它是如何作为计算机系统的核心软件,管理硬件资源......
  • 30天自制操作系统
    01_daysCPU其实只算是个集成电路板,它只能忠实地执行电信号给它的指令,输出相应的电信号。也就是说,其实CPU只是个乖乖执行命令的笨蛋。人们发现把二进制的01与电信号的开关对应起来,CPU就从一个处理电信号的机器摇身一变为一个神奇的二进制数计算机。给文字进行编码,就把文字也转换......
  • day04_操作系统入门
    今日笔记学操作系统基础概念linux系统linux系统(centos)+vmware安装起来(网络配置,磁盘分区)ubuntu安装xshell服务器的远程连接服务器网站的前后端,数据库app的前后端,数据库微信、腾讯微信的服务器移动端设备上,安装的微信客户端在线笔记笔记对运维来说,就是一个宝藏,mar......
  • [转帖]Unix操作系统的前世今生
    Unix是一种多用户、多任务操作系统,最初由AT&T贝尔实验室的肯·汤普逊(KenThompson)和丹尼斯·里奇(DennisRitchie)等人开发于上世纪70年代初。它被设计成一种通用的操作系统,支持跨多种硬件平台,并提供了许多先进的特性,如多任务处理、分时处理、多用户能力和可移植性。Unix的......