diff options
| author | Nick Khyl <nickk@tailscale.com> | 2024-12-05 13:16:48 -0600 |
|---|---|---|
| committer | Nick Khyl <nickk@tailscale.com> | 2024-12-05 13:16:48 -0600 |
| commit | 0267fe83b200f1702a2fa0a395442c02a053fadb (patch) | |
| tree | 63654c55225eeb834de59a5a0bc8d19033c6145b /paths/migrate.go | |
| parent | 87546a5edf6b6503a87eeb2d666baba57398a066 (diff) | |
| download | tailscale-1.78.0.tar.xz tailscale-1.78.0.zip | |
VERSION.txt: this is v1.78.0v1.78.0
Signed-off-by: Nick Khyl <nickk@tailscale.com>
Diffstat (limited to 'paths/migrate.go')
| -rw-r--r-- | paths/migrate.go | 116 |
1 files changed, 58 insertions, 58 deletions
diff --git a/paths/migrate.go b/paths/migrate.go index 3a23ecca3..11d90a627 100644 --- a/paths/migrate.go +++ b/paths/migrate.go @@ -1,58 +1,58 @@ -// Copyright (c) Tailscale Inc & AUTHORS -// SPDX-License-Identifier: BSD-3-Clause - -package paths - -import ( - "os" - "path/filepath" - - "tailscale.com/types/logger" -) - -// TryConfigFileMigration carefully copies the contents of oldFile to -// newFile, returning the path which should be used to read the config. -// - if newFile already exists, don't modify it just return its path -// - if neither oldFile nor newFile exist, return newFile for a fresh -// default config to be written to. -// - if oldFile exists but copying to newFile fails, return oldFile so -// there will at least be some config to work with. -func TryConfigFileMigration(logf logger.Logf, oldFile, newFile string) string { - _, err := os.Stat(newFile) - if err == nil { - // Common case for a system which has already been migrated. - return newFile - } - if !os.IsNotExist(err) { - logf("TryConfigFileMigration failed; new file: %v", err) - return newFile - } - - contents, err := os.ReadFile(oldFile) - if err != nil { - // Common case for a new user. - return newFile - } - - if err = MkStateDir(filepath.Dir(newFile)); err != nil { - logf("TryConfigFileMigration failed; MkStateDir: %v", err) - return oldFile - } - - err = os.WriteFile(newFile, contents, 0600) - if err != nil { - removeErr := os.Remove(newFile) - if removeErr != nil { - logf("TryConfigFileMigration failed; write newFile no cleanup: %v, remove err: %v", - err, removeErr) - return oldFile - } - logf("TryConfigFileMigration failed; write newFile: %v", err) - return oldFile - } - - logf("TryConfigFileMigration: successfully migrated: from %v to %v", - oldFile, newFile) - - return newFile -} +// Copyright (c) Tailscale Inc & AUTHORS
+// SPDX-License-Identifier: BSD-3-Clause
+
+package paths
+
+import (
+ "os"
+ "path/filepath"
+
+ "tailscale.com/types/logger"
+)
+
+// TryConfigFileMigration carefully copies the contents of oldFile to
+// newFile, returning the path which should be used to read the config.
+// - if newFile already exists, don't modify it just return its path
+// - if neither oldFile nor newFile exist, return newFile for a fresh
+// default config to be written to.
+// - if oldFile exists but copying to newFile fails, return oldFile so
+// there will at least be some config to work with.
+func TryConfigFileMigration(logf logger.Logf, oldFile, newFile string) string {
+ _, err := os.Stat(newFile)
+ if err == nil {
+ // Common case for a system which has already been migrated.
+ return newFile
+ }
+ if !os.IsNotExist(err) {
+ logf("TryConfigFileMigration failed; new file: %v", err)
+ return newFile
+ }
+
+ contents, err := os.ReadFile(oldFile)
+ if err != nil {
+ // Common case for a new user.
+ return newFile
+ }
+
+ if err = MkStateDir(filepath.Dir(newFile)); err != nil {
+ logf("TryConfigFileMigration failed; MkStateDir: %v", err)
+ return oldFile
+ }
+
+ err = os.WriteFile(newFile, contents, 0600)
+ if err != nil {
+ removeErr := os.Remove(newFile)
+ if removeErr != nil {
+ logf("TryConfigFileMigration failed; write newFile no cleanup: %v, remove err: %v",
+ err, removeErr)
+ return oldFile
+ }
+ logf("TryConfigFileMigration failed; write newFile: %v", err)
+ return oldFile
+ }
+
+ logf("TryConfigFileMigration: successfully migrated: from %v to %v",
+ oldFile, newFile)
+
+ return newFile
+}
|
