Skip to contentSkip to conversation

add untar command

PATCH In review [email protected] 2 participants 2 messages Aug 3, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 add untar command

From: Yegor Yefremov <[email protected]>

Use busybox implementation as a reference.

---
 commands/Kconfig  |   8 +
 commands/Makefile |   1 +
 commands/untar.c  | 598 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 607 insertions(+)
 create mode 100644 commands/untar.c
commands/Kconfig +8 -0
index 3789f33c3..b1f6ec1cd 100644
@@ -998,6 +998,14 @@ config CMD_UNCOMPRESS
 
 	  Usage: uncompress INFILE OUTFILE
 
+config CMD_UNTAR
+	bool
+	prompt "untar"
+	help
+	  Unpack a tar file.
+
+	  Usage: untar INFILE DIRECTORY
+
 # end File commands
 endmenu
 
commands/Makefile +1 -0
index 01082de44..5cde39399 100644
@@ -61,6 +61,7 @@ obj-$(CONFIG_USB_GADGET_DFU)	+= dfu.o
 obj-$(CONFIG_USB_GADGET_SERIAL)	+= usbserial.o
 obj-$(CONFIG_CMD_GPIO)		+= gpio.o
 obj-$(CONFIG_CMD_UNCOMPRESS)	+= uncompress.o
+obj-$(CONFIG_CMD_UNTAR)		+= untar.o
 obj-$(CONFIG_CMD_I2C)		+= i2c.o
 obj-$(CONFIG_CMD_SPI)		+= spi.o
 obj-$(CONFIG_CMD_UBI)		+= ubi.o
commands/untar.c new file +598 -0

598 lines not on this page · raw message

Signature · 7 lines
2.17.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

Hi Yegor,

Quoted 3 lines · On Mon, Aug 03, 2020 at 07:07:00AM +0200, [email protected] wrote:

From: Yegor Yefremov <[email protected]>

Use busybox implementation as a reference.

Would you add the busybox version to the commit message for reference and to make future updates easier?

Quoted 6 lines
commands/untar.c new file +6 -0
+/* Concatenate path and filename to new allocated buffer.
+ * Add '/' only as needed (no duplicate // are produced).
+ * If path is NULL, it is assumed to be "/".
+ * filename should not be NULL.
+ */
+static char* concat_path_file(const char *path, const char *filename)

commands/untar.c line 219

Quoted 10 lines
commands/untar.c new file +10 -0
+{
+      char *lc;
+
+      if (!path)
+          path = "";
+      lc = last_char_is(path, '/');
+      while (*filename == '/')
+          filename++;
+      return xasprintf("%s%s%s", path, (lc==NULL ? "/" : ""), filename);
+}

We already have this function in lib/libbb.c, better use it than adding it again. You add more functions that seem to be generic busybox functions, please check if they are better added to lib/libbb.c in case we need them for other code.

commands/untar.c line 572

Quoted 9 lines
commands/untar.c new file +9 -0
+static int do_untar(int argc, char *argv[])
+{
+	archive_handle_t *handle;
+	int ret;
+
+	if (argc < 2)
+		return COMMAND_ERROR_USAGE;
+
+	handle = init_handle();

This allocates memory which you never free, neither in the successful case nor in the error path.

commands/untar.c line 580

Quoted 8 lines
commands/untar.c new file +8 -0
+	handle->src_fd = open(argv[1], O_RDONLY);
+	if (handle->src_fd < 0) {
+		perror("open");
+		return 1;
+	}
+
+	if (argc == 3) {
+		chdir(argv[2]);

Doesn't this change the current directory also in the calling shell? Also this call can fail when the path doesn't exist or is not a directory. You should check this.

commands/untar.c line 587

Quoted 7 lines
commands/untar.c new file +7 -0
+	}
+
+	while(!get_header(handle))
+		ret = 0;
+
+	if (ret)
+		printf("failed to decompress\n");

'ret' is either used uninitialized or initialized to zero.

commands/untar.c line 596

Quoted 9 lines
commands/untar.c new file +9 -0
+
+	close(handle->src_fd);
+	return ret;
+}
+
+BAREBOX_CMD_START(untar)
+	.cmd            = do_untar,
+	BAREBOX_CMD_DESC("unpack a tar file")
+	BAREBOX_CMD_OPTS("INFILE [DIRECTORY]")

I don't like positional arguments very much. It's fine for the infile, but IMO the directory should be given with an option instead.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox