@@ -97,29 +97,31 @@ impl Efi {
9797 return esp_device;
9898 }
9999
100- pub ( crate ) fn ensure_mounted_esp ( & self , root : & Path ) -> Result < PathBuf > {
101- let mut mountpoint = self . mountpoint . borrow_mut ( ) ;
102- if let Some ( mountpoint ) = mountpoint . as_deref ( ) {
103- return Ok ( mountpoint . to_owned ( ) ) ;
104- }
105- for & mnt in ESP_MOUNTS {
106- let mnt = root . join ( mnt ) ;
107- if ! mnt. exists ( ) {
108- continue ;
109- }
110- let st =
111- rustix :: fs :: statfs ( & mnt ) . with_context ( || format ! ( "statfs failed for {mnt:?}" ) ) ? ;
112- if st . f_type != libc :: MSDOS_SUPER_MAGIC {
113- continue ;
114- }
115- util :: ensure_writable_mount ( & mnt) ? ;
116- log :: debug! ( "Reusing existing {mnt:?}" ) ;
117- return Ok ( mnt ) ;
100+ // Get mounted point for esp
101+ pub ( crate ) fn get_mounted_esp ( & self , root : & Path ) -> Result < Option < PathBuf > > {
102+ // First check all potential mount points without holding the borrow
103+ let found_mount = ESP_MOUNTS . iter ( )
104+ . map ( | & mnt| root . join ( mnt ) )
105+ . find ( |mnt| {
106+ mnt . exists ( ) &&
107+ rustix :: fs :: statfs ( mnt) . map_or ( false , |st| st . f_type == libc :: MSDOS_SUPER_MAGIC ) &&
108+ util :: ensure_writable_mount ( mnt ) . is_ok ( )
109+ } ) ;
110+
111+ // Only borrow mutably if we found a mount point
112+ if let Some ( mnt ) = found_mount {
113+ log :: debug! ( "Reusing existing mount point {mnt:?}" ) ;
114+ * self . mountpoint . borrow_mut ( ) = Some ( mnt . clone ( ) ) ;
115+ Ok ( Some ( mnt) )
116+ } else {
117+ Ok ( None )
118118 }
119+ }
120+
121+ // Mount the passed esp_device, return mount point
122+ pub ( crate ) fn mount_esp_device ( & self , root : & Path , esp_device : & Path ) -> Result < PathBuf > {
123+ let mut mountpoint = None ;
119124
120- let esp_device = self
121- . get_esp_device ( )
122- . ok_or_else ( || anyhow:: anyhow!( "Failed to find ESP device" ) ) ?;
123125 for & mnt in ESP_MOUNTS . iter ( ) {
124126 let mnt = root. join ( mnt) ;
125127 if !mnt. exists ( ) {
@@ -131,10 +133,25 @@ impl Efi {
131133 . run ( )
132134 . with_context ( || format ! ( "Failed to mount {:?}" , esp_device) ) ?;
133135 log:: debug!( "Mounted at {mnt:?}" ) ;
134- * mountpoint = Some ( mnt) ;
136+ mountpoint = Some ( mnt) ;
135137 break ;
136138 }
137- Ok ( mountpoint. as_deref ( ) . unwrap ( ) . to_owned ( ) )
139+ let mnt = mountpoint. ok_or_else ( || anyhow:: anyhow!( "No mount point found" ) ) ?;
140+ * self . mountpoint . borrow_mut ( ) = Some ( mnt. clone ( ) ) ;
141+ Ok ( mnt)
142+ }
143+
144+ // Firstly check if esp is already mounted, then mount the passed esp device
145+ pub ( crate ) fn ensure_mounted_esp ( & self , root : & Path , esp_device : & Path ) -> Result < PathBuf > {
146+ if let Some ( mountpoint) = self . mountpoint . borrow ( ) . as_deref ( ) {
147+ return Ok ( mountpoint. to_owned ( ) ) ;
148+ }
149+ let destdir = if let Some ( destdir) = self . get_mounted_esp ( Path :: new ( root) ) ? {
150+ destdir
151+ } else {
152+ self . mount_esp_device ( root, esp_device) ?
153+ } ;
154+ Ok ( destdir)
138155 }
139156
140157 fn unmount ( & self ) -> Result < ( ) > {
0 commit comments