📄 inode.java
字号:
/*
* INode.java,ver0.1.0
*
* Created on 2006年11月30日, 下午6:45
*
*This Class abstracted the i-Node for the file system
*
*History: 2006年11月30日,ver 0.1.0 first release
*/
package ossimulation;
import java.util.*;
/**
*
* @author Decell.Chou
*/
public class INode {
private char[] name;//the name of the i node
private char extName;//the extended name of the i node
private int fileLength;//the length of the file
private char[] iNode;//the entity of the i node
private int fileDescriptor;//the file descriptor,actually is the index of the block of the inode
private int index;//the index of the data field of the i node
Disk simDisk;//the simulation disk
/** Creates a new instance of INode */
public INode(String argName,Disk argDisk) {
//creat a -inode
name = new char[10];
iNode = new char[64];
simDisk = argDisk;
fileDescriptor = -1;
index = 12;
argName.getChars(0,argName.length(),name,0);//deal with the i-node name
extName = 'd';//set the default extends name as d,indicate the the inode is a directory
fileLength = 0;//set the default file length as zero.
//initialized the inode entity
for(int i = 0;i <= 63;i++){
iNode[i] = 0;
}
//copy the name to the inode entity
for(int i = 0;i <= 9;i++){
iNode[i] = name[i];
}
iNode[10] = extName;//copy the extended name to the inode entity
iNode[11] = (char)fileLength;//copy the file length to the inode entity
//requst free space in the disk,write the inode entity into it;
writeToDisk(iNode);
}
/**used to write contend to the disk,the content must de char[64]*/
private void writeToDisk(char[] content){
//if the fd is null, then allocate new space to the inode
if(fileDescriptor == -1){
fileDescriptor = simDisk.findNextFree();
}
simDisk.eraseBlock(fileDescriptor);
simDisk.writeBlock(content,fileDescriptor);
}
/**used to increase the length of the file */
private void increaseLength(){
//increase the length of the file
fileLength++;
iNode[11] = (char)fileLength;
//update the disk content
this.writeToDisk(iNode);
}
/**the the fileDesciptor of the inode*/
public int getFileDescriptor(){
return fileDescriptor;
}
/**get the inode name*/
public String getName(){
String nameString;
nameString = String.copyValueOf(name);
return nameString;
}
/**get the file length*/
public int getFileLength(){
return(fileLength);
}
/**set the inode name*/
public void setName(String argName){
for(int i = 0;i <= 9;i++){
name[i] = 0;
}
argName.getChars(0,argName.length(),name,0);
for(int i = 0;i <= 9;i++){
iNode[i] = name[i];
}
writeToDisk(iNode);
}
/**get the ExtName of the inode*/
public char getExtName(){
return extName;
}
/**set the ExtName of the inode*/
public void setExtName(char argExtName){
extName = argExtName;
iNode[10] = argExtName;
writeToDisk(iNode);
}
/**add content to the data field of the i node*/
public void addPointer(int argFileDescriptor){
int i;
char fd = (char)argFileDescriptor;
//search for the space
for(i = 12;i <= 63 && iNode[i] != 0;i++);
iNode[i] = fd;
//if there is no space in the data field of the i node, then allocate a new space to it
if(i == index){
index ++;//increase the data field index
}
writeToDisk(iNode);
}
/**delete a pointer from the data field of the i node*/
public void delPointer(int argFileDescriptor){
int i;
char fd = (char)argFileDescriptor;
//search for the propriety i node pointer
for( i = 12;i <= 63;i++){
if(iNode[i] == fd){
break;
}
}
//clear the corresponding bit
iNode[i] = 0;
writeToDisk(iNode);
}
/**destroy the i node*/
public void destroy(int argFileDescriptor){
char[] targetBlock = simDisk.readBlock(argFileDescriptor);
if(targetBlock[10] == 'd'){
for(int i = 12;i <= 63;i++){
if(targetBlock[i] == 0){
continue;
}
destroy((int)targetBlock[i]);
}
} else{
for(int i = 12;targetBlock[i] != 0;i++){
simDisk.eraseBlock((int)targetBlock[i]);
}
}
simDisk.eraseBlock(argFileDescriptor);
}
/**Edit the file content sepcify by the i node*/
public void addContent(ArrayList newContent){
Iterator contentsIterator = newContent.iterator();//the iterator interface,used to get contents from the ArrayList
int fd;//the file descriptor;
char[] lineContent = new char[64];
String tmpContent;
for(;contentsIterator.hasNext();){
//write the content to the disk
tmpContent = (String)contentsIterator.next();
fd = simDisk.findNextFree();
//clear the previous line content
for(int i = 0;i <= 63;i++){
lineContent[i] = 0;
}
tmpContent.getChars(0,tmpContent.length(),lineContent,0);
simDisk.writeBlock(lineContent,fd);
//add the file descriptor to the data field of the i node
this.addPointer(fd);
//update the length of the file
this.increaseLength();
}
}
/**read the content from the inode */
public ArrayList readContent(){
ArrayList contentList = new ArrayList();
char[] content = new char[64];
for(int i = 0 ;i < this.getFileLength();i++){
contentList.add(String.copyValueOf(simDisk.readBlock((char)iNode[12 + i])));
}
return contentList;
}
/**The toString method */
public String toString(){
int nameLength;
for(nameLength = 0;(nameLength < name.length) && name[nameLength] != 0;nameLength++);
return String.copyValueOf(name,0,nameLength);
}
/**Existed only for Class Debuging*/
static public void main(String[] args){
Disk simDisk = new Disk();
INode iNode1 = new INode("t inode 1",simDisk);
System.out.println(iNode1.getFileDescriptor());//target is 2
//the the construction methoe of the Class
System.out.println(iNode1.getFileDescriptor());//target is 2
System.out.println(String.copyValueOf(simDisk.readBlock(iNode1.getFileDescriptor())));
//test the getName and the setName method
iNode1.setName("ssdf");
System.out.println(iNode1.getName());//target is ssdf
System.out.println(iNode1.getFileDescriptor());//target is 2
//test the add and del pointer method
for(int i = 12;i <= 50;i++){
iNode1.addPointer(i);
}
iNode1.delPointer(24);
iNode1.delPointer(50);
iNode1.delPointer(12);
for(int i = 120;i <= 127;i++){
iNode1.addPointer(i);
}
//test the destroy method
iNode1.delPointer(44);
iNode1.destroy(iNode1.getFileDescriptor());
System.out.println(iNode1.getFileDescriptor());
System.out.println(String.copyValueOf(simDisk.readBlock(iNode1.getFileDescriptor())));
//test the editContent method
INode iNode2 = new INode("t inode 2",simDisk);
iNode2.setExtName('e');
ArrayList contentList = new ArrayList();
//print the inode imformation
System.out.println(String.copyValueOf(simDisk.readBlock(iNode2.getFileDescriptor())));
//add content
contentList.add("asdasd");
contentList.add("sdjhfjsgdjf");
contentList.add("sjdjsdgfjsdgfkajdhsgfkajdsf");
contentList.add("asasdas");
contentList.add("");
contentList.add("x++");
iNode2.addContent(contentList);
char[] content = new char[64];
content = simDisk.readBlock(iNode2.getFileDescriptor());
char[] fileContent;
for(int i = 12; i <= 63;i++){
fileContent = simDisk.readBlock((int)content[i]);
System.out.println(String.copyValueOf(fileContent));
}
System.out.println(iNode2.getFileLength());
//test the readContent method
ArrayList inodeContent = iNode2.readContent();
Iterator contentIterator = inodeContent.iterator();
for(;contentIterator.hasNext();){
System.out.println(contentIterator.next());
}
//test the toString method
System.out.println(iNode2.toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -