/
util.go
111 lines (96 loc) · 2.97 KB
/
util.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
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 filesystem
import (
"context"
"fmt"
"io"
)
// Read fully reads the given file from the file system.
func Read(ctx context.Context, fs Interface, filename string) ([]byte, error) {
r, err := fs.OpenRead(ctx, filename)
if err != nil {
return nil, err
}
defer r.Close()
return io.ReadAll(r)
}
// Write writes the given content to the file system.
func Write(ctx context.Context, fs Interface, filename string, data []byte) error {
w, err := fs.OpenWrite(ctx, filename)
if err != nil {
return err
}
if _, err := w.Write(data); err != nil {
return err
}
return w.Close()
}
// Copy replicates the file at oldpath to newpath. Requires the paths to
// be on the same filesystem.
//
// If the file system implements Copier, it uses that, otherwise it does so manually.
func Copy(ctx context.Context, fs Interface, oldpath, newpath string) error {
if cp, ok := fs.(Copier); ok {
if err := cp.Copy(ctx, oldpath, newpath); err != nil {
return err
}
return nil
}
w, err := fs.OpenWrite(ctx, newpath)
if err != nil {
return err
}
r, err := fs.OpenRead(ctx, oldpath)
if err != nil {
return err
}
defer r.Close()
if _, err := io.Copy(w, r); err != nil {
return err
}
return w.Close()
}
// Rename moves the file at oldpath to newpath. Requires the paths to
// be on the same filesystem.
//
// Rename will use Renamer, Remover, and Copier interfaces if implemented.
// Renamer is tried first, and used if available. Otherwise, Rename requires
// Remover to be implemented, and calls Copy.
func Rename(ctx context.Context, fs Interface, oldpath, newpath string) error {
if rn, ok := fs.(Renamer); ok {
return rn.Rename(ctx, oldpath, newpath)
}
// Eagerly check if we can remove the temp files.
rm, ok := fs.(Remover)
if !ok {
return &unimplementedError{fs, "Remover", "Rename"}
}
if err := Copy(ctx, fs, oldpath, newpath); err != nil {
return err
}
// Clean up the old path.
if err := rm.Remove(ctx, oldpath); err != nil {
return err
}
return nil
}
type unimplementedError struct {
fs Interface
iface, mthd string
}
func (e *unimplementedError) Error() string {
return fmt.Sprintf("%T doesn't implement filesystem.%v: can't use filesystem.%v", e.fs, e.iface, e.mthd)
}