1
1
use std:: {
2
2
fmt:: Display ,
3
- fs,
4
3
path:: { Path , PathBuf } ,
5
4
sync:: atomic:: Ordering ,
6
5
} ;
@@ -16,6 +15,7 @@ use oma_pm::{
16
15
apt:: { AptConfig , FilterMode , OmaApt , OmaAptArgs } ,
17
16
matches:: { GetArchMethod , PackagesMatcher } ,
18
17
pkginfo:: OmaPackageWithoutVersion ,
18
+ utils:: pkg_is_current_kernel,
19
19
} ;
20
20
use oma_utils:: dpkg:: dpkg_arch;
21
21
use once_cell:: sync:: OnceCell ;
@@ -276,47 +276,24 @@ impl CliExecuter for Topics {
276
276
277
277
// linux-kernel-VER 包在关闭 topic 的时候应该直接删除
278
278
if pkg_name. starts_with ( "linux-kernel-" ) {
279
- let kernel_ver = kernel_ver. get_or_try_init ( || {
279
+ let current_kernel_ver = kernel_ver. get_or_try_init ( || {
280
280
System :: kernel_version ( ) . context ( "Failed to get kernel version" )
281
281
} ) ?;
282
282
283
- debug ! ( "kernel version = {kernel_ver}" ) ;
284
-
285
- let image_name = image_name. get_or_init ( get_kernel_image_filename) ;
286
- debug ! ( "image name = {image_name:?}" ) ;
287
-
288
- let mut is_current_kernel = false ;
289
-
290
- if let Some ( image_name) = image_name {
291
- is_current_kernel =
292
- is_installed_pkg_contains_file ( pkg_name, image_name, & sysroot)
293
- . unwrap_or_else ( |e| {
294
- warn ! ( "{e}" ) ;
295
- false
296
- } ) ;
297
- } else {
298
- for i in [ "vmlinuz" , "vmlinux" ] {
299
- if is_installed_pkg_contains_file (
300
- pkg_name,
301
- & format ! ( "{i}-{kernel_ver}" ) ,
302
- & sysroot,
303
- )
304
- . unwrap_or_else ( |e| {
305
- warn ! ( "{e}" ) ;
306
- false
307
- } ) {
308
- is_current_kernel = true ;
309
- break ;
310
- }
311
- }
312
- }
283
+ debug ! ( "kernel version = {current_kernel_ver}" ) ;
284
+
285
+ let is_current_kernel =
286
+ pkg_is_current_kernel ( & sysroot, & image_name, pkg_name, current_kernel_ver) ;
313
287
314
288
debug ! ( "Deleting kernel package name = {pkg_name}" ) ;
315
289
316
290
// 如果现在删除的版本是正在使用的内核版本,将拒绝操作
317
291
if is_current_kernel {
318
292
return Err ( OutputError {
319
- description : fl ! ( "not-allow-delete-using-kernel" , ver = kernel_ver) ,
293
+ description : fl ! (
294
+ "not-allow-delete-using-kernel" ,
295
+ ver = current_kernel_ver
296
+ ) ,
320
297
source : None ,
321
298
} ) ;
322
299
}
@@ -418,44 +395,6 @@ impl CliExecuter for Topics {
418
395
}
419
396
}
420
397
421
- fn is_installed_pkg_contains_file (
422
- pkg_name : & str ,
423
- file_name : & str ,
424
- sysroot : & Path ,
425
- ) -> anyhow:: Result < bool > {
426
- let p = sysroot. join ( format ! ( "var/lib/dpkg/info/{pkg_name}.list" ) ) ;
427
- let file = fs:: read_to_string ( & p)
428
- . with_context ( || format ! ( "Failed to read dpkg list file: {}" , p. display( ) ) ) ?;
429
-
430
- for line in file. lines ( ) {
431
- if Path :: new ( line)
432
- . file_name ( )
433
- . is_some_and ( |n| n. to_string_lossy ( ) == file_name)
434
- {
435
- return Ok ( true ) ;
436
- }
437
- }
438
-
439
- Ok ( false )
440
- }
441
-
442
- fn get_kernel_image_filename ( ) -> Option < String > {
443
- let cmdline = fs:: read_to_string ( "/proc/cmdline" ) . ok ( ) ?;
444
-
445
- for i in cmdline. split_ascii_whitespace ( ) {
446
- if let Some ( image_path) = i. strip_prefix ( "BOOT_IMAGE=" ) {
447
- return Some (
448
- Path :: new ( image_path)
449
- . file_name ( )
450
- . map ( |x| x. to_string_lossy ( ) . to_string ( ) )
451
- . unwrap_or_else ( || image_path. to_string ( ) ) ,
452
- ) ;
453
- }
454
- }
455
-
456
- None
457
- }
458
-
459
398
fn refresh < ' a > (
460
399
network_threads : usize ,
461
400
no_progress : bool ,
0 commit comments