From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 5/6] commands: add new truncate command
Date: Thu, 20 Mar 2025 06:20:18 +0100 [thread overview]
Message-ID: <20250320052019.1726331-6-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250320052019.1726331-1-a.fatoum@pengutronix.de>
The truncate command offers an easy way to exercise a file system's
truncate operation.
Signed-off-by: Ahmad Fatoum <a.fatoum@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@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
next prev parent reply other threads:[~2025-03-20 5:20 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-20 5:20 [PATCH 0/6] fs: implement tree and truncate Ahmad Fatoum
2025-03-20 5:20 ` [PATCH 1/6] fs: use filename_create/filename_lookup instead of open-coding Ahmad Fatoum
2025-03-20 5:20 ` [PATCH 2/6] fs: implement O_DIRECTORY Ahmad Fatoum
2025-03-20 5:20 ` [PATCH 3/6] fs: implement opendir in terms of fdopendir Ahmad Fatoum
2025-03-20 5:20 ` [PATCH 4/6] commands: implement tree command Ahmad Fatoum
2025-03-20 5:20 ` Ahmad Fatoum [this message]
2025-03-20 5:20 ` [PATCH 6/6] Documentation: devel: add short section on file systems Ahmad Fatoum
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250320052019.1726331-6-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox