| 1 | // Copyright 2020 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package proxydir provides functions for writing module data to a directory |
| 6 | // in proxy format, so that it can be used as a module proxy by setting |
| 7 | // GOPROXY="file://<dir>". |
| 8 | package proxydir |
| 9 | |
| 10 | import ( |
| 11 | "archive/zip" |
| 12 | "fmt" |
| 13 | "io" |
| 14 | "os" |
| 15 | "path/filepath" |
| 16 | "strings" |
| 17 | ) |
| 18 | |
| 19 | // WriteModuleVersion creates a directory in the proxy dir for a module. |
| 20 | func WriteModuleVersion(rootDir, module, ver string, files map[string][]byte) (rerr error) { |
| 21 | dir := filepath.Join(rootDir, module, "@v") |
| 22 | if err := os.MkdirAll(dir, 0755); err != nil { |
| 23 | return err |
| 24 | } |
| 25 | |
| 26 | // The go command checks for versions by looking at the "list" file. Since |
| 27 | // we are supporting multiple versions, create this file if it does not exist |
| 28 | // or append the version number to the preexisting file. |
| 29 | f, err := os.OpenFile(filepath.Join(dir, "list"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
| 30 | if err != nil { |
| 31 | return err |
| 32 | } |
| 33 | defer checkClose("list file", f, &rerr) |
| 34 | if _, err := f.WriteString(ver + "\n"); err != nil { |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | // Serve the go.mod file on the <version>.mod url, if it exists. Otherwise, |
| 39 | // serve a stub. |
| 40 | modContents, ok := files["go.mod"] |
| 41 | if !ok { |
| 42 | modContents = []byte("module " + module) |
| 43 | } |
| 44 | if err := os.WriteFile(filepath.Join(dir, ver+".mod"), modContents, 0644); err != nil { |
| 45 | return err |
| 46 | } |
| 47 | |
| 48 | // info file, just the bare bones. |
| 49 | infoContents := fmt.Appendf(nil, `{"Version": "%v", "Time":"2017-12-14T13:08:43Z"}`, ver) |
| 50 | if err := os.WriteFile(filepath.Join(dir, ver+".info"), infoContents, 0644); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | // zip of all the source files. |
| 55 | f, err = os.OpenFile(filepath.Join(dir, ver+".zip"), os.O_CREATE|os.O_WRONLY, 0644) |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | defer checkClose("zip file", f, &rerr) |
| 60 | z := zip.NewWriter(f) |
| 61 | defer checkClose("zip writer", z, &rerr) |
| 62 | for name, contents := range files { |
| 63 | zf, err := z.Create(module + "@" + ver + "/" + name) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | if _, err := zf.Write(contents); err != nil { |
| 68 | return err |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | func checkClose(name string, closer io.Closer, err *error) { |
| 76 | if cerr := closer.Close(); cerr != nil && *err == nil { |
| 77 | *err = fmt.Errorf("closing %s: %v", name, cerr) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // ToURL returns the file uri for a proxy directory. |
| 82 | func ToURL(dir string) string { |
| 83 | // file URLs on Windows must start with file:///. See golang.org/issue/6027. |
| 84 | path := filepath.ToSlash(dir) |
| 85 | if !strings.HasPrefix(path, "/") { |
| 86 | path = "/" + path |
| 87 | } |
| 88 | return "file://" + path |
| 89 | } |