-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
81 lines (54 loc) · 1.45 KB
/
fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package bucket
import (
"context"
"fmt"
"io"
"io/fs"
"github.com/protomaps/go-pmtiles/pmtiles"
)
func NewBucketWithFS(bucket_fs fs.FS, bucketURL string, prefix string) (pmtiles.Bucket, error) {
ctx := context.Background()
bucketURL, _, err := pmtiles.NormalizeBucketKey(bucketURL, prefix, "")
if err != nil {
return nil, err
}
gc_bucket, err := NewGoCloudBucket(ctx, bucketURL, prefix)
if err != nil {
return nil, fmt.Errorf("Failed to open bucket, %v", err)
}
var walk_func func(path string, d fs.DirEntry, err error) error
walk_func = func(path string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("Failed to walk %s, %w", path, err)
}
if d.IsDir() {
if path == "." {
return nil
}
return fs.WalkDir(bucket_fs, path, walk_func)
}
r, err := bucket_fs.Open(path)
if err != nil {
return fmt.Errorf("Failed to open %s for reading, %w", path, err)
}
defer r.Close()
wr, err := gc_bucket.NewWriter(ctx, path, nil)
if err != nil {
return fmt.Errorf("Failed to create %s for writing, %w", path, err)
}
_, err = io.Copy(wr, r)
if err != nil {
return fmt.Errorf("Failed to copy %s, %w", path, err)
}
err = wr.Close()
if err != nil {
return fmt.Errorf("Failed to close %s, %w", path, err)
}
return nil
}
err = fs.WalkDir(bucket_fs, ".", walk_func)
if err != nil {
return nil, fmt.Errorf("Failed to walk filesystem, %w", err)
}
return gc_bucket, nil
}