Index | Thread | Search

From:
Hiltjo Posthuma <hiltjo@codemadness.org>
Subject:
Question about cp -P (without -R) and POSIX compliance
To:
tech@openbsd.org
Date:
Thu, 5 Sep 2024 21:44:45 +0200

Download raw body.

Thread
Hi,

I have a question about cp on OpenBSD and the -P flag.

It seems cp -P (without the -R option) to copy a symlink behaves differently
for single files on OpenBSD than POSIX specifies and compared to NetBSD,
FreeBSD, Linux (GNU, busybox, toybox, etc).

Test-case to reproduce it:

	touch file
	ln -s file link
	cp -P link link2
	ls -l link link2

NetBSD and FreeBSD changed the check a while ago (Feb 23, 2022), for example:

	https://github.com/freebsd/freebsd-src/commit/97e13037915c22162f199461f56951793d669f57

Snippet:

	} else if (!Pflag) {
		fts_options &= ~FTS_PHYSICAL;
		fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;

The OpenBSD man page specifies:

"if the -R option is also specified, no symbolic links are followed".

and:

"Symbolic links are always followed unless the -R flag is set, in which case
symbolic links are not followed, by default. The -H or -L flags (in conjunction
with the -R flag) cause symbolic links to be followed as described above. The
-H, -L, and -P options are ignored unless the -R option is specified. In
addition, these options override each other and the command's actions are
determined by the last one specified."


But POSIX specifies:

"If the -R option was not specified, cp shall take actions based on the type
and contents of the file referenced by the symbolic link, and not by the
symbolic link itself, unless the -P option was specified."

Reference: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html

Should this be changed?

Thank you,

-- 
Kind regards,
Hiltjo