From df88f546b69a8c2c524b73d86f51ec65bb3390f4 Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Tue, 28 Feb 2023 10:43:37 +0100 Subject: [PATCH] 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 --- libpod/sqlite_state.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/libpod/sqlite_state.go b/libpod/sqlite_state.go index 2ae010c08d..499d1b966f 100644 --- a/libpod/sqlite_state.go +++ b/libpod/sqlite_state.go @@ -2046,24 +2046,25 @@ func (s *SQLiteState) LookupVolume(name string) (*Volume, error) { 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 { return nil, fmt.Errorf("querying database for volume %s: %w", name, err) } defer rows.Close() - var configJSON string - foundResult := false + var foundName, configJSON string for rows.Next() { - if foundResult { + if foundName != "" { 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) } - foundResult = true + if foundName == name { + break + } } - if !foundResult { + if foundName == "" { return nil, fmt.Errorf("no volume with name %q found: %w", name, define.ErrNoSuchVolume) }