list.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. package daemon
  2. import (
  3. "errors"
  4. "fmt"
  5. "sort"
  6. "strconv"
  7. "strings"
  8. "github.com/Sirupsen/logrus"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/api/types/filters"
  11. networktypes "github.com/docker/docker/api/types/network"
  12. "github.com/docker/docker/container"
  13. "github.com/docker/docker/image"
  14. "github.com/docker/docker/volume"
  15. "github.com/docker/go-connections/nat"
  16. )
  17. var acceptedVolumeFilterTags = map[string]bool{
  18. "dangling": true,
  19. "name": true,
  20. "driver": true,
  21. "label": true,
  22. }
  23. var acceptedPsFilterTags = map[string]bool{
  24. "ancestor": true,
  25. "before": true,
  26. "exited": true,
  27. "id": true,
  28. "isolation": true,
  29. "label": true,
  30. "name": true,
  31. "status": true,
  32. "health": true,
  33. "since": true,
  34. "volume": true,
  35. "network": true,
  36. "is-task": true,
  37. "publish": true,
  38. "expose": true,
  39. }
  40. // iterationAction represents possible outcomes happening during the container iteration.
  41. type iterationAction int
  42. // containerReducer represents a reducer for a container.
  43. // Returns the object to serialize by the api.
  44. type containerReducer func(*container.Container, *listContext) (*types.Container, error)
  45. const (
  46. // includeContainer is the action to include a container in the reducer.
  47. includeContainer iterationAction = iota
  48. // excludeContainer is the action to exclude a container in the reducer.
  49. excludeContainer
  50. // stopIteration is the action to stop iterating over the list of containers.
  51. stopIteration
  52. )
  53. // errStopIteration makes the iterator to stop without returning an error.
  54. var errStopIteration = errors.New("container list iteration stopped")
  55. // List returns an array of all containers registered in the daemon.
  56. func (daemon *Daemon) List() []*container.Container {
  57. return daemon.containers.List()
  58. }
  59. // listContext is the daemon generated filtering to iterate over containers.
  60. // This is created based on the user specification from types.ContainerListOptions.
  61. type listContext struct {
  62. // idx is the container iteration index for this context
  63. idx int
  64. // ancestorFilter tells whether it should check ancestors or not
  65. ancestorFilter bool
  66. // names is a list of container names to filter with
  67. names map[string][]string
  68. // images is a list of images to filter with
  69. images map[image.ID]bool
  70. // filters is a collection of arguments to filter with, specified by the user
  71. filters filters.Args
  72. // exitAllowed is a list of exit codes allowed to filter with
  73. exitAllowed []int
  74. // beforeFilter is a filter to ignore containers that appear before the one given
  75. beforeFilter *container.Container
  76. // sinceFilter is a filter to stop the filtering when the iterator arrive to the given container
  77. sinceFilter *container.Container
  78. // taskFilter tells if we should filter based on wether a container is part of a task
  79. taskFilter bool
  80. // isTask tells us if the we should filter container that are a task (true) or not (false)
  81. isTask bool
  82. // publish is a list of published ports to filter with
  83. publish map[nat.Port]bool
  84. // expose is a list of exposed ports to filter with
  85. expose map[nat.Port]bool
  86. // ContainerListOptions is the filters set by the user
  87. *types.ContainerListOptions
  88. }
  89. // byContainerCreated is a temporary type used to sort a list of containers by creation time.
  90. type byContainerCreated []*container.Container
  91. func (r byContainerCreated) Len() int { return len(r) }
  92. func (r byContainerCreated) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
  93. func (r byContainerCreated) Less(i, j int) bool {
  94. return r[i].Created.UnixNano() < r[j].Created.UnixNano()
  95. }
  96. // Containers returns the list of containers to show given the user's filtering.
  97. func (daemon *Daemon) Containers(config *types.ContainerListOptions) ([]*types.Container, error) {
  98. return daemon.reduceContainers(config, daemon.transformContainer)
  99. }
  100. func (daemon *Daemon) filterByNameIDMatches(ctx *listContext) []*container.Container {
  101. idSearch := false
  102. names := ctx.filters.Get("name")
  103. ids := ctx.filters.Get("id")
  104. if len(names)+len(ids) == 0 {
  105. // if name or ID filters are not in use, return to
  106. // standard behavior of walking the entire container
  107. // list from the daemon's in-memory store
  108. return daemon.List()
  109. }
  110. // idSearch will determine if we limit name matching to the IDs
  111. // matched from any IDs which were specified as filters
  112. if len(ids) > 0 {
  113. idSearch = true
  114. }
  115. matches := make(map[string]bool)
  116. // find ID matches; errors represent "not found" and can be ignored
  117. for _, id := range ids {
  118. if fullID, err := daemon.idIndex.Get(id); err == nil {
  119. matches[fullID] = true
  120. }
  121. }
  122. // look for name matches; if ID filtering was used, then limit the
  123. // search space to the matches map only; errors represent "not found"
  124. // and can be ignored
  125. if len(names) > 0 {
  126. for id, idNames := range ctx.names {
  127. // if ID filters were used and no matches on that ID were
  128. // found, continue to next ID in the list
  129. if idSearch && !matches[id] {
  130. continue
  131. }
  132. for _, eachName := range idNames {
  133. if ctx.filters.Match("name", eachName) {
  134. matches[id] = true
  135. }
  136. }
  137. }
  138. }
  139. cntrs := make([]*container.Container, 0, len(matches))
  140. for id := range matches {
  141. if c := daemon.containers.Get(id); c != nil {
  142. cntrs = append(cntrs, c)
  143. }
  144. }
  145. // Restore sort-order after filtering
  146. // Created gives us nanosec resolution for sorting
  147. sort.Sort(sort.Reverse(byContainerCreated(cntrs)))
  148. return cntrs
  149. }
  150. // reduceContainers parses the user's filtering options and generates the list of containers to return based on a reducer.
  151. func (daemon *Daemon) reduceContainers(config *types.ContainerListOptions, reducer containerReducer) ([]*types.Container, error) {
  152. var (
  153. containers = []*types.Container{}
  154. )
  155. ctx, err := daemon.foldFilter(config)
  156. if err != nil {
  157. return nil, err
  158. }
  159. // fastpath to only look at a subset of containers if specific name
  160. // or ID matches were provided by the user--otherwise we potentially
  161. // end up locking and querying many more containers than intended
  162. containerList := daemon.filterByNameIDMatches(ctx)
  163. for _, container := range containerList {
  164. t, err := daemon.reducePsContainer(container, ctx, reducer)
  165. if err != nil {
  166. if err != errStopIteration {
  167. return nil, err
  168. }
  169. break
  170. }
  171. if t != nil {
  172. containers = append(containers, t)
  173. ctx.idx++
  174. }
  175. }
  176. return containers, nil
  177. }
  178. // reducePsContainer is the basic representation for a container as expected by the ps command.
  179. func (daemon *Daemon) reducePsContainer(container *container.Container, ctx *listContext, reducer containerReducer) (*types.Container, error) {
  180. container.Lock()
  181. // filter containers to return
  182. action := includeContainerInList(container, ctx)
  183. switch action {
  184. case excludeContainer:
  185. container.Unlock()
  186. return nil, nil
  187. case stopIteration:
  188. container.Unlock()
  189. return nil, errStopIteration
  190. }
  191. // transform internal container struct into api structs
  192. newC, err := reducer(container, ctx)
  193. container.Unlock()
  194. if err != nil {
  195. return nil, err
  196. }
  197. // release lock because size calculation is slow
  198. if ctx.Size {
  199. sizeRw, sizeRootFs := daemon.getSize(newC.ID)
  200. newC.SizeRw = sizeRw
  201. newC.SizeRootFs = sizeRootFs
  202. }
  203. return newC, nil
  204. }
  205. // foldFilter generates the container filter based on the user's filtering options.
  206. func (daemon *Daemon) foldFilter(config *types.ContainerListOptions) (*listContext, error) {
  207. psFilters := config.Filters
  208. if err := psFilters.Validate(acceptedPsFilterTags); err != nil {
  209. return nil, err
  210. }
  211. var filtExited []int
  212. err := psFilters.WalkValues("exited", func(value string) error {
  213. code, err := strconv.Atoi(value)
  214. if err != nil {
  215. return err
  216. }
  217. filtExited = append(filtExited, code)
  218. return nil
  219. })
  220. if err != nil {
  221. return nil, err
  222. }
  223. err = psFilters.WalkValues("status", func(value string) error {
  224. if !container.IsValidStateString(value) {
  225. return fmt.Errorf("Unrecognised filter value for status: %s", value)
  226. }
  227. config.All = true
  228. return nil
  229. })
  230. if err != nil {
  231. return nil, err
  232. }
  233. var taskFilter, isTask bool
  234. if psFilters.Include("is-task") {
  235. if psFilters.ExactMatch("is-task", "true") {
  236. taskFilter = true
  237. isTask = true
  238. } else if psFilters.ExactMatch("is-task", "false") {
  239. taskFilter = true
  240. isTask = false
  241. } else {
  242. return nil, fmt.Errorf("Invalid filter 'is-task=%s'", psFilters.Get("is-task"))
  243. }
  244. }
  245. err = psFilters.WalkValues("health", func(value string) error {
  246. if !container.IsValidHealthString(value) {
  247. return fmt.Errorf("Unrecognised filter value for health: %s", value)
  248. }
  249. return nil
  250. })
  251. if err != nil {
  252. return nil, err
  253. }
  254. var beforeContFilter, sinceContFilter *container.Container
  255. err = psFilters.WalkValues("before", func(value string) error {
  256. beforeContFilter, err = daemon.GetContainer(value)
  257. return err
  258. })
  259. if err != nil {
  260. return nil, err
  261. }
  262. err = psFilters.WalkValues("since", func(value string) error {
  263. sinceContFilter, err = daemon.GetContainer(value)
  264. return err
  265. })
  266. if err != nil {
  267. return nil, err
  268. }
  269. imagesFilter := map[image.ID]bool{}
  270. var ancestorFilter bool
  271. if psFilters.Include("ancestor") {
  272. ancestorFilter = true
  273. psFilters.WalkValues("ancestor", func(ancestor string) error {
  274. id, err := daemon.GetImageID(ancestor)
  275. if err != nil {
  276. logrus.Warnf("Error while looking up for image %v", ancestor)
  277. return nil
  278. }
  279. if imagesFilter[id] {
  280. // Already seen this ancestor, skip it
  281. return nil
  282. }
  283. // Then walk down the graph and put the imageIds in imagesFilter
  284. populateImageFilterByParents(imagesFilter, id, daemon.imageStore.Children)
  285. return nil
  286. })
  287. }
  288. publishFilter := map[nat.Port]bool{}
  289. err = psFilters.WalkValues("publish", portOp("publish", publishFilter))
  290. if err != nil {
  291. return nil, err
  292. }
  293. exposeFilter := map[nat.Port]bool{}
  294. err = psFilters.WalkValues("expose", portOp("expose", exposeFilter))
  295. if err != nil {
  296. return nil, err
  297. }
  298. return &listContext{
  299. filters: psFilters,
  300. ancestorFilter: ancestorFilter,
  301. images: imagesFilter,
  302. exitAllowed: filtExited,
  303. beforeFilter: beforeContFilter,
  304. sinceFilter: sinceContFilter,
  305. taskFilter: taskFilter,
  306. isTask: isTask,
  307. publish: publishFilter,
  308. expose: exposeFilter,
  309. ContainerListOptions: config,
  310. names: daemon.nameIndex.GetAll(),
  311. }, nil
  312. }
  313. func portOp(key string, filter map[nat.Port]bool) func(value string) error {
  314. return func(value string) error {
  315. if strings.Contains(value, ":") {
  316. return fmt.Errorf("filter for '%s' should not contain ':': %s", key, value)
  317. }
  318. //support two formats, original format <portnum>/[<proto>] or <startport-endport>/[<proto>]
  319. proto, port := nat.SplitProtoPort(value)
  320. start, end, err := nat.ParsePortRange(port)
  321. if err != nil {
  322. return fmt.Errorf("error while looking up for %s %s: %s", key, value, err)
  323. }
  324. for i := start; i <= end; i++ {
  325. p, err := nat.NewPort(proto, strconv.FormatUint(i, 10))
  326. if err != nil {
  327. return fmt.Errorf("error while looking up for %s %s: %s", key, value, err)
  328. }
  329. filter[p] = true
  330. }
  331. return nil
  332. }
  333. }
  334. // includeContainerInList decides whether a container should be included in the output or not based in the filter.
  335. // It also decides if the iteration should be stopped or not.
  336. func includeContainerInList(container *container.Container, ctx *listContext) iterationAction {
  337. // Do not include container if it's in the list before the filter container.
  338. // Set the filter container to nil to include the rest of containers after this one.
  339. if ctx.beforeFilter != nil {
  340. if container.ID == ctx.beforeFilter.ID {
  341. ctx.beforeFilter = nil
  342. }
  343. return excludeContainer
  344. }
  345. // Stop iteration when the container arrives to the filter container
  346. if ctx.sinceFilter != nil {
  347. if container.ID == ctx.sinceFilter.ID {
  348. return stopIteration
  349. }
  350. }
  351. // Do not include container if it's stopped and we're not filters
  352. if !container.Running && !ctx.All && ctx.Limit <= 0 {
  353. return excludeContainer
  354. }
  355. // Do not include container if the name doesn't match
  356. if !ctx.filters.Match("name", container.Name) {
  357. return excludeContainer
  358. }
  359. // Do not include container if the id doesn't match
  360. if !ctx.filters.Match("id", container.ID) {
  361. return excludeContainer
  362. }
  363. if ctx.taskFilter {
  364. if ctx.isTask != container.Managed {
  365. return excludeContainer
  366. }
  367. }
  368. // Do not include container if any of the labels don't match
  369. if !ctx.filters.MatchKVList("label", container.Config.Labels) {
  370. return excludeContainer
  371. }
  372. // Do not include container if isolation doesn't match
  373. if excludeContainer == excludeByIsolation(container, ctx) {
  374. return excludeContainer
  375. }
  376. // Stop iteration when the index is over the limit
  377. if ctx.Limit > 0 && ctx.idx == ctx.Limit {
  378. return stopIteration
  379. }
  380. // Do not include container if its exit code is not in the filter
  381. if len(ctx.exitAllowed) > 0 {
  382. shouldSkip := true
  383. for _, code := range ctx.exitAllowed {
  384. if code == container.ExitCode() && !container.Running && !container.StartedAt.IsZero() {
  385. shouldSkip = false
  386. break
  387. }
  388. }
  389. if shouldSkip {
  390. return excludeContainer
  391. }
  392. }
  393. // Do not include container if its status doesn't match the filter
  394. if !ctx.filters.Match("status", container.State.StateString()) {
  395. return excludeContainer
  396. }
  397. // Do not include container if its health doesn't match the filter
  398. if !ctx.filters.ExactMatch("health", container.State.HealthString()) {
  399. return excludeContainer
  400. }
  401. if ctx.filters.Include("volume") {
  402. volumesByName := make(map[string]*volume.MountPoint)
  403. for _, m := range container.MountPoints {
  404. if m.Name != "" {
  405. volumesByName[m.Name] = m
  406. } else {
  407. volumesByName[m.Source] = m
  408. }
  409. }
  410. volumeExist := fmt.Errorf("volume mounted in container")
  411. err := ctx.filters.WalkValues("volume", func(value string) error {
  412. if _, exist := container.MountPoints[value]; exist {
  413. return volumeExist
  414. }
  415. if _, exist := volumesByName[value]; exist {
  416. return volumeExist
  417. }
  418. return nil
  419. })
  420. if err != volumeExist {
  421. return excludeContainer
  422. }
  423. }
  424. if ctx.ancestorFilter {
  425. if len(ctx.images) == 0 {
  426. return excludeContainer
  427. }
  428. if !ctx.images[container.ImageID] {
  429. return excludeContainer
  430. }
  431. }
  432. networkExist := fmt.Errorf("container part of network")
  433. if ctx.filters.Include("network") {
  434. err := ctx.filters.WalkValues("network", func(value string) error {
  435. if _, ok := container.NetworkSettings.Networks[value]; ok {
  436. return networkExist
  437. }
  438. for _, nw := range container.NetworkSettings.Networks {
  439. if nw.EndpointSettings == nil {
  440. continue
  441. }
  442. if strings.HasPrefix(nw.NetworkID, value) {
  443. return networkExist
  444. }
  445. }
  446. return nil
  447. })
  448. if err != networkExist {
  449. return excludeContainer
  450. }
  451. }
  452. if len(ctx.publish) > 0 {
  453. shouldSkip := true
  454. for port := range ctx.publish {
  455. if _, ok := container.HostConfig.PortBindings[port]; ok {
  456. shouldSkip = false
  457. break
  458. }
  459. }
  460. if shouldSkip {
  461. return excludeContainer
  462. }
  463. }
  464. if len(ctx.expose) > 0 {
  465. shouldSkip := true
  466. for port := range ctx.expose {
  467. if _, ok := container.Config.ExposedPorts[port]; ok {
  468. shouldSkip = false
  469. break
  470. }
  471. }
  472. if shouldSkip {
  473. return excludeContainer
  474. }
  475. }
  476. return includeContainer
  477. }
  478. // transformContainer generates the container type expected by the docker ps command.
  479. func (daemon *Daemon) transformContainer(container *container.Container, ctx *listContext) (*types.Container, error) {
  480. newC := &types.Container{
  481. ID: container.ID,
  482. Names: ctx.names[container.ID],
  483. ImageID: container.ImageID.String(),
  484. }
  485. if newC.Names == nil {
  486. // Dead containers will often have no name, so make sure the response isn't null
  487. newC.Names = []string{}
  488. }
  489. image := container.Config.Image // if possible keep the original ref
  490. if image != container.ImageID.String() {
  491. id, err := daemon.GetImageID(image)
  492. if _, isDNE := err.(ErrImageDoesNotExist); err != nil && !isDNE {
  493. return nil, err
  494. }
  495. if err != nil || id != container.ImageID {
  496. image = container.ImageID.String()
  497. }
  498. }
  499. newC.Image = image
  500. if len(container.Args) > 0 {
  501. args := []string{}
  502. for _, arg := range container.Args {
  503. if strings.Contains(arg, " ") {
  504. args = append(args, fmt.Sprintf("'%s'", arg))
  505. } else {
  506. args = append(args, arg)
  507. }
  508. }
  509. argsAsString := strings.Join(args, " ")
  510. newC.Command = fmt.Sprintf("%s %s", container.Path, argsAsString)
  511. } else {
  512. newC.Command = container.Path
  513. }
  514. newC.Created = container.Created.Unix()
  515. newC.State = container.State.StateString()
  516. newC.Status = container.State.String()
  517. newC.HostConfig.NetworkMode = string(container.HostConfig.NetworkMode)
  518. // copy networks to avoid races
  519. networks := make(map[string]*networktypes.EndpointSettings)
  520. for name, network := range container.NetworkSettings.Networks {
  521. if network == nil || network.EndpointSettings == nil {
  522. continue
  523. }
  524. networks[name] = &networktypes.EndpointSettings{
  525. EndpointID: network.EndpointID,
  526. Gateway: network.Gateway,
  527. IPAddress: network.IPAddress,
  528. IPPrefixLen: network.IPPrefixLen,
  529. IPv6Gateway: network.IPv6Gateway,
  530. GlobalIPv6Address: network.GlobalIPv6Address,
  531. GlobalIPv6PrefixLen: network.GlobalIPv6PrefixLen,
  532. MacAddress: network.MacAddress,
  533. NetworkID: network.NetworkID,
  534. }
  535. if network.IPAMConfig != nil {
  536. networks[name].IPAMConfig = &networktypes.EndpointIPAMConfig{
  537. IPv4Address: network.IPAMConfig.IPv4Address,
  538. IPv6Address: network.IPAMConfig.IPv6Address,
  539. }
  540. }
  541. }
  542. newC.NetworkSettings = &types.SummaryNetworkSettings{Networks: networks}
  543. newC.Ports = []types.Port{}
  544. for port, bindings := range container.NetworkSettings.Ports {
  545. p, err := nat.ParsePort(port.Port())
  546. if err != nil {
  547. return nil, err
  548. }
  549. if len(bindings) == 0 {
  550. newC.Ports = append(newC.Ports, types.Port{
  551. PrivatePort: uint16(p),
  552. Type: port.Proto(),
  553. })
  554. continue
  555. }
  556. for _, binding := range bindings {
  557. h, err := nat.ParsePort(binding.HostPort)
  558. if err != nil {
  559. return nil, err
  560. }
  561. newC.Ports = append(newC.Ports, types.Port{
  562. PrivatePort: uint16(p),
  563. PublicPort: uint16(h),
  564. Type: port.Proto(),
  565. IP: binding.HostIP,
  566. })
  567. }
  568. }
  569. newC.Labels = container.Config.Labels
  570. newC.Mounts = addMountPoints(container)
  571. return newC, nil
  572. }
  573. // Volumes lists known volumes, using the filter to restrict the range
  574. // of volumes returned.
  575. func (daemon *Daemon) Volumes(filter string) ([]*types.Volume, []string, error) {
  576. var (
  577. volumesOut []*types.Volume
  578. )
  579. volFilters, err := filters.FromParam(filter)
  580. if err != nil {
  581. return nil, nil, err
  582. }
  583. if err := volFilters.Validate(acceptedVolumeFilterTags); err != nil {
  584. return nil, nil, err
  585. }
  586. volumes, warnings, err := daemon.volumes.List()
  587. if err != nil {
  588. return nil, nil, err
  589. }
  590. filterVolumes, err := daemon.filterVolumes(volumes, volFilters)
  591. if err != nil {
  592. return nil, nil, err
  593. }
  594. for _, v := range filterVolumes {
  595. apiV := volumeToAPIType(v)
  596. if vv, ok := v.(interface {
  597. CachedPath() string
  598. }); ok {
  599. apiV.Mountpoint = vv.CachedPath()
  600. } else {
  601. apiV.Mountpoint = v.Path()
  602. }
  603. volumesOut = append(volumesOut, apiV)
  604. }
  605. return volumesOut, warnings, nil
  606. }
  607. // filterVolumes filters volume list according to user specified filter
  608. // and returns user chosen volumes
  609. func (daemon *Daemon) filterVolumes(vols []volume.Volume, filter filters.Args) ([]volume.Volume, error) {
  610. // if filter is empty, return original volume list
  611. if filter.Len() == 0 {
  612. return vols, nil
  613. }
  614. var retVols []volume.Volume
  615. for _, vol := range vols {
  616. if filter.Include("name") {
  617. if !filter.Match("name", vol.Name()) {
  618. continue
  619. }
  620. }
  621. if filter.Include("driver") {
  622. if !filter.ExactMatch("driver", vol.DriverName()) {
  623. continue
  624. }
  625. }
  626. if filter.Include("label") {
  627. v, ok := vol.(volume.DetailedVolume)
  628. if !ok {
  629. continue
  630. }
  631. if !filter.MatchKVList("label", v.Labels()) {
  632. continue
  633. }
  634. }
  635. retVols = append(retVols, vol)
  636. }
  637. danglingOnly := false
  638. if filter.Include("dangling") {
  639. if filter.ExactMatch("dangling", "true") || filter.ExactMatch("dangling", "1") {
  640. danglingOnly = true
  641. } else if !filter.ExactMatch("dangling", "false") && !filter.ExactMatch("dangling", "0") {
  642. return nil, fmt.Errorf("Invalid filter 'dangling=%s'", filter.Get("dangling"))
  643. }
  644. retVols = daemon.volumes.FilterByUsed(retVols, !danglingOnly)
  645. }
  646. return retVols, nil
  647. }
  648. func populateImageFilterByParents(ancestorMap map[image.ID]bool, imageID image.ID, getChildren func(image.ID) []image.ID) {
  649. if !ancestorMap[imageID] {
  650. for _, id := range getChildren(imageID) {
  651. populateImageFilterByParents(ancestorMap, id, getChildren)
  652. }
  653. ancestorMap[imageID] = true
  654. }
  655. }