download.go 9.4 KB

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