/
osfiles.go
105 lines (93 loc) · 2.18 KB
/
osfiles.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
package torrent
import (
"errors"
"os"
"path"
"strings"
)
// a torrent FileSystem that is backed by real OS files
type osFileSystem struct {
storePath string
}
// A torrent File that is backed by an OS file
type osFile struct {
filePath string
}
type OsFsProvider struct{}
func (o OsFsProvider) NewFS(directory string) (fs FileSystem, err error) {
return &osFileSystem{directory}, nil
}
func (o *osFileSystem) Open(name []string, length int64) (file File, err error) {
// Clean the source path before appending to the storePath. This
// ensures that source paths that start with ".." can't escape.
cleanSrcPath := path.Clean("/" + path.Join(name...))[1:]
fullPath := path.Join(o.storePath, cleanSrcPath)
err = ensureDirectory(fullPath)
if err != nil {
return
}
osfile := &osFile{fullPath}
file = osfile
err = osfile.ensureExists(length)
return
}
func (o *osFileSystem) Close() error {
return nil
}
func (o *osFile) Close() (err error) {
return
}
func ensureDirectory(fullPath string) (err error) {
fullPath = path.Clean(fullPath)
if !strings.HasPrefix(fullPath, "/") {
// Transform into absolute path.
var cwd string
if cwd, err = os.Getwd(); err != nil {
return
}
fullPath = cwd + "/" + fullPath
}
base, _ := path.Split(fullPath)
if base == "" {
panic("Programming error: could not find base directory for absolute path " + fullPath)
}
err = os.MkdirAll(base, 0755)
return
}
func (o *osFile) ensureExists(length int64) (err error) {
name := o.filePath
st, err := os.Stat(name)
if err != nil && os.IsNotExist(err) {
f, err := os.Create(name)
defer f.Close()
if err != nil {
return err
}
} else {
if st.Size() == length {
return
}
}
err = os.Truncate(name, length)
if err != nil {
err = errors.New("Could not truncate file.")
return
}
return
}
func (o *osFile) ReadAt(p []byte, off int64) (n int, err error) {
file, err := os.OpenFile(o.filePath, os.O_RDWR, 0600)
if err != nil {
return
}
defer file.Close()
return file.ReadAt(p, off)
}
func (o *osFile) WriteAt(p []byte, off int64) (n int, err error) {
file, err := os.OpenFile(o.filePath, os.O_RDWR, 0600)
if err != nil {
return
}
defer file.Close()
return file.WriteAt(p, off)
}