-
Notifications
You must be signed in to change notification settings - Fork 5
/
fetch.go
112 lines (95 loc) · 2.78 KB
/
fetch.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
package main
import (
"context"
"encoding/hex"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/hedzr/progressbar"
"github.com/zeebo/blake3"
)
func fetchBinaryFromURLToDest(ctx context.Context, bar progressbar.PB, url, checksum, destination string) (string, error) {
// Create a new HTTP request with cache-control headers
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return "", fmt.Errorf("failed to create request for %s: %v", url, err)
}
// Add headers to disable caching
req.Header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
req.Header.Set("Pragma", "no-cache")
req.Header.Set("Expires", "0")
req.Header.Set("User-Agent", fmt.Sprintf("dbin/%s", Version))
// Perform the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
bar.UpdateRange(0, resp.ContentLength)
// Ensure the parent directory exists
if err := os.MkdirAll(filepath.Dir(destination), 0755); err != nil {
return "", fmt.Errorf("failed to create parent directories for %s: %v", destination, err)
}
// Create temp file
tempFile := destination + ".tmp"
out, err := os.Create(tempFile)
if err != nil {
return "", err
}
defer out.Close()
buf := make([]byte, 4096)
hash := blake3.New()
downloadLoop:
for {
select {
case <-ctx.Done():
_ = os.Remove(tempFile)
return "", ctx.Err()
default:
n, err := resp.Body.Read(buf)
if n > 0 {
if _, err = io.MultiWriter(out, hash, bar).Write(buf[:n]); err != nil {
_ = os.Remove(tempFile)
return "", err
}
}
if err == io.EOF {
break downloadLoop
}
if err != nil {
_ = os.Remove(tempFile)
return "", err
}
}
}
// Final checksum verification
if checksum != "" {
calculatedChecksum := hex.EncodeToString(hash.Sum(nil))
if calculatedChecksum != checksum && checksum != "null" {
fmt.Fprintf(os.Stderr, "checksum verification failed: expected %s, got %s\n", checksum, calculatedChecksum)
}
} else {
fmt.Println("Warning: No checksum exists for this binary in the metadata files, skipping verification.")
}
// Make a few corrections in case the downloaded binary is a nix object
if err := removeNixGarbageFoundInTheRepos(tempFile); err != nil {
_ = os.Remove(tempFile)
return "", err
}
if err := os.Rename(tempFile, destination); err != nil {
_ = os.Remove(tempFile)
return "", err
}
if err := os.Chmod(destination, 0755); err != nil {
_ = os.Remove(destination)
return "", fmt.Errorf("failed to set executable bit for %s: %v", destination, err)
}
// Mark the binary as ours
//if err := xattr.Set(destination, "user.ManagedBy", []byte("dbin")); err != nil {
// return "", fmt.Errorf("failed to set xattr for %s: %v", destination, err)
//}
return destination, nil
}