This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
posixfs.go
191 lines (164 loc) · 3.65 KB
/
posixfs.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package posixfs
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/goftp/server"
)
type BasicFileInfo struct {
os.FileInfo
}
func (f *BasicFileInfo) Owner() string {
return "root"
}
func (f *BasicFileInfo) Group() string {
return "root"
}
type PosixFSDriver struct {
RootPath string
}
func (driver *PosixFSDriver) ChangeDir(path string) error {
rPath := filepath.Join(driver.RootPath, path)
f, err := os.Lstat(rPath)
if err != nil {
return err
}
if f.IsDir() {
return nil
}
return errors.New("Not a dir")
}
func (driver *PosixFSDriver) Stat(path string) (server.FileInfo, error) {
basepath := filepath.Join(driver.RootPath, path)
rPath, err := filepath.Abs(basepath)
if err != nil {
return nil, err
}
f, err := os.Lstat(rPath)
if err != nil {
return nil, err
}
return &BasicFileInfo{f}, nil
}
func (driver *PosixFSDriver) ListDir(path string, callback func(server.FileInfo) error) error {
basepath := filepath.Join(driver.RootPath, path)
fis, err := ioutil.ReadDir(basepath)
if err != nil {
return err
}
for _, finfo := range fis {
err = callback(&BasicFileInfo{finfo})
if err != nil {
return err
}
}
return nil
}
func (driver *PosixFSDriver) DeleteDir(path string) error {
rPath := filepath.Join(driver.RootPath, path)
f, err := os.Lstat(rPath)
if err != nil {
return err
}
if f.IsDir() {
return os.Remove(rPath)
}
return errors.New("Not a directory")
}
func (driver *PosixFSDriver) DeleteFile(path string) error {
rPath := filepath.Join(driver.RootPath, path)
f, err := os.Lstat(rPath)
if err != nil {
return err
}
if !f.IsDir() {
return os.Remove(rPath)
}
return errors.New("Not a file")
}
func (driver *PosixFSDriver) Rename(fromPath string, toPath string) error {
oldPath := filepath.Join(driver.RootPath, fromPath)
newPath := filepath.Join(driver.RootPath, toPath)
return os.Rename(oldPath, newPath)
}
func (driver *PosixFSDriver) MakeDir(path string) error {
rPath := filepath.Join(driver.RootPath, path)
return os.Mkdir(rPath, os.ModePerm)
}
func (driver *PosixFSDriver) GetFile(path string, offset int64) (int64, io.ReadCloser, error) {
rPath := filepath.Join(driver.RootPath, path)
f, err := os.Open(rPath)
if err != nil {
return 0, nil, err
}
info, err := f.Stat()
if err != nil {
return 0, nil, err
}
f.Seek(offset, os.SEEK_SET)
return info.Size(), f, nil
}
func (driver *PosixFSDriver) PutFile(destPath string, data io.Reader, appendData bool) (int64, error) {
rPath := filepath.Join(driver.RootPath, destPath)
var isExist bool
f, err := os.Lstat(rPath)
if err == nil {
isExist = true
if f.IsDir() {
return 0, errors.New("A dir has the same name")
}
} else {
if os.IsNotExist(err) {
isExist = false
} else {
return 0, errors.New(fmt.Sprintln("Put File error:", err))
}
}
if !appendData {
if isExist {
err = os.Remove(rPath)
if err != nil {
return 0, err
}
}
f, err := os.Create(rPath)
if err != nil {
return 0, err
}
defer f.Close()
bytes, err := io.Copy(f, data)
if err != nil {
return 0, err
}
return bytes, nil
}
if !isExist {
return 0, errors.New("Append data but file not exsit")
}
of, err := os.OpenFile(rPath, os.O_APPEND|os.O_RDWR, 0660)
if err != nil {
return 0, err
}
defer of.Close()
_, err = of.Seek(0, os.SEEK_END)
if err != nil {
return 0, err
}
bytes, err := io.Copy(of, data)
if err != nil {
return 0, err
}
return bytes, nil
}
func NewPosixFSFactory(rootDir string) *PosixFSFactory {
return &PosixFSFactory{rootDir}
}
type PosixFSFactory struct {
RootPath string
}
func (bdf *PosixFSFactory) NewDriver() (server.Driver, error) {
return &PosixFSDriver{bdf.RootPath}, nil
}