diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2019-09-09 21:48:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-09 21:48:30 +0200 |
commit | 9a55bce9e4d7e1bb3d57dc8879e1a23f559e18ba (patch) | |
tree | b048b0e2228fa9ae67c9a73bd4fecf1739f64d75 /libpod/boltdb_state.go | |
parent | 7042a3d7a539bae79ed63bdc87f432b8ec73afd8 (diff) | |
parent | 046178e55f72ed9db7cf5898d3be91b0112ab94f (diff) | |
download | podman-9a55bce9e4d7e1bb3d57dc8879e1a23f559e18ba.tar.gz podman-9a55bce9e4d7e1bb3d57dc8879e1a23f559e18ba.tar.bz2 podman-9a55bce9e4d7e1bb3d57dc8879e1a23f559e18ba.zip |
Merge pull request #3896 from mheon/volume_lookup
Add ability to look up volumes by unambiguous partial name
Diffstat (limited to 'libpod/boltdb_state.go')
-rw-r--r-- | libpod/boltdb_state.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/libpod/boltdb_state.go b/libpod/boltdb_state.go index 31b551b36..4e7f78f13 100644 --- a/libpod/boltdb_state.go +++ b/libpod/boltdb_state.go @@ -1735,6 +1735,74 @@ func (s *BoltState) Volume(name string) (*Volume, error) { return volume, nil } +// LookupVolume locates a volume from a partial name. +func (s *BoltState) LookupVolume(name string) (*Volume, error) { + if name == "" { + return nil, define.ErrEmptyID + } + + if !s.valid { + return nil, define.ErrDBClosed + } + + volName := []byte(name) + + volume := new(Volume) + volume.config = new(VolumeConfig) + + db, err := s.getDBCon() + if err != nil { + return nil, err + } + defer s.deferredCloseDBCon(db) + + err = db.View(func(tx *bolt.Tx) error { + volBkt, err := getVolBucket(tx) + if err != nil { + return err + } + + allVolsBkt, err := getAllVolsBucket(tx) + if err != nil { + return err + } + + // Check for exact match on name + volDB := volBkt.Bucket(volName) + if volDB != nil { + return s.getVolumeFromDB(volName, volume, volBkt) + } + + // No exact match. Search all names. + foundMatch := false + err = allVolsBkt.ForEach(func(checkName, checkName2 []byte) error { + if strings.HasPrefix(string(checkName), name) { + if foundMatch { + return errors.Wrapf(define.ErrVolumeExists, "more than one result for volume name %q", name) + } + foundMatch = true + volName = checkName + } + return nil + }) + if err != nil { + return err + } + + if !foundMatch { + return errors.Wrapf(define.ErrNoSuchVolume, "no volume with name %q found", name) + } + + return s.getVolumeFromDB(volName, volume, volBkt) + }) + if err != nil { + return nil, err + } + + return volume, nil + +} + // HasVolume returns true if the given volume exists in the state, otherwise it returns false func (s *BoltState) HasVolume(name string) (bool, error) { if name == "" { |