11package packages
22
33import (
4+ "crypto/rand"
5+ "encoding/hex"
46 "encoding/json"
57 "fmt"
68 "os"
@@ -27,9 +29,17 @@ type MigrationReport struct {
2729 ToVersion string `json:"to_version"`
2830 Interrupted bool `json:"interrupted"`
2931 Results []PackageResult `json:"results"`
32+
33+ // tag is the on-disk-filename discriminator; assigned lazily on
34+ // the first Path()/Save() call. Not serialized — it's purely a
35+ // filename concern, not part of the report's content.
36+ tag string
3037}
3138
32- // NewMigrationReport creates a fresh report for a migration.
39+ // NewMigrationReport creates a fresh report for a migration. The
40+ // returned report has a unique 4-byte hex tag in its Timestamp-derived
41+ // filename (see Path/Save) so two upgrades running within the same
42+ // wall-clock second can't accidentally clobber each other's report.
3343func NewMigrationReport (manager , fromVersion , toVersion string ) * MigrationReport {
3444 return & MigrationReport {
3545 Timestamp : time .Now (),
@@ -39,24 +49,80 @@ func NewMigrationReport(manager, fromVersion, toVersion string) *MigrationReport
3949 }
4050}
4151
42- // Save writes the report to ~/.nodeup/reports/<timestamp>.json.
52+ // Save writes the report to <DataDir>/reports/migration-<ts>-<tag>.json.
53+ // The first call computes a stable 4-byte hex tag and caches it on the
54+ // report; subsequent calls (Path and Save) reuse the same tag, so the
55+ // CLI can call Path() before Save() to print the path and have it
56+ // match the file that Save() actually writes.
57+ //
58+ // The tag uses crypto/rand entropy, so two reports produced within the
59+ // same wall-clock second get distinct filenames. 32 bits of entropy is
60+ // plenty to make accidental collisions vanishingly unlikely across the
61+ // lifetime of a single machine's DataDir (~1 report per upgrade,
62+ // upgrades are minutes-to-hours apart for a real user).
63+ //
64+ // Save is NOT safe to call concurrently on the same report instance:
65+ // the tag is assigned once and shared across Path/Save, and two
66+ // parallel Save calls would race to write the same file. Callers
67+ // don't actually need concurrent saves — a single upgrade writes at
68+ // most one report.
4369func (r * MigrationReport ) Save () error {
44- dir , err := platform . ReportsDir ()
70+ path , err := r . Path ()
4571 if err != nil {
4672 return err
4773 }
48-
49- filename := fmt .Sprintf ("migration-%s.json" , r .Timestamp .Format ("20060102-150405" ))
50- path := filepath .Join (dir , filename )
51-
5274 data , err := json .MarshalIndent (r , "" , " " )
5375 if err != nil {
5476 return fmt .Errorf ("marshal report: %w" , err )
5577 }
56-
5778 return os .WriteFile (path , data , 0o644 )
5879}
5980
81+ // Path returns the on-disk path Save() would write to. The first call
82+ // assigns a stable random suffix; subsequent calls return the same
83+ // filename. Exposed so the CLI can print the path to the user without
84+ // re-deriving the timestamp-formatting rule.
85+ //
86+ // Two different MigrationReport instances (i.e., two upgrades) get
87+ // independent random suffixes — Path() called on each one resolves to
88+ // a unique filename even within the same second.
89+ func (r * MigrationReport ) Path () (string , error ) {
90+ if r .tag == "" {
91+ tag , err := reportTag ()
92+ if err != nil {
93+ return "" , fmt .Errorf ("generate report tag: %w" , err )
94+ }
95+ r .tag = tag
96+ }
97+ dir , err := platform .ReportsDir ()
98+ if err != nil {
99+ return "" , err
100+ }
101+ return filepath .Join (dir , r .filename ()), nil
102+ }
103+
104+ // filename is the basename portion of the on-disk path. Centralized
105+ // so Path() and any future tests stay in lockstep with the format.
106+ func (r * MigrationReport ) filename () string {
107+ return fmt .Sprintf ("migration-%s-%s.json" , r .Timestamp .Format ("20060102-150405" ), r .tag )
108+ }
109+
110+ // reportTag returns 4 hex bytes (8 chars) of crypto-random entropy
111+ // sourced from crypto/rand. 32 bits of entropy is plenty to make
112+ // accidental collisions vanishingly unlikely across the lifetime of a
113+ // single machine's DataDir (~1 report per upgrade, upgrades are
114+ // minutes-to-hours apart for a real user). We deliberately do NOT use
115+ // math/rand here — that would couple report-naming to Go's seeded PRNG
116+ // and could produce identical tags in two different processes
117+ // initialized identically.
118+ func reportTag () (string , error ) {
119+ var b [4 ]byte
120+ if _ , err := rand .Read (b [:]); err != nil {
121+ return "" , err
122+ }
123+ return hex .EncodeToString (b [:]), nil
124+ }
125+
60126// AddResult appends a package result to the report.
61127func (r * MigrationReport ) AddResult (pr PackageResult ) {
62128 r .Results = append (r .Results , pr )
0 commit comments