2016-11-01 17:19:30 +0000, Martijn Dekker:
[...]
> | -C (Uppercase C.) Prevent existing files from being overwritten by
> | the shell's '>' redirection operator (see Redirecting Output);
> 
> This says nothing about how long the files are supposed to have existed
> or how stable they are supposed to be before -C prevents them from being
> overwritten. As written, the spec inherently requires atomicity.
> 
> AFAIK, there also is no other POSIX shell mechanism that could create
> files both atomically and non-destructively.
> 
> If 'mktemp' were a POSIX standard utility, this wouldn't be an issue,
> but it's not, so it is.
[...]

And as already discussed that makes for security vulnerabilities
in scripts that assume "set -C" will give you the guarantee that
redirections will not clobber file or follow symlinks (like
O_EXCL) does.

See also
https://stackoverflow.com/questions/12187859/create-new-file-but-add-number-if-filename-already-exists-in-bash/12194427#12194427

zsh has sysopen -o excl to open a file with O_EXCL (also
has nofollow).

Generally, you use "-C" for its O_EXCL, the only reason that
exclusion for device/fifos is there is because of redirections
to /dev/null.

Ideally, we want to decide of the flags for each redirection.

In

ls > file 2> /dev/null

we (may) want the O_EXCL for "file", but not for "/dev/null".

In zsh, that can be addressed with:

(sysopen -wu 1 -o excl file &&
 ls 2> /dev/null)

Which is a bit unwieldy and doesn't exit the script upon failure
of that sysopen

Ideally, we'd want a different redirection operator like

ls >? file 2> /dev/null

Related: http://www.zsh.org/mla/workers/2015/msg01575.html

-- 
Stephane

Reply via email to