Skip to content

Commit 1e9d63c

Browse files
committed
mv x/http/fs/cached.Dir => x/http/fs.Dir
1 parent 3e74aa0 commit 1e9d63c

6 files changed

Lines changed: 114 additions & 80 deletions

File tree

http/fs/cached/cached.go

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (p *fsCached) Open(name string) (file http.File, err error) {
109109
f.Close()
110110
return nil, e
111111
}
112-
file = Dir(f, fis)
112+
file = xfs.Dir(f, fis)
113113
} else {
114114
file = f
115115
}
@@ -118,82 +118,6 @@ func (p *fsCached) Open(name string) (file http.File, err error) {
118118

119119
// -----------------------------------------------------------------------------------------
120120

121-
type StatCloser interface {
122-
Stat() (fs.FileInfo, error)
123-
Close() error
124-
}
125-
126-
type dir struct {
127-
items []fs.FileInfo
128-
file StatCloser
129-
off int
130-
}
131-
132-
func Dir(base StatCloser, fis []fs.FileInfo) http.File {
133-
return &dir{fis, base, 0}
134-
}
135-
136-
func (p *dir) Close() error {
137-
return p.file.Close()
138-
}
139-
140-
func (p *dir) Read(b []byte) (n int, err error) {
141-
return 0, fs.ErrPermission
142-
}
143-
144-
func (p *dir) Seek(offset int64, whence int) (int64, error) {
145-
if whence == io.SeekStart && offset == 0 {
146-
p.off = 0
147-
return 0, nil
148-
}
149-
return 0, fs.ErrPermission
150-
}
151-
152-
func (p *dir) Stat() (fs.FileInfo, error) {
153-
return p.file.Stat()
154-
}
155-
156-
func (p *dir) Readdir(n int) (fis []fs.FileInfo, err error) {
157-
fis = p.items[p.off:]
158-
if n <= 0 {
159-
p.off = len(p.items)
160-
return
161-
}
162-
if len(fis) > n {
163-
fis = fis[:n]
164-
} else {
165-
err = io.EOF
166-
}
167-
p.off += len(fis)
168-
return
169-
}
170-
171-
func (p *dir) ReadDir(n int) (items []fs.DirEntry, err error) {
172-
fis, err := p.Readdir(n)
173-
if err != nil && err != io.EOF {
174-
return
175-
}
176-
items = make([]fs.DirEntry, len(fis))
177-
for i, fi := range fis {
178-
items[i] = dirEntry{fi}
179-
}
180-
return
181-
}
182-
183-
type dirEntry struct {
184-
fs.FileInfo
185-
}
186-
187-
func (d dirEntry) Info() (fs.FileInfo, error) {
188-
return d.FileInfo, nil
189-
}
190-
191-
func (d dirEntry) Type() fs.FileMode {
192-
return d.FileInfo.Mode().Type()
193-
}
194-
195-
// -----------------------------------------------------------------------------------------
196-
197121
func DownloadFile(localFile string, file http.File) (err error) {
198122
_, err = file.Seek(0, io.SeekStart)
199123
if err != nil {

http/fs/cached/remote/remote.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"strings"
2828
"time"
2929

30+
xfs "github.com/qiniu/x/http/fs"
3031
"github.com/qiniu/x/http/fs/cached"
3132
xdir "github.com/qiniu/x/http/fs/cached/dir"
3233
)
@@ -220,7 +221,7 @@ func (p *remote) SyncOpen(local string, name string) (f http.File, err error) {
220221
log.Printf("[WARN] writeStubFile fail (%d errors)", nError)
221222
}
222223
}()
223-
return cached.Dir(f, fis), nil
224+
return xfs.Dir(f, fis), nil
224225
}
225226
if p.cacheFile {
226227
localFile := filepath.Join(local, name)

http/fs/fs.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,84 @@ func (p *DirInfo) Close() error {
188188

189189
// -----------------------------------------------------------------------------------------
190190

191+
// StatCloser is the interface that groups the basic Stat and Close methods.
192+
type StatCloser interface {
193+
Stat() (fs.FileInfo, error)
194+
Close() error
195+
}
196+
197+
type dir struct {
198+
items []fs.FileInfo
199+
file StatCloser
200+
off int
201+
}
202+
203+
// Dir implements a http.File by a list of fs.FileInfo.
204+
func Dir(base StatCloser, fis []fs.FileInfo) http.File {
205+
return &dir{fis, base, 0}
206+
}
207+
208+
func (p *dir) Close() error {
209+
return p.file.Close()
210+
}
211+
212+
func (p *dir) Read(b []byte) (n int, err error) {
213+
return 0, fs.ErrPermission
214+
}
215+
216+
func (p *dir) Seek(offset int64, whence int) (int64, error) {
217+
if whence == io.SeekStart && offset == 0 {
218+
p.off = 0
219+
return 0, nil
220+
}
221+
return 0, fs.ErrPermission
222+
}
223+
224+
func (p *dir) Stat() (fs.FileInfo, error) {
225+
return p.file.Stat()
226+
}
227+
228+
func (p *dir) Readdir(n int) (fis []fs.FileInfo, err error) {
229+
fis = p.items[p.off:]
230+
if n <= 0 {
231+
p.off = len(p.items)
232+
return
233+
}
234+
if len(fis) > n {
235+
fis = fis[:n]
236+
} else {
237+
err = io.EOF
238+
}
239+
p.off += len(fis)
240+
return
241+
}
242+
243+
func (p *dir) ReadDir(n int) (items []fs.DirEntry, err error) {
244+
fis, err := p.Readdir(n)
245+
if err != nil && err != io.EOF {
246+
return
247+
}
248+
items = make([]fs.DirEntry, len(fis))
249+
for i, fi := range fis {
250+
items[i] = dirEntry{fi}
251+
}
252+
return
253+
}
254+
255+
type dirEntry struct {
256+
fs.FileInfo
257+
}
258+
259+
func (d dirEntry) Info() (fs.FileInfo, error) {
260+
return d.FileInfo, nil
261+
}
262+
263+
func (d dirEntry) Type() fs.FileMode {
264+
return d.FileInfo.Mode().Type()
265+
}
266+
267+
// -----------------------------------------------------------------------------------------
268+
191269
type rootDir struct {
192270
}
193271

http/fs/fs_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2024 Qiniu Limited (qiniu.com)
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package fs
218

319
import (

http/fs/fstest/fstest.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"strings"
2727

2828
xfs "github.com/qiniu/x/http/fs"
29-
"github.com/qiniu/x/http/fs/cached"
3029
)
3130

3231
// -----------------------------------------------------------------------------------------
@@ -63,7 +62,7 @@ func Dir(name string, entries ...http.File) http.File {
6362
}
6463
fis[i] = item
6564
}
66-
return &fsDir{cached.Dir(fi, fis), entries}
65+
return &fsDir{xfs.Dir(fi, fis), entries}
6766
}
6867

6968
// -----------------------------------------------------------------------------------------

http/fs/fsx_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2024 Qiniu Limited (qiniu.com)
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package fs_test
218

319
import (

0 commit comments

Comments
 (0)