isFirstRun checks if this is the first time docker agent is being run. It atomically creates a marker file in the user's config directory using os.O_EXCL to avoid a race condition when multiple processes start concurrently.
()
| 322 | // using os.O_EXCL to avoid a race condition when multiple processes |
| 323 | // start concurrently. |
| 324 | func isFirstRun() bool { |
| 325 | configDir := paths.GetConfigDir() |
| 326 | markerFile := filepath.Join(configDir, ".cagent_first_run") |
| 327 | |
| 328 | // Ensure the config directory exists before trying to create the marker file |
| 329 | if err := os.MkdirAll(configDir, 0o700); err != nil { |
| 330 | slog.Warn("Failed to create config directory for first run marker", "error", err) |
| 331 | return false |
| 332 | } |
| 333 | |
| 334 | // Atomically create the marker file. If it already exists, OpenFile returns an error. |
| 335 | f, err := os.OpenFile(markerFile, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o644) //nolint:gosec // empty marker file with no sensitive content |
| 336 | if err != nil { |
| 337 | return false // File already exists or other error, not first run |
| 338 | } |
| 339 | if err := f.Close(); err != nil { |
| 340 | slog.Warn("Failed to close first run marker file", "error", err) |
| 341 | } |
| 342 | |
| 343 | return true |
| 344 | } |
no test coverage detected