1. 程序演示
home目录下包含如下文件
其中root文件夹内存有account.txt,用于存储用户登录信息,在虚拟磁盘初始化时将之读入用户组结构并用于验证登录。 运行程序:
1. 登录–login
运行程序后只能先登录才能进行操作,不然会系统提示进行登录,输入help可以查看命令帮助文件。
2. 切换目录— cd
3. 展示文件列表—ls
对应本地真实目录可知,初始化成功,目录树建立成功,且各个文件/文件夹的大小、路径、文件类型、读写权限、所属用户都初始化成功!
4. 查看物理块使用情况以及物理块存储内容 — df
df命令中展示了已占用物理块的存储内容: (1)complete file of xxx : 表明该物理块存储了一个完整的文件 (2)parts of xxx: 表明该物理块存储了一个文件的部分内容 (3)IndexBlock of xxx: 表明该物理块为索引物理块 可见初始化时占用物理块为462块,大约占用了虚拟磁盘462KB的空间,由于在该虚拟磁盘中文件夹都单独占用一个索引物理块,且所有的索引物理块不存储具体文件内容,故其空间开支稍大于实际空间开支,可由比较本地实际目录大小(436KB)可证明。
5. cd命令的延伸 :
(1)cd ./ :返回当前目录 (2)cd …/ :返回上一级目录 (3)cd home :返回用户根目录
6. 查看当前目录名和父目录名 —— now
7. 创建文件—— touch
同时在本地文件中也进行了创建文件的操作
8. 打开文件 —— open
9. 查看当前活动文件 —— ASL
若输入的文件名不正确会有相应提示。
10. 写文件操作 —— write
(1) write -a 追加写入文件 (2) write -c 覆盖写入文件 若是没有将文件打开就要写入则会提示先将文件打开; 成功写入。
注意在写入文件后,文件的大小以及所属父目录的大小都得到更新。
11. 读文件的全部内容 —— cat
若没有将文件打开就进行读操作则会进行提示。
下面读取inode.txt为例,由于文件较长,只截取开头和结尾部分(inode.txt为编写该项目的过程中留下的一份备份txt文件):
12. head -num 读取文件头num行
13. tail -num 读取头文件num行
14. 关闭文件 —— close
15. 递归查找文件 —— find
16. 复制文件 —— cp
本地文件夹同步实现了复制操作 而且物理块也实现了相应的变化,增加了新文件占用的物理块:
17. 移动文件 —— move
18. 创建文件夹 —— mkdir
19. 删除文件夹 —— rm -rf
在新建的OSmenu文件夹中新建两个文件(一个创建,一个通过复制创建)后进行删除 本地文件夹中也同步了操作
下面进行删除,可以将文件夹以及其中的文件都删掉 文件夹也同步了该操作
20. 删除文件 —— rm -f
本地文件夹也同步了相应操作
2. 代码部分
2.1 本项目使用的结构体
代码中使用到的结构体信息如下:
2.1.1 用户结构
用户结构体中包含用户的用户名和密码,以及该用户的个人根目录。
`typedef struct User_Type { string username;//用户名 string password;//密码 struct FSct_Type *userFile;//指向该用户的"根"目录 }User;
2.1.2 文件控制块
该文件控制块结构在文件结构中的集合构成了文件目录。
typedef struct FCB{ User *Owner[12];//指向用户数据结构User的指针 int innerNum; //内部号 string fileName; //文件名 string type; // 文件类型 double length; //文件长度 long int authority; //读写权限 int phNum; //物理块号 struct PHB_Type *firstBlock; //首物理块 }FCB;
2.1.3 文件结构
文件结构中包含了以下信息: (1)文件的名称; (2)文件的路径; (3)文件的所属用户组(考虑到可以实现文件共享); (4)以及该目录下的文件目录表(由12个文件控制块组成,即限制了每个文件夹最多放置12个文件); (5)文件内容(由于文件类型较多,此处的文件内容暂时用”parts of xxx”等来替代); (6)子文件/子目录指针(与文件控制块fcb相对应,共12个); (7)父目录指针(只有一个);
typedef struct FSct_Type{ string fileName; string filePath; User *Owner[12]; //用户账号 FCB *fcb[12]; //文件目录 char content[50]; //文件内容 struct FSct_Type *child[12]; //指针数组,指向二级文件结构 struct FSct_Type *pre; //指针,指向父目录 }FSct;
2.1.4 物理块结构
物理块结构包含了以下信息: (1)物理块号; (2)每个物理块的大小:该项目规定每个物理块的大小为1024B,即1KB; (3)物理块状态标识符,空闲状态为0,占用状态为0; (4)物理块索引表:当所存储的文件大小大于1KB时,其首物理块存储一个物理块索引表; (5)索引指针:当所存储的文件大小大于1KB时,指向其他实际存储文件信息的物理块; (8)物理块所存内容:由于文件类型较多,并不一定都可按字符读出保存到虚拟磁盘,此处的文件内容暂时用”parts/complete/index of xxx”等来替代。
typedef struct PHB_Type{ long int phNum; //物理块号 long int chunckSize = 1024; //每个块大小为1024B 即1KB int state = 0; //物理块状态标识符 long int indexList[300]; //该块所保存的索引表 struct PHB_Type *child[300]; //索引表指向的块 string content;//该块所保存的内容 }PHB;
2.1.5 活动符号名表
活动符号名表用于存储打开文件的信息,该结构包含以下信息: (1)文件名; (2)对应文件控制块的内部号; (3)文件路径(便于区分不同目录下的同名文件); (4)指向活动文件表的指针。
typedef struct Active_symbol_list{ string fileName; int innerNum;//内部号 string path;//文件路径 struct Active_file_list *afl; struct Active_symbol_list *next; }ASL;
2.1.6活动文件表 活动文件表用于存储文件目录的及本条目,即文件控制块。
typedef struct Active_file_list{ FCB *fcb;//由基本文件目录项(FCB块)组成的数组 struct Active_file_list *next; }AFL;
2.2 完整代码
#include <pthread.h>
#define NUM_THREADS 1
#include <iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<iomanip>
#include <malloc.h>
#include <vector>
#include <io.h>
#include <Windows.h>
#include <sys\stat.h>
using namespace std;
#define BUFFER_SIZE
string homePath("C:\\Users\\SeanWayen\\Desktop\\home");
string path("C:\\Users\\SeanWayen\\Desktop\\home");
string tmpPath = homePath;
typedef struct User_Type
{
string username;
string password;
struct FSct_Type *userFile;
}User;
typedef struct FCB{
User *Owner[12];
int innerNum;
string fileName;
string type;
double length;
long int authority;
int phNum;
struct PHB_Type *firstBlock;
}FCB;
typedef struct FSct_Type{
int i;
string fileName;
string filePath;
User *Owner[12];
FCB *fcb[12];
char content[50];
struct FSct_Type *child[12];
struct FSct_Type *parent;
struct FSct_Type *pre;
}FSct;
typedef struct PHB_Type{
long int phNum;
long int chunckSize = 1024;
int state = 0;
long int indexList[300];
struct PHB_Type *child[30];
string content;
}PHB;
typedef struct Active_symbol_list{
string fileName;
int innerNum;
string path;
struct Active_file_list *afl;
struct Active_symbol_list *next;
}ASL;
typedef struct Active_file_list{
FCB *fcb;
struct Active_file_list *next;
}AFL;
PHB phBlock[1024*1024];
User uGroup[12];
FSct *UserFile[12];
FSct *home = new FSct;
string userPath[12];
ASL *Shead = new ASL;
AFL *Fhead = new AFL;
int FileNum_Curr = 0;
void help(){
cout <<endl;
cout << "*******************欢迎使用多级文件系统*******************" << endl<<endl;
cout << " 命令 说明 " << endl;
cout << " login 登录 " << endl;
cout << " cd 更改当前目录 " << endl;
cout << " ls 展示文件列表 " << endl;
cout << " touch 创建文件 " << endl;
cout << " rm -f 删除文件 " << endl;
cout << " mkdir 创建目录 " << endl;
cout << " rm -rf 递归地删除目录及其文件 " << endl;
cout << " open 打开文件 " << endl;
cout << " close 关闭文件 " << endl;
cout << " cat 读文件到控制台 " << endl;
cout << " tail -num 显示文件尾num行 " << endl;
cout << " head -num 显示文件头num行 " << endl;
cout << " write -c 写入文件(覆盖) " << endl;
cout << " write -a 写入文件(追加) " << endl;
cout << " find 递归地查找文件 " << endl;
cout << " cp 复制文件 " << endl;
cout << " move 移动文件 " << endl;
cout << " export 导出文件 " << endl;
cout << " import 导入文件 " << endl<<endl;
cout << "*******************欢迎使用多级文件系统*******************" << endl;
cout <<endl;
}
void check_uGroup(){
cout<<"——————————" <<endl<<"查看用户组"<<endl;
for(int i=0; i<sizeof(uGroup); i++){
if(uGroup[i].username.length()!=0){
cout<<"uGroup["<<i<<"]:"<<endl;
cout<<"username:"<<uGroup[i].username<<endl;
cout<<"password:"<<uGroup[i].password<<endl;
}else{
break;
}
}
cout<<"——————————" <<endl;
}
void check_phBlock(){
cout<<"——————————" <<endl<<"查看物理块使用情况"<<endl;
double Use = 0.0;
double NotUse = 0.0;
double UsingRate;
cout<<"已被使用的物理块号:"<<endl;
for(long int i=0; i<1024*1024; i++){
if(phBlock[i].state==1){
cout<<i<<"|";
Use++;
}else{
NotUse++;
}
}
UsingRate = Use/(Use+NotUse);
cout<<endl<<"已使用物理块数目:"<<Use<<endl;
cout<<"空闲物理块数目:"<<NotUse<<endl;
cout<<"物理块总数:"<<Use+NotUse<<endl;
cout<<"磁盘利用率:"<< UsingRate<<endl;
cout<<"——————————" <<endl;
}
int account(string path){
ifstream in(path);
string line;
int i=0;
if(in)
while (getline (in, line)){
string info = line;
string ID = info.substr(0,info.find_first_of(","));
string PW = info.substr(info.find_first_of(",")+1,info.length());
uGroup[i].username = ID;
uGroup[i].password = PW;
i++;
}
else
cout <<"no such file" << endl;
in.close();
return i;
}
int index(){
int num = -1;
int flag= 0;
while(num == -1){
cout<<"localhost: " ;
string tmpOrder;
cin>>tmpOrder;
if(tmpOrder == "login"){
cout<<"user: ";
string ID;
cin>>ID;
cout<<"passwords: ";
string PW;
cin>>PW;
for(int i=0;i<sizeof(uGroup);i++){
if(uGroup[i].username == ID && uGroup[i].password == PW){
num = i;
flag =1 ;
break;
}
}
}
else if(tmpOrder == "help"){
help();
}
else{
cout<<"please login first !"<<endl;
}
}
if(flag == 0){
cout<<"No such account!"<<endl;
return -1;
}
else if(flag == 1){
cout<<"Login succed !"<<endl;
return num;
}
}
int REGISTER()
{
cout << "请输入用户名:";
string userName;
cin >> userName;
cout << "请输入密码:";
string passWord;
cin >> passWord;
for(int i=0; i<sizeof(uGroup); i++){
if (uGroup[i].username == userName)
{
cout<<i<<endl;
cout<< uGroup[i].username<<endl;
cout << "注册失败,该用户名已存在" << endl;
break;
}
if(uGroup[i].username.length()==0){
uGroup[i].username = userName;
uGroup[i].password = passWord;
cout << "注册成功" << endl;
home->fcb[i] = new FCB;
home->fcb[i]->innerNum = i;
home->fcb[i]->fileName = uGroup[i].username;
home->fcb[i]->authority = 666;
home->fcb[i]->type = "dir";
home->fcb[i]->phNum = i;
home->fcb[i]->firstBlock = new PHB;
home->fcb[i]->firstBlock->state = 1;
home->fcb[i]->firstBlock->phNum = i;
phBlock[i]. state = 1;
phBlock[i].content = "用户根目录"+i;
home->child[i] = new FSct;
home->child[i]->fileName = uGroup[i].username;
home->child[i]->Owner[0] = new User;
home->child[i]->Owner[0]->username=uGroup[i].username;
break;
}
}
return 1;
}
double get_length(string filePath){
double sizeKB = 0;
ifstream fin( filePath );
if( fin.is_open() )
{
fin.seekg( 0, ios::end );
double size = fin.tellg();
if(size == 0){
sizeKB = 0;
}else{
sizeKB = size/1024;
}
fin.close();
}
return sizeKB;
}
double getDirSize(string path)
{
intptr_t hFile = 0;
vector<string> files;
struct _finddata_t fileinfo;
string p;
double filesize;
int i = 0;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0){
filesize +=getDirSize(p.assign(path).append("\\").append(fileinfo.name));
}
}
else
{
filesize += get_length(path+"\\"+fileinfo.name);
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
return filesize;
}
void Give_Length(){
string path("C:\\Users\\SeanWayen\\Desktop\\home");
intptr_t hFile = 0;
struct _finddata_t fileinfo;
string p;
int i=0;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
string filePath(path+"\\"+fileinfo.name);
if ((fileinfo.attrib & _A_SUBDIR))
{
double sizeKB = getDirSize(filePath);
home->fcb[i]->length = sizeKB;
i++ ;
}
else
{
double sizeKB = get_length(filePath);
home->fcb[i]->length = sizeKB;
i++ ;
}
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
void ls_alh(){
int j=0;
while(home->fcb[j]){
cout<<home->fcb[j]->type<<'\t'<<home->fcb[j]->authority<<"\t"<<home->fcb[j]->fileName<<"\t";
if(home->fcb[j]->length < 512 && home->fcb[j]->length > 0.01){
cout<<setprecision(3)<< home->fcb[j]->length<<"KB"<<"\t";
}else if(home->fcb[j]->length > 512){
cout<<setprecision(3)<< (home->fcb[j]->length)/1024 <<"MB"<<"\t";
}else if(home->fcb[j]->length < 0.01){
cout<<setprecision(3)<< (home->fcb[j]->length)*1024 <<"B"<<"\t";
}
cout<<home->fcb[j]->fileName<<'\t'<<endl;
j++;
}
}
string ID_path = "C:\\Users\\SeanWayen\\Desktop\\home\\root\\account.txt";
void Initial(string path){
int userNum = account(ID_path);
Shead->next = NULL;
Fhead->next =NULL;
vector<string> fileNames;
cout<<"初始化... "<<endl;
intptr_t hFile = 0;
int key = 0;
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0){
userPath[key]=(path+"\\"+fileinfo.name);
key++;
for(int i=0; i<sizeof(uGroup); i++){
if(uGroup[i].username == fileinfo.name){
home->fcb[i] = new FCB;
home->fcb[i]->innerNum = i;
home->fcb[i]->fileName = uGroup[i].username;
home->fcb[i]->authority = 666;
if ((fileinfo.attrib & _A_SUBDIR)){
home->fcb[i]->type = "dir";
}else{
home->fcb[i]->type ="—";
}
home->fcb[i]->phNum = i;
home->fcb[i]->Owner[0]=new User;
home->fcb[i]->Owner[0]->username=uGroup[i].username;
home->fcb[i]->firstBlock = new PHB;
home->fcb[i]->firstBlock->state = 1;
home->fcb[i]->firstBlock->phNum = i;
phBlock[i]. state = 1;
phBlock[i].content = ("User Directory: "+home->fcb[i]->fileName);
home->child[i] = new FSct;
home->child[i]->fileName = fileinfo.name;
home->child[i]->filePath = (path+"\\"+fileinfo.name);
home->child[i]->Owner[0] = new User;
home->child[i]->Owner[0]->username=uGroup[i].username;
break;
}
}
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
Give_Length();
}
void Give_Length_Sec_ByTree(string path,FSct *tmpFile){
intptr_t hFile = 0;
struct _finddata_t fileinfo;
string p;
int i=0;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
string filePath(path+"\\"+fileinfo.name);
if ((fileinfo.attrib & _A_SUBDIR))
{
double sizeKB = getDirSize(filePath);
tmpFile->fcb[i]->length = sizeKB;
i++ ;
}
else
{
double sizeKB = get_length(filePath);
tmpFile->fcb[i]->length = sizeKB;
i++ ;
}
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
void setBlock_ByTree(FSct *tmpFile){
for(int i = 0; i<12;i++){
if(tmpFile->fcb[i] != NULL){
if(tmpFile->fcb[i]->type == "—"){
if(tmpFile->fcb[i]->length < 1)
{
tmpFile->fcb[i]->firstBlock->content = ("complete file of "+tmpFile->fcb[i]->fileName);
long int phNum = tmpFile->fcb[i]->firstBlock->phNum;
string tmpContent = ("complete of "+tmpFile->fcb[i]->fileName);
phBlock[phNum].content =tmpContent;
}
else if(tmpFile->fcb[i]->length > 1)
{
tmpFile->fcb[i]->firstBlock->content = ("IndexBlock of "+tmpFile->fcb[i]->fileName);
long int phNum = tmpFile->fcb[i]->firstBlock->phNum;
phBlock[phNum].content = ("IndexBlock of "+tmpFile->fcb[i]->fileName);
int count = tmpFile->fcb[i]->length/1.0;
if(tmpFile->fcb[i]->length > count+0.5) count++;
int tmpNum = 0;
for(long int num=0;num < 1024*1024; num++){
if(phBlock[num].state==0 && tmpNum<=count){
phBlock[num].state = 1;
string tmpContent = ("Parts file of "+tmpFile->fcb[i]->fileName);
phBlock[num].content = tmpContent;
int first = tmpFile->fcb[i]->firstBlock->phNum;
phBlock[first].indexList[tmpNum] = num;
tmpNum++;
}
if(tmpNum == count) break;
}
}
}
else if(tmpFile->fcb[i]->type == "dir") {
tmpFile->fcb[i]->firstBlock->content = ("DirBlock of "+tmpFile->fcb[i]->fileName);
long int phNum = tmpFile->fcb[i]->firstBlock->phNum;
string tmpContent = ("DirBlock of"+tmpFile->fcb[i]->fileName);
phBlock[phNum].content =tmpContent;
}
}
}
}
void Initial_Sec_ByTree(string path, FSct *TMPFILE, FSct *parentFile, int key){
vector<string> fileNames;
intptr_t hFile = 0;
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0){
for(int i=0; i<12; i++){
if(TMPFILE->fcb[i] == NULL){
FSct *tmpFile = new FSct;
tmpFile = TMPFILE;
tmpFile->child[i] = new FSct;
tmpFile->child[i]->fileName = fileinfo.name;
tmpFile->child[i]->filePath = (path+"\\"+tmpFile->child[i]->fileName);
tmpFile->fcb[i] = new FCB;
tmpFile->fcb[i]->innerNum = i;
tmpFile->fcb[i]->fileName = fileinfo.name;
tmpFile->fcb[i]->authority = 666;
if ((fileinfo.attrib & _A_SUBDIR)){
tmpFile->fcb[i]->type = "dir";
}else{
tmpFile->fcb[i]->type ="—";
}
for(long int num=0;num<1024*1024;num++){
if(phBlock[num].state == 0 ){
tmpFile->fcb[i]->phNum = num;
phBlock[num].state = 1;
tmpFile->fcb[i]->firstBlock = new PHB;
tmpFile->fcb[i]->firstBlock->state = 1;
tmpFile->fcb[i]->firstBlock->phNum = num;
break;
}
}
tmpFile->fcb[i]->Owner[0]=new User;
tmpFile->fcb[i]->Owner[0]->username = uGroup[key].username;
if(tmpFile->fcb[i]->type == "dir" ){
FSct *newFile = new FSct;
newFile = tmpFile->child[i];
Initial_Sec_ByTree((path+"\\"+fileinfo.name),newFile,tmpFile,key);
}
break;
}
}
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
Give_Length_Sec_ByTree(path,TMPFILE);
setBlock_ByTree(TMPFILE);
}
void ls_alh_ByTree(FSct *tmpFile){
for(int j=0;j<12;j++){
if(tmpFile->fcb[j]){
cout<<tmpFile->fcb[j]->type<<'\t'<<tmpFile->fcb[j]->authority<<"\t"<<tmpFile->fcb[j]->Owner[0]->username<<"\t";
if(tmpFile->fcb[j]->length < 512 && tmpFile->fcb[j]->length > 0.01){
cout<<setprecision(3)<< tmpFile->fcb[j]->length<<"KB"<<"\t";
}else if(tmpFile->fcb[j]->length > 512){
cout<<setprecision(3)<< (tmpFile->fcb[j]->length)/1024 <<"MB"<<"\t";
}else if(tmpFile->fcb[j]->length < 0.01){
cout<<setprecision(3)<< (tmpFile->fcb[j]->length)*1024 <<"B"<<"\t";
}
cout<<tmpFile->fcb[j]->fileName<<'\t'<<tmpFile->child[j]->filePath<<endl;
}
}
cout<<"————————————————————————————————————"<<endl;
}
void setParent_ByTree(FSct *tmpPath){
for(int i=0;i<12;i++){
if(tmpPath->fcb[i]){
if(tmpPath->fcb[i]->type == "dir"){
setParent_ByTree(tmpPath->child[i]);
}
tmpPath->child[i]->pre=tmpPath;
}
}
}
void setParent(){
for(int i=0;i<12;i++){
if(home->fcb[i]){
home->child[i]->pre=home;
}
}
}
void Initial_Sec_All(){
cout<<"构建目录结构树并分配物理块..."<<endl;
for(int key=0;key<12;key++){
if(!userPath[key].empty()){
FSct *tmpFile = new FSct;
tmpFile = home->child[key];
Initial_Sec_ByTree(userPath[key],tmpFile,home,key);
}
}
cout<<"初始化成功!请先登录!输入help可查看命令帮助文本..."<<endl;
}
void check_Block_Content(){
for(long int i=0;i<1024*1024; i++){
if(phBlock[i].state == 1){
cout<<"物理块"<<i<<"存储的内容:"<<phBlock[i].content<<endl;
}
}
}
void walkAll(FSct *tmpFile){
cout<<"*****************************************************************"<<endl;
for(int i=0;i<12;i++){
if(tmpFile->fcb[i]){
ls_alh_ByTree(home->child[i]);
}
}
cout<<"*****************************************************************"<<endl;
cout<<"*****************************************************************"<<endl;
}
void create_txt(string path){
ofstream File;
char *tmp = &path[0];
File.open(tmp);
File.close();
}
void rm_f(string realFilePath,FSct *tmpF,int i){
char *savePath = &realFilePath[0];
if(remove(savePath)==0){
cout<<"删除成功"<<endl;
}else {
cout<<"删除失败"<<endl;
}
int first = tmpF->fcb[i]->firstBlock->phNum;
if(phBlock[first].indexList[0]){
for(int k=0;k<300;k++){
if( phBlock[first].indexList[k] ){
phBlock[phBlock[first].indexList[k]].state = 0;
phBlock[phBlock[first].indexList[k]].content = "";
}
}
phBlock[first].state = 0;
phBlock[first].content = "";
free(tmpF->fcb[i] );
tmpF->fcb[i] = NULL;
free(tmpF->child[i]);
tmpF->child[i] = NULL;
}
else{
phBlock[first].state = 0;
phBlock[first].content = "";
free(tmpF->fcb[i] );
tmpF->fcb[i] = NULL;
free(tmpF->child[i]);
tmpF->child[i] = NULL;
}
}
void rm_rf(string realFilePath,FSct *tmpF,int j) {
FSct *Origin = tmpF;
for(int i=0;i<12;i++){
if(!tmpF->child[j]->fcb[i]) {
continue;
}else{
}
if(tmpF->child[j]->child[i] && tmpF->child[j]->fcb[i]->type == "—" )
{
string newFilePath = tmpF->child[j]->child[i]->filePath;
rm_f(newFilePath,tmpF->child[j],i);
}
else if(tmpF->child[j]->child[i] && tmpF->child[j]->fcb[i]->type == "dir" )
{
string newFilePath = tmpF->child[j]->child[i]->filePath;
rm_rf(newFilePath,tmpF->child[j],i);
int first = tmpF->child[j]->fcb[i]->firstBlock->phNum;
phBlock[first].state = 0;
phBlock[first].content = "";
free(tmpF->child[j]->fcb[i] );
tmpF->child[j]->fcb[i] = NULL;
free(tmpF->child[j]->child[i]);
tmpF->child[j]->child[i] = NULL;
bool flag = RemoveDirectory(newFilePath.c_str());
}
}
string dirPath = Origin->child[j]->filePath;
int first = Origin->fcb[j]->firstBlock->phNum;
phBlock[first].state = 0;
phBlock[first].content = "";
free(Origin->fcb[j] );
Origin->fcb[j] = NULL;
free(Origin->child[j]);
Origin->child[j] = NULL;
bool flag = RemoveDirectory(dirPath.c_str());
}
int find(string path,string fileName,string username)
{
vector<string> fileNames;
intptr_t hFile = 0;
struct _finddata_t fileinfo;
string p;
int flag = 0;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
if ((fileinfo.attrib & _A_SUBDIR))
{
if(fileName == fileinfo.name){
string realPath = path+"\\"+fileinfo.name+"\\";
cout<<fileinfo.name<<"\\:\t"<<realPath<<endl;
flag=1;
}
else{
string newPath = path+"\\"+fileinfo.name;
int num = find(newPath,fileName,username);
flag = num;
}
}
else{
if(fileName == fileinfo.name){
string realPath = path+"\\"+fileinfo.name;
cout<<fileinfo.name<<":"<<realPath<<endl;
flag=1;
}
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
return flag;
}
int getlineNum(string path){
ifstream in(path);
string line;
int i=0;
if(in)
while (getline (in, line)) i++;
else
cout <<"no such file" << endl;
in.close();
return i;
}
int cat(string path){
SetConsoleOutputCP(65001);
ifstream in(path);
string line;
int i=0;
if(in)
while (getline (in, line)){
i++;
cout<< line <<endl;
}
else
cout <<"no such file" << endl;
in.close();
SetConsoleOutputCP(936);
return i;
}
int head(string path,int num){
SetConsoleOutputCP(65001);
ifstream in(path);
string line;
int i=0;
if(in)
while (getline (in, line)){
i++;
cout<< line <<endl;
if(i>num){
break;
}
}
else
cout <<"no such file" << endl;
in.close();
SetConsoleOutputCP(936);
return i;
}
int tail(string path,int num){
SetConsoleOutputCP(65001);
ifstream in(path);
string line;
int i=0;
if(in)
while (getline (in, line)){
i++;
if(i>=num){
cout<< line <<endl;
}
}
else
cout <<"no such file" << endl;
in.close();
SetConsoleOutputCP(936);
return i;
}
FSct * search(FSct *find,string dst_real_dir){
FSct *result;
for(int i=0;i<12;i++){
if(!find->fcb[i] || find->fcb[i]->type == "—") continue;
else if(find->child[i]->filePath == dst_real_dir && find->fcb[i]->type == "dir")
{
result = find->child[i];
break;
}
else if(find->child[i]->filePath != dst_real_dir && find->fcb[i]->type == "dir")
{
result = search(find->child[i],dst_real_dir);
}
}
return result;
}
string deal_path(string dst_path,string f){
int last = dst_path.find_last_of("\\");
int first = dst_path.find_first_of("\\");
string dst_dir = dst_path.substr(first,last-first);
string dst_fileName = dst_path.substr(last+1,dst_path.length());
string dst = f+dst_dir;
return dst;
}
void copy(string source,string destination){
char *src = &source[0];
char *dst = &destination[0];
CopyFile(src,dst,FALSE);
}
void cover(string path){
ofstream ofs;
cout<<"请输入写入内容并以#end结束:"<<endl;
ofs.open(path,ios::out);
string content;
while(1){
cin>>content;
if(content != "#end")
ofs<<content<<" ";
else break;
}
ofs.close();
}
void app(string path){
ofstream ofs;
cout<<"请输入写入内容并以#end结束:"<<endl;
ofs.open(path,ios::app);
string content;
ofs<<endl;
while(1){
cin>>content;
if(content != "#end")
ofs<<content<<" ";
else break;
}
ofs.close();
}
void cd(User u,FSct *f){
string username = u.username;
string tmpPath = f->fileName;
FSct *tmpF;
FSct *preF;
tmpF = f;
preF = f;
string prePath = tmpPath;
if(f->fcb[0]){
string f_path = f->child[0]->filePath.substr(0,f->child[0]->filePath.find_last_of("\\"));
f->filePath = f_path;
}
while(1){
cout<<"localhost@"<<tmpPath<<":";
string tmpOrder;
cin>> tmpOrder;
if(tmpOrder == "cd"){
string newPath;
cin>>newPath;
if(newPath == "home"){
tmpPath = f->fileName;
tmpF = f;
preF = f;
}
else if(newPath == "../"){
if(prePath.empty()){
cout<<"No such file !"<<endl;
}else{
tmpPath = prePath;
int num = prePath.find_last_of("\\");
prePath = prePath.substr(0,num);
string old_path = tmpF->filePath;
string tf = old_path.substr(0,old_path.find_last_of("\\"));
if(preF==f){
tmpF = f;
}else{
tmpF = search(f,tf);
}
string pf = tf.substr(0,tf.find_last_of("\\"));
int judge = 0;
for(int i=0;i<12;i++){
if(!f->fcb[i]) continue;
else if(tmpF->fileName == f->child[i]->fileName){
preF = f;
judge = 1;
}
}
if(judge == 0)
preF = search(f,pf);
if(preF == NULL || tmpF == f){
preF = f;
}
}
}
else if(newPath == "./"){ }
else{
int flag = 0;
for(int i=0;i<12;i++){
if(tmpF->fcb[i] && tmpF->fcb[i]->fileName == newPath &&tmpF->fcb[i]->type == "dir"){
flag = 1;
prePath = tmpPath;
tmpPath += "\\"+newPath;
if(tmpF->child[i]->child[1]){
FSct *sec = tmpF->child[i]->child[1];
preF = tmpF;
FSct *t = tmpF->child[i];
tmpF = t;
tmpF->pre = preF;
tmpF->child[1] = sec;
}else{
preF = tmpF;
FSct *t = tmpF->child[i];
tmpF = t;
tmpF->pre = preF;
}
break;
}
if(tmpF->fcb[i] && tmpF->fcb[i]->fileName == newPath &&tmpF->fcb[i]->type == "—"){
cout<<"Not a directory!"<<endl;
flag = 1;
}
}
if(flag == 0){
cout<<"No such file !"<<endl;
}
}
}
else if(tmpOrder == "help"){
help();
}
else if(tmpOrder == "ls"){
ls_alh_ByTree(tmpF);
}
else if(tmpOrder == "touch"){
string tmpFileName;
cin>>tmpFileName;
string realFilePath = tmpF->filePath+"\\"+tmpFileName;
create_txt(realFilePath);
for(int i=0;i<12;i++){
if(tmpF->fcb[i] == NULL){
tmpF->fcb[i] = new FCB;
tmpF->fcb[i]->innerNum = i;
tmpF->fcb[i]->fileName = tmpFileName;
tmpF->fcb[i]->authority = 666;
tmpF->fcb[i]->type = "—";
tmpF->fcb[i]->length = 0;
tmpF->fcb[i]->Owner[0] = new User;
tmpF->fcb[i]->Owner[0]->username = u.username;
tmpF->child[i] = new FSct;
tmpF->child[i]->fileName = tmpFileName;
tmpF->child[i]->filePath = realFilePath;
for(long int num=0; num<1024*1024; num++){
if(phBlock[num].state == 0){
tmpF->fcb[i]->phNum = num;
phBlock[num].state = 1;
phBlock[num].content = ("complete of "+ tmpFileName);
tmpF->fcb[i]->firstBlock = new PHB;
tmpF->fcb[i]->firstBlock->state = 1;
tmpF->fcb[i]->firstBlock->phNum = num;
cout<<"文件"<<tmpF->child[i]->fileName<<"创建成功,(首)物理块号为:"<<tmpF->fcb[i]->phNum<<" "<<endl;
break;
}
}
break;
}
}
}
else if(tmpOrder == "write"){
string Option;
cin>> Option;
string file_name;
cin>>file_name;
int flag;
for(int i=0;i<12;i++){
if(!tmpF->fcb[i]) continue;
else if(file_name == tmpF->fcb[i]->fileName){
flag=1;
ASL *p = Shead->next;
while(p){
if(p->fileName == file_name){
string file_path = p->path;
if(Option == "-a"){
app(file_path);
flag = 2;
}
else if(Option == "-c"){
cover(file_path);
flag = 2;
}
tmpF->fcb[i]->length = get_length(file_path);
for(int j=0;j<12;j++){
if(!tmpF->pre->fcb[j]) continue;
else if(tmpF->pre->child[j]->filePath == tmpF->filePath){
tmpF->pre->fcb[j]->length += get_length(file_path);
}
}
if(tmpF->fcb[i]->length < 1)
{
tmpF->fcb[i]->firstBlock->content = ("complete file of "+tmpF->fcb[i]->fileName);
long int phNum = tmpF->fcb[i]->firstBlock->phNum;
string tmpContent = ("complete file of "+tmpF->fcb[i]->fileName);
phBlock[phNum].content =tmpContent;
}
else if(tmpF->fcb[i]->length > 1)
{
tmpF->fcb[i]->firstBlock->content = ("IndexBlock of "+tmpF->fcb[i]->fileName);
long int phNum = tmpF->fcb[i]->firstBlock->phNum;
phBlock[phNum].content = ("IndexBlock of "+tmpF->fcb[i]->fileName);
int count = tmpF->fcb[i]->length/1.0;
if(tmpF->fcb[i]->length > count+0.5) count++;
int tmpNum = 0;
for(long int num=0;num < 1024*1024; num++){
if(phBlock[num].state==0 && tmpNum<=count){
phBlock[num].state = 1;
string tmpContent = ("Parts file of "+tmpF->fcb[i]->fileName);
phBlock[num].content = tmpContent;
int first = tmpF->fcb[i]->firstBlock->phNum;
phBlock[first].indexList[tmpNum] = num;
tmpNum++;
}
if(tmpNum == count) break;
}
}
break;
}
else{
p=p->next;
}
}
}
}
if(flag == 0){
cout<<"No such file!"<<endl;
}else if(flag == 1){
cout<<"file hasNot been opend !"<<endl;
}
}
else if(tmpOrder == "now"){
cout<<"当前文件结构:"<<tmpF->fileName<<endl;
cout<<"当前文件父结构:"<<preF->fileName<<endl;
}
else if(tmpOrder == "df") {
check_Block_Content();
check_phBlock();
}
else if(tmpOrder == "rm"){
string Option;
cin>>Option;
if(Option == "-f")
{
string tmpFileName;
cin>>tmpFileName;
string realFilePath = tmpF->filePath+"\\"+tmpFileName;
for(int i=0;i<12;i++){
if(tmpF->child[i] && tmpF->child[i]->fileName == tmpFileName){
if(tmpF->fcb[i]->type == "—"){
rm_f(realFilePath,tmpF,i);
}
else if(tmpF->fcb[i]->type == "dir"){
cout<<"Not a File!It is a directory !"<<endl;
break;
}
break;
}
}
}
else if(Option == "-rf")
{
cout<<"in rm -rf process"<<endl;
string tmpFileName;
cin>>tmpFileName;
string realFilePath = tmpF->filePath+"\\"+tmpFileName;
for(int i=0;i<12;i++){
if(tmpF->fcb[i] && tmpF->child[i]->fileName == tmpFileName && tmpF->fcb[i]->type == "dir"){
rm_rf(realFilePath,tmpF,i);
break;
}
}
}
}
else if(tmpOrder == "mkdir"){
string tmpFileName;
cin>>tmpFileName;
string realFilePath = tmpF->filePath+"\\"+tmpFileName;
bool flag = CreateDirectory(realFilePath.c_str(), NULL);
if(flag == true){
cout<<"创建文件夹成功!"<<endl;
}else{
cout<<"创建文件夹失败!"<<endl;
}
for(int i=0;i<12;i++){
if(tmpF->fcb[i] == NULL){
tmpF->fcb[i] = new FCB;
tmpF->fcb[i]->innerNum = i;
tmpF->fcb[i]->fileName = tmpFileName;
tmpF->fcb[i]->authority = 666;
tmpF->fcb[i]->type = "dir";
tmpF->fcb[i]->length = 0;
tmpF->fcb[i]->Owner[0] = new User;
tmpF->fcb[i]->Owner[0]->username = u.username;
tmpF->child[i] = new FSct;
tmpF->child[i]->fileName = tmpFileName;
tmpF->child[i]->filePath = realFilePath;
for(long int num=0; num<1024*1024; num++){
if(phBlock[num].state == 0){
tmpF->fcb[i]->phNum = num;
phBlock[num].state = 1;
phBlock[num].content = ("DirBlock of "+ tmpFileName);
tmpF->fcb[i]->firstBlock = new PHB;
tmpF->fcb[i]->firstBlock->state = 1;
tmpF->fcb[i]->firstBlock->phNum = num;
cout<<"文件夹"<<tmpF->child[i]->fileName<<"创建成功,(首)物理块号为:"<<tmpF->fcb[i]->phNum<<" "<<endl;
break;
}
}
break;
}
}
}
else if(tmpOrder == "find"){
string fileName;
cin>>fileName;
int flag = find(tmpF->filePath,fileName,u.username);
if(flag == 0){
cout<<"-bash- : NO such file!"<<endl;
}
}
else if(tmpOrder == "open") {
int flag = 0;
string file_name;
cin>>file_name;
for(int i=0;i<12;i++){
if(tmpF->fcb[i] == NULL) continue;
else{
if(tmpF->child[i]->fileName == file_name && tmpF->fcb[i]->type == "—"){
AFL *t = Fhead;
while(t->next != NULL){
t = t->next;
}
t->next = new AFL;
t = t->next;
t->fcb = tmpF->fcb[i];
t->next = NULL;
ASL *p = Shead;
while(p->next != NULL){
p = p->next;
}
p->next = new ASL;
p=p->next;
p->fileName = tmpF->child[i]->fileName;
p->path = tmpF->child[i]->filePath;
p->innerNum = tmpF->fcb[i]->innerNum;
p->next = NULL;
p->afl = t;
FileNum_Curr++;
flag = 1;
cout<<p->fileName<<"文件已打开,当前活动文件数:"<< FileNum_Curr<<endl;
}else if(tmpF->child[i]->fileName == file_name && tmpF->fcb[i]->type == "dir"){
cout<<"Not a file! It's a directory!"<<endl;
flag = 1;
}
}
}
if(flag == 0){
cout<<"No such file!"<<endl;
}
}
else if(tmpOrder == "close"){
string file_name;
cin>>file_name;
ASL *p = Shead;
int flag = 0;
while(p->next){
if(p->next->fileName == file_name)
{
cout<<"Remove from ASL!"<<endl;
ASL *tmp = p->next;
p->next = tmp->next;
delete(tmp);
flag = 1;
break;
}
else
{
p = p->next;
}
}
if(flag == 1){
AFL *t = Fhead;
while(t->next){
if(t->next->fcb->fileName == file_name)
{
cout<<"Remove from AFL!"<<endl;
AFL *tmp = t->next;
t->next = tmp->next;
delete(tmp);
FileNum_Curr--;
cout<<"关闭文件成功!"<<'\t'<<"当前活动文件数:"<<FileNum_Curr<<endl;
}
else
{
t = t->next;
}
}
}else{
cout<<"关闭文件失败!"<<endl;
}
}
else if(tmpOrder == "ASL"){
cout<<"当前活动文件数:"<<FileNum_Curr<<endl;
ASL *p = Shead->next;
if(!p){
cout<<"当前没有活动文件"<<endl;
}
else{
int i=1;
while(p){
cout<<"("<<i<<") 文件名:"<<p->fileName<<'\t'<<"文件内部号:"<<p->innerNum<<'\t'<<"路径:"<<p->path<<endl;
p=p->next;
i++;
}
}
}
else if(tmpOrder == "cat"){
string file_name;
cin>>file_name;
int flag;
for(int i=0;i<12;i++){
if(!tmpF->fcb[i]) continue;
else if(file_name == tmpF->fcb[i]->fileName){
flag=1;
ASL *p = Shead->next;
while(p){
if(p->fileName == file_name){
string file_path = p->path;
int tmpnum = cat(file_path);
flag = 2;
break;
}
else{
p=p->next;
}
}
}
}
if(flag == 0){
cout<<"No such file!"<<endl;
}else if(flag == 1){
cout<<"file hasNot been opend !"<<endl;
}
}
else if(tmpOrder == "head"){
string Option;
cin>>Option;
string file_name;
cin>>file_name;
string subOption = Option.substr(1,Option.length());
int num = stoi(subOption.c_str());
int flag = 0;
for(int i=0;i<12;i++){
if(!tmpF->fcb[i]) continue;
else if(file_name == tmpF->fcb[i]->fileName){
flag=1;
ASL *p = Shead->next;
while(p){
if(p->fileName == file_name){
string file_path = p->path;
int tmpnum = head(file_path,num);
flag = 2;
break;
}
else{
p=p->next;
}
}
}
}
if(flag == 0){
cout<<"Read file fail!"<<endl;
}else if(flag == 1){
cout<<"file hasNot been opend !"<<endl;
}
}
else if(tmpOrder == "tail"){
string Option;
cin>>Option;
string file_name;
cin>>file_name;
string subOption = Option.substr(1,Option.length());
int tmp_num = stoi(subOption.c_str());
int flag = 0;
for(int i=0;i<12;i++){
if(!tmpF->fcb[i]) continue;
else if(file_name == tmpF->fcb[i]->fileName){
flag=1;
ASL *p = Shead->next;
while(p){
if(p->fileName == file_name){
string file_path = p->path;
int file_line_length = getlineNum(file_path);
int num = file_line_length - tmp_num;
int tmpnum = tail(file_path,num);
flag = 2;
break;
}
else{
p=p->next;
}
}
}
}
if(flag == 0){
cout<<"Read file fail!"<<endl;
}else if(flag == 1){
cout<<"file hasNot been opend !"<<endl;
}
}
else if(tmpOrder == "cp") {
string source;
string destination;
string src_name;
cin>>src_name;
string dst_path;
cin>>dst_path;
for(int i=0;i<12;i++){
if(!tmpF->fcb[i]) continue;
else if(tmpF->child[i]->fileName == src_name and tmpF->fcb[i]->type == "—")
{
cout<<"source file found !"<<endl;
source = tmpF->child[i]->filePath;
string real_path = deal_path(dst_path,f->filePath);
destination = real_path+"\\"+src_name;
FSct *dst = search(f,real_path);
if(real_path == f->filePath) dst = f;
for(int j =0;j<12;j++){
if(!dst->fcb[j])
{
dst->child[j] = new FSct;
dst->child[j]->fileName = src_name;
dst->child[j]->filePath = real_path+"\\"+src_name;
dst->child[j]->Owner[0] = new User;
dst->child[j]->Owner[0] = f->Owner[0];
dst->fcb[j] = new FCB;
dst->fcb[j]->authority = 666;
dst->fcb[j]->fileName = src_name;
dst->fcb[j]->innerNum = j;
dst->fcb[j]->Owner[0] = new User;
dst->fcb[j]->Owner[0]->username = u.username;
dst->fcb[j]->type = "—";
dst->fcb[j]->length = tmpF->fcb[i]->length;
for(long int num=0; num<1024*1024; num++){
if(phBlock[num].state == 0){
dst->fcb[j]->phNum = num;
phBlock[num].state = 1;
phBlock[num].content = ("complete file of "+ src_name);
dst->fcb[j]->firstBlock = new PHB;
dst->fcb[j]->firstBlock->state = 1;
dst->fcb[j]->firstBlock->phNum = num;
cout<<"文件"<<dst->child[j]->fileName<<"复制成功,(首)物理块号为:"<<dst->fcb[j]->phNum<<" "<<endl;
break;
}
}
if(dst->fcb[j]->length < 1)
{
dst->fcb[j]->firstBlock->content = ("complete file of "+dst->fcb[j]->fileName);
long int phNum = dst->fcb[j]->firstBlock->phNum;
string tmpContent = ("complete file of "+dst->fcb[j]->fileName);
phBlock[phNum].content =tmpContent;
}
else if(dst->fcb[j]->length > 1)
{
dst->fcb[j]->firstBlock->content = ("IndexBlock of "+dst->fcb[j]->fileName);
long int phNum = dst->fcb[j]->firstBlock->phNum;
phBlock[phNum].content = ("IndexBlock of "+dst->fcb[j]->fileName);
int count = dst->fcb[j]->length/1.0;
if(dst->fcb[j]->length > count+0.5) count++;
int tmpNum = 0;
for(long int num=0;num < 1024*1024; num++){
if(phBlock[num].state==0 && tmpNum<=count){
phBlock[num].state = 1;
string tmpContent = ("Parts file of "+dst->fcb[j]->fileName);
phBlock[num].content = tmpContent;
int first = dst->fcb[j]->firstBlock->phNum;
phBlock[first].indexList[tmpNum] = num;
tmpNum++;
}
if(tmpNum == count) break;
}
}
copy(source,destination);
cout<<"文件复制成功!"<<endl;
break;
}
}
}
}
}
else if(tmpOrder == "move") {
string source;
string destination;
string src_name;
cin>>src_name;
string dst_path;
cin>>dst_path;
for(int i=0;i<12;i++){
if(!tmpF->fcb[i]) continue;
else if(tmpF->child[i]->fileName == src_name and tmpF->fcb[i]->type == "—")
{
cout<<"source file found !"<<endl;
source = tmpF->child[i]->filePath;
string real_path = deal_path(dst_path,f->filePath);
destination = real_path+"\\"+src_name;
FSct *dst = search(f,real_path);
if(real_path == f->filePath) dst = f;
for(int j =0;j<12;j++){
if(!dst->fcb[j])
{
dst->child[j] = new FSct;
dst->child[j]->fileName = src_name;
dst->child[j]->filePath = real_path+"\\"+src_name;
dst->child[j]->Owner[0] = new User;
dst->child[j]->Owner[0] = f->Owner[0];
dst->fcb[j] = new FCB;
dst->fcb[j]->authority = 666;
dst->fcb[j]->fileName = src_name;
dst->fcb[j]->innerNum = j;
dst->fcb[j]->Owner[0] = new User;
dst->fcb[j]->Owner[0]->username = u.username;
dst->fcb[j]->type = "—";
dst->fcb[j]->length = tmpF->fcb[i]->length;
dst->fcb[j]->firstBlock = new PHB;
dst->fcb[j]->firstBlock = tmpF->fcb[i]->firstBlock;
dst->fcb[j]->phNum = dst->fcb[j]->firstBlock->phNum;
copy(source,destination);
free(tmpF->fcb[i] );
tmpF->fcb[i] = NULL;
free(tmpF->child[i]);
tmpF->child[i] = NULL;
char *savePath = &source[0];
if(remove(savePath)==0){
cout<<"文件移动成功"<<endl;
}else {
cout<<"文件移动失败"<<endl;
}
break;
}
}
}
}
}
else if(tmpOrder == "export") {
string src_file;
cin>>src_file;
string destination;
cin>>destination;
for(int i=0;i<12;i++) {
if(!tmpF->fcb[i]) continue;
else if(tmpF->child[i]->fileName == src_file){
cout<<"Found source file !"<<endl;
string source = tmpF->child[i]->filePath;
copy(source,destination);
cout<<"Export succeed !"<<endl;
break;
}
}
}
else if(tmpOrder == "import"){
string destination;
string source;
cin>>source;
string dst_fileName;
cin>>dst_fileName;
double src_length = get_length(source);
cout<<src_length<<endl;
for(int i=0;i<12;i++){
if(!tmpF->fcb[i]){
tmpF->child[i] = new FSct;
tmpF->child[i]->fileName = dst_fileName;
tmpF->child[i]->filePath = tmpF->filePath+"\\"+dst_fileName;
destination = tmpF->child[i]->filePath;
tmpF->child[i]->Owner[0] = new User;
tmpF->child[i]->Owner[0] = f->Owner[0];
tmpF->fcb[i] = new FCB;
tmpF->fcb[i]->authority = 666;
tmpF->fcb[i]->fileName = dst_fileName;
tmpF->fcb[i]->innerNum = i;
tmpF->fcb[i]->Owner[0] = new User;
tmpF->fcb[i]->Owner[0]->username = u.username;
tmpF->fcb[i]->type = "—";
tmpF->fcb[i]->length = src_length;
for(long int num=0; num<1024*1024; num++){
if(phBlock[num].state == 0){
tmpF->fcb[i]->phNum = num;
phBlock[num].state = 1;
phBlock[num].content = ("complete file of "+ dst_fileName);
tmpF->fcb[i]->firstBlock = new PHB;
tmpF->fcb[i]->firstBlock->state = 1;
tmpF->fcb[i]->firstBlock->phNum = num;
cout<<"文件"<<tmpF->child[i]->fileName<<"导入成功,(首)物理块号为:"<<tmpF->fcb[i]->phNum<<" "<<endl;
break;
}
}
if(tmpF->fcb[i]->length < 1)
{
tmpF->fcb[i]->firstBlock->content = ("complete file of "+tmpF->fcb[i]->fileName);
long int phNum = tmpF->fcb[i]->firstBlock->phNum;
string tmpContent = ("complete file of "+tmpF->fcb[i]->fileName);
phBlock[phNum].content =tmpContent;
}
else if(tmpF->fcb[i]->length > 1)
{
tmpF->fcb[i]->firstBlock->content = ("IndexBlock of "+tmpF->fcb[i]->fileName);
long int phNum = tmpF->fcb[i]->firstBlock->phNum;
phBlock[phNum].content = ("IndexBlock of "+tmpF->fcb[i]->fileName);
int count = tmpF->fcb[i]->length/1.0;
if(tmpF->fcb[i]->length > count+0.5) count++;
int tmpNum = 0;
for(long int num=0;num < 1024*1024; num++){
if(phBlock[num].state==0 && tmpNum<=count){
phBlock[num].state = 1;
string tmpContent = ("Parts file of "+tmpF->fcb[i]->fileName);
phBlock[num].content = tmpContent;
int first = tmpF->fcb[i]->firstBlock->phNum;
phBlock[first].indexList[tmpNum] = num;
tmpNum++;
}
if(tmpNum == count) break;
}
}
copy(source,destination);
cout<<"Import succeed!"<<endl;
break;
}
}
}
else{
cout<<"input error!"<<endl;
}
}
}
void* operation(void* args)
{
Initial(path);
Initial_Sec_All();
cout<<"——————————————————————————————————————————"<<endl;
int key = -1;
help();
while(key == -1){
key = index();
if(key != -1) break;
}
cd(uGroup[key],home->child[key]);
return 0;
}
int main()
{
pthread_t tids[NUM_THREADS];
for(int i = 0; i < NUM_THREADS; ++i)
{
int ret = pthread_create(&tids[i], NULL, operation, NULL);
if (ret != 0)
{
cout << "pthread_create error: error_code=" << ret << endl;
}
}
pthread_exit(NULL);
}
3.结语
这个是在大二刚学习完操作系统的时候根据所学到的知识去写的,有些指令Linux并不是这样的但是功能是类似的,另外算法能力还不足,还没充分去考虑性能的问题想到什么功能就花了一周去写了,但结果似乎看起来不差,当时课设95分,发出来看看以后有没有机会再拿出来改一下,也供有类似课设的同学参考,主要还是想涨点浏览量害哈哈哈…
|