[PATCH 5/6] commands: add new truncate command

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Mar 19 22:20:18 PDT 2025


The truncate command offers an easy way to exercise a file system's
truncate operation.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 commands/Kconfig    |  15 +++++++
 commands/Makefile   |   1 +
 commands/truncate.c | 100 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 116 insertions(+)
 create mode 100644 commands/truncate.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 8a7a6e94f025..8c05b8eff606 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -1904,6 +1904,21 @@ config CMD_DETECT
 		  -e	bail out if one device fails to detect
 		  -a	detect all devices
 
+config CMD_TRUNCATE
+	tristate
+	prompt "truncate"
+	help
+
+	truncate [-c] -s [+]SIZE FILE...
+
+	truncate truncates files to a given size. If a file does
+	not exist, it is created unless told otherwise.
+	Options:
+	-c		Do not create files
+	-s [+]SIZE	truncate file to SIZE, or increase by +SIZE
+
+	This command is mainly useful for VFS development.
+
 config CMD_SYNC
 	tristate
 	prompt "sync"
diff --git a/commands/Makefile b/commands/Makefile
index fa7de443cc13..36ef59b38759 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -41,6 +41,7 @@ obj-$(CONFIG_CMD_FINDMNT)	+= findmnt.o
 obj-$(CONFIG_CMD_CRC)		+= crc.o
 obj-$(CONFIG_CMD_CLEAR)		+= clear.o
 obj-$(CONFIG_CMD_TEST)		+= test.o
+obj-$(CONFIG_CMD_TRUNCATE)	+= truncate.o
 obj-$(CONFIG_CMD_SYNC)		+= sync.o
 obj-$(CONFIG_CMD_FLASH)		+= flash.o
 obj-$(CONFIG_CMD_MEMINFO)	+= meminfo.o
diff --git a/commands/truncate.c b/commands/truncate.c
new file mode 100644
index 000000000000..5c6d74257da1
--- /dev/null
+++ b/commands/truncate.c
@@ -0,0 +1,100 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2015 by Ari Sundholm <ari at tuxera.com>
+ */
+
+#include <common.h>
+#include <command.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <fs.h>
+
+static int do_truncate(int argc, char *argv[])
+{
+	int flags = O_CREAT | O_WRONLY;
+	int opt, modify = 0, ret = 0;
+	const char *size_str;
+	off_t size = -1;
+
+	while((opt = getopt(argc, argv, "cs:")) > 0) {
+		switch(opt) {
+		case 'c':
+			flags &= ~O_CREAT;
+			break;
+		case 's':
+			size_str = optarg;
+			if (!size_str)
+				return COMMAND_ERROR_USAGE;
+			switch (*size_str) {
+			case '+':
+				modify = 1;
+				size_str++;
+				break;
+			case '-':
+				return COMMAND_ERROR_USAGE;
+			}
+			size = strtoull_suffix(size_str, NULL, 10);
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	argv += optind;
+	argc -= optind;
+
+	if (size == -1 || argc == 0)
+		return COMMAND_ERROR_USAGE;
+
+	for (int i = 0; i < argc; i++) {
+		int fd = open(argv[i], flags, 0666);
+		if (fd < 0) {
+			if (errno != ENOENT || !(flags & O_CREAT)) {
+				perror("open");
+				ret = 1;
+			}
+			/* else: ENOENT && OPT_NOCREATE:
+			 * do not report error, exitcode is also 0.
+			 */
+			continue;
+		}
+
+		if (modify) {
+			struct stat st;
+
+			if (fstat(fd, &st) == -1) {
+				perror("fstat");
+				ret = 1;
+				goto close;
+			}
+
+			size = st.st_size + modify * size;
+		}
+
+		if (ftruncate(fd, size) == -1) {
+			perror("truncate");
+			ret = 1;
+		}
+close:
+		close(fd);
+	}
+
+	return ret;
+}
+
+BAREBOX_CMD_HELP_START(truncate)
+BAREBOX_CMD_HELP_TEXT("truncate truncates files to a given size. If a file does")
+BAREBOX_CMD_HELP_TEXT("not exist, it is created unless told otherwise.")
+BAREBOX_CMD_HELP_TEXT("")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT ("-c",     "Do not create files")
+BAREBOX_CMD_HELP_OPT ("-s [+]SIZE", "truncate file to SIZE, or increase by +SIZE")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(truncate)
+	.cmd		= do_truncate,
+	BAREBOX_CMD_DESC("truncate files to size")
+	BAREBOX_CMD_OPTS("[-c] -s [+]SIZE FILE...")
+	BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP)
+	BAREBOX_CMD_HELP(cmd_truncate_help)
+BAREBOX_CMD_END
-- 
2.39.5




More information about the barebox mailing list