MCPcopy
hub / github.com/syncthing/syncthing / LoadConfigAtStartup

Function LoadConfigAtStartup

lib/syncthing/utils.go:88–118  ·  view source on GitHub ↗

LoadConfigAtStartup loads an existing config. If it doesn't yet exist, it creates a default one. Otherwise it checks the version, and archives and upgrades the config if necessary or returns an error, if the version isn't compatible.

(path string, cert tls.Certificate, evLogger events.Logger, allowNewerConfig, skipPortProbing bool)

Source from the content-addressed store, hash-verified

86// upgrades the config if necessary or returns an error, if the version
87// isn't compatible.
88func LoadConfigAtStartup(path string, cert tls.Certificate, evLogger events.Logger, allowNewerConfig, skipPortProbing bool) (config.Wrapper, error) {
89 myID := protocol.NewDeviceID(cert.Certificate[0])
90 cfg, originalVersion, err := config.Load(path, myID, evLogger)
91 if fs.IsNotExist(err) {
92 cfg, err = DefaultConfig(path, myID, evLogger, skipPortProbing)
93 if err != nil {
94 return nil, fmt.Errorf("failed to generate default config: %w", err)
95 }
96 err = cfg.Save()
97 if err != nil {
98 return nil, fmt.Errorf("failed to save default config: %w", err)
99 }
100 slog.Info("Default config saved; edit to taste (with Syncthing stopped) or use the GUI", slogutil.FilePath(cfg.ConfigPath()))
101 } else if errors.Is(err, io.EOF) {
102 return nil, errors.New("failed to load config: unexpected end of file. Truncated or empty configuration?")
103 } else if err != nil {
104 return nil, fmt.Errorf("failed to load config: %w", err)
105 }
106
107 if originalVersion != config.CurrentVersion {
108 if originalVersion > config.CurrentVersion && !allowNewerConfig {
109 return nil, fmt.Errorf("config file version (%d) is newer than supported version (%d); if this is expected, use --allow-newer-config to override", originalVersion, config.CurrentVersion)
110 }
111 err = archiveAndSaveConfig(cfg, originalVersion)
112 if err != nil {
113 return nil, fmt.Errorf("config archive: %w", err)
114 }
115 }
116
117 return cfg, nil
118}
119
120func archiveAndSaveConfig(cfg config.Wrapper, originalVersion int) error {
121 // Copy the existing config to an archive copy

Callers 1

syncthingMainMethod · 0.92

Calls 10

NewDeviceIDFunction · 0.92
LoadFunction · 0.92
IsNotExistFunction · 0.92
FilePathFunction · 0.92
DefaultConfigFunction · 0.85
archiveAndSaveConfigFunction · 0.85
SaveMethod · 0.65
ConfigPathMethod · 0.65
NewMethod · 0.65
IsMethod · 0.45

Tested by

no test coverage detected