| 1 | package installlayout |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | const transientRetryAttempts = 10 |
| 9 | |
| 10 | // transientRetryDelay is a test seam; production backs off linearly to |
| 11 | // about eleven seconds, well inside the activation lock timeout. |
| 12 | var transientRetryDelay = time.Sleep |
| 13 | |
| 14 | // retryTransient repeats op while it fails with a Windows sharing, lock, or |
| 15 | // access-denied error. Antivirus and indexers hold freshly written files for |
| 16 | // moments, and MoveFileEx/DeleteFile report that instead of waiting. |
| 17 | func retryTransient(op func() error) error { |
| 18 | var err error |
| 19 | for attempt := 1; attempt <= transientRetryAttempts; attempt++ { |
| 20 | err = op() |
| 21 | if err == nil || !transientFileError(err) || attempt == transientRetryAttempts { |
| 22 | return err |
| 23 | } |
| 24 | transientRetryDelay(time.Duration(attempt) * 250 * time.Millisecond) |
| 25 | } |
| 26 | return err |
| 27 | } |
| 28 | |
| 29 | func renameRetry(oldPath, newPath string) error { |
| 30 | return retryTransient(func() error { return os.Rename(oldPath, newPath) }) |
| 31 | } |
| 32 | |
| 33 | func removeRetry(path string) error { |
| 34 | return retryTransient(func() error { return os.Remove(path) }) |
| 35 | } |
| 36 | |
| 37 | func removeAllRetry(path string) error { |
| 38 | return retryTransient(func() error { return os.RemoveAll(path) }) |
| 39 | } |
| 40 |