download.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strings"
  10. "sync"
  11. alpm "github.com/Jguer/go-alpm"
  12. "github.com/Jguer/yay/v9/pkg/types"
  13. )
  14. // Decide what download method to use:
  15. // Use the config option when the destination does not already exits
  16. // If .git exists in the destination use git
  17. // Otherwise use a tarrball
  18. func shouldUseGit(path string) bool {
  19. _, err := os.Stat(path)
  20. if os.IsNotExist(err) {
  21. return config.GitClone
  22. }
  23. _, err = os.Stat(filepath.Join(path, ".git"))
  24. return err == nil || os.IsExist(err)
  25. }
  26. func downloadFile(path string, url string) (err error) {
  27. // Create the file
  28. out, err := os.Create(path)
  29. if err != nil {
  30. return err
  31. }
  32. defer out.Close()
  33. // Get the data
  34. resp, err := http.Get(url)
  35. if err != nil {
  36. return err
  37. }
  38. defer resp.Body.Close()
  39. // Writer the body to file
  40. _, err = io.Copy(out, resp.Body)
  41. return err
  42. }
  43. func gitHasDiff(path string, name string) (bool, error) {
  44. stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "HEAD", "HEAD@{upstream}"))
  45. if err != nil {
  46. return false, fmt.Errorf("%s%s", stderr, err)
  47. }
  48. lines := strings.Split(stdout, "\n")
  49. head := lines[0]
  50. upstream := lines[1]
  51. return head != upstream, nil
  52. }
  53. // TODO: yay-next passes args through the header, use that to unify ABS and AUR
  54. func gitDownloadABS(url string, path string, name string) (bool, error) {
  55. _, err := os.Stat(filepath.Join(path, name))
  56. if os.IsNotExist(err) {
  57. cmd := passToGit(path, "clone", "--no-progress", "--single-branch",
  58. "-b", "packages/"+name, url, name)
  59. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  60. _, stderr, err := capture(cmd)
  61. if err != nil {
  62. return false, fmt.Errorf("error cloning %s: %s", name, stderr)
  63. }
  64. return true, nil
  65. } else if err != nil {
  66. return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
  67. }
  68. cmd := passToGit(filepath.Join(path, name), "fetch")
  69. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  70. _, stderr, err := capture(cmd)
  71. if err != nil {
  72. return false, fmt.Errorf("error fetching %s: %s", name, stderr)
  73. }
  74. return true, nil
  75. }
  76. func gitDownload(url string, path string, name string) (bool, error) {
  77. _, err := os.Stat(filepath.Join(path, name, ".git"))
  78. if os.IsNotExist(err) {
  79. cmd := passToGit(path, "clone", "--no-progress", url, name)
  80. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  81. _, stderr, err := capture(cmd)
  82. if err != nil {
  83. return false, fmt.Errorf("error cloning %s: %s", name, stderr)
  84. }
  85. return true, nil
  86. } else if err != nil {
  87. return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
  88. }
  89. cmd := passToGit(filepath.Join(path, name), "fetch")
  90. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  91. _, stderr, err := capture(cmd)
  92. if err != nil {
  93. return false, fmt.Errorf("error fetching %s: %s", name, stderr)
  94. }
  95. return false, nil
  96. }
  97. func gitMerge(path string, name string) error {
  98. _, stderr, err := capture(passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD"))
  99. if err != nil {
  100. return fmt.Errorf("error resetting %s: %s", name, stderr)
  101. }
  102. _, stderr, err = capture(passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff"))
  103. if err != nil {
  104. return fmt.Errorf("error merging %s: %s", name, stderr)
  105. }
  106. return nil
  107. }
  108. // DownloadAndUnpack downloads url tgz and extracts to path.
  109. func downloadAndUnpack(url string, path string) error {
  110. err := os.MkdirAll(path, 0755)
  111. if err != nil {
  112. return err
  113. }
  114. fileName := filepath.Base(url)
  115. tarLocation := filepath.Join(path, fileName)
  116. defer os.Remove(tarLocation)
  117. err = downloadFile(tarLocation, url)
  118. if err != nil {
  119. return err
  120. }
  121. _, stderr, err := capture(exec.Command(config.TarBin, "-xf", tarLocation, "-C", path))
  122. if err != nil {
  123. return fmt.Errorf("%s", stderr)
  124. }
  125. return nil
  126. }
  127. func getPkgbuilds(pkgs []string) error {
  128. missing := false
  129. wd, err := os.Getwd()
  130. if err != nil {
  131. return err
  132. }
  133. pkgs = removeInvalidTargets(pkgs)
  134. aur, repo, err := packageSlices(pkgs)
  135. if err != nil {
  136. return err
  137. }
  138. for n := range aur {
  139. _, pkg := splitDBFromName(aur[n])
  140. aur[n] = pkg
  141. }
  142. info, err := aurInfoPrint(aur)
  143. if err != nil {
  144. return err
  145. }
  146. if len(repo) > 0 {
  147. missing, err = getPkgbuildsfromABS(repo, wd)
  148. if err != nil {
  149. return err
  150. }
  151. }
  152. if len(aur) > 0 {
  153. allBases := getBases(info)
  154. bases := make([]Base, 0)
  155. for _, base := range allBases {
  156. name := base.Pkgbase()
  157. _, err = os.Stat(filepath.Join(wd, name))
  158. switch {
  159. case err != nil && !os.IsNotExist(err):
  160. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  161. continue
  162. case os.IsNotExist(err), cmdArgs.existsArg("f", "force"), shouldUseGit(filepath.Join(wd, name)):
  163. if err = os.RemoveAll(filepath.Join(wd, name)); err != nil {
  164. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  165. continue
  166. }
  167. default:
  168. fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
  169. continue
  170. }
  171. bases = append(bases, base)
  172. }
  173. if _, err = downloadPkgbuilds(bases, nil, wd); err != nil {
  174. return err
  175. }
  176. missing = missing || len(aur) != len(info)
  177. }
  178. if missing {
  179. err = fmt.Errorf("")
  180. }
  181. return err
  182. }
  183. // GetPkgbuild downloads pkgbuild from the ABS.
  184. func getPkgbuildsfromABS(pkgs []string, path string) (bool, error) {
  185. var wg sync.WaitGroup
  186. var mux sync.Mutex
  187. var errs types.MultiError
  188. names := make(map[string]string)
  189. missing := make([]string, 0)
  190. downloaded := 0
  191. dbList, err := alpmHandle.SyncDBs()
  192. if err != nil {
  193. return false, err
  194. }
  195. for _, pkgN := range pkgs {
  196. var pkg *alpm.Package
  197. var err error
  198. var url string
  199. pkgDB, name := splitDBFromName(pkgN)
  200. if pkgDB != "" {
  201. if db, err := alpmHandle.SyncDBByName(pkgDB); err == nil {
  202. pkg = db.Pkg(name)
  203. }
  204. } else {
  205. dbList.ForEach(func(db alpm.DB) error {
  206. if pkg = db.Pkg(name); pkg != nil {
  207. return fmt.Errorf("")
  208. }
  209. return nil
  210. })
  211. }
  212. if pkg == nil {
  213. missing = append(missing, name)
  214. continue
  215. }
  216. name = pkg.Base()
  217. if name == "" {
  218. name = pkg.Name()
  219. }
  220. // TODO: Check existence with ls-remote
  221. // https://git.archlinux.org/svntogit/packages.git
  222. switch pkg.DB().Name() {
  223. case "core", "extra", "testing":
  224. url = "https://git.archlinux.org/svntogit/packages.git"
  225. case "community", "multilib", "community-testing", "multilib-testing":
  226. url = "https://git.archlinux.org/svntogit/community.git"
  227. default:
  228. missing = append(missing, name)
  229. continue
  230. }
  231. _, err = os.Stat(filepath.Join(path, name))
  232. switch {
  233. case err != nil && !os.IsNotExist(err):
  234. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  235. continue
  236. case os.IsNotExist(err), cmdArgs.existsArg("f", "force"):
  237. if err = os.RemoveAll(filepath.Join(path, name)); err != nil {
  238. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  239. continue
  240. }
  241. default:
  242. fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
  243. continue
  244. }
  245. names[name] = url
  246. }
  247. if len(missing) != 0 {
  248. fmt.Println(yellow(bold(smallArrow)), "Missing ABS packages: ", cyan(strings.Join(missing, " ")))
  249. }
  250. download := func(pkg string, url string) {
  251. defer wg.Done()
  252. if _, err := gitDownloadABS(url, config.BuildDir, pkg); err != nil {
  253. errs.Add(fmt.Errorf("%s Failed to get pkgbuild: %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(err.Error()))))
  254. return
  255. }
  256. _, stderr, err := capture(exec.Command("ln", "-s", filepath.Join(config.BuildDir, pkg, "trunk"), filepath.Join(path, pkg)))
  257. mux.Lock()
  258. downloaded++
  259. if err != nil {
  260. errs.Add(fmt.Errorf("%s Failed to link %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(stderr))))
  261. } else {
  262. fmt.Printf(bold(cyan("::"))+" Downloaded PKGBUILD from ABS (%d/%d): %s\n", downloaded, len(names), cyan(pkg))
  263. }
  264. mux.Unlock()
  265. }
  266. count := 0
  267. for name, url := range names {
  268. wg.Add(1)
  269. go download(name, url)
  270. count++
  271. if count%25 == 0 {
  272. wg.Wait()
  273. }
  274. }
  275. wg.Wait()
  276. errs.Add(os.RemoveAll(filepath.Join(config.BuildDir, "packages")))
  277. return len(missing) != 0, errs.Return()
  278. }