-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstorage_disk.go
More file actions
60 lines (53 loc) · 1.11 KB
/
storage_disk.go
File metadata and controls
60 lines (53 loc) · 1.11 KB
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
package gemu
import (
"io"
"os"
"path/filepath"
)
type DiskStorage struct {
basepath string
}
func NewDiskStorage(basepath string) Storage {
return &DiskStorage{basepath: basepath}
}
func (DS *DiskStorage) Exists(Item string) bool {
_, err := os.Stat(filepath.Join(DS.basepath, Item))
if err == nil {
return true
}
return false
}
func (DS *DiskStorage) Length(Item string) int {
s, err := os.Stat(filepath.Join(DS.basepath, Item))
if err != nil {
return 0
}
return int(s.Size())
}
func (DS *DiskStorage) Read(Item string, offset int, data []byte) {
if DS.Exists(Item) {
file, err := os.Open(filepath.Join(DS.basepath, Item))
if err != nil {
return
}
defer file.Close()
_, err = file.Seek(int64(offset), os.SEEK_SET)
if err != nil {
return
}
_, err = io.ReadFull(file, data)
if err != nil {
return
}
} else {
}
}
func (DS *DiskStorage) Write(Item string, offset int, data []byte) {
file, err := os.OpenFile(filepath.Join(DS.basepath, Item), os.O_CREATE | os.O_WRONLY, 0777)
if err != nil {
return
}
defer file.Close()
file.Seek(int64(offset), os.SEEK_SET)
file.Write(data)
}