@@ -16,26 +16,22 @@ const (
16
16
defaultCreateMode = 0666
17
17
)
18
18
19
- // OS is a filesystem based on the os filesystem
19
+ // OS is a filesystem based on the os filesystem.
20
20
type OS struct {
21
21
base string
22
22
}
23
23
24
- // New returns a new OS filesystem
24
+ // New returns a new OS filesystem.
25
25
func New (baseDir string ) * OS {
26
26
return & OS {
27
27
base : baseDir ,
28
28
}
29
29
}
30
30
31
- // Create creates a file and opens it with standard permissions
32
- // and modes O_RDWR, O_CREATE and O_TRUNC.
33
31
func (fs * OS ) Create (filename string ) (billy.File , error ) {
34
32
return fs .OpenFile (filename , os .O_RDWR | os .O_CREATE | os .O_TRUNC , defaultCreateMode )
35
33
}
36
34
37
- // OpenFile is equivalent to standard os.OpenFile.
38
- // If flag os.O_CREATE is set, all parent directories will be created.
39
35
func (fs * OS ) OpenFile (filename string , flag int , perm os.FileMode ) (billy.File , error ) {
40
36
fullpath := fs .absolutize (filename )
41
37
@@ -69,8 +65,6 @@ func (fs *OS) createDir(fullpath string) error {
69
65
return nil
70
66
}
71
67
72
- // ReadDir returns the filesystem info for all the archives under the specified
73
- // path.
74
68
func (fs * OS ) ReadDir (path string ) ([]billy.FileInfo , error ) {
75
69
fullpath := fs .absolutize (path )
76
70
@@ -87,7 +81,6 @@ func (fs *OS) ReadDir(path string) ([]billy.FileInfo, error) {
87
81
return s , nil
88
82
}
89
83
90
- // Rename moves a file in disk from _from_ to _to_.
91
84
func (fs * OS ) Rename (from , to string ) error {
92
85
from = fs .absolutize (from )
93
86
to = fs .absolutize (to )
@@ -99,24 +92,20 @@ func (fs *OS) Rename(from, to string) error {
99
92
return os .Rename (from , to )
100
93
}
101
94
102
- // MkdirAll creates a directory.
103
95
func (fs * OS ) MkdirAll (path string , perm os.FileMode ) error {
104
96
fullpath := fs .absolutize (path )
105
97
return os .MkdirAll (fullpath , defaultDirectoryMode )
106
98
}
107
99
108
- // Open opens a file in read-only mode.
109
100
func (fs * OS ) Open (filename string ) (billy.File , error ) {
110
101
return fs .OpenFile (filename , os .O_RDONLY , 0 )
111
102
}
112
103
113
- // Remove deletes a file in disk.
114
104
func (fs * OS ) Remove (filename string ) error {
115
105
fullpath := fs .absolutize (filename )
116
106
return os .Remove (fullpath )
117
107
}
118
108
119
- // TempFile creates a new temporal file.
120
109
func (fs * OS ) TempFile (dir , prefix string ) (billy.File , error ) {
121
110
fullpath := fs .absolutize (dir )
122
111
if err := fs .createDir (fullpath + string (os .PathSeparator )); err != nil {
@@ -141,24 +130,18 @@ func (fs *OS) TempFile(dir, prefix string) (billy.File, error) {
141
130
return newOSFile (filename , f ), nil
142
131
}
143
132
144
- // Join joins the specified elements using the filesystem separator.
145
133
func (fs * OS ) Join (elem ... string ) string {
146
134
return filepath .Join (elem ... )
147
135
}
148
136
149
- // Dir returns a new Filesystem from the same type of fs using as baseDir the
150
- // given path
151
137
func (fs * OS ) Dir (path string ) billy.Filesystem {
152
138
return New (fs .absolutize (path ))
153
139
}
154
140
155
- // Base returns the base path of the filesytem
156
141
func (fs * OS ) Base () string {
157
142
return fs .base
158
143
}
159
144
160
- // RemoveAll removes a file or directory recursively. Removes everything it can,
161
- // but returns the first error.
162
145
func (fs * OS ) RemoveAll (path string ) error {
163
146
fullpath := fs .Join (fs .base , path )
164
147
return os .RemoveAll (fullpath )
@@ -169,7 +152,6 @@ func (fs *OS) Lstat(filename string) (billy.FileInfo, error) {
169
152
return os .Lstat (fullpath )
170
153
}
171
154
172
- // Symlink imlements billy.Symlinker.Symlink.
173
155
func (fs * OS ) Symlink (target , link string ) error {
174
156
target = filepath .FromSlash (target )
175
157
@@ -186,7 +168,6 @@ func (fs *OS) Symlink(target, link string) error {
186
168
return os .Symlink (target , link )
187
169
}
188
170
189
- // Readlink implements billy.Symlinker.Readlink.
190
171
func (fs * OS ) Readlink (link string ) (string , error ) {
191
172
fullpath := fs .Join (fs .base , link )
192
173
target , err := os .Readlink (fullpath )
@@ -206,6 +187,13 @@ func (fs *OS) Readlink(link string) (string, error) {
206
187
return string (os .PathSeparator ) + target , nil
207
188
}
208
189
190
+ func (fs * OS ) absolutize (relpath string ) string {
191
+ fullpath := filepath .FromSlash (filepath .ToSlash (relpath ))
192
+
193
+ fullpath = fs .Join (fs .base , fullpath )
194
+ return filepath .Clean (fullpath )
195
+ }
196
+
209
197
// osFile represents a file in the os filesystem
210
198
type osFile struct {
211
199
billy.BaseFile
@@ -240,10 +228,3 @@ func (f *osFile) Close() error {
240
228
func (f * osFile ) ReadAt (p []byte , off int64 ) (int , error ) {
241
229
return f .file .ReadAt (p , off )
242
230
}
243
-
244
- func (fs * OS ) absolutize (relpath string ) string {
245
- fullpath := filepath .FromSlash (filepath .ToSlash (relpath ))
246
-
247
- fullpath = fs .Join (fs .base , fullpath )
248
- return filepath .Clean (fullpath )
249
- }
0 commit comments