summaryrefslogtreecommitdiff
path: root/aports/busybox/0002-cpio-add-support-for-renumber-inodes-like-GNU-cpio.patch
blob: 9febb66dd4be814573e70f70efae1c0de6ad4bed (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
From 67240ede641cdda29ef74a373bc9f5aa8a18f4e8 Mon Sep 17 00:00:00 2001
From: Ariadne Conill <ariadne@dereferenced.org>
Date: Mon, 28 Jun 2021 08:23:09 -0600
Subject: [PATCH 2/2] cpio: add support for --renumber-inodes like GNU cpio

The --renumber-inodes option renumbers the inodes starting from 1,
so that the sequence of inodes is always stable.  This helps with
reproducibility.

function                                             old     new   delta
cpio_o                                               961    1045     +84
.rodata                                            78422   78440     +18
bbconfig_config_bz2                                 6168    6164      -4
packed_usage                                       25764   25756      -8
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/2 up/down: 102/-12)            Total: 90 bytes

Signed-off-by: Ariadne Conill <ariadne@dereferenced.org>
---
 archival/cpio.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/archival/cpio.c b/archival/cpio.c
index 4d386d38d..14f0b5b84 100644
--- a/archival/cpio.c
+++ b/archival/cpio.c
@@ -45,6 +45,13 @@
 //config:	depends on FEATURE_CPIO_O && LONG_OPTS
 //config:	help
 //config:	Optionally ignore device numbers when creating archives.
+//config:
+//config:config FEATURE_CPIO_RENUMBER_INODES
+//config:	bool "Support --renumber-inodes like GNU cpio"
+//config:	default y
+//config:	depends on FEATURE_CPIO_O && LONG_OPTS
+//config:	help
+//config:	Optionally renumber inodes when creating archives.
 
 //applet:IF_CPIO(APPLET(cpio, BB_DIR_BIN, BB_SUID_DROP))
 
@@ -85,6 +92,9 @@
 //usage:	IF_FEATURE_CPIO_IGNORE_DEVNO(
 //usage:     "\n	--ignore-devno"
 //usage:	)
+//usage:	IF_FEATURE_CPIO_RENUMBER_INODES(
+//usage:     "\n	--renumber-inodes"
+//usage:	)
 
 /* GNU cpio 2.9 --help (abridged):
 
@@ -173,18 +183,21 @@ enum {
 	IF_LONG_OPTS(     OPTBIT_QUIET      ,)
 	IF_LONG_OPTS(     OPTBIT_2STDOUT    ,)
 	IF_FEATURE_CPIO_IGNORE_DEVNO(OPTBIT_IGNORE_DEVNO,)
+	IF_FEATURE_CPIO_RENUMBER_INODES(OPTBIT_RENUMBER_INODES,)
 	OPT_CREATE             = IF_FEATURE_CPIO_O((1 << OPTBIT_CREATE     )) + 0,
 	OPT_FORMAT             = IF_FEATURE_CPIO_O((1 << OPTBIT_FORMAT     )) + 0,
 	OPT_PASSTHROUGH        = IF_FEATURE_CPIO_P((1 << OPTBIT_PASSTHROUGH)) + 0,
 	OPT_QUIET              = IF_LONG_OPTS(     (1 << OPTBIT_QUIET      )) + 0,
 	OPT_2STDOUT            = IF_LONG_OPTS(     (1 << OPTBIT_2STDOUT    )) + 0,
 	OPT_IGNORE_DEVNO       = IF_FEATURE_CPIO_IGNORE_DEVNO((1 << OPTBIT_IGNORE_DEVNO)) + 0,
+	OPT_RENUMBER_INODES    = IF_FEATURE_CPIO_RENUMBER_INODES((1 << OPTBIT_RENUMBER_INODES)) + 0,
 };
 
 #define OPTION_STR "it0uvdmLF:R:"
 
 struct globals {
 	struct bb_uidgid_t owner_ugid;
+	ino_t next_inode;
 } FIX_ALIASING;
 #define G (*(struct globals*)bb_common_bufsiz1)
 void BUG_cpio_globals_too_big(void);
@@ -218,6 +231,9 @@ static NOINLINE int cpio_o(void)
 		struct inodes_s *next;
 		struct name_s *names;
 		struct stat st;
+#if ENABLE_FEATURE_CPIO_RENUMBER_INODES
+		ino_t mapped_inode;
+#endif
 	};
 
 	struct inodes_s *links = NULL;
@@ -272,6 +288,10 @@ static NOINLINE int cpio_o(void)
 						l = xzalloc(sizeof(*l));
 						l->st = st;
 						l->next = links;
+#if ENABLE_FEATURE_CPIO_RENUMBER_INODES
+						if (option_mask32 & OPT_RENUMBER_INODES)
+							l->mapped_inode = ++G.next_inode;
+#endif
 						links = l;
 						break;
 					}
@@ -290,6 +310,11 @@ static NOINLINE int cpio_o(void)
 				free(line);
 				continue;
 			}
+#if ENABLE_FEATURE_CPIO_RENUMBER_INODES
+			else if (option_mask32 & OPT_RENUMBER_INODES) {
+				st.st_ino = ++G.next_inode;
+			}
+#endif
 		} else { /* line == NULL: EOF */
  next_link:
 			if (links) {
@@ -297,6 +322,10 @@ static NOINLINE int cpio_o(void)
 				st = links->st;
 				name = links->names->name;
 				links->names = links->names->next;
+#if ENABLE_FEATURE_CPIO_RENUMBER_INODES
+				if (links->mapped_inode)
+					st.st_ino = links->mapped_inode;
+#endif
 				/* GNU cpio is reported to emit file data
 				 * only for the last instance. Mimic that. */
 				if (links->names == NULL)
@@ -398,6 +427,9 @@ int cpio_main(int argc UNUSED_PARAM, char **argv)
 		"to-stdout\0"    No_argument       "\xfe"
 #if ENABLE_FEATURE_CPIO_IGNORE_DEVNO
 		"ignore-devno\0" No_argument	   "\xfd"
+#endif
+#if ENABLE_FEATURE_CPIO_RENUMBER_INODES
+		"renumber-inodes\0" No_argument    "\xfc"
 #endif
 		;
 #endif
-- 
2.32.0