Let a cleanup failure keep its own errno in copy and create

Both cleanup blocks saved errno on entry and restored it whenever rc had
become -1, including when it was the cleanup itself that had just failed.
On the success path the saved value is meaningless, so a genuine fclose
error was reported through a stale errno, usually 0:

  $ fsnap create dir /dev/full
  create failed: Success

A buffered write may succeed and only fail at flush or close, which is
exactly the case SPEC.md section 8 asks not to lose. Adopt the scheme
already used in list(): a pre-existing error keeps errno, a cleanup error
keeps its own, and either one yields -1.

  $ fsnap create dir /dev/full
  create failed: No space left on device

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
This commit is contained in:
Fabio Scotto di Santolo
2026-08-26 15:11:06 +02:00
parent 9e6581986e
commit 7f0c18df76
2 changed files with 34 additions and 10 deletions

View File

@@ -72,12 +72,24 @@ int create(const char *directory, const char *output_file) {
rc = 0;
cleanup:;
/* An error that happened before the cleanup owns errno; one raised by the
cleanup itself owns it only when nothing had failed yet. A buffered write
can fail for the first time in fclose, so that errno must survive. */
const int saved_errno = errno;
int cleanup_errno = 0;
free(resolved);
unlink(template);
if (tmp != NULL && fclose(tmp) != 0 && rc == 0)
rc = -1;
if (rc == -1)
if (tmp != NULL && fclose(tmp) != 0)
cleanup_errno = errno;
if (rc == -1) {
errno = saved_errno;
return rc;
return -1;
}
if (cleanup_errno != 0) {
errno = cleanup_errno;
return -1;
}
return 0;
}