Skip to content

Commit b1ea794

Browse files
authored
Bring back support for file:// sources (#86)
Solbuild 1.5.3.0 replace the way files are downloaded. The feature to download files using `file://` sources was lost. Restore the functionality to set sources using `file://` by copying the file when encountered. Resolves #38.
1 parent 1cabb85 commit b1ea794

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

builder/source/file.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package source
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/url"
7+
"os"
8+
)
9+
10+
const (
11+
dstFileMode = 0o644
12+
dstFileFlags = os.O_CREATE | os.O_TRUNC | os.O_WRONLY
13+
)
14+
15+
// IsFileURI returns true if the given URI is a file:// URI.
16+
func IsFileURI(uri *url.URL) bool {
17+
return uri.Scheme == "file"
18+
}
19+
20+
// CopyFile copies a file.
21+
func CopyFile(src, dst string) error {
22+
srcFile, err := os.Open(src)
23+
if err != nil {
24+
return fmt.Errorf("unable to open source file: %w", err)
25+
}
26+
27+
dstFile, err := os.OpenFile(dst, dstFileFlags, dstFileMode)
28+
if err != nil {
29+
return fmt.Errorf("unable to open destination file: %w", err)
30+
}
31+
32+
if _, err = io.Copy(dstFile, srcFile); err != nil {
33+
return fmt.Errorf("unable to copy file contents: %w", err)
34+
}
35+
36+
return nil
37+
}

builder/source/simple.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ func (s *SimpleSource) IsFetched() bool {
126126

127127
// download downloads simple files using go grab.
128128
func (s *SimpleSource) download(destination string) error {
129+
if IsFileURI(s.url) {
130+
return CopyFile(s.url.Path, destination)
131+
}
132+
129133
// Some web servers (*cough* sourceforge) have strange redirection behavior. It's possible to work around this by clearing the Referer header on every redirect
130134
headHttpClient := &http.Client{
131135
CheckRedirect: func(req *http.Request, via []*http.Request) error {

0 commit comments

Comments
 (0)