forked from Morganamilo/paru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo.rs
450 lines (383 loc) · 12 KB
/
repo.rs
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use crate::config::{Config, LocalRepos, Sign};
use crate::exec;
use crate::fmt::print_indent;
use crate::printtr;
use crate::util::ask;
use std::collections::HashMap;
use std::env::current_exe;
use std::ffi::OsStr;
use std::fs::{read_dir, read_link};
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};
use std::process::Command;
use alpm::{AlpmListMut, Db};
use ansiterm::Style;
use anyhow::{Context, Error, Result};
use nix::unistd::{Gid, Uid, User};
use tr::tr;
use unicode_width::UnicodeWidthStr;
pub fn add<P: AsRef<Path>, S: AsRef<OsStr>>(
config: &Config,
path: P,
name: &str,
pkgs: &[S],
) -> Result<()> {
let db = path.as_ref().join(format!("{}.db", name));
let name = if db.exists() {
if pkgs.is_empty() {
return Ok(());
}
read_link(&db).context("readlink")?
} else if !name.contains(".db.") {
PathBuf::from(format!("{}.db.tar.gz", name))
} else {
PathBuf::from(name)
};
let path = path.as_ref();
let file = path.join(name);
if !db.exists() {
let mut cmd = Command::new("install");
cmd.arg("-dm755").arg(path);
if exec::command_output(&mut cmd).is_err() {
let mut cmd = Command::new(&config.sudo_bin);
cmd.arg("install")
.arg("-dm755")
.arg("-o")
.arg(Uid::current().as_raw().to_string())
.arg("-g")
.arg(Gid::current().as_raw().to_string())
.arg(path);
exec::command(&mut cmd)?;
}
}
let pkgs = pkgs
.iter()
.map(|p| path.join(Path::new(p.as_ref()).file_name().unwrap()))
.collect::<Vec<_>>();
let mut cmd = Command::new("repo-add");
if !config.keep_repo_cache {
cmd.arg("-R");
}
cmd.arg(file).args(pkgs);
if config.sign_db != Sign::No {
cmd.arg("-s");
if let Sign::Key(ref k) = config.sign_db {
cmd.arg("-k");
cmd.arg(k);
}
}
let err = exec::command(&mut cmd);
let user = User::from_uid(Uid::current()).unwrap().unwrap();
if err.is_err() {
eprintln!(
"Could not add packages to repo:
paru now expects local repos to be writable as your user:
You should chown/chmod your repos to be writable by you:
chown -R {}: {}",
user.name,
path.display()
);
}
err
}
pub fn remove<P: AsRef<Path>, S: AsRef<OsStr>>(
config: &Config,
path: P,
name: &str,
pkgs: &[S],
) -> Result<()> {
let path = path.as_ref();
let db = path.join(format!("{}.db", name));
if pkgs.is_empty() || !db.exists() {
return Ok(());
}
let name = read_link(db)?;
let file = path.join(name);
let mut cmd = Command::new("repo-remove");
cmd.arg(file);
if config.sign_db != Sign::No {
cmd.arg("-s");
if let Sign::Key(ref k) = config.sign_db {
cmd.arg("-k");
cmd.arg(k);
}
}
cmd.args(pkgs);
exec::command(&mut cmd)?;
Ok(())
}
pub fn init<P: AsRef<Path>>(config: &Config, path: P, name: &str) -> Result<()> {
let pkgs: &[&str] = &[];
add(config, path, name, pkgs)
}
fn is_configured_local_db(config: &Config, db: &Db) -> bool {
match config.repos {
LocalRepos::None => false,
LocalRepos::Default => is_local_db(db),
LocalRepos::Repo(ref r) => is_local_db(db) && r.iter().any(|r| *r == db.name()),
}
}
pub fn file(repo: &Db) -> Option<&str> {
repo.servers()
.first()
.map(|s| s.trim_start_matches("file://"))
}
pub fn all_files(config: &Config) -> Vec<String> {
config
.alpm
.syncdbs()
.iter()
.flat_map(|db| db.servers())
.filter(|f| f.starts_with("file://"))
.map(|s| s.trim_start_matches("file://").to_string())
.collect()
}
fn is_local_db(db: &alpm::Db) -> bool {
!db.servers().is_empty() && db.servers().iter().all(|s| s.starts_with("file://"))
}
pub fn repo_aur_dbs(config: &Config) -> (AlpmListMut<&Db>, AlpmListMut<&Db>) {
let dbs = config.alpm.syncdbs();
let mut aur = dbs.to_list_mut();
let mut repo = dbs.to_list_mut();
aur.retain(|db| is_configured_local_db(config, db));
repo.retain(|db| !is_configured_local_db(config, db));
(repo, aur)
}
pub fn delete(config: &mut Config) -> Result<(), Error> {
let (_, mut repos) = repo_aur_dbs(config);
repos.retain(|r| {
config.delete >= 1
|| config.uninstall
|| config.targets.is_empty()
|| config.targets.contains(&r.name().to_string())
});
if config.delete >= 1 {
let mut rm = HashMap::<&str, Vec<&str>>::new();
let mut rmfiles = Vec::new();
for repo in &repos {
for pkg in repo.pkgs() {
if config.targets.iter().any(|p| p == pkg.name()) {
rm.entry(repo.name()).or_default().push(pkg.name());
}
}
}
let cb = config.alpm.take_raw_log_cb();
for repo in &repos {
if let Some(pkgs) = rm.get(&repo.name()) {
let path = repo
.servers()
.first()
.unwrap()
.trim_start_matches("file://");
remove(config, path, repo.name(), pkgs)?;
let files = read_dir(path)?;
for file in files {
let file = file?;
if let Ok(pkg) = config.alpm.pkg_load(
file.path().as_os_str().as_bytes(),
false,
alpm::SigLevel::NONE,
) {
if pkgs.contains(&pkg.name()) {
rmfiles.push(file.path());
let mut sig = file.path().to_path_buf().into_os_string();
sig.push(".sig");
let sig = PathBuf::from(sig);
if sig.exists() {
rmfiles.push(sig);
}
}
}
}
}
}
config.alpm.set_raw_log_cb(cb);
if !rmfiles.is_empty() {
let mut cmd = Command::new(&config.sudo_bin);
cmd.arg("rm").args(rmfiles);
exec::command(&mut cmd)?;
}
let repos = repos
.into_iter()
.map(|r| r.name().to_string())
.collect::<Vec<_>>();
refresh(config, &repos)?;
if config.delete >= 2 {
config.need_root = true;
let db = config.alpm.localdb();
let pkgs = config
.targets
.iter()
.map(|p| p.as_str())
.filter(|p| db.pkg(*p).is_ok());
let mut args = config.pacman_globals();
args.op("remove");
args.targets = pkgs.collect();
if !args.targets.is_empty() {
exec::pacman(config, &args)?.success()?;
}
}
return Ok(());
}
Ok(())
}
pub fn refresh<S: AsRef<OsStr>>(config: &mut Config, repos: &[S]) -> Result<i32> {
let exe = current_exe().context(tr!("failed to get current exe"))?;
let c = config.color;
let mut dbs = config.alpm.syncdbs().to_list_mut();
dbs.retain(|db| is_local_db(db));
if !repos.is_empty() {
dbs.retain(|db| repos.iter().any(|r| r.as_ref() == db.name()));
}
for db in dbs {
let path = file(db);
if let Some(path) = path {
init(config, path, db.name())?;
}
}
if !nix::unistd::getuid().is_root() && !cfg!(feature = "mock") {
let mut cmd = Command::new(&config.sudo_bin);
cmd.arg(exe);
if let Some(ref conf) = config.pacman_conf {
cmd.arg("--config").arg(conf);
}
cmd.arg("--dbpath")
.arg(config.alpm.dbpath())
.arg("-Ly")
.args(repos);
let status = cmd.spawn()?.wait()?;
return Ok(status.code().unwrap_or(1));
}
let mut dbs = config.alpm.syncdbs_mut().to_list_mut();
dbs.retain(|db| is_local_db(db));
if !repos.is_empty() {
dbs.retain(|db| repos.iter().any(|r| r.as_ref() == db.name()));
}
println!(
"{} {}",
c.action.paint("::"),
c.bold.paint(tr!("syncing local databases..."))
);
if !dbs.is_empty() {
dbs.list().update(cfg!(feature = "mock"))?;
} else {
printtr!(" nothing to do");
}
Ok(0)
}
pub fn clean(config: &mut Config) -> Result<i32> {
let c = config.color;
let (_, repos) = repo_aur_dbs(config);
let repo_names = repos
.iter()
.map(|r| r.name().to_string())
.collect::<Vec<_>>();
drop(repos);
refresh(config, &repo_names)?;
let (_, repos) = repo_aur_dbs(config);
let db = config.alpm.localdb();
let mut rem = repos
.iter()
.map(|repo| {
repo.pkgs()
.iter()
.filter(|pkg| db.pkg(pkg.name()).is_err())
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
rem.retain(|r| !r.is_empty());
drop(repos);
if rem.is_empty() {
printtr!("there is nothing to do");
return Ok(0);
}
println!();
let count = rem.iter().fold(0, |acc, r| acc + r.len());
let fmt = format!("{} ({}) ", tr!("Packages"), count);
let start = fmt.width();
print!("{}", c.bold.paint(fmt));
print_indent(
Style::new(),
start,
4,
config.cols,
" ",
rem.iter().flatten().map(|p| p.name()),
);
println!();
if !ask(config, &tr!("Proceed with removal?"), true) {
return Ok(1);
}
for pkgs in &rem {
let repo = pkgs[0].db().unwrap();
let path = file(repo).unwrap();
let pkgs = pkgs.iter().map(|p| p.name()).collect::<Vec<_>>();
remove(config, path, repo.name(), &pkgs)?;
}
let mut rmfiles = Vec::new();
for pkg in rem.iter().flatten() {
let repo = pkg.db().unwrap();
let path = file(repo).unwrap();
let pkgfile = Path::new(path).join(pkg.filename().unwrap());
rmfiles.push(pkgfile);
}
if !rmfiles.is_empty() {
let mut cmd = Command::new(&config.sudo_bin);
cmd.arg("rm").args(rmfiles);
exec::command(&mut cmd)?;
}
let (_, repos) = repo_aur_dbs(config);
let repo_names = repos
.iter()
.map(|r| r.name().to_string())
.collect::<Vec<_>>();
drop(repos);
refresh(config, &repo_names)?;
Ok(0)
}
pub fn print(
repos: AlpmListMut<&alpm::Db>,
config: &Config,
repoc: Style,
pkgc: Style,
version: Style,
installedc: Style,
) {
for repo in repos {
if config.list {
for pkg in repo.pkgs() {
if config.quiet {
println!("{}", pkg.name());
} else {
print!(
"{} {} {}",
repoc.paint(repo.name()),
pkgc.paint(pkg.name()),
version.paint(pkg.version().as_str())
);
let local_pkg = config.alpm.localdb().pkg(pkg.name());
if let Ok(local_pkg) = local_pkg {
let installed = if local_pkg.version() != pkg.version() {
tr!(" [installed: {}]", local_pkg.version())
} else {
tr!(" [installed]")
};
print!("{}", installedc.paint(installed));
}
println!();
}
}
} else if config.quiet {
println!("{}", repo.name());
} else {
println!(
"{} {}",
repo.name(),
repo.servers()
.first()
.unwrap()
.trim_start_matches("file://")
);
}
}
}