/***************************************************************************************
fs.cpp
辅助函数,执行命令函数会被反复调用的函数
***************************************************************************************/
#include "fs.h"
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdio.h>
#include <io.h>
#include <windows.h>
void fs::creatFS()
{
cout << "正在创建文件系统 ..." << endl;
int i, j;
fstream FILE("VirtualDisk", ios::binary|ios::in|ios::out); //申请磁盘空间
if (!FILE)
{
cerr << "磁盘空间无法创建!" << endl;
return;
}
FILE.seekp(0, ios::beg); //申请空间,先将其都填充为" "
for (i = 0; i < blockNum * blockSize; i++) //共申请Block_Num个磁盘块
{
FILE.write(" ", sizeof(char));
}
FILE.seekp(0, ios::beg); //填充超级块区
Super.freeBlock = 101867; //用于储存文件的磁盘块为101867块
Super.freeInode = 1023;
FILE.write((char*)&Super, sizeof(super));
FILE.seekp(1016, ios::cur); //把文件的写指针从当前位置向后移1016个字节
for (i = 0; i < inodeNum; i++) //填充inode位图
{
if (i == 0) inodeBMP[i] = 1;
else inodeBMP[i] = 0;
FILE.write((char*)&inodeBMP[i], sizeof(int));
}
for (i = 0; i < 533; i++) //填充block位图,前533块已被占用
{
blockBMP[i] = 1;
FILE.write((char*)&blockBMP[i], sizeof(int));
}
for (i = 533; i < blockNum; i++) //后面的磁盘块都未被占用
{
blockBMP[i] = 0;
FILE.write((char*)&blockBMP[i], sizeof(int));
}
Inode[0].isFile = 0; //写根目录
strcpy(Inode[0].name, "Z:"); //根目录命名为Z:盘
Inode[0].firstBlock = -1; //根目录不额外占用磁盘块
Inode[0].length = 0;
Inode[0].index = 0;
Inode[0].parent = -1;
Inode[0].childNum = 0; //子节点个数为0
for (j = 0; j < 15; j++) //将每个目录的子节点赋为-1
{
Inode[0].child[j] = -1;
}
GetLocalTime(&Inode[0].TIME); //获取当前系统时间
FILE.write((char*)&Inode[0], sizeof(inode));
for(i = 1; i < inodeNum; i++) //填充Inode区
{
Inode[i].isFile = -1;
Inode[i].index = i;
for (j = 0; j < 15; j++) //将每个目录的子节点赋为-1
{
Inode[i].child[j] = -1;
}
FILE.write((char*)&Inode[i], sizeof(inode));
}
FILE.close();
curIndex = 0; //初始化当前目录为根目录
curInode = &Inode[curIndex]; //当前i-node索引指针
strcpy(curPath, curInode->name);
// strcat(curPath, "/");
system("cls");
cout << "成功创建!" << endl;
Sleep(1500);
system("cls");
}
void fs::load()
{
fstream FILE("VirtualDisk", ios::binary|ios::in|ios::out); //申请虚拟磁盘空间
if (!FILE)
{
cerr << "没有找到虚拟磁盘" << endl;
return;
}
FILE.seekp(0, ios::beg); //文件指针回到起始处
FILE.read((char*)&Super, sizeof(super));
FILE.seekg(1016, ios::cur);
int i;
for (i = 0; i < inodeNum; i++)
{
FILE.read((char*)&inodeBMP[i], sizeof(int));
}
for (i = 0; i < 533; i++)
{
FILE.read((char*)&blockBMP[i], sizeof(int));
}
for (i = 0; i < inodeNum; i++)
{
FILE.read((char*)&Inode[i], sizeof(inode));
}
FILE.close();
curIndex = 0; //初始化当前目录为根目录
curInode = &Inode[curIndex]; //当前i-node索引指针
strcpy(curPath, curInode->name);
// strcat(curPath, "/");
}
void fs::save()
{
fstream FILE("VirtualDisk", ios::binary|ios::in|ios::out);
int i = 0;
FILE.seekp(0, ios::beg);
FILE.write((char*)&Super, sizeof(super));
FILE.seekp(1016, ios::cur);
for (i = 0; i < inodeNum; i++)
{
FILE.write((char*)&inodeBMP[i], sizeof(int));
}
for (i = 0; i < 533; i++)
{
FILE.write((char*)&blockBMP[i], sizeof(int));
}
for (i = 0; i < inodeNum; i++)
{
FILE.write((char*)&Inode[i], sizeof(inode));
}
FILE.close();
}
int fs::find(char * name, int parent) //搜索该文件或目录是否已经存在
{
int i, temp;
struct inode * Parent = &Inode[parent];
for (i = 0; i < 15; i++)
{
if(inodeBMP[Parent->child[i]] == 1) //使用Bitmap来判断该子节点是否存在
{
temp = Parent->child[i];
if (strcmp(name, Inode[temp].name) == 0)
return temp;
}
}
return -1;
}
int fs::subfind(char * name, int parent)
{
int work, i = 0;
inode * parentInode = &Inode[parent];
//for (int i = 0; i < 15; i++)
while (i < 15)
{
if (inodeBMP[parentInode->child[i]] == 1)
{
work = parentInode->child[i];
if (strcmp(name, Inode[work].name) == 0) return i;
}
i++;
}
return -1;
}
char * fs::getAbsolutePath(int index)
{
char * absolutePath = new char;
char * temp = new char;
int parent;
if (!index)
{
strcpy(absolutePath, "Z:");
return absolutePath;
}
else
{
parent = index;
while (parent != -1)
{
if (parent == index)
{
strcpy(absolutePath, Inode[parent].name);
parent = Inode[parent].parent;
}
else
{
strcpy(temp, Inode[parent].name);
strcat(temp, "/");
strcat(temp, absolutePath);
strcpy(absolutePath, temp);
parent = Inode[parent].parent;
}
}
return absolutePath;
}
}
char * fs::getName(char * path)
{
char * temp = new char;
char * rtnName = new char;
int length = strlen(path);
int i = 0;
int j = 0;
temp = strchr(path, '/'); //strchr函数返回第一次出现字符的位置指针
if (!temp) //相对路径或根目录
{
return path;
}
else //完整路径
{ //找到最后一个文件名
while(* path != '\0') //j为最后一个'/'的位置
{
path++;
i++;
if(* path == '/')
j = i;
}
rtnName = path - length + j + 1; //当前文件或目录名
return rtnName;
}
}
int fs::getParentIndex(char * path)
{
int i, j, length, parent;
i = j = 0;
length = strlen(path);
int Curr_Path_Num=0; //记录与name相同的文件或目录的数目
int Same_Path_Inode[15]; //记录与name相同的文件或目录的inode号
char * temp = new char;
char * Curr_Temp = new char;
char * Parent_Temp = new char;
char * Path_Parent = new char;
char * Parent_Name = new char;
temp = strchr(path, '/');
if (!strcmp(path,"Z:")) //根目录情况
{
return -1; //path为根目录,其索引值为-1
}
else
{
if (!temp) //相对路径情况
{
return curIndex;
}
else //绝对路径情况
{
while(*path != '\0') //查找其父目录
{
path++;
i++;
if (*path == '/')
j = i;
}
strncpy(Path_Parent, path - length, j);
Path_Parent[j] = '\0'; //Path_Parent为父目录名
Parent_Name = getName(Path_Parent);
for (int k = 0; k < 15; k++) //初始化Same_Path_Inode[15]数组
{
Same_Path_Inode[k] = 0;
}
for (int l=0; l < inodeNum; l++)
{
if (!strcmp(Parent_Name, Inode[l].name))
//计算与name相同的文件名数目及inode号
{
Curr_Path_Num++;
Same_Path_Inode[Curr_Path_Num] = Inode[l].index;
}
}
for (int m = 1; m <= Curr_Path_Num; m++)
{
parent = Same_Path_Inode[m];
do
{
if (parent == Same_Path_Inode[m])
{
strcpy(Curr_Temp, Inode[parent].name);
parent = Inode[parent].parent;
}
else
{
strcpy(Parent_Temp, Inode[parent].name);
strcat(Parent_Temp, "/");
strcat(Parent_Temp, Curr_Temp);
strcpy(Curr_Temp, Parent_Temp);
parent = Inode[parent].parent;
}
}
while (parent != -1);
if (!strcmp(Curr_Temp, Path_Parent) && Inode[Same_Path_Inode[m]].isFile == 0)
{
return Same_Path_Inode[m]; //找到父目录
}
}
return -2; //找不到父目录
评论30
最新资源