Skip to content

Commit 0f36902

Browse files
committed
Migrate to rust 2024
1 parent 9833a89 commit 0f36902

File tree

2 files changed

+77
-66
lines changed

2 files changed

+77
-66
lines changed

native/src/boot/cli.rs

Lines changed: 71 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use crate::cpio::{cpio_commands, print_cpio_usage};
2-
use crate::dtb::{dtb_commands, print_dtb_usage, DtbAction};
2+
use crate::dtb::{DtbAction, dtb_commands, print_dtb_usage};
33
use crate::ffi::{
4-
cleanup, compress, decompress_raw, formats, repack, sign, split_image_dtb, unpack, verify
4+
cleanup, compress, decompress_raw, formats, repack, sign, split_image_dtb, unpack, verify,
55
};
66
use crate::patch::hexpatch;
77
use crate::payload::extract_boot_from_payload;
88
use crate::sign::sha1_hash;
99
use argh::FromArgs;
1010
use base::{
11-
cmdline_logging, libc::umask, log_err, map_args, raw_cstr, EarlyExitExt, LoggedResult,
12-
MappedFile, ResultExt, Utf8CStr,
11+
EarlyExitExt, LoggedResult, MappedFile, ResultExt, Utf8CStr, cmdline_logging, libc::umask,
12+
log_err, map_args, raw_cstr,
1313
};
1414
use std::ffi::c_char;
1515

@@ -259,14 +259,10 @@ Supported actions:
259259
);
260260
}
261261

262-
#[no_mangle]
263-
pub unsafe extern "C" fn main(
264-
argc: i32,
265-
argv: *const *const c_char,
266-
_envp: *const *const c_char,
267-
) -> i32 {
262+
#[unsafe(no_mangle)]
263+
pub extern "C" fn main(argc: i32, argv: *const *const c_char, _envp: *const *const c_char) -> i32 {
268264
cmdline_logging();
269-
umask(0);
265+
unsafe { umask(0) };
270266
let res: LoggedResult<()> = try {
271267
let mut cmds = map_args(argc, argv)?;
272268
if argc < 2 {
@@ -295,44 +291,61 @@ pub unsafe extern "C" fn main(
295291
dump_header,
296292
ref mut img,
297293
}) => {
298-
return unpack(
299-
Utf8CStr::from_string(img).as_ptr(),
300-
no_decompress,
301-
dump_header,
302-
);
294+
return unsafe {
295+
unpack(
296+
Utf8CStr::from_string(img).as_ptr(),
297+
no_decompress,
298+
dump_header,
299+
)
300+
};
303301
}
304302
Action::Repack(Repack {
305303
no_compress,
306304
ref mut img,
307305
ref mut out,
308306
}) => {
309-
repack(
310-
Utf8CStr::from_string(img).as_ptr(),
311-
Utf8CStr::from_string(out).as_ptr(),
312-
no_compress,
313-
);
307+
unsafe {
308+
repack(
309+
Utf8CStr::from_string(img).as_ptr(),
310+
Utf8CStr::from_string(out).as_ptr(),
311+
no_compress,
312+
)
313+
};
314314
}
315315
Action::Verify(Verify {
316316
ref mut img,
317317
ref mut cert,
318318
}) => {
319-
return verify(Utf8CStr::from_string(img).as_ptr(), cert.as_mut().map(|x| Utf8CStr::from_string(x).as_ptr()).unwrap_or(std::ptr::null()));
319+
return unsafe {
320+
verify(
321+
Utf8CStr::from_string(img).as_ptr(),
322+
cert.as_mut()
323+
.map(|x| Utf8CStr::from_string(x).as_ptr())
324+
.unwrap_or(std::ptr::null()),
325+
)
326+
};
320327
}
321328
Action::Sign(Sign {
322329
ref mut img,
323330
ref mut args,
324331
}) => {
325332
let (pem, pk8) = match args.get_mut(1..=2) {
326-
Some([pem,pk8]) => (Utf8CStr::from_string(pem).as_ptr(), Utf8CStr::from_string(pk8).as_ptr()),
333+
Some([pem, pk8]) => (
334+
Utf8CStr::from_string(pem).as_ptr(),
335+
Utf8CStr::from_string(pk8).as_ptr(),
336+
),
327337
_ => (std::ptr::null(), std::ptr::null()),
328338
};
329-
return sign(
330-
Utf8CStr::from_string(img).as_ptr(),
331-
args.first_mut()
332-
.map(|x| Utf8CStr::from_string(x).as_ptr())
333-
.unwrap_or(raw_cstr!("/boot")),
334-
pem, pk8
335-
)
339+
return unsafe {
340+
sign(
341+
Utf8CStr::from_string(img).as_ptr(),
342+
args.first_mut()
343+
.map(|x| Utf8CStr::from_string(x).as_ptr())
344+
.unwrap_or(raw_cstr!("/boot")),
345+
pem,
346+
pk8,
347+
)
348+
};
336349
}
337350
Action::Extract(Extract {
338351
ref payload,
@@ -353,7 +366,11 @@ pub unsafe extern "C" fn main(
353366
ref mut src,
354367
ref mut dest,
355368
}) => {
356-
if !hexpatch(file, Utf8CStr::from_string(src), Utf8CStr::from_string(dest)) {
369+
if !hexpatch(
370+
file,
371+
Utf8CStr::from_string(src),
372+
Utf8CStr::from_string(dest),
373+
) {
357374
Err(log_err!("Failed to patch"))?;
358375
}
359376
}
@@ -368,7 +385,7 @@ pub unsafe extern "C" fn main(
368385
0
369386
} else {
370387
1
371-
}
388+
};
372389
}
373390
Action::Dtb(Dtb {
374391
ref mut file,
@@ -381,13 +398,15 @@ pub unsafe extern "C" fn main(
381398
0
382399
} else {
383400
1
384-
}
401+
};
385402
}
386403
Action::Split(Split {
387404
no_decompress,
388405
ref mut file,
389406
}) => {
390-
return split_image_dtb(Utf8CStr::from_string(file).as_ptr(), no_decompress);
407+
return unsafe {
408+
split_image_dtb(Utf8CStr::from_string(file).as_ptr(), no_decompress)
409+
};
391410
}
392411
Action::Sha1(Sha1 { ref mut file }) => {
393412
let file = MappedFile::open(Utf8CStr::from_string(file))?;
@@ -406,31 +425,31 @@ pub unsafe extern "C" fn main(
406425
ref mut file,
407426
ref mut out,
408427
}) => {
409-
decompress_raw(
410-
Utf8CStr::from_string(file).as_mut_ptr(),
411-
out.as_mut()
412-
.map(|x| Utf8CStr::from_string(x).as_ptr())
413-
.unwrap_or(std::ptr::null()),
414-
);
428+
unsafe {
429+
decompress_raw(
430+
Utf8CStr::from_string(file).as_mut_ptr(),
431+
out.as_mut()
432+
.map(|x| Utf8CStr::from_string(x).as_ptr())
433+
.unwrap_or(std::ptr::null()),
434+
)
435+
};
415436
}
416437
Action::Compress(Compress {
417438
ref mut file,
418439
ref mut format,
419440
ref mut out,
420441
}) => {
421-
compress(
422-
Utf8CStr::from_string(format).as_ptr(),
423-
Utf8CStr::from_string(file).as_ptr(),
424-
out.as_mut()
425-
.map(|x| Utf8CStr::from_string(x).as_ptr())
426-
.unwrap_or(std::ptr::null()),
427-
);
442+
unsafe {
443+
compress(
444+
Utf8CStr::from_string(format).as_ptr(),
445+
Utf8CStr::from_string(file).as_ptr(),
446+
out.as_mut()
447+
.map(|x| Utf8CStr::from_string(x).as_ptr())
448+
.unwrap_or(std::ptr::null()),
449+
)
450+
};
428451
}
429452
}
430453
};
431-
if res.is_ok() {
432-
0
433-
} else {
434-
1
435-
}
454+
if res.is_ok() { 0 } else { 1 }
436455
}

native/src/sepolicy/cli.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::ffi::SePolicy;
22
use argh::FromArgs;
33
use base::{
4-
cmdline_logging, cstr, libc::umask, log_err, map_args, EarlyExitExt, LoggedResult, Utf8CStr,
4+
EarlyExitExt, LoggedResult, Utf8CStr, cmdline_logging, cstr, libc::umask, log_err, map_args,
55
};
66
use std::{ffi::c_char, io::Cursor};
77

@@ -60,18 +60,14 @@ it will load from current live policies (/sys/fs/selinux/policy)
6060
"#,
6161
cmd
6262
);
63-
63+
6464
SePolicy::print_statement_help();
6565
}
6666

67-
#[no_mangle]
68-
pub unsafe extern "C" fn main(
69-
argc: i32,
70-
argv: *const *const c_char,
71-
_envp: *const *const c_char,
72-
) -> i32 {
67+
#[unsafe(no_mangle)]
68+
pub extern "C" fn main(argc: i32, argv: *const *const c_char, _envp: *const *const c_char) -> i32 {
7369
cmdline_logging();
74-
umask(0);
70+
unsafe { umask(0) };
7571
let res: LoggedResult<()> = try {
7672
let cmds = map_args(argc, argv)?;
7773
if argc < 2 {
@@ -127,9 +123,5 @@ pub unsafe extern "C" fn main(
127123
}
128124
}
129125
};
130-
if res.is_ok() {
131-
0
132-
} else {
133-
1
134-
}
126+
if res.is_ok() { 0 } else { 1 }
135127
}

0 commit comments

Comments
 (0)