download.go 9.5 KB

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