Skip to content

Commit 9e70b67

Browse files
TMHSDigitalclaude
andcommitted
Replace a symlink at the default resume state path
The default state file, subenum-resume.json in the working directory, is a path the user never named, so a symlink found there is now replaced by the state file instead of written through. A -state path the user gives is still followed, as -o is. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b427548 commit 9e70b67

4 files changed

Lines changed: 59 additions & 6 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ This table describes `main`, which can be ahead of the latest release: a release
283283
| `-diff-records` | `false` | With `-diff`, also report names whose records changed (`~`): a re-pointed CNAME or moved addresses, compared over the `-type` record types. Needs a JSON, JSONL or CSV `-diff` file |
284284
| **Resuming** | | |
285285
| `-resume <file>` | n/a | Resume a run that was interrupted, ran out of `-max-duration` or was aborted, from its state file; takes no other arguments but the pacing flags (`-rate`, `-t`, `-timeout`, ...) |
286-
| `-state <file>` | `subenum-resume.json` | Where a run that was interrupted, ran out of `-max-duration` or was aborted saves its state for `-resume`. The state holds the full command line, so a credential in a `-dns-server` URL is in it; reports, results and logs show such a server only as its scheme and host |
286+
| `-state <file>` | `subenum-resume.json` | Where a run that was interrupted, ran out of `-max-duration` or was aborted saves its state for `-resume`. A symlink at the default path is replaced, not written through; a path you name is followed like `-o`'s. The state holds the full command line, so a credential in a `-dns-server` URL is in it; reports, results and logs show such a server only as its scheme and host |
287287
| **Simulation and labs** | | |
288288
| `-simulate` | `false` | Simulation mode: no real DNS queries |
289289
| `-hit-rate <n>` | `15` | Simulated resolution rate, percent (1-100), applied uniformly to every name |

‎internal/cli/resume.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,11 +607,15 @@ func saveResumeState(s *session) bool {
607607
return true
608608
}
609609

610-
// writeResumeState saves where an interrupted run stopped.
610+
// writeResumeState saves where an interrupted run stopped. The default file
611+
// is written without following a symlink there: the user never named it.
611612
func writeResumeState(path string, st resumeState) error {
612613
data, err := json.MarshalIndent(st, "", " ")
613614
if err != nil {
614615
return err
615616
}
617+
if path == defaultStateFile {
618+
return output.WriteFileNoFollow(path, append(data, '\n'))
619+
}
616620
return output.WriteFile(path, append(data, '\n'))
617621
}

‎internal/output/file.go‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,22 @@ type File struct {
3030
// would, so a umask of 077 keeps results private (#119). A path that is a
3131
// symlink is followed: the file it points at is replaced and the link kept.
3232
func CreateFile(path string) (*File, error) {
33-
if resolved, err := filepath.EvalSymlinks(path); err == nil {
34-
path = resolved
33+
return createFile(path, true)
34+
}
35+
36+
// createFile is CreateFile; without follow, a symlink or other non-regular
37+
// file at path is not opened but replaced by the new file, and what a link
38+
// pointed at is left alone.
39+
func createFile(path string, follow bool) (*File, error) {
40+
stat := os.Lstat
41+
if follow {
42+
if resolved, err := filepath.EvalSymlinks(path); err == nil {
43+
path = resolved
44+
}
45+
stat = os.Stat
3546
}
3647
keep := os.FileMode(0)
37-
if fi, err := os.Stat(path); err == nil {
48+
if fi, err := stat(path); err == nil && (follow || fi.Mode().IsRegular()) {
3849
if !fi.Mode().IsRegular() {
3950
// No O_TRUNC: truncating a device or pipe is meaningless, and
4051
// Windows rejects it on the NUL device with ERROR_INVALID_FUNCTION
@@ -60,7 +71,17 @@ func CreateFile(path string) (*File, error) {
6071
// WriteFile writes data to path the way CreateFile does: atomically, so a
6172
// failed write leaves an existing file unchanged (#331).
6273
func WriteFile(path string, data []byte) error {
63-
f, err := CreateFile(path)
74+
return writeFile(path, data, true)
75+
}
76+
77+
// WriteFileNoFollow is WriteFile for a file the user did not name: a symlink
78+
// or other non-regular file at path is replaced, never written through.
79+
func WriteFileNoFollow(path string, data []byte) error {
80+
return writeFile(path, data, false)
81+
}
82+
83+
func writeFile(path string, data []byte, follow bool) error {
84+
f, err := createFile(path, follow)
6485
if err != nil {
6586
return err
6687
}

‎internal/output/file_unix_test.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,31 @@ func TestCreateFileFollowsSymlink(t *testing.T) {
7070
t.Errorf("target mode %v, want its original 0640", fi.Mode().Perm())
7171
}
7272
}
73+
74+
// TestWriteFileNoFollowReplacesSymlink: a file the user did not name, such
75+
// as the default resume state, replaces a symlink at its path and leaves
76+
// the file the link points at untouched.
77+
func TestWriteFileNoFollowReplacesSymlink(t *testing.T) {
78+
dir := t.TempDir()
79+
target := filepath.Join(dir, "elsewhere.txt")
80+
if err := os.WriteFile(target, []byte("keep\n"), 0o600); err != nil {
81+
t.Fatal(err)
82+
}
83+
link := filepath.Join(dir, "state.json")
84+
if err := os.Symlink(target, link); err != nil {
85+
t.Fatal(err)
86+
}
87+
if err := WriteFileNoFollow(link, []byte("{}\n")); err != nil {
88+
t.Fatal(err)
89+
}
90+
if data, _ := os.ReadFile(target); string(data) != "keep\n" {
91+
t.Errorf("link target holds %q, want it untouched", data)
92+
}
93+
fi, err := os.Lstat(link)
94+
if err != nil || !fi.Mode().IsRegular() {
95+
t.Fatalf("state path mode %v (err %v), want a regular file", fi.Mode(), err)
96+
}
97+
if data, _ := os.ReadFile(link); string(data) != "{}\n" {
98+
t.Errorf("state file holds %q, want the new state", data)
99+
}
100+
}

0 commit comments

Comments
 (0)