@@ -216,16 +216,16 @@ fn ensure_writable_boot() -> Result<()> {
216216}
217217
218218/// daemon implementation of component update
219- pub ( crate ) fn update ( name : & str ) -> Result < ComponentUpdateResult > {
220- let mut state = SavedState :: load_from_disk ( "/" ) ?. unwrap_or_default ( ) ;
219+ pub ( crate ) fn update ( name : & str , rootcxt : & RootContext ) -> Result < ComponentUpdateResult > {
220+ let mut state = SavedState :: load_from_disk ( & rootcxt . path ) ?. unwrap_or_default ( ) ;
221221 let component = component:: new_from_name ( name) ?;
222222 let inst = if let Some ( inst) = state. installed . get ( name) {
223223 inst. clone ( )
224224 } else {
225225 anyhow:: bail!( "Component {} is not installed" , name) ;
226226 } ;
227- let sysroot = openat :: Dir :: open ( "/" ) ? ;
228- let update = component. query_update ( & sysroot) ?;
227+ let sysroot = & rootcxt . sysroot ;
228+ let update = component. query_update ( sysroot) ?;
229229 let update = match update. as_ref ( ) {
230230 Some ( p) if inst. meta . can_upgrade_to ( p) => p,
231231 _ => return Ok ( ComponentUpdateResult :: AtLatestVersion ) ,
@@ -236,6 +236,7 @@ pub(crate) fn update(name: &str) -> Result<ComponentUpdateResult> {
236236 let mut pending_container = state. pending . take ( ) . unwrap_or_default ( ) ;
237237 let interrupted = pending_container. get ( component. name ( ) ) . cloned ( ) ;
238238 pending_container. insert ( component. name ( ) . into ( ) , update. clone ( ) ) ;
239+ let sysroot = sysroot. try_clone ( ) ?;
239240 let mut state_guard =
240241 SavedState :: acquire_write_lock ( sysroot) . context ( "Failed to acquire write lock" ) ?;
241242 state_guard
@@ -257,19 +258,20 @@ pub(crate) fn update(name: &str) -> Result<ComponentUpdateResult> {
257258}
258259
259260/// daemon implementation of component adoption
260- pub ( crate ) fn adopt_and_update ( name : & str ) -> Result < ContentMetadata > {
261- let sysroot = openat :: Dir :: open ( "/" ) ? ;
262- let mut state = SavedState :: load_from_disk ( "/" ) ?. unwrap_or_default ( ) ;
261+ pub ( crate ) fn adopt_and_update ( name : & str , rootcxt : & RootContext ) -> Result < ContentMetadata > {
262+ let sysroot = & rootcxt . sysroot ;
263+ let mut state = SavedState :: load_from_disk ( & rootcxt . path ) ?. unwrap_or_default ( ) ;
263264 let component = component:: new_from_name ( name) ?;
264265 if state. installed . contains_key ( name) {
265266 anyhow:: bail!( "Component {} is already installed" , name) ;
266267 } ;
267268
268269 ensure_writable_boot ( ) ?;
269270
270- let Some ( update) = component. query_update ( & sysroot) ? else {
271+ let Some ( update) = component. query_update ( sysroot) ? else {
271272 anyhow:: bail!( "Component {} has no available update" , name) ;
272273 } ;
274+ let sysroot = sysroot. try_clone ( ) ?;
273275 let mut state_guard =
274276 SavedState :: acquire_write_lock ( sysroot) . context ( "Failed to acquire write lock" ) ?;
275277
@@ -412,7 +414,8 @@ pub(crate) fn print_status(status: &Status) -> Result<()> {
412414pub struct RootContext {
413415 pub sysroot : openat:: Dir ,
414416 pub path : Utf8PathBuf ,
415- pub devices : Vec < String >
417+ #[ allow( dead_code) ]
418+ pub devices : Vec < String > ,
416419}
417420
418421impl RootContext {
@@ -430,15 +433,12 @@ fn prep_before_update() -> Result<RootContext> {
430433 let path = "/" ;
431434 let sysroot = openat:: Dir :: open ( path) . context ( "Opening root dir" ) ?;
432435 let devices = crate :: blockdev:: get_devices ( path) . context ( "get parent devices" ) ?;
433- Ok ( RootContext :: new (
434- sysroot,
435- path,
436- devices
437- ) )
436+ Ok ( RootContext :: new ( sysroot, path, devices) )
438437}
439438
440439pub ( crate ) fn client_run_update ( ) -> Result < ( ) > {
441440 crate :: try_fail_point!( "update" ) ;
441+ let rootcxt = prep_before_update ( ) ?;
442442 let status: Status = status ( ) ?;
443443 if status. components . is_empty ( ) && status. adoptable . is_empty ( ) {
444444 println ! ( "No components installed." ) ;
@@ -450,7 +450,7 @@ pub(crate) fn client_run_update() -> Result<()> {
450450 ComponentUpdatable :: Upgradable => { }
451451 _ => continue ,
452452 } ;
453- match update ( name) ? {
453+ match update ( name, & rootcxt ) ? {
454454 ComponentUpdateResult :: AtLatestVersion => {
455455 // Shouldn't happen unless we raced with another client
456456 eprintln ! (
@@ -478,7 +478,7 @@ pub(crate) fn client_run_update() -> Result<()> {
478478 }
479479 for ( name, adoptable) in status. adoptable . iter ( ) {
480480 if adoptable. confident {
481- let r: ContentMetadata = adopt_and_update ( name) ?;
481+ let r: ContentMetadata = adopt_and_update ( name, & rootcxt ) ?;
482482 println ! ( "Adopted and updated: {}: {}" , name, r. version) ;
483483 updated = true ;
484484 } else {
@@ -492,12 +492,13 @@ pub(crate) fn client_run_update() -> Result<()> {
492492}
493493
494494pub ( crate ) fn client_run_adopt_and_update ( ) -> Result < ( ) > {
495+ let rootcxt = prep_before_update ( ) ?;
495496 let status: Status = status ( ) ?;
496497 if status. adoptable . is_empty ( ) {
497498 println ! ( "No components are adoptable." ) ;
498499 } else {
499500 for ( name, _) in status. adoptable . iter ( ) {
500- let r: ContentMetadata = adopt_and_update ( name) ?;
501+ let r: ContentMetadata = adopt_and_update ( name, & rootcxt ) ?;
501502 println ! ( "Adopted and updated: {}: {}" , name, r. version) ;
502503 }
503504 }
0 commit comments