-
Notifications
You must be signed in to change notification settings - Fork 77
/
file.go
127 lines (107 loc) · 3.03 KB
/
file.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2020 The Cloudprober Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
Package file implements utilities to read files from various backends.
*/
package file
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"sync"
"time"
"golang.org/x/oauth2/google"
)
type cacheEntry struct {
b []byte
lastReload time.Time
}
var global = struct {
cache map[string]cacheEntry
mu sync.RWMutex
}{
cache: make(map[string]cacheEntry),
}
type readFunc func(path string) ([]byte, error)
type modTimeFunc func(path string) (time.Time, error)
var prefixToReadfunc = map[string]readFunc{
"gs://": readFileFromGCS,
}
var prefixToModTimeFunc = map[string]modTimeFunc{
"gs://": modTimeGCS,
}
func readFileFromGCS(objectPath string) ([]byte, error) {
hc, err := google.DefaultClient(context.Background())
if err != nil {
return nil, err
}
objURL := "https://storage.googleapis.com/" + objectPath
res, err := hc.Get(objURL)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("got error while retrieving GCS object, http status: %s, status code: %d", res.Status, res.StatusCode)
}
defer res.Body.Close()
return io.ReadAll(res.Body)
}
func modTimeGCS(objectPath string) (time.Time, error) {
return time.Time{}, errors.New("mod-time is not implemented for GCS files yet")
}
// ReadFile returns file contents as a slice of bytes. It's similar to ioutil's
// ReadFile, but includes support for files on non-disk locations. For example,
// files with paths starting with gs:// are assumed to be on GCS, and are read
// from GCS.
func ReadFile(fname string) ([]byte, error) {
for prefix, f := range prefixToReadfunc {
if strings.HasPrefix(fname, prefix) {
return f(fname[len(prefix):])
}
}
return os.ReadFile(fname)
}
func ReadWithCache(fname string, refreshInterval time.Duration) ([]byte, error) {
global.mu.RLock()
fc, ok := global.cache[fname]
global.mu.RUnlock()
if ok && (time.Since(fc.lastReload) < refreshInterval) {
return fc.b, nil
}
b, err := ReadFile(fname)
if err != nil {
return nil, err
}
global.mu.Lock()
global.cache[fname] = cacheEntry{b: b, lastReload: time.Now()}
global.mu.Unlock()
return b, nil
}
// ModTime returns file's modified timestamp.
func ModTime(fname string) (time.Time, error) {
for prefix, f := range prefixToModTimeFunc {
if strings.HasPrefix(fname, prefix) {
return f(fname[len(prefix):])
}
}
statInfo, err := os.Stat(fname)
if err != nil {
return time.Time{}, err
}
return statInfo.ModTime(), nil
}