query.go 10 KB

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