File: composefs-dump.c

package info (click to toggle)
composefs 1.0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,040 kB
  • sloc: ansic: 9,004; sh: 416; python: 225; makefile: 5
file content (88 lines) | stat: -rw-r--r-- 1,755 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* lcfs
   Copyright (C) 2023 Alexander Larsson <alexl@redhat.com>

   SPDX-License-Identifier: GPL-2.0-or-later OR Apache-2.0
*/

#define _GNU_SOURCE

#include "config.h"

#include "libcomposefs/lcfs-writer.h"
#include "libcomposefs/lcfs-utils.h"

#include <stdio.h>
#include <string.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>

static void usage(const char *argv0)
{
	fprintf(stderr, "usage: %s SRC DEST\n", argv0);
}

static ssize_t write_cb(void *_file, void *buf, size_t count)
{
	FILE *file = _file;

	return fwrite(buf, 1, count, file);
}

int main(int argc, char **argv)
{
	const char *bin = argv[0];
	int fd, version;
	struct lcfs_node_s *root;
	const char *src_path = NULL;
	const char *dst_path = NULL;
	struct lcfs_write_options_s options = { 0 };

	if (argc <= 1) {
		fprintf(stderr, "No source path specified\n");
		usage(bin);
		exit(1);
	}
	src_path = argv[1];

	if (argc <= 2) {
		fprintf(stderr, "No destination path specified\n");
		usage(bin);
		exit(1);
	}
	dst_path = argv[2];

	fd = open(src_path, O_RDONLY | O_CLOEXEC);
	if (fd < 0) {
		err(EXIT_FAILURE, "Failed to open '%s'", src_path);
	}

	version = lcfs_version_from_fd(fd);
	if (version < 0) {
		err(EXIT_FAILURE, "Failed to get image version '%s'", src_path);
	}

	root = lcfs_load_node_from_fd(fd);
	if (root == NULL) {
		err(EXIT_FAILURE, "Failed to load '%s'", src_path);
	}

	close(fd);

	options.format = LCFS_FORMAT_EROFS;
	options.version = version;

	FILE *out_file = fopen(dst_path, "we");
	if (out_file == NULL)
		err(EXIT_FAILURE, "failed to open '%s'", dst_path);

	options.file = out_file;
	options.file_write_cb = write_cb;

	if (lcfs_write_to(root, &options) < 0)
		err(EXIT_FAILURE, "cannot write file");

	lcfs_node_unref(root);

	return 0;
}