1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package httpfs implements http.FileSystem using a godoc vfs.FileSystem.
6 package httpfs // import "golang.org/x/tools/godoc/vfs/httpfs"
14 "golang.org/x/tools/godoc/vfs"
17 func New(fs vfs.FileSystem) http.FileSystem {
25 func (h *httpFS) Open(name string) (http.File, error) {
26 fi, err := h.fs.Stat(name)
31 return &httpDir{h.fs, name, nil}, nil
33 f, err := h.fs.Open(name)
37 return &httpFile{h.fs, f, name}, nil
40 // httpDir implements http.File for a directory in a FileSystem.
47 func (h *httpDir) Close() error { return nil }
48 func (h *httpDir) Stat() (os.FileInfo, error) { return h.fs.Stat(h.name) }
49 func (h *httpDir) Read([]byte) (int, error) {
50 return 0, fmt.Errorf("cannot Read from directory %s", h.name)
53 func (h *httpDir) Seek(offset int64, whence int) (int64, error) {
54 if offset == 0 && whence == 0 {
58 return 0, fmt.Errorf("unsupported Seek in directory %s", h.name)
61 func (h *httpDir) Readdir(count int) ([]os.FileInfo, error) {
63 d, err := h.fs.ReadDir(h.name)
68 d = []os.FileInfo{} // not nil
73 if len(h.pending) == 0 && count > 0 {
76 if count <= 0 || count > len(h.pending) {
77 count = len(h.pending)
79 d := h.pending[:count]
80 h.pending = h.pending[count:]
84 // httpFile implements http.File for a file (not directory) in a FileSystem.
85 type httpFile struct {
91 func (h *httpFile) Stat() (os.FileInfo, error) { return h.fs.Stat(h.name) }
92 func (h *httpFile) Readdir(int) ([]os.FileInfo, error) {
93 return nil, fmt.Errorf("cannot Readdir from file %s", h.name)