[git commit] cp: fix --sparse to have mandatory argument, fix --remove-destination bit position

Denys Vlasenko vda.linux at googlemail.com
Tue Jul 14 21:14:24 UTC 2026


commit: https://git.busybox.net/busybox/commit/?id=ec0c5cc142f1f9ea57235df5d093fbe180ad9c7d
branch: https://git.busybox.net/busybox/log/?h=master

cp --remove-destination was broken by commit 56bbbfae "cp: implement -n"

function                                             old     new   delta
cp_main                                              527     533      +6

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 coreutils/cp.c  | 15 ++++++++++++++-
 include/libbb.h |  9 +++++----
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/coreutils/cp.c b/coreutils/cp.c
index 3ddf90088..85af104e6 100644
--- a/coreutils/cp.c
+++ b/coreutils/cp.c
@@ -113,6 +113,8 @@
 #include "libbb.h"
 #include "libcoreutils/coreutils.h"
 
+#define DBG_OPTION_PARSING 0
+
 /* This is a NOEXEC applet. Be very careful! */
 
 int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -168,7 +170,7 @@ int cp_main(int argc, char **argv)
 		"reflink\0"        Optional_argument "\xfd"
 # endif
 # if ENABLE_FEATURE_CP_SPARSE
-		"sparse\0"         Optional_argument "\xfc"
+		"sparse\0"         Required_argument "\xfc"
 # endif
 		, &last
 # if ENABLE_FEATURE_CP_REFLINK
@@ -179,6 +181,7 @@ int cp_main(int argc, char **argv)
 		, &sparse
 # endif
 	);
+
 # if ENABLE_FEATURE_CP_REFLINK
 	BUILD_BUG_ON((int)OPT_reflink != (int)FILEUTILS_REFLINK);
 	if (flags & FILEUTILS_REFLINK) {
@@ -190,6 +193,16 @@ int cp_main(int argc, char **argv)
 			bb_show_usage();
 	}
 # endif
+# if DBG_OPTION_PARSING
+	bb_error_msg("flags: 0x%08x", flags);
+#  define showopt(o) bb_error_msg("flags & %s(%x):\t%x", #o, o, flags & o);
+	showopt(FILEUTILS_RMDEST        );
+	showopt(OPT_parents             );
+	showopt(OPT_reflink             );
+	showopt(FILEUTILS_REFLINK_ALWAYS);
+	return 0;
+#  undef showopt
+# endif
 #else
 	flags = getopt32(argv, "^"
 		FILEUTILS_CP_OPTSTR
diff --git a/include/libbb.h b/include/libbb.h
index 37f9da59e..873149b7c 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -505,16 +505,17 @@ enum {	/* cp.c, mv.c, install.c depend on these values. CAREFUL when changing th
 #endif
 #define FILEUTILS_CP_OPTSTR "pdRfinlsLHarPvuTt:" IF_SELINUX("c")
 /* How many bits in FILEUTILS_CP_OPTSTR? */
-	FILEUTILS_CP_OPTBITS      = 18 - !ENABLE_SELINUX,
+	FILEUTILS_CP_OPTBITS      = (18 - !ENABLE_SELINUX),
 
-	FILEUTILS_RMDEST          = 1 << (19 - !ENABLE_SELINUX), /* cp --remove-destination */
-	/* bit 18 skipped for "cp --parents" */
+	FILEUTILS_RMDEST          = 1 << (18 - !ENABLE_SELINUX), /* cp --remove-destination */
+	/* bit 19 is used in cp for "cp --parents" */
 	FILEUTILS_REFLINK         = 1 << (20 - !ENABLE_SELINUX), /* cp --reflink=auto */
-	FILEUTILS_REFLINK_ALWAYS  = 1 << (21 - !ENABLE_SELINUX), /* cp --reflink[=always] */
+	/* bit 21 is used in cp for "cp --sparse=... */
 	/*
 	 * Hole. cp may have some bits set here,
 	 * they should not affect remove_file()/copy_file()
 	 */
+	FILEUTILS_REFLINK_ALWAYS  = 1 << 29, /* cp --reflink, --reflink=always */
 #if ENABLE_SELINUX
 	FILEUTILS_SET_SECURITY_CONTEXT = 1 << 30,
 #endif


More information about the busybox-cvs mailing list