| 1 |
// Copyright 2023 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package blob |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"fmt" |
| 20 |
"io" |
| 21 |
"io/fs" |
| 22 |
"path/filepath" |
| 23 |
"time" |
| 24 |
|
| 25 |
"gocloud.dev/gcerrors" |
| 26 |
"gocloud.dev/internal/gcerr" |
| 27 |
) |
| 28 |
|
| 29 |
// Ensure that Bucket implements various io/fs interfaces. |
| 30 |
var ( |
| 31 |
_ = fs.FS(&Bucket{}) |
| 32 |
_ = fs.SubFS(&Bucket{}) |
| 33 |
) |
| 34 |
|
| 35 |
// iofsFileInfo describes a single file in an io/fs.FS. |
| 36 |
// It implements fs.FileInfo and fs.DirEntry. |
| 37 |
type iofsFileInfo struct { |
| 38 |
lo *ListObject |
| 39 |
name string |
| 40 |
} |
| 41 |
|
| 42 |
func (f *iofsFileInfo) Name() string { return f.name } |
| 43 |
func (f *iofsFileInfo) Size() int64 { return f.lo.Size } |
| 44 |
func (f *iofsFileInfo) Mode() fs.FileMode { return fs.ModeIrregular } |
| 45 |
func (f *iofsFileInfo) ModTime() time.Time { return f.lo.ModTime } |
| 46 |
func (f *iofsFileInfo) IsDir() bool { return false } |
| 47 |
func (f *iofsFileInfo) Sys() any { return f.lo } |
| 48 |
func (f *iofsFileInfo) Info() (fs.FileInfo, error) { return f, nil } |
| 49 |
func (f *iofsFileInfo) Type() fs.FileMode { return fs.ModeIrregular } |
| 50 |
|
| 51 |
// iofsOpenFile describes a single open file in an io/fs.FS. |
| 52 |
// It implements fs.FileInfo and fs.File. |
| 53 |
type iofsOpenFile struct { |
| 54 |
*Reader |
| 55 |
name string |
| 56 |
} |
| 57 |
|
| 58 |
func (f *iofsOpenFile) Name() string { return f.name } |
| 59 |
func (f *iofsOpenFile) Mode() fs.FileMode { return fs.ModeIrregular } |
| 60 |
func (f *iofsOpenFile) IsDir() bool { return false } |
| 61 |
func (f *iofsOpenFile) Sys() any { return f.r } |
| 62 |
func (f *iofsOpenFile) Stat() (fs.FileInfo, error) { return f, nil } |
| 63 |
|
| 64 |
// iofsDir describes a single directory in an io/fs.FS. |
| 65 |
// It implements fs.FileInfo, fs.DirEntry, and fs.File. |
| 66 |
type iofsDir struct { |
| 67 |
b *Bucket |
| 68 |
key string |
| 69 |
name string |
| 70 |
// If opened is true, we've read entries via openOnce(). |
| 71 |
opened bool |
| 72 |
entries []fs.DirEntry |
| 73 |
offset int |
| 74 |
} |
| 75 |
|
| 76 |
func newDir(b *Bucket, key, name string) *iofsDir { |
| 77 |
return &iofsDir{b: b, key: key, name: name} |
| 78 |
} |
| 79 |
|
| 80 |
func (d *iofsDir) Name() string { return d.name } |
| 81 |
func (d *iofsDir) Size() int64 { return 0 } |
| 82 |
func (d *iofsDir) Mode() fs.FileMode { return fs.ModeDir } |
| 83 |
func (d *iofsDir) Type() fs.FileMode { return fs.ModeDir } |
| 84 |
func (d *iofsDir) ModTime() time.Time { return time.Time{} } |
| 85 |
func (d *iofsDir) IsDir() bool { return true } |
| 86 |
func (d *iofsDir) Sys() any { return d } |
| 87 |
func (d *iofsDir) Info() (fs.FileInfo, error) { return d, nil } |
| 88 |
func (d *iofsDir) Stat() (fs.FileInfo, error) { return d, nil } |
| 89 |
func (d *iofsDir) Read([]byte) (int, error) { |
| 90 |
return 0, &fs.PathError{Op: "read", Path: d.key, Err: fs.ErrInvalid} |
| 91 |
} |
| 92 |
func (d *iofsDir) Close() error { return nil } |
| 93 |
func (d *iofsDir) ReadDir(count int) ([]fs.DirEntry, error) { |
| 94 |
if err := d.openOnce(); err != nil { |
| 95 |
return nil, err |
| 96 |
} |
| 97 |
n := len(d.entries) - d.offset |
| 98 |
if n == 0 && count > 0 { |
| 99 |
return nil, io.EOF |
| 100 |
} |
| 101 |
if count > 0 && n > count { |
| 102 |
n = count |
| 103 |
} |
| 104 |
list := make([]fs.DirEntry, n) |
| 105 |
for i := range list { |
| 106 |
list[i] = d.entries[d.offset+i] |
| 107 |
} |
| 108 |
d.offset += n |
| 109 |
return list, nil |
| 110 |
} |
| 111 |
|
| 112 |
func (d *iofsDir) openOnce() error { |
| 113 |
if d.opened { |
| 114 |
return nil |
| 115 |
} |
| 116 |
d.opened = true |
| 117 |
|
| 118 |
// blob expects directories to end in the delimiter, except at the top level. |
| 119 |
prefix := d.key |
| 120 |
if prefix != "" { |
| 121 |
prefix += "/" |
| 122 |
} |
| 123 |
listOpts := ListOptions{ |
| 124 |
Prefix: prefix, |
| 125 |
Delimiter: "/", |
| 126 |
} |
| 127 |
ctx, _ := d.b.ioFSCallback() |
| 128 |
|
| 129 |
// Fetch all the directory entries. |
| 130 |
// Conceivably we could only fetch a few here, and fetch the rest lazily |
| 131 |
// on demand, but that would add significant complexity. |
| 132 |
iter := d.b.List(&listOpts) |
| 133 |
for { |
| 134 |
item, err := iter.Next(ctx) |
| 135 |
if err == io.EOF { |
| 136 |
break |
| 137 |
} |
| 138 |
if err != nil { |
| 139 |
return err |
| 140 |
} |
| 141 |
name := filepath.Base(item.Key) |
| 142 |
if item.IsDir { |
| 143 |
d.entries = append(d.entries, newDir(d.b, item.Key, name)) |
| 144 |
} else { |
| 145 |
d.entries = append(d.entries, &iofsFileInfo{item, name}) |
| 146 |
} |
| 147 |
} |
| 148 |
// There is no such thing as an empty directory in Bucket, so if |
| 149 |
// we didn't find anything, it doesn't exist. |
| 150 |
if len(d.entries) == 0 { |
| 151 |
return fs.ErrNotExist |
| 152 |
} |
| 153 |
return nil |
| 154 |
} |
| 155 |
|
| 156 |
// SetIOFSCallback sets a callback that is used during Open and calls on the objects |
| 157 |
// returned from Open. |
| 158 |
// |
| 159 |
// fn should return a context.Context and *ReaderOptions that can be used in |
| 160 |
// calls to List and NewReader on b. It may be called more than once. |
| 161 |
// |
| 162 |
// If SetIOFSCallback is never called, io.FS functions will use context.Background |
| 163 |
// and a default ReaderOptions. |
| 164 |
func (b *Bucket) SetIOFSCallback(fn func() (context.Context, *ReaderOptions)) { |
| 165 |
b.ioFSCallback = fn |
| 166 |
} |
| 167 |
|
| 168 |
// Open implements fs.FS.Open (https://pkg.go.dev/io/fs#FS). |
| 169 |
func (b *Bucket) Open(path string) (fs.File, error) { |
| 170 |
if !fs.ValidPath(path) { |
| 171 |
return nil, &fs.PathError{Op: "open", Path: path, Err: fs.ErrInvalid} |
| 172 |
} |
| 173 |
|
| 174 |
// Check if it's a file. If not, assume it's a directory until proven otherwise. |
| 175 |
ctx, readerOpts := b.ioFSCallback() |
| 176 |
var isDir bool |
| 177 |
var key, name string // name is the last part of the path |
| 178 |
if path == "." { |
| 179 |
// Root is always a directory, but blob doesn't want the "." in the key. |
| 180 |
isDir = true |
| 181 |
key, name = "", "." |
| 182 |
} else { |
| 183 |
exists, _ := b.Exists(ctx, path) |
| 184 |
isDir = !exists |
| 185 |
key, name = path, filepath.Base(path) |
| 186 |
} |
| 187 |
|
| 188 |
// If it's a directory, list the directory contents. We can't do this lazily |
| 189 |
// because we need to error out here if it doesn't exist. |
| 190 |
if isDir { |
| 191 |
dir := newDir(b, key, name) |
| 192 |
err := dir.openOnce() |
| 193 |
if err != nil { |
| 194 |
if err == fs.ErrNotExist && path == "." { |
| 195 |
// The root directory must exist. |
| 196 |
return dir, nil |
| 197 |
} |
| 198 |
return nil, &fs.PathError{Op: "open", Path: path, Err: err} |
| 199 |
} |
| 200 |
return dir, nil |
| 201 |
} |
| 202 |
|
| 203 |
// It's a file; open it and return a wrapper. |
| 204 |
r, err := b.NewReader(ctx, path, readerOpts) |
| 205 |
if err != nil { |
| 206 |
code := gcerrors.Code(err) |
| 207 |
switch code { |
| 208 |
case gcerrors.NotFound: |
| 209 |
err = fmt.Errorf("%w: %w", err, fs.ErrNotExist) |
| 210 |
case gcerrors.PermissionDenied: |
| 211 |
err = fmt.Errorf("%w: %w", err, fs.ErrPermission) |
| 212 |
} |
| 213 |
return nil, &fs.PathError{Op: "open", Path: path, Err: err} |
| 214 |
} |
| 215 |
return &iofsOpenFile{r, filepath.Base(path)}, nil |
| 216 |
} |
| 217 |
|
| 218 |
// Sub implements fs.SubFS.Sub. |
| 219 |
// |
| 220 |
// SetIOFSCallback must be called prior to calling this function. |
| 221 |
func (b *Bucket) Sub(dir string) (fs.FS, error) { |
| 222 |
if b.ioFSCallback == nil { |
| 223 |
return nil, gcerr.Newf(gcerr.InvalidArgument, nil, "blob: Sub -- SetIOFSCallback must be called before Sub") |
| 224 |
} |
| 225 |
if dir == "." { |
| 226 |
return b, nil |
| 227 |
} |
| 228 |
// blob expects directories to end in the delimiter, except at the top level. |
| 229 |
pb := PrefixedBucket(b, dir+"/") |
| 230 |
pb.SetIOFSCallback(b.ioFSCallback) |
| 231 |
return pb, nil |
| 232 |
} |