query.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "sync"
  7. alpm "github.com/jguer/go-alpm"
  8. rpc "github.com/mikkeloscar/aur"
  9. )
  10. // Query is a collection of Results
  11. type aurQuery []rpc.Pkg
  12. // Query holds the results of a repository search.
  13. type repoQuery []alpm.Package
  14. func (q aurQuery) Len() int {
  15. return len(q)
  16. }
  17. func (q aurQuery) Less(i, j int) bool {
  18. if config.SortMode == BottomUp {
  19. return q[i].NumVotes < q[j].NumVotes
  20. }
  21. return q[i].NumVotes > q[j].NumVotes
  22. }
  23. func (q aurQuery) Swap(i, j int) {
  24. q[i], q[j] = q[j], q[i]
  25. }
  26. // FilterPackages filters packages based on source and type from local repository.
  27. func filterPackages() (local []alpm.Package, remote []alpm.Package,
  28. localNames []string, remoteNames []string, err error) {
  29. localDb, err := alpmHandle.LocalDb()
  30. if err != nil {
  31. return
  32. }
  33. dbList, err := alpmHandle.SyncDbs()
  34. if err != nil {
  35. return
  36. }
  37. f := func(k alpm.Package) error {
  38. found := false
  39. // For each DB search for our secret package.
  40. _ = dbList.ForEach(func(d alpm.Db) error {
  41. if found {
  42. return nil
  43. }
  44. _, err := d.PkgByName(k.Name())
  45. if err == nil {
  46. found = true
  47. local = append(local, k)
  48. localNames = append(localNames, k.Name())
  49. }
  50. return nil
  51. })
  52. if !found {
  53. remote = append(remote, k)
  54. remoteNames = append(remoteNames, k.Name())
  55. }
  56. return nil
  57. }
  58. err = localDb.PkgCache().ForEach(f)
  59. return
  60. }
  61. // NarrowSearch searches AUR and narrows based on subarguments
  62. func narrowSearch(pkgS []string, sortS bool) (aurQuery, error) {
  63. if len(pkgS) == 0 {
  64. return nil, nil
  65. }
  66. r, err := rpc.Search(pkgS[0])
  67. if err != nil {
  68. return nil, err
  69. }
  70. if len(pkgS) == 1 {
  71. if sortS {
  72. sort.Sort(aurQuery(r))
  73. }
  74. return r, err
  75. }
  76. var aq aurQuery
  77. var n int
  78. for _, res := range r {
  79. match := true
  80. for _, pkgN := range pkgS[1:] {
  81. if !(strings.Contains(res.Name, pkgN) || strings.Contains(strings.ToLower(res.Description), pkgN)) {
  82. match = false
  83. break
  84. }
  85. }
  86. if match {
  87. n++
  88. aq = append(aq, res)
  89. }
  90. }
  91. if sortS {
  92. sort.Sort(aq)
  93. }
  94. return aq, err
  95. }
  96. // SyncSearch presents a query to the local repos and to the AUR.
  97. func syncSearch(pkgS []string) (err error) {
  98. aq, err := narrowSearch(pkgS, true)
  99. if err != nil {
  100. return err
  101. }
  102. pq, _, err := queryRepo(pkgS)
  103. if err != nil {
  104. return err
  105. }
  106. if config.SortMode == BottomUp {
  107. aq.printSearch(1)
  108. pq.printSearch()
  109. } else {
  110. pq.printSearch()
  111. aq.printSearch(1)
  112. }
  113. return nil
  114. }
  115. // SyncInfo serves as a pacman -Si for repo packages and AUR packages.
  116. func syncInfo(pkgS []string) (err error) {
  117. var info []rpc.Pkg
  118. aurS, repoS, err := packageSlices(pkgS)
  119. if err != nil {
  120. return
  121. }
  122. if len(aurS) != 0 {
  123. noDb := make([]string, 0, len(aurS))
  124. for _, pkg := range aurS {
  125. _, name := splitDbFromName(pkg)
  126. noDb = append(noDb, name)
  127. }
  128. info, err = aurInfo(noDb)
  129. if err != nil {
  130. fmt.Println(err)
  131. }
  132. }
  133. // Repo always goes first
  134. if len(repoS) != 0 {
  135. arguments := cmdArgs.copy()
  136. arguments.delTarget(aurS...)
  137. err = passToPacman(arguments)
  138. if err != nil {
  139. return
  140. }
  141. }
  142. if len(aurS) != 0 {
  143. for _, pkg := range info {
  144. PrintInfo(&pkg)
  145. }
  146. }
  147. return
  148. }
  149. // Search handles repo searches. Creates a RepoSearch struct.
  150. func queryRepo(pkgInputN []string) (s repoQuery, n int, err error) {
  151. dbList, err := alpmHandle.SyncDbs()
  152. if err != nil {
  153. return
  154. }
  155. // BottomUp functions
  156. initL := func(len int) int {
  157. if config.SortMode == TopDown {
  158. return 0
  159. }
  160. return len - 1
  161. }
  162. compL := func(len int, i int) bool {
  163. if config.SortMode == TopDown {
  164. return i < len
  165. }
  166. return i > -1
  167. }
  168. finalL := func(i int) int {
  169. if config.SortMode == TopDown {
  170. return i + 1
  171. }
  172. return i - 1
  173. }
  174. dbS := dbList.Slice()
  175. lenDbs := len(dbS)
  176. for f := initL(lenDbs); compL(lenDbs, f); f = finalL(f) {
  177. pkgS := dbS[f].PkgCache().Slice()
  178. lenPkgs := len(pkgS)
  179. for i := initL(lenPkgs); compL(lenPkgs, i); i = finalL(i) {
  180. match := true
  181. for _, pkgN := range pkgInputN {
  182. if !(strings.Contains(pkgS[i].Name(), pkgN) || strings.Contains(strings.ToLower(pkgS[i].Description()), pkgN)) {
  183. match = false
  184. break
  185. }
  186. }
  187. if match {
  188. n++
  189. s = append(s, pkgS[i])
  190. }
  191. }
  192. }
  193. return
  194. }
  195. // PackageSlices separates an input slice into aur and repo slices
  196. func packageSlices(toCheck []string) (aur []string, repo []string, err error) {
  197. dbList, err := alpmHandle.SyncDbs()
  198. if err != nil {
  199. return
  200. }
  201. for _, _pkg := range toCheck {
  202. db, name := splitDbFromName(_pkg)
  203. found := false
  204. if db == "aur" {
  205. aur = append(aur, _pkg)
  206. continue
  207. } else if db != "" {
  208. repo = append(repo, _pkg)
  209. continue
  210. }
  211. _ = dbList.ForEach(func(db alpm.Db) error {
  212. _, err := db.PkgByName(name)
  213. if err == nil {
  214. found = true
  215. return fmt.Errorf("")
  216. }
  217. return nil
  218. })
  219. if !found {
  220. _, errdb := dbList.PkgCachebyGroup(name)
  221. found = errdb == nil
  222. }
  223. if found {
  224. repo = append(repo, _pkg)
  225. } else {
  226. aur = append(aur, _pkg)
  227. }
  228. }
  229. return
  230. }
  231. // HangingPackages returns a list of packages installed as deps
  232. // and unneeded by the system
  233. // removeOptional decides whether optional dependencies are counted or not
  234. func hangingPackages(removeOptional bool) (hanging []string, err error) {
  235. localDb, err := alpmHandle.LocalDb()
  236. if err != nil {
  237. return
  238. }
  239. // safePackages represents every package in the system in one of 3 states
  240. // State = 0 - Remove package from the system
  241. // State = 1 - Keep package in the system; need to iterate over dependencies
  242. // State = 2 - Keep package and have iterated over dependencies
  243. safePackages := make(map[string]uint8)
  244. // provides stores a mapping from the provides name back to the original package name
  245. // Assumption - multiple installed packages don't provide the same dependency
  246. provides := make(map[string]string)
  247. packages := localDb.PkgCache()
  248. // Mark explicit dependencies and enumerate the provides list
  249. setupResources := func(pkg alpm.Package) error {
  250. if pkg.Reason() == alpm.PkgReasonExplicit {
  251. safePackages[pkg.Name()] = 1
  252. } else {
  253. safePackages[pkg.Name()] = 0
  254. }
  255. pkg.Provides().ForEach(func(dep alpm.Depend) error {
  256. provides[dep.Name] = pkg.Name()
  257. return nil
  258. })
  259. return nil
  260. }
  261. packages.ForEach(setupResources)
  262. iterateAgain := true
  263. processDependencies := func(pkg alpm.Package) error {
  264. if state, _ := safePackages[pkg.Name()]; state == 0 || state == 2 {
  265. return nil
  266. }
  267. safePackages[pkg.Name()] = 2
  268. // Update state for dependencies
  269. markDependencies := func(dep alpm.Depend) error {
  270. // Don't assume a dependency is installed
  271. state, ok := safePackages[dep.Name]
  272. if !ok {
  273. // Check if dep is a provides rather than actual package name
  274. if p, ok2 := provides[dep.Name]; ok2 {
  275. iterateAgain = true
  276. safePackages[p] = 1
  277. }
  278. return nil
  279. }
  280. if state == 0 {
  281. iterateAgain = true
  282. safePackages[dep.Name] = 1
  283. }
  284. return nil
  285. }
  286. pkg.Depends().ForEach(markDependencies)
  287. if !removeOptional {
  288. pkg.OptionalDepends().ForEach(markDependencies)
  289. }
  290. return nil
  291. }
  292. for iterateAgain {
  293. iterateAgain = false
  294. packages.ForEach(processDependencies)
  295. }
  296. // Build list of packages to be removed
  297. packages.ForEach(func(pkg alpm.Package) error {
  298. if safePackages[pkg.Name()] == 0 {
  299. hanging = append(hanging, pkg.Name())
  300. }
  301. return nil
  302. })
  303. return
  304. }
  305. // Statistics returns statistics about packages installed in system
  306. func statistics() (info struct {
  307. Totaln int
  308. Expln int
  309. TotalSize int64
  310. }, err error) {
  311. var tS int64 // TotalSize
  312. var nPkg int
  313. var ePkg int
  314. localDb, err := alpmHandle.LocalDb()
  315. if err != nil {
  316. return
  317. }
  318. for _, pkg := range localDb.PkgCache().Slice() {
  319. tS += pkg.ISize()
  320. nPkg++
  321. if pkg.Reason() == 0 {
  322. ePkg++
  323. }
  324. }
  325. info = struct {
  326. Totaln int
  327. Expln int
  328. TotalSize int64
  329. }{
  330. nPkg, ePkg, tS,
  331. }
  332. return
  333. }
  334. // Queries the aur for information about specified packages.
  335. // All packages should be queried in a single rpc request except when the number
  336. // of packages exceeds the number set in config.RequestSplitN.
  337. // If the number does exceed config.RequestSplitN multiple rpc requests will be
  338. // performed concurrently.
  339. func aurInfo(names []string) ([]rpc.Pkg, error) {
  340. info := make([]rpc.Pkg, 0, len(names))
  341. seen := make(map[string]int)
  342. var mux sync.Mutex
  343. var wg sync.WaitGroup
  344. var err error
  345. missing := make([]string, 0, len(names))
  346. orphans := make([]string, 0, len(names))
  347. outOfDate := make([]string, 0, len(names))
  348. makeRequest := func(n, max int) {
  349. defer wg.Done()
  350. tempInfo, requestErr := rpc.Info(names[n:max])
  351. if err != nil {
  352. return
  353. }
  354. if requestErr != nil {
  355. err = requestErr
  356. return
  357. }
  358. mux.Lock()
  359. info = append(info, tempInfo...)
  360. mux.Unlock()
  361. }
  362. for n := 0; n < len(names); n += config.RequestSplitN {
  363. max := min(len(names), n+config.RequestSplitN)
  364. wg.Add(1)
  365. go makeRequest(n, max)
  366. }
  367. wg.Wait()
  368. if err != nil {
  369. return info, err
  370. }
  371. for k, pkg := range info {
  372. seen[pkg.Name] = k
  373. }
  374. for _, name := range names {
  375. i, ok := seen[name]
  376. if !ok {
  377. missing = append(missing, name)
  378. continue
  379. }
  380. pkg := info[i]
  381. if pkg.Maintainer == "" {
  382. orphans = append(orphans, name)
  383. }
  384. if pkg.OutOfDate != 0 {
  385. outOfDate = append(outOfDate, name)
  386. }
  387. }
  388. if len(missing) > 0 {
  389. fmt.Print(bold(red(arrow + " Missing AUR Packages:")))
  390. for _, name := range missing {
  391. fmt.Print(" " + bold(magenta(name)))
  392. }
  393. fmt.Println()
  394. }
  395. if len(orphans) > 0 {
  396. fmt.Print(bold(red(arrow + " Orphaned AUR Packages:")))
  397. for _, name := range orphans {
  398. fmt.Print(" " + bold(magenta(name)))
  399. }
  400. fmt.Println()
  401. }
  402. if len(outOfDate) > 0 {
  403. fmt.Print(bold(red(arrow + " Out Of Date AUR Packages:")))
  404. for _, name := range outOfDate {
  405. fmt.Print(" " + bold(magenta(name)))
  406. }
  407. fmt.Println()
  408. }
  409. return info, nil
  410. }