-
Notifications
You must be signed in to change notification settings - Fork 5
/
dpkg_popen.cc
177 lines (146 loc) · 4.52 KB
/
dpkg_popen.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
// Copyright © 2015 Alexandre Detiste <[email protected]>
// SPDX-License-Identifier: GPL-2.0-or-later
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include "dpkg.h"
#include "usr_merge.h"
void dpkg_start(const string& root_dir) {}
void dpkg_end() {}
int query(const char *path)
{
// not implemented: diversions
FILE* fp;
setenv("LANG", "C", 1);
char buf[4000];
char *pos;
sprintf(buf, "dpkg-query --search '%s' 2>/dev/null", path);
if ((fp = popen(buf, "r")) == NULL) return 0;
if (!fgets(buf, sizeof(buf), fp)) return 0;
pos = strchr(buf, ':');
pos[0] = '\0';
pos = strchr(buf, ',');
if(pos) pos[0] = '\0';
puts(buf);
return 1;
}
int read_dpkg_header(vector<string>& packages)
{
bool debug=getenv("DEBUG") != NULL;
if (debug) cerr << "DPKG DATA\n";
FILE* fp;
if ((fp = popen("dpkg-query --show --showformat '${Package}\n' | sort -u", "r")) == NULL) return 1;
const int SIZEBUF = 4096;
char buf[SIZEBUF];
string package;
while (fgets(buf, sizeof(buf),fp))
{
package=buf;
package=package.substr(0,package.size() - 1); // remove '/n'
//cerr << package << endl;
packages.push_back(package);
}
pclose(fp);
if (debug) cerr << packages.size() << " packages installed" << endl << endl;
return 0;
}
int read_diversions(vector<Diversion>& diversions)
{
bool debug=getenv("DEBUG") != NULL;
FILE* fp;
setenv("LANG", "C", 1);
if ((fp = popen("dpkg-divert --list", "r")) == NULL) return 1;
const int SIZEBUF = 4096;
char buf[SIZEBUF];
while (fgets(buf, sizeof(buf),fp))
{
const char* delim = " ";
bool local;
const char* LOCAL = "local";
string oldfile,newfile,package;
if (debug) cerr << buf << endl;
//diversion of /usr/share/dict/words to /usr/share/dict/words.pre-dictionaries-common by dictionaries-common
//diversion of /usr/share/man/man1/sh.1.gz to /usr/share/man/man1/sh.distrib.1.gz by dash
//diversion of /usr/bin/firefox to /usr/bin/firefox.real by firefox-esr
//diversion of /bin/sh to /bin/sh.distrib by dash
//local diversion of /etc/apt/apt.conf.d/20packagekit to /etc/PackageKit/20packagekit.distrib
// bug #1010362
local = !strncmp(strtok((char*)buf, delim), LOCAL, strlen(LOCAL));
if (local) {
strtok(NULL, delim);
}
strtok(NULL, delim);
oldfile = strtok(NULL, delim);
strtok(NULL, delim);
newfile = strtok(NULL, delim);
if (local) {
newfile = newfile.substr(0,newfile.size() - 1); // remove '/n'
package = LOCAL;
} else {
strtok(NULL, delim);
package = strtok(NULL, delim);
package = package.substr(0,package.size() - 1); // remove '/n'
}
diversions.push_back(Diversion(oldfile,newfile,package));
}
pclose(fp);
if (debug) cerr << diversions.size() << " files diverted" << endl << endl;
return 0;
}
static int read_dpkg_items(vector<string>& dpkg)
{
bool debug=getenv("DEBUG") != NULL;
if (debug) cerr << "READING FILES IN DPKG DATABASE" << endl;
vector<Diversion> diversions;
read_diversions(diversions);
dpkg.push_back("/");
string command="dpkg-query --listfiles $(dpkg-query --show --showformat '${binary:Package} ')|sort -u";
const int SIZEBUF = 300;
char buf[SIZEBUF];
FILE* fp;
if ((fp = popen(command.c_str(), "r")) == NULL) return 1;
while (fgets(buf, sizeof(buf),fp))
{
string filename=buf;
if (filename.substr(0,1)!="/") continue;
filename=filename.substr(0,filename.size() - 1);
vector<Diversion>::iterator it=diversions.begin();
struct stat stat_buffer;
for(;it !=diversions.end();it++) {
if (filename==(*it).oldfile
&& stat(filename.c_str(),&stat_buffer)!= 0) filename=(*it).newfile;
}
dpkg.push_back(usr_merge(filename));
// also consider all intermediate subdirectories under /etc
if (filename.substr(0,5)!="/etc/")
continue;
if (stat(filename.c_str(),&stat_buffer) == 0)
if ((stat_buffer.st_mode & S_IFDIR) != 0)
continue;
while (1)
{
size_t found;
found=filename.find_last_of("/");
filename=filename.substr(0,found);
if (filename == "/etc")
break;
dpkg.push_back(filename);
}
}
pclose(fp);
if (debug) cerr << "done" << endl;
sort(dpkg.begin(), dpkg.end());
// remove duplicates
dpkg.erase( unique( dpkg.begin(), dpkg.end() ), dpkg.end() );
if (debug) cerr << dpkg.size() << " files in DPKG database" << endl;
return 0;
}
int read_dpkg(vector<string>& packages, vector<string>& files, bool csv, const string& root_dir) {
read_dpkg_header(packages);
read_dpkg_items(files);
// some more horrible hack for Buster backport
if (csv) (system("/usr/lib/cruft/dpkg_csv.py")==0);
return 0;
}