-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
86 lines (75 loc) · 1.72 KB
/
storage.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
package raft
import (
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
)
var (
FileDir = ".data"
FilePath = ""
)
type Store interface {
CreateFile(perm fs.FileMode, flag int)
ReadFile(buf []byte, file *os.File)
WriteFile()
}
type DiskStore struct {
StoreDir string
FileName string
}
func NewDiskStore(fileName string) *DiskStore {
dir, file := filepath.Split(fileName)
if dir == "" {
dir = FileDir
}
if file == "" {
log.Fatal("Filename cannot be empty")
}
return &DiskStore{StoreDir: dir, FileName: file}
}
func (d *DiskStore) CreateFile(perm fs.FileMode, flag int) (*os.File, error) {
if d.StoreDir == "" && FileDir == "" {
return nil, errors.New("directory cannot be empty")
}
if d.FileName == "" {
return nil, errors.New("filename cannot be empty")
}
filePath := filepath.Join(d.StoreDir, d.FileName)
err := os.MkdirAll(d.StoreDir, os.ModePerm)
if err != nil {
return nil, fmt.Errorf("unable to create directory: %v", err)
}
file, err := os.OpenFile(filePath, flag, perm)
if err != nil {
return nil, fmt.Errorf("unable to create file: %v", err)
}
return file, nil
}
func (d *DiskStore) ReadFile(buf []byte, file *os.File) (int, error) {
if file == nil {
newFile, err := d.CreateFile(0644, os.O_WRONLY|os.O_CREATE)
if file == nil {
return -1, fmt.Errorf("could not create file: %v", err)
}
file = newFile
}
nRead, err := file.Read(buf)
if err != nil && err != io.EOF {
return -1, fmt.Errorf("error reading file: %v", err)
}
if nRead <= 0 {
return nRead, errors.New("file is empty")
}
return nRead, nil
}
func (d *DiskStore) WriteFile(buf []byte, file *os.File) error {
_, err := file.Write(buf)
if err != nil {
return fmt.Errorf("write error: %v", err)
}
return nil
}