-
Notifications
You must be signed in to change notification settings - Fork 5
/
dpkg_lib.cc
212 lines (179 loc) · 5.05 KB
/
dpkg_lib.cc
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
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright © 2022 Alexandre Detiste <[email protected]>
// SPDX-License-Identifier: GPL-2.0-or-later
#define LIBDPKG_VOLATILE_API
#include <array>
#include <iostream>
#include <algorithm>
extern "C" {
#include <sys/stat.h>
#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/db-fsys.h>
#include <dpkg/fsys.h>
#include <dpkg/pkg-array.h>
#include <dpkg/pkg-list.h>
#include <dpkg/pkg-show.h>
}
#include "dpkg.h"
#include "usr_merge.h"
#ifndef LIBDPKG_HAS_NEW_FHFF
#define FHFF_NO_COPY FHFF_NOCOPY
#endif
/*
https://www.dpkg.org/doc/libdpkg/structpkginfo.html
*/
void dpkg_start(const string& root_dir) {
dpkg_fsys_set_dir(root_dir == "/" ? "" : root_dir.c_str());
dpkg_program_init("cruft");
modstatdb_open(msdbrw_readonly);
}
void dpkg_end() {
modstatdb_shutdown();
dpkg_program_done();
}
int query(const char *path)
{
ensure_allinstfiles_available_quiet();
ensure_diversions();
struct fsys_namenode *namenode;
namenode = fsys_hash_find_node(path, FHFF_NO_COPY);
if (namenode->divert) {
cout << namenode->divert->pkgset->name << '\n';
return 1;
} else if(namenode->packages) {
cout << namenode->packages->pkg->set->name << '\n';
return 1;
}
return 0;
}
int read_dpkg_header(vector<string>& packages)
{
struct pkg_array array;
pkg_array_init_from_hash(&array);
pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
int i;
for (i = 0; i < array.n_pkgs; i++) {
struct pkginfo *pkg;
pkg = array.pkgs[i];
if (pkg->status == PKG_STAT_INSTALLED || pkg->status == PKG_STAT_CONFIGFILES)
packages.emplace_back(pkg->set->name);
}
pkg_array_destroy(&array);
return 0;
}
static void csv(const char *dpkg_name, string realname, const char *package)
{
struct stat st;
if (stat(dpkg_name, &st) != 0)
return;
if (S_ISDIR(st.st_mode))
return;
char type;
type = S_ISLNK(st.st_mode) ? 'l' : 'f';
replace(realname.begin(), realname.end(), ';', '_');
cout << realname << ';'
<< package << ';'
<< type << ';'
<< '0' << ';'
<< st.st_size << '\n';
}
static const char *admindir;
#ifdef DB_API
#include <dpkg/db-ctrl.h>
vector<string>* var_lib_dpkg_info = NULL;
static void callback(const char *filename, const char *filetype)
{
var_lib_dpkg_info->push_back(filename);
}
#endif
int read_dpkg(vector<string>& packages, vector<string>& output, bool print_csv, const string& root_dir)
{
struct pkg_array array;
struct pkginfo *pkg;
bool debug = getenv("DEBUG_DPKG") != nullptr;
struct stat buffer;
admindir = dpkg_db_set_dir(admindir);
#ifdef DB_API
var_lib_dpkg_info = &output;
#else
const std::array<string, 16> suffixes {
".clilibs",
".conffiles",
".config",
".fortran_mod",
".list",
".md5sums",
".postinst",
".postrm",
".preinst",
".prerm",
".runit",
".shlibs",
".starlibs",
".symbols",
".templates",
".triggers",
};
#endif
pkg_array_init_from_hash(&array);
pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
ensure_diversions();
ensure_allinstfiles_available_quiet();
auto root_dir_length = root_dir.length()-1;
for (int i = 0; i < array.n_pkgs; i++) {
pkg = array.pkgs[i];
if (pkg->status == PKG_STAT_INSTALLED || pkg->status == PKG_STAT_CONFIGFILES) {
if(pkg->status == PKG_STAT_INSTALLED) {
packages.emplace_back(pkg->set->name);
}
if (debug) cout << "PACKAGE:" << pkg->set->name << '\n';
#ifdef DB_API
// this is just too slow, from 2 seconds to 40
// I hope this will go away in the big DPKG rewrite
pkg_infodb_foreach(pkg, &pkg->installed, callback);
#else
string control_ = admindir;
control_ += "/info/";
// not ok for i386 only packages:
// steam, steamcmd, zsnes ...
//control += pkg_name(pkg, pnaw_nonambig);
control_ += pkg->set->name;
if (pkg->installed.multiarch == PKG_MULTIARCH_SAME) {
control_ += ":";
control_ += pkg->installed.arch->name;
}
for (const auto& suffix: suffixes) {
string control = control_ + suffix;
if (stat(control.c_str(), &buffer) == 0) {
output.emplace_back(control.substr(root_dir_length));
}
}
#endif
for (const struct fsys_namenode_list *file = pkg->files; file != nullptr; file = file->next) {
const struct fsys_namenode *namenode = file->namenode;
if (debug) cout << namenode->name << '\n';
if (namenode->divert) {
// We trust DPKG state for now
if (stat(namenode->name, &buffer) == 0) {
string realname = usr_merge(namenode->name);
if (print_csv) csv(namenode->name, realname, pkg->set->name);
output.emplace_back(std::move(realname));
}
if (stat(namenode->divert->useinstead->name, &buffer) == 0) {
string realname = usr_merge(namenode->divert->useinstead->name);
if (print_csv) csv(namenode->name, realname, pkg->set->name);
output.emplace_back(std::move(realname));
}
} else {
string realname = usr_merge(namenode->name);
if (print_csv) csv(namenode->name, realname, pkg->set->name);
output.emplace_back(std::move(realname));
}
}
}
}
pkg_array_destroy(&array);
sort(output.begin(), output.end());
output.erase( unique( output.begin(), output.end() ), output.end() );
return 0;
}