-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_unpak.c
200 lines (175 loc) · 4.11 KB
/
lib_unpak.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <archive.h>
#include <archive_entry.h>
#include <rpm/rpmlib.h>
#include <rpm/rpmts.h>
#include <rpm/rpmio.h>
#include <rpm/rpmpgp.h>
#include <rpm/rpmdb.h>
struct archive;
/**
* Initializes the archive structure for unpacking tar.gz files.
*
* @param a The archive structure to initialize.
* @return Always returns 0.
*/
int
unpack_targz (struct archive *a)
{
archive_read_support_filter_all (a);
archive_read_support_format_all (a);
return 0;
}
/**
* Initializes the archive structure for unpacking zip files.
*
* @param a The archive structure to initialize.
* @return Always returns 0.
*/
int
unpack_zip (struct archive *a)
{
archive_read_support_format_zip (a);
return 0;
}
/**
* Initializes the archive structure for unpacking 7z files.
*
* @param a The archive structure to initialize.
* @return Always returns 0.
*/
int
unpack_7z (struct archive *a)
{
archive_read_support_format_7zip (a);
return 0;
}
/**
* Initializes the archive structure for unpacking tar.bz2 files.
*
* @param a The archive structure to initialize.
* @return Always returns 0.
*/
int
unpack_tarbz2 (struct archive *a)
{
archive_read_support_filter_bzip2 (a);
archive_read_support_format_tar (a);
return 0;
}
/**
* Initializes the archive structure for unpacking tar.xz files.
*
* @param a The archive structure to initialize.
* @return Always returns 0.
*/
int
unpack_tarxz (struct archive *a)
{
archive_read_support_filter_xz (a);
archive_read_support_format_tar (a);
return 0;
}
/**
* Initializes the archive structure for unpacking xz files.
*
* @param a The archive structure to initialize.
* @return Always returns 0.
*/
int
unpack_xz (struct archive *a)
{
archive_read_support_filter_xz (a);
archive_read_support_format_raw (a);
return 0;
}
/**
* Initializes the archive structure for unpacking bz2 files.
*
* @param a The archive structure to initialize.
* @return Always returns 0.
*/
int
unpack_bz2 (struct archive *a)
{
archive_read_support_filter_bzip2 (a);
archive_read_support_format_raw (a);
return 0;
}
/**
* Initializes the archive structure for unpacking .deb files.
*
* @param a The archive structure to initialize.
* @return Always returns 0.
*/
int
unpack_deb (struct archive *a)
{
archive_read_support_format_ar (a);
archive_read_support_format_tar (a);
archive_read_support_filter_gzip (a);
archive_read_support_filter_xz (a);
return 0;
}
/**
* Unpacks an RPM file to a specified output directory.
*
* This function takes an RPM file as input and extracts its contents to
* the specified output directory. It handles signature verification bypass
* and uses rpm2cpio and cpio for actual extraction.
*
* @param rpm_file The path to the RPM file to be unpacked.
* @param output_dir The directory where the contents will be extracted.
* @return 0 on success, -1 on failure.
*/
int
unpack_rpm (const char *rpm_file, const char *output_dir)
{
rpmts ts = NULL;
FD_t fd = NULL;
Header hdr = NULL;
rpmRC rc;
int result = 0;
// Initialize RPM library
rpmReadConfigFiles (NULL, NULL);
ts = rpmtsCreate ();
// Disable signature checking
rpmtsSetVSFlags (ts, _RPMVSF_NOSIGNATURES);
// Open the RPM file
fd = Fopen (rpm_file, "r.ufdio");
if (fd == NULL)
{
fprintf (stderr, "Failed to open RPM file: %s\n", rpm_file);
result = -1;
goto cleanup;
}
// Read the RPM header
rc = rpmReadPackageFile (ts, fd, rpm_file, &hdr);
if (rc != RPMRC_OK)
{
fprintf (stderr, "Failed to read RPM header (error code: %d)\n", rc);
result = -1;
goto cleanup;
}
// Create the output directory
char cmd[1024];
snprintf (cmd, sizeof (cmd), "mkdir -p %s", output_dir);
system (cmd);
// Extract the contents
snprintf (cmd, sizeof (cmd), "rpm2cpio %s | (cd %s && cpio -idmv)",
rpm_file, output_dir);
if (system (cmd) != 0)
{
fprintf (stderr, "Failed to extract RPM contents\n");
result = -1;
goto cleanup;
}
printf ("RPM unpacked successfully to: %s\n", output_dir);
cleanup:
if (hdr)
headerFree (hdr);
if (fd)
Fclose (fd);
if (ts)
rpmtsFree (ts);
return result;
}