-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathremote.go
More file actions
143 lines (128 loc) · 4.03 KB
/
remote.go
File metadata and controls
143 lines (128 loc) · 4.03 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"archive/tar"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/creativeprojects/clog"
"github.com/creativeprojects/resticprofile/constants"
"github.com/creativeprojects/resticprofile/fuse"
"github.com/creativeprojects/resticprofile/remote"
)
func loadRemoteFiles(ctx context.Context, endpoint string) ([]fuse.File, *remote.Manifest, error) {
var parameters *remote.Manifest
client := http.DefaultClient
request, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, http.NoBody)
if err != nil {
return nil, nil, fmt.Errorf("failed to create request: %w", err)
}
request.Header.Set("Accept", "application/x-tar")
resp, err := client.Do(request)
if err != nil {
return nil, nil, fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
buf := &bytes.Buffer{}
_, _ = buf.ReadFrom(resp.Body)
return nil, nil, fmt.Errorf("http error %d: %q", resp.StatusCode, strings.TrimSpace(buf.String()))
}
if resp.Header.Get("Content-Type") != "application/x-tar" {
return nil, nil, fmt.Errorf("unexpected content type: %s", resp.Header.Get("Content-Type"))
}
files := []fuse.File{}
reader := tar.NewReader(resp.Body)
for {
hdr, err := reader.Next()
if errors.Is(err, io.EOF) {
break // End of archive
}
if err != nil {
return nil, nil, fmt.Errorf("failed to read tar header: %w", err)
}
if !filepath.IsLocal(hdr.Name) {
return nil, nil, fmt.Errorf("invalid file name: %s", hdr.Name)
}
if hdr.Size < 0 {
return nil, nil, fmt.Errorf("invalid file size %d", hdr.Size)
}
if hdr.Name == constants.ManifestFilename {
clog.Debugf("downloading manifest (%d bytes)", hdr.Size)
parameters, err = getManifestParameters(reader)
if err != nil {
return nil, nil, fmt.Errorf("failed to read manifest: %w", err)
}
} else {
clog.Debugf("downloading file %s (%d bytes)", hdr.Name, hdr.Size)
data := make([]byte, hdr.Size)
read, err := reader.Read(data) // will read the entire file
if err != nil && err != io.EOF {
return nil, nil, fmt.Errorf("failed to download file content: %w", err)
}
if read != int(hdr.Size) {
return nil, nil, fmt.Errorf("file size mismatch: expected %d, got %d", hdr.Size, read)
}
files = append(files, *fuse.NewFile(hdr.Name, hdr.FileInfo(), data))
}
}
return files, parameters, nil
}
func getManifestParameters(reader io.Reader) (*remote.Manifest, error) {
manifest := &remote.Manifest{}
decoder := json.NewDecoder(reader)
err := decoder.Decode(manifest)
if err != nil {
return nil, fmt.Errorf("failed to decode manifest: %w", err)
}
return manifest, nil
}
// setupRemoteConfiguration downloads the configuration files from the remote endpoint and mounts the virtual FS
func setupRemoteConfiguration(ctx context.Context, remoteEndpoint string) (func(), *remote.Manifest, error) {
files, parameters, err := loadRemoteFiles(ctx, remoteEndpoint)
if err != nil {
return nil, nil, err
}
if parameters == nil {
return nil, nil, fmt.Errorf("manifest file %q not found in remote configuration", constants.ManifestFilename)
}
clog.Debugf("using configuration file from manifest: %q", parameters.ConfigurationFile)
closeMountpoint := func() {}
mountpoint := parameters.Mountpoint
if mountpoint == "" {
// generates a temporary directory
mountpoint, err = os.MkdirTemp("", "resticprofile-")
if err != nil {
return nil, parameters, fmt.Errorf("failed to create mount directory: %w", err)
}
closeMountpoint = func() {
err = os.Remove(mountpoint)
if err != nil {
clog.Errorf("failed to remove mountpoint: %v", err)
}
}
}
closeFs, err := fuse.MountFS(mountpoint, files)
if err != nil {
return closeMountpoint, parameters, err
}
wd, _ := os.Getwd()
err = os.Chdir(mountpoint)
if err != nil {
return func() {
closeFs()
closeMountpoint()
}, parameters, fmt.Errorf("failed to change directory: %w", err)
}
return func() {
_ = os.Chdir(wd)
closeFs()
closeMountpoint()
}, parameters, nil
}