mirror of
https://github.com/containers/podman.git
synced 2025-05-22 01:27:07 +08:00
sqlite: LookupVolume: fix partial name match
A partial name match is tricky as we want it to be fast but also make sure there's only one partial match iff there's no full one. [NO NEW TESTS NEEDED] as it fixes a system test. Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
This commit is contained in:
@ -2046,24 +2046,25 @@ func (s *SQLiteState) LookupVolume(name string) (*Volume, error) {
|
|||||||
return nil, define.ErrDBClosed
|
return nil, define.ErrDBClosed
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := s.conn.Query("SELECT JSON FROM VolumeConfig WHERE Name LIKE ?;", name+"%")
|
rows, err := s.conn.Query("SELECT Name, JSON FROM VolumeConfig WHERE Name LIKE ? ORDER BY LENGTH(Name) ASC;", name+"%")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("querying database for volume %s: %w", name, err)
|
return nil, fmt.Errorf("querying database for volume %s: %w", name, err)
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
var configJSON string
|
var foundName, configJSON string
|
||||||
foundResult := false
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
if foundResult {
|
if foundName != "" {
|
||||||
return nil, fmt.Errorf("more than one result for volume name %s: %w", name, define.ErrVolumeExists)
|
return nil, fmt.Errorf("more than one result for volume name %s: %w", name, define.ErrVolumeExists)
|
||||||
}
|
}
|
||||||
if err := rows.Scan(&configJSON); err != nil {
|
if err := rows.Scan(&foundName, &configJSON); err != nil {
|
||||||
return nil, fmt.Errorf("retrieving volume %s config from database: %w", name, err)
|
return nil, fmt.Errorf("retrieving volume %s config from database: %w", name, err)
|
||||||
}
|
}
|
||||||
foundResult = true
|
if foundName == name {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
if !foundResult {
|
}
|
||||||
|
if foundName == "" {
|
||||||
return nil, fmt.Errorf("no volume with name %q found: %w", name, define.ErrNoSuchVolume)
|
return nil, fmt.Errorf("no volume with name %q found: %w", name, define.ErrNoSuchVolume)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user