1
- package subdirfs
1
+ package chroot
2
2
3
3
import (
4
4
"errors"
@@ -13,20 +13,20 @@ import (
13
13
// underlying filesystem does not support symlinking.
14
14
var ErrSymlinkNotSupported = errors .New ("symlink not supported" )
15
15
16
- // SubDir is a helper to implement billy.Filesystem.Dir in other filesystems .
17
- type SubDir struct {
16
+ // ChrootHelper is a helper to implement billy.Chroot .
17
+ type ChrootHelper struct {
18
18
underlying billy.Filesystem
19
19
base string
20
20
}
21
21
22
22
// New creates a new filesystem wrapping up the given 'fs'.
23
- // The created filesystem has its base in the given subdirectory of the
23
+ // The created filesystem has its base in the given ChrootHelperectory of the
24
24
// underlying filesystem.
25
25
func New (fs billy.Filesystem , base string ) billy.Filesystem {
26
- return & SubDir {fs , base }
26
+ return & ChrootHelper {fs , base }
27
27
}
28
28
29
- func (fs * SubDir ) underlyingPath (filename string ) (string , error ) {
29
+ func (fs * ChrootHelper ) underlyingPath (filename string ) (string , error ) {
30
30
if isCrossBoundaries (filename ) {
31
31
return "" , billy .ErrCrossedBoundary
32
32
}
@@ -41,7 +41,7 @@ func isCrossBoundaries(path string) bool {
41
41
return strings .HasPrefix (path , ".." )
42
42
}
43
43
44
- func (fs * SubDir ) Create (filename string ) (billy.File , error ) {
44
+ func (fs * ChrootHelper ) Create (filename string ) (billy.File , error ) {
45
45
fullpath , err := fs .underlyingPath (filename )
46
46
if err != nil {
47
47
return nil , err
@@ -55,7 +55,7 @@ func (fs *SubDir) Create(filename string) (billy.File, error) {
55
55
return newFile (fs , f , filename ), nil
56
56
}
57
57
58
- func (fs * SubDir ) Open (filename string ) (billy.File , error ) {
58
+ func (fs * ChrootHelper ) Open (filename string ) (billy.File , error ) {
59
59
fullpath , err := fs .underlyingPath (filename )
60
60
if err != nil {
61
61
return nil , err
@@ -69,7 +69,7 @@ func (fs *SubDir) Open(filename string) (billy.File, error) {
69
69
return newFile (fs , f , filename ), nil
70
70
}
71
71
72
- func (fs * SubDir ) OpenFile (filename string , flag int , mode os.FileMode ) (billy.File , error ) {
72
+ func (fs * ChrootHelper ) OpenFile (filename string , flag int , mode os.FileMode ) (billy.File , error ) {
73
73
fullpath , err := fs .underlyingPath (filename )
74
74
if err != nil {
75
75
return nil , err
@@ -83,7 +83,7 @@ func (fs *SubDir) OpenFile(filename string, flag int, mode os.FileMode) (billy.F
83
83
return newFile (fs , f , filename ), nil
84
84
}
85
85
86
- func (fs * SubDir ) TempFile (dir , prefix string ) (billy.File , error ) {
86
+ func (fs * ChrootHelper ) TempFile (dir , prefix string ) (billy.File , error ) {
87
87
fullpath , err := fs .underlyingPath (dir )
88
88
if err != nil {
89
89
return nil , err
@@ -97,7 +97,7 @@ func (fs *SubDir) TempFile(dir, prefix string) (billy.File, error) {
97
97
return newFile (fs , f , fs .Join (dir , filepath .Base (f .Name ()))), nil
98
98
}
99
99
100
- func (fs * SubDir ) Rename (from , to string ) error {
100
+ func (fs * ChrootHelper ) Rename (from , to string ) error {
101
101
var err error
102
102
from , err = fs .underlyingPath (from )
103
103
if err != nil {
@@ -112,7 +112,7 @@ func (fs *SubDir) Rename(from, to string) error {
112
112
return fs .underlying .Rename (from , to )
113
113
}
114
114
115
- func (fs * SubDir ) Remove (path string ) error {
115
+ func (fs * ChrootHelper ) Remove (path string ) error {
116
116
fullpath , err := fs .underlyingPath (path )
117
117
if err != nil {
118
118
return err
@@ -121,7 +121,7 @@ func (fs *SubDir) Remove(path string) error {
121
121
return fs .underlying .Remove (fullpath )
122
122
}
123
123
124
- func (fs * SubDir ) MkdirAll (filename string , perm os.FileMode ) error {
124
+ func (fs * ChrootHelper ) MkdirAll (filename string , perm os.FileMode ) error {
125
125
fullpath , err := fs .underlyingPath (filename )
126
126
if err != nil {
127
127
return err
@@ -130,7 +130,7 @@ func (fs *SubDir) MkdirAll(filename string, perm os.FileMode) error {
130
130
return fs .underlying .MkdirAll (fullpath , perm )
131
131
}
132
132
133
- func (fs * SubDir ) Stat (filename string ) (os.FileInfo , error ) {
133
+ func (fs * ChrootHelper ) Stat (filename string ) (os.FileInfo , error ) {
134
134
fullpath , err := fs .underlyingPath (filename )
135
135
if err != nil {
136
136
return nil , err
@@ -139,7 +139,7 @@ func (fs *SubDir) Stat(filename string) (os.FileInfo, error) {
139
139
return fs .underlying .Stat (fullpath )
140
140
}
141
141
142
- func (fs * SubDir ) Lstat (filename string ) (os.FileInfo , error ) {
142
+ func (fs * ChrootHelper ) Lstat (filename string ) (os.FileInfo , error ) {
143
143
fullpath , err := fs .underlyingPath (filename )
144
144
if err != nil {
145
145
return nil , err
@@ -148,7 +148,7 @@ func (fs *SubDir) Lstat(filename string) (os.FileInfo, error) {
148
148
return fs .underlying .Lstat (fullpath )
149
149
}
150
150
151
- func (fs * SubDir ) ReadDir (path string ) ([]os.FileInfo , error ) {
151
+ func (fs * ChrootHelper ) ReadDir (path string ) ([]os.FileInfo , error ) {
152
152
fullpath , err := fs .underlyingPath (path )
153
153
if err != nil {
154
154
return nil , err
@@ -157,11 +157,11 @@ func (fs *SubDir) ReadDir(path string) ([]os.FileInfo, error) {
157
157
return fs .underlying .ReadDir (fullpath )
158
158
}
159
159
160
- func (fs * SubDir ) Join (elem ... string ) string {
160
+ func (fs * ChrootHelper ) Join (elem ... string ) string {
161
161
return fs .underlying .Join (elem ... )
162
162
}
163
163
164
- func (fs * SubDir ) Symlink (target , link string ) error {
164
+ func (fs * ChrootHelper ) Symlink (target , link string ) error {
165
165
target = filepath .FromSlash (target )
166
166
167
167
// only rewrite target if it's already absolute
@@ -181,7 +181,7 @@ func (fs *SubDir) Symlink(target, link string) error {
181
181
return fs .underlying .Symlink (target , link )
182
182
}
183
183
184
- func (fs * SubDir ) isTargetOutBounders (link , target string ) bool {
184
+ func (fs * ChrootHelper ) isTargetOutBounders (link , target string ) bool {
185
185
fulllink := fs .Join (fs .base , link )
186
186
fullpath := fs .Join (filepath .Dir (fulllink ), target )
187
187
target , err := filepath .Rel (fs .base , fullpath )
@@ -192,7 +192,7 @@ func (fs *SubDir) isTargetOutBounders(link, target string) bool {
192
192
return isCrossBoundaries (target )
193
193
}
194
194
195
- func (fs * SubDir ) Readlink (link string ) (string , error ) {
195
+ func (fs * ChrootHelper ) Readlink (link string ) (string , error ) {
196
196
fullpath , err := fs .underlyingPath (link )
197
197
if err != nil {
198
198
return "" , err
@@ -216,7 +216,7 @@ func (fs *SubDir) Readlink(link string) (string, error) {
216
216
return string (os .PathSeparator ) + target , nil
217
217
}
218
218
219
- func (fs * SubDir ) Chroot (path string ) (billy.Basic , error ) {
219
+ func (fs * ChrootHelper ) Chroot (path string ) (billy.Basic , error ) {
220
220
fullpath , err := fs .underlyingPath (path )
221
221
if err != nil {
222
222
return nil , err
@@ -225,6 +225,6 @@ func (fs *SubDir) Chroot(path string) (billy.Basic, error) {
225
225
return New (fs .underlying , fullpath ), nil
226
226
}
227
227
228
- func (fs * SubDir ) Root () string {
228
+ func (fs * ChrootHelper ) Root () string {
229
229
return fs .base
230
230
}
0 commit comments