-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk.go
104 lines (81 loc) · 1.96 KB
/
disk.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
package main
import (
"bytes"
"encoding/binary"
"fmt"
"os"
)
const DISK_SIZE = 2048
const BLOCK_SIZE = 128
const BLOCK_GROUP_0 int16 = 0x100
const BLOCK_GROUP_1 int16 = 0x200
const FILE_NAME = "disk.img"
var fd *os.File
type Disk_t struct {
Size int16
InodeCount int16
Root_inode Inode_t
//inode_table [DISK_SIZE]byte;
}
/* @Desc: If a disk img exists open the file and return the contents as a *Disk_t
* If a disk img does not exist create a new img file and return a new *Disk_t
* Set the fileDescripter variable for future use
*/
func Open() *Disk_t{
disk := new(Disk_t)
_, err := os.Stat(FILE_NAME)
if os.IsNotExist(err) {
fd, err = os.Create(FILE_NAME)
fd.Write(make([]byte,DISK_SIZE))
}else{
fd, err = os.OpenFile(FILE_NAME,os.O_RDWR,os.ModePerm)
binary.Read(fd,binary.LittleEndian,disk)
}
if err!=nil{
return nil
}
return disk
}
/* @Desc: Re-Writes data to disk and then closes fd
*/
func (disk *Disk_t)Close() {
disk.WriteDisk()
fd.Close()
}
/* @Desc: Reads n bytes from address offset and returns data in []byte
*/
func (disk * Disk_t)ReadData(n, offset int16) []byte{
buff := make([]byte,n)
wroteN, err := fd.ReadAt(buff,int64(offset))
if err != nil{
fmt.Println(err, wroteN)
}
return buff
}
/* @Desc: Writes len(data) bytes to disk at address offset and returns number of bytes written
*/
func (disk * Disk_t)WriteData(data []byte, offset int16) int16{
var buff bytes.Buffer
binary.Write(&buff, binary.LittleEndian, data)
n, err := fd.WriteAt(buff.Bytes(),int64(offset))
if err!=nil{
fmt.Printf("ERROR: %e\n",err)
return -1
}
return int16(n)
}
/* @Desc: Writes Whole Disk to img file
*/
func (disk * Disk_t)WriteDisk(){
var buff bytes.Buffer
binary.Write(&buff, binary.LittleEndian, disk)
_, err := fd.Write(buff.Bytes())
if err!=nil{
fmt.Printf("ERROR: %e\n",err)
}
}
func (disk * Disk_t)mallocBlock() int16{
address := disk.Size+BLOCK_GROUP_0;
disk.Size+=BLOCK_SIZE
return address
}