-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
58 lines (51 loc) · 1.08 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
package sh
import (
"errors"
"os"
"path/filepath"
)
type Storage struct {
Dir string
NotAutoClean bool
}
func (s *Storage) RemoveOrTruncate(file *os.File, size int64) error {
if file == nil {
return nil
}
if s.NotAutoClean {
return s.Truncate(file, size)
}
return os.Remove(file.Name())
}
func (s *Storage) Truncate(file *os.File, size int64) error {
if file == nil || size == 0 {
return nil
}
if stat, err := file.Stat(); err != nil {
return err
} else {
if size >= stat.Size() {
return file.Truncate(stat.Size() - 1)
}
return file.Truncate(stat.Size() - size)
}
}
func (s *Storage) CreateFile(filename string) (*os.File, error) {
if filename == "" {
return nil, errors.New("filename is empty")
}
return os.Create(filepath.Join(s.Dir, filename))
}
func CheckStorage(s *Storage) (bool, error) {
if s == nil || s.Dir == "" {
return false, nil
}
f, err := os.Stat(s.Dir)
if err != nil {
return false, err
}
if !f.IsDir() {
return false, &os.PathError{Op: "IsDir", Path: s.Dir, Err: errors.New("no such directory")}
}
return true, nil
}