From 4b9501590eea6c8fe2fdec17dc67811590727559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 25 Mar 2022 14:08:09 +0100 Subject: [PATCH] Shaman: only add random suffix to checkout dir if already existing Only add a random suffix to the checkout dir if it is necessary to ensure uniqueness. If the client-supplied checkout directory doesn't exist yet, it will be used as-is. --- pkg/shaman/checkout/manager.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/shaman/checkout/manager.go b/pkg/shaman/checkout/manager.go index 1dabdbae..86cfdd77 100644 --- a/pkg/shaman/checkout/manager.go +++ b/pkg/shaman/checkout/manager.go @@ -110,11 +110,11 @@ func (m *Manager) PrepareCheckout(checkoutPath string) (ResolvedCheckoutInfo, er defer m.checkoutUniquenessMutex.Unlock() var lastErr error - // Just try 10 different random tokens. If that still doesn't work, fail. - for try := 0; try < 10; try++ { - randomisedPath := fmt.Sprintf("%s-%s", checkoutPath, randomisedToken()) + attemptCheckoutPath := checkoutPath - checkoutPaths, err := m.pathForCheckout(randomisedPath) + // Just try 10 different random suffixes. If that still doesn't work, fail. + for try := 0; try < 10; try++ { + checkoutPaths, err := m.pathForCheckout(attemptCheckoutPath) if err != nil { return ResolvedCheckoutInfo{}, err } @@ -127,13 +127,15 @@ func (m *Manager) PrepareCheckout(checkoutPath string) (ResolvedCheckoutInfo, er if stat, err := os.Stat(checkoutPaths.absolutePath); !os.IsNotExist(err) { if err == nil { // No error stat'ing this path, indicating it's an existing checkout. - // Just retry another random token. lastErr = ErrCheckoutAlreadyExists if stat.IsDir() { logger.Debug().Msg("shaman: checkout path exists") } else { logger.Warn().Msg("shaman: checkout path exists but is not a directory") } + + // Retry with (another) random suffix. + attemptCheckoutPath = fmt.Sprintf("%s-%s", checkoutPath, randomisedToken()) continue } // If it's any other error, it's really a problem on our side. Don't retry.