Skip to content

Truncate existing output files before writing - #115

Merged
esimov merged 1 commit into
esimov:masterfrom
mertkayacs:fix/truncate-output
Oct 24, 2026
Merged

esimov merged 1 commit into
esimov:masterfrom
mertkayacs:fix/truncate-output

Conversation

@mertkayacs

Copy link
Copy Markdown
Contributor

If the output file already exists and the new image is smaller, caire leaves the end of the old file in place. pathToFile opens the output with os.O_CREATE|os.O_WRONLY and no os.O_TRUNC.

caire -in testdata/sample.jpg -out out.png -width 500 -preview=false
caire -in testdata/sample.jpg -out out.png -width 200 -preview=false

After the second run out.png is still 430,993 bytes. A fresh 200px output is 201,064 bytes, so about 230 KB of the first image stays after the PNG's IEND chunk. Viewers stop reading at IEND, so the picture looks right, but the file keeps the old bytes.

The fix adds os.O_TRUNC. The test puts a 1 MB file at the output path, runs the normal write path and checks that the result is the same size as a fresh write. go vet and the tests pass on Go 1.22 and 1.26 (face detection tests left to CI).

@esimov esimov left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Approved.

@esimov
esimov merged commit 941f4cc into esimov:master Oct 24, 2026
@esimov

esimov commented Oct 24, 2026

Copy link
Copy Markdown
Owner

@mertkayacs thanks for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants