-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
112 lines (92 loc) · 1.92 KB
/
fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"strings"
)
var Disk *Disk_t
var root_dir *Inode_t // '/'
var cur_dir *Inode_t // '$'
func initDisk(){
Disk = Open()
//Create root inode
_, root := CreateInode(Disk,0x800)
Disk.Root_inode = root
//Write changes to disk
Disk.WriteDisk()
root_dir = &Disk.Root_inode
cur_dir = root_dir
}
func cd(inode * Inode_t){
cur_dir = inode
}
func ls(inode * Inode_t){
if inode == nil{
inode = cur_dir
}
if inode.I_mode != 0x800{
fmt.Printf("ls: File is not a directory")
return
}
entries := inode.ReadDirInode(Disk)
for _,e := range entries{
name :=string(e.Name[:])
fmt.Printf("%s ",name)
}
fmt.Println()
}
func cat(inode * Inode_t) string{
return string(inode.ReadInodeData(Disk))
}
func touch(parent * Inode_t, name, contents string) Inode_t{
if parent == nil{
parent = cur_dir
}
//malloc new entry
_, entry := parent.CreateDirEntry(Disk, name,REGULAR_FILE_INODE)
entry.WriteInodeData(Disk,[]byte(contents))
return entry
}
func mkdir(parent * Inode_t, name string) Inode_t{
if parent == nil{
parent = cur_dir
}
_ , ind := parent.CreateDirEntry(Disk,name,0x400)
return ind
}
func tree(inode Inode_t, h int){
entries := inode.ReadDirInode(Disk)
if len(entries)>0 && h==0{
fmt.Println(".")
}
for _,e := range entries{
name := string(e.Name[:])
path :=strings.Repeat(" ",h)
ind := ReadInode(Disk, e.Inode_P)
if ind.I_mode == DIRECTORY_INODE{
if h>0 {
fmt.Printf("│")
}
fmt.Printf("%s└─ %s/\n",path, name)
tree(ind, h+1)
}else{
val := cat(&ind)
fmt.Printf("│%s└─ %s: '%s'\n",path, name,val)
}
}
}
func main() {
initDisk()
//Add home dir to root
//Add /usr
usrDir := mkdir(root_dir,"usr")
//Add /dev
mkdir(root_dir,"dev")
//Add /usr
touch(&usrDir,"notes.txt", "hello")
cd(&usrDir)
//Disk.Root_inode.CreateDirEntry(Disk,"bin",0x400)
tree(Disk.Root_inode, 0)
ls(root_dir)
//readFile(Disk.Root_inode)
Disk.Close()
}