MCPcopy Index your code
hub / github.com/tailscale/tailscale / TryConfigFileMigration

Function TryConfigFileMigration

paths/migrate.go:20–58  ·  view source on GitHub ↗

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

(logf logger.Logf, oldFile, newFile string)

Source from the content-addressed store, hash-verified

18// - if oldFile exists but copying to newFile fails, return oldFile so
19// there will at least be some config to work with.
20func TryConfigFileMigration(logf logger.Logf, oldFile, newFile string) string {
21 _, err := os.Stat(newFile)
22 if err == nil {
23 // Common case for a system which has already been migrated.
24 return newFile
25 }
26 if !os.IsNotExist(err) {
27 logf("TryConfigFileMigration failed; new file: %v", err)
28 return newFile
29 }
30
31 contents, err := os.ReadFile(oldFile)
32 if err != nil {
33 // Common case for a new user.
34 return newFile
35 }
36
37 if err = MkStateDir(filepath.Dir(newFile)); err != nil {
38 logf("TryConfigFileMigration failed; MkStateDir: %v", err)
39 return oldFile
40 }
41
42 err = os.WriteFile(newFile, contents, 0600)
43 if err != nil {
44 removeErr := os.Remove(newFile)
45 if removeErr != nil {
46 logf("TryConfigFileMigration failed; write newFile no cleanup: %v, remove err: %v",
47 err, removeErr)
48 return oldFile
49 }
50 logf("TryConfigFileMigration failed; write newFile: %v", err)
51 return oldFile
52 }
53
54 logf("TryConfigFileMigration: successfully migrated: from %v to %v",
55 oldFile, newFile)
56
57 return newFile
58}

Callers 2

initMethod · 0.92

Calls 5

MkStateDirFunction · 0.85
StatMethod · 0.65
ReadFileMethod · 0.65
WriteFileMethod · 0.65
RemoveMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…