11use std:: { env, process} ;
2- use test_programs:: preview1:: { assert_errno, config , create_file, open_scratch_directory} ;
2+ use test_programs:: preview1:: { assert_errno, create_file, open_scratch_directory} ;
33
44unsafe fn test_path_rename ( dir_fd : wasi:: Fd ) {
55 // First, try renaming a dir to nonexistent path
@@ -27,41 +27,28 @@ unsafe fn test_path_rename(dir_fd: wasi::Fd) {
2727 wasi:: fd_close ( fd) . expect ( "closing a file" ) ;
2828 wasi:: path_remove_directory ( dir_fd, "target" ) . expect ( "removing a directory" ) ;
2929
30- // Yes, renaming a dir to an empty dir is a property guaranteed by rename(2)
31- // and its fairly important that it is atomic.
32- // But, we haven't found a way to emulate it on windows. So, sometimes this
33- // behavior is just hosed. Sorry.
34- if config ( ) . support_rename_dir_to_empty_dir ( ) {
35- // Now, try renaming renaming a dir to existing empty dir
36- wasi:: path_create_directory ( dir_fd, "source" ) . expect ( "creating a directory" ) ;
37- wasi:: path_create_directory ( dir_fd, "target" ) . expect ( "creating a directory" ) ;
38- wasi:: path_rename ( dir_fd, "source" , dir_fd, "target" ) . expect ( "renaming a directory" ) ;
39-
40- // Check that source directory doesn't exist anymore
41- assert_errno ! (
42- wasi:: path_open( dir_fd, 0 , "source" , wasi:: OFLAGS_DIRECTORY , 0 , 0 , 0 )
43- . expect_err( "opening a nonexistent path as a directory" ) ,
44- wasi:: ERRNO_NOENT
45- ) ;
46-
47- // Check that target directory exists
48- fd = wasi:: path_open ( dir_fd, 0 , "target" , wasi:: OFLAGS_DIRECTORY , 0 , 0 , 0 )
49- . expect ( "opening renamed path as a directory" ) ;
50- assert ! (
51- fd > libc:: STDERR_FILENO as wasi:: Fd ,
52- "file descriptor range check" ,
53- ) ;
54-
55- wasi:: fd_close ( fd) . expect ( "closing a file" ) ;
56- wasi:: path_remove_directory ( dir_fd, "target" ) . expect ( "removing a directory" ) ;
57- } else {
58- wasi:: path_create_directory ( dir_fd, "source" ) . expect ( "creating a directory" ) ;
59- wasi:: path_create_directory ( dir_fd, "target" ) . expect ( "creating a directory" ) ;
60- wasi:: path_rename ( dir_fd, "source" , dir_fd, "target" )
61- . expect_err ( "windows does not support renaming a directory to an empty directory" ) ;
62- wasi:: path_remove_directory ( dir_fd, "target" ) . expect ( "removing a directory" ) ;
63- wasi:: path_remove_directory ( dir_fd, "source" ) . expect ( "removing a directory" ) ;
64- }
30+ // Now, try renaming renaming a dir to existing empty dir
31+ wasi:: path_create_directory ( dir_fd, "source" ) . expect ( "creating a directory" ) ;
32+ wasi:: path_create_directory ( dir_fd, "target" ) . expect ( "creating a directory" ) ;
33+ wasi:: path_rename ( dir_fd, "source" , dir_fd, "target" ) . expect ( "renaming a directory" ) ;
34+
35+ // Check that source directory doesn't exist anymore
36+ assert_errno ! (
37+ wasi:: path_open( dir_fd, 0 , "source" , wasi:: OFLAGS_DIRECTORY , 0 , 0 , 0 )
38+ . expect_err( "opening a nonexistent path as a directory" ) ,
39+ wasi:: ERRNO_NOENT
40+ ) ;
41+
42+ // Check that target directory exists
43+ fd = wasi:: path_open ( dir_fd, 0 , "target" , wasi:: OFLAGS_DIRECTORY , 0 , 0 , 0 )
44+ . expect ( "opening renamed path as a directory" ) ;
45+ assert ! (
46+ fd > libc:: STDERR_FILENO as wasi:: Fd ,
47+ "file descriptor range check" ,
48+ ) ;
49+
50+ wasi:: fd_close ( fd) . expect ( "closing a file" ) ;
51+ wasi:: path_remove_directory ( dir_fd, "target" ) . expect ( "removing a directory" ) ;
6552
6653 // Now, try renaming a dir to existing non-empty dir
6754 wasi:: path_create_directory ( dir_fd, "source" ) . expect ( "creating a directory" ) ;
@@ -71,29 +58,17 @@ unsafe fn test_path_rename(dir_fd: wasi::Fd) {
7158 assert_errno ! (
7259 wasi:: path_rename( dir_fd, "source" , dir_fd, "target" )
7360 . expect_err( "renaming directory to a nonempty directory" ) ,
74- windows => wasi:: ERRNO_ACCES ,
75- unix => wasi:: ERRNO_NOTEMPTY
61+ wasi:: ERRNO_NOTEMPTY
7662 ) ;
7763
78- // This is technically a different property, but the root of these divergent behaviors is in
79- // the semantics that windows gives us around renaming directories. So, it lives under the same
80- // flag.
81- if config ( ) . support_rename_dir_to_empty_dir ( ) {
82- // Try renaming dir to a file
83- assert_errno ! (
84- wasi:: path_rename( dir_fd, "source" , dir_fd, "target/file" )
85- . expect_err( "renaming a directory to a file" ) ,
86- wasi:: ERRNO_NOTDIR
87- ) ;
88- wasi:: path_unlink_file ( dir_fd, "target/file" ) . expect ( "removing a file" ) ;
89- wasi:: path_remove_directory ( dir_fd, "source" ) . expect ( "removing a directory" ) ;
90- } else {
91- // Windows will let you erase a file by renaming a directory to it.
92- // WASI users can't depend on this error getting caught to prevent data loss.
64+ // Try renaming dir to a file
65+ assert_errno ! (
9366 wasi:: path_rename( dir_fd, "source" , dir_fd, "target/file" )
94- . expect ( "windows happens to support renaming a directory to a file" ) ;
95- wasi:: path_remove_directory ( dir_fd, "target/file" ) . expect ( "removing a file" ) ;
96- }
67+ . expect_err( "renaming a directory to a file" ) ,
68+ wasi:: ERRNO_NOTDIR
69+ ) ;
70+ wasi:: path_unlink_file ( dir_fd, "target/file" ) . expect ( "removing a file" ) ;
71+ wasi:: path_remove_directory ( dir_fd, "source" ) . expect ( "removing a directory" ) ;
9772 wasi:: path_remove_directory ( dir_fd, "target" ) . expect ( "removing a directory" ) ;
9873
9974 // Now, try renaming a file to a nonexistent path
0 commit comments