@@ -146,7 +146,22 @@ impl Mountable for MountSpec {
146146 let target_p = target. as_ptr ( ) ;
147147
148148 if self . create_mountpoint {
149- fs:: create_dir_all ( & self . target ) ?;
149+ let source_is_file = self . bind
150+ && self
151+ . source
152+ . as_deref ( )
153+ . and_then ( |s| std:: path:: Path :: new ( s) . metadata ( ) . ok ( ) )
154+ . map ( |m| !m. is_dir ( ) )
155+ . unwrap_or ( false ) ;
156+
157+ if source_is_file {
158+ if let Some ( parent) = std:: path:: Path :: new ( & self . target ) . parent ( ) {
159+ fs:: create_dir_all ( parent) ?;
160+ }
161+ fs:: File :: create ( & self . target ) ?;
162+ } else {
163+ fs:: create_dir_all ( & self . target ) ?;
164+ }
150165 }
151166
152167 let mut flags: c_ulong = libc:: MS_SILENT ;
@@ -163,10 +178,28 @@ impl Mountable for MountSpec {
163178 flags |= libc:: MS_REC ;
164179 }
165180
181+ let data_cstr = self
182+ . data
183+ . as_ref ( )
184+ . map ( |d| CString :: new ( d. as_str ( ) ) . unwrap ( ) ) ;
185+ let data_ptr = data_cstr
186+ . as_ref ( )
187+ . map ( |c| c. as_ptr ( ) as * const libc:: c_void )
188+ . unwrap_or ( ptr:: null ( ) ) ;
189+
166190 unsafe {
167- let rc = libc:: mount ( source_p, target_p, fstype_p, flags, ptr :: null ( ) ) ;
191+ let rc = libc:: mount ( source_p, target_p, fstype_p, flags, data_ptr ) ;
168192 if rc < 0 {
169- bail ! ( "unable to mount" ) ;
193+ let err = io:: Error :: last_os_error ( ) ;
194+ bail ! (
195+ "unable to mount: source={:?} target={:?} fstype={:?} bind={} flags=0x{:x}: {}" ,
196+ self . source,
197+ self . target,
198+ self . fstype,
199+ self . bind,
200+ flags,
201+ err
202+ ) ;
170203 }
171204 }
172205
0 commit comments