[PATCH 2/2 v4] truncate: new applet

Ari Sundholm ari at tuxera.com
Thu Mar 5 19:08:13 UTC 2015


On Thu, 2015-03-05 at 19:17 +0100, Bernhard Reutner-Fischer wrote:
> On March 5, 2015 2:28:28 PM GMT+01:00, Ari Sundholm <ari at tuxera.com> wrote:
> >From: Ari Sundholm <ari at tuxera.com>
> >
> >bloat-o-meter:
> >
> >function                                             old     new  
> >delta
> >.rodata                                           154411  154480    
> >+69
> >------------------------------------------------------------------------------
> >(add/remove: 0/0 grow/shrink: 1/0 up/down: 69/0)               Total:
> >69 bytes
> 
> Wrong baseline?

I rechecked and got the same result. It is entirely possible that I am
using bloat-o-meter wrong, so here is what I did:

1. git checkout <last commit before the truncate patch>
2. make
3. mv busybox busybox-beforetruncate
4. git checkout <commit with the truncate patch>
5. make oldconfig, say Y to truncate
6. make
7. scripts/bloat-o-meter busybox-beforetruncate busybox
8. get the results above: +69 bytes.

Now that I think of it, I probably should use the unstripped
executables? The results look different and seem to make more sense when
I compare those.

If this is indeed the case, I guess I'll roll a v5 with the correct
values.

> Why is that C impl better than a shell script around dd?

In terms of size or in general? The applet's small in terms of code and
the usage strings are compressed, but now that I think of it, 69 bytes
just sounds too little. If you meant the latter, it is a matter of
taste, I guess. I actually used such a dd wrapper script for quite a
while until I wrote this applet.

Best regards,
Ari Sundholm
ari at tuxera.com

> Thanks,
> >
> >v2: Make dd and truncate share a common suffix struct.
> >v3: Split suffix struct move into separate commit, make applet NOFORK,
> >adjust
> >    error message.
> >v4: Use bb_error_msg_and_die() instead of bb_error_msg() + xfunc_die().
> >
> >Signed-off-by: Ari Sundholm <ari at tuxera.com>
> >---
> >coreutils/truncate.c | 73
> >++++++++++++++++++++++++++++++++++++++++++++++++++++
> > 1 file changed, 73 insertions(+)
> > create mode 100644 coreutils/truncate.c
> >
> >diff --git a/coreutils/truncate.c b/coreutils/truncate.c
> >new file mode 100644
> >index 0000000..5d7c4f9
> >--- /dev/null
> >+++ b/coreutils/truncate.c
> >@@ -0,0 +1,73 @@
> >+/*
> >+ * Mini truncate implementation for busybox
> >+ *
> >+ * Copyright (C) 2015 by Ari Sundholm <ari at tuxera.com>
> >+ *
> >+ * Licensed under GPLv2 or later, see file LICENSE in this source
> >tree.
> >+ */
> >+
> >+//config:config TRUNCATE
> >+//config:	bool "truncate"
> >+//config:	default y
> >+//config:	help
> >+//config:	  truncate truncates files to a given size. If a file does
> >+//config:	  not exist, it is created unless told otherwise.
> >+
> >+//kbuild:lib-$(CONFIG_TRUNCATE) += truncate.o
> >+//applet:IF_TRUNCATE(APPLET_NOFORK(truncate, truncate, BB_DIR_USR_BIN,
> >BB_SUID_DROP, truncate))
> >+
> >+//usage:#define truncate_trivial_usage
> >+//usage:       "[-c] -s SIZE FILE..."
> >+//usage:#define truncate_full_usage "\n\n"
> >+//usage:	"Truncate FILEs to the given size.\n"
> >+//usage:	"\n	-c	Do not create any files."
> >+//usage:	"\n	-s SIZE	Truncate to SIZE."
> >+//usage:
> >+//usage:#define truncate_example_usage
> >+//usage:	"$ truncate -s 1G foo"
> >+
> >+#include "libbb.h"
> >+
> >+#if ENABLE_LFS
> >+# define XATOU_SFX xatoull_sfx
> >+#else
> >+# define XATOU_SFX xatoul_sfx
> >+#endif
> >+
> >+/* This is a NOFORK applet. Be very careful! */
> >+
> >+int truncate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
> >+int truncate_main(int argc UNUSED_PARAM, char **argv) {
> >+	unsigned opts;
> >+	int flags = O_CREAT|O_RDWR;
> >+	int fd;
> >+	char *size_str;
> >+	off_t size;
> >+
> >+	enum {
> >+		OPT_NOCREATE  = (1 << 0),
> >+		OPT_SIZE = (1 << 1),
> >+	};
> >+
> >+	opt_complementary = "s";
> >+	opts = getopt32(argv, "cs:", &size_str);
> >+
> >+	argv += optind;
> >+	if (!*argv)
> >+		bb_error_msg_and_die("no files specified!");
> >+
> >+	if (opts & OPT_NOCREATE)
> >+		flags = O_RDWR;
> >+
> >+	size = XATOU_SFX(size_str, cwbkMG_suffixes);
> >+
> >+	while (*argv) {
> >+		fd = xopen(*argv, flags);
> >+		if (ftruncate(fd, size) == -1)
> >+			bb_perror_msg_and_die("ftruncate failed");
> >+		xclose(fd);
> >+		++argv;
> >+	}
> >+
> >+	return 0;
> >+}
> 
> 




More information about the busybox mailing list