2021-09-15 03:29:07 +01:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package paths
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-09-20 23:57:06 +01:00
|
|
|
|
|
|
|
"tailscale.com/types/logger"
|
2021-09-15 03:29:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2021-09-20 23:57:06 +01:00
|
|
|
func TryConfigFileMigration(logf logger.Logf, oldFile, newFile string) string {
|
2021-09-15 03:29:07 +01:00
|
|
|
_, err := os.Stat(newFile)
|
|
|
|
if err == nil {
|
|
|
|
// Common case for a system which has already been migrated.
|
|
|
|
return newFile
|
|
|
|
}
|
|
|
|
if !os.IsNotExist(err) {
|
2021-09-20 23:57:06 +01:00
|
|
|
logf("TryConfigFileMigration failed; new file: %v", err)
|
2021-09-15 03:29:07 +01:00
|
|
|
return newFile
|
|
|
|
}
|
|
|
|
|
|
|
|
contents, err := os.ReadFile(oldFile)
|
|
|
|
if err != nil {
|
|
|
|
// Common case for a new user.
|
|
|
|
return newFile
|
|
|
|
}
|
|
|
|
|
2021-09-21 22:00:30 +01:00
|
|
|
if err = MkStateDir(filepath.Dir(newFile)); err != nil {
|
|
|
|
logf("TryConfigFileMigration failed; MkStateDir: %v", err)
|
|
|
|
return oldFile
|
|
|
|
}
|
|
|
|
|
2021-09-15 03:29:07 +01:00
|
|
|
err = os.WriteFile(newFile, contents, 0600)
|
|
|
|
if err != nil {
|
|
|
|
removeErr := os.Remove(newFile)
|
|
|
|
if removeErr != nil {
|
2021-09-20 23:57:06 +01:00
|
|
|
logf("TryConfigFileMigration failed; write newFile no cleanup: %v, remove err: %v",
|
2021-09-15 03:29:07 +01:00
|
|
|
err, removeErr)
|
|
|
|
return oldFile
|
|
|
|
}
|
2021-09-20 23:57:06 +01:00
|
|
|
logf("TryConfigFileMigration failed; write newFile: %v", err)
|
2021-09-15 03:29:07 +01:00
|
|
|
return oldFile
|
|
|
|
}
|
|
|
|
|
2021-09-20 23:57:06 +01:00
|
|
|
logf("TryConfigFileMigration: successfully migrated: from %v to %v",
|
2021-09-15 03:29:07 +01:00
|
|
|
oldFile, newFile)
|
|
|
|
|
|
|
|
return newFile
|
|
|
|
}
|