Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Any way to allow clobbering empty files when noclobber is set?



Roman Perepelitsa wrote on Thu, 04 Jun 2020 07:41 +0200:
> The regular noclobber can use O_CREAT | O_EXCL to avoid races. How
> would any of the proposed modifications work? That is, how to
> implement unlink-if-empty without ever unlinking non-empty files?

I don't think that's possible: it'd require an atomic
stat-and-conditionally-unlink operation, and I don't think there's
a syscall for this.  fstatat(2)-then-unlinkat(2) gets close, but it's
still racy.

Good point.

> Or how to implement clobber-if-empty without ever clobbering non-empty
> files?

By using a non-clobbering open(), then calling fstat() on the open fd.
If it's zero length, just carry on.  If it's not zero length, close()
it and report an error.

Cheers,

Daniel



Messages sorted by: Reverse Date, Date, Thread, Author