download.go 9.4 KB

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