@@ -1604,17 +1604,15 @@ mod remove_dir_impl {
1604
1604
}
1605
1605
}
1606
1606
1607
- fn remove_dir_all_recursive ( parent_fd : Option < RawFd > , p : & Path ) -> io:: Result < ( ) > {
1608
- let pcstr = cstr ( p) ?;
1609
-
1607
+ fn remove_dir_all_recursive ( parent_fd : Option < RawFd > , path : & CStr ) -> io:: Result < ( ) > {
1610
1608
// try opening as directory
1611
- let fd = match openat_nofollow_dironly ( parent_fd, & pcstr ) {
1609
+ let fd = match openat_nofollow_dironly ( parent_fd, & path ) {
1612
1610
Err ( err) if err. raw_os_error ( ) == Some ( libc:: ENOTDIR ) => {
1613
1611
// not a directory - don't traverse further
1614
1612
return match parent_fd {
1615
1613
// unlink...
1616
1614
Some ( parent_fd) => {
1617
- cvt ( unsafe { unlinkat ( parent_fd, pcstr . as_ptr ( ) , 0 ) } ) . map ( drop)
1615
+ cvt ( unsafe { unlinkat ( parent_fd, path . as_ptr ( ) , 0 ) } ) . map ( drop)
1618
1616
}
1619
1617
// ...unless this was supposed to be the deletion root directory
1620
1618
None => Err ( err) ,
@@ -1627,26 +1625,27 @@ mod remove_dir_impl {
1627
1625
let ( dir, fd) = fdreaddir ( fd) ?;
1628
1626
for child in dir {
1629
1627
let child = child?;
1628
+ let child_name = child. name_cstr ( ) ;
1630
1629
match is_dir ( & child) {
1631
1630
Some ( true ) => {
1632
- remove_dir_all_recursive ( Some ( fd) , Path :: new ( & child . file_name ( ) ) ) ?;
1631
+ remove_dir_all_recursive ( Some ( fd) , child_name ) ?;
1633
1632
}
1634
1633
Some ( false ) => {
1635
- cvt ( unsafe { unlinkat ( fd, child . name_cstr ( ) . as_ptr ( ) , 0 ) } ) ?;
1634
+ cvt ( unsafe { unlinkat ( fd, child_name . as_ptr ( ) , 0 ) } ) ?;
1636
1635
}
1637
1636
None => {
1638
1637
// POSIX specifies that calling unlink()/unlinkat(..., 0) on a directory can succeed
1639
1638
// if the process has the appropriate privileges. This however can causing orphaned
1640
1639
// directories requiring an fsck e.g. on Solaris and Illumos. So we try recursing
1641
1640
// into it first instead of trying to unlink() it.
1642
- remove_dir_all_recursive ( Some ( fd) , Path :: new ( & child . file_name ( ) ) ) ?;
1641
+ remove_dir_all_recursive ( Some ( fd) , child_name ) ?;
1643
1642
}
1644
1643
}
1645
1644
}
1646
1645
1647
1646
// unlink the directory after removing its contents
1648
1647
cvt ( unsafe {
1649
- unlinkat ( parent_fd. unwrap_or ( libc:: AT_FDCWD ) , pcstr . as_ptr ( ) , libc:: AT_REMOVEDIR )
1648
+ unlinkat ( parent_fd. unwrap_or ( libc:: AT_FDCWD ) , path . as_ptr ( ) , libc:: AT_REMOVEDIR )
1650
1649
} ) ?;
1651
1650
Ok ( ( ) )
1652
1651
}
@@ -1659,7 +1658,7 @@ mod remove_dir_impl {
1659
1658
if attr. file_type ( ) . is_symlink ( ) {
1660
1659
crate :: fs:: remove_file ( p)
1661
1660
} else {
1662
- remove_dir_all_recursive ( None , p )
1661
+ remove_dir_all_recursive ( None , & cstr ( p ) ? )
1663
1662
}
1664
1663
}
1665
1664
0 commit comments