Extra calls of fd_close are needed for Windows, which will not remove the directory until all handles are closed.
(dir_fd: wasip1::Fd)
| 54 | // Extra calls of fd_close are needed for Windows, which will not remove |
| 55 | // the directory until all handles are closed. |
| 56 | unsafe fn test_path_link(dir_fd: wasip1::Fd) { |
| 57 | // Create a file |
| 58 | let create_fd = |
| 59 | wasip1::path_open(dir_fd, 0, "file", wasip1::OFLAGS_CREAT, 0, 0, 0).expect("create file"); |
| 60 | wasip1::fd_close(create_fd).unwrap(); |
| 61 | |
| 62 | // Open a fresh descriptor to the file. We won't have a write right that was implied by OFLAGS_CREAT |
| 63 | // above. |
| 64 | let file_fd = wasip1::path_open(dir_fd, 0, "file", 0, 0, 0, 0).expect("open file"); |
| 65 | |
| 66 | // Create a link in the same directory and compare rights |
| 67 | wasip1::path_link(dir_fd, 0, "file", dir_fd, "link") |
| 68 | .expect("creating a link in the same directory"); |
| 69 | |
| 70 | let link_fd = wasip1::path_open(dir_fd, 0, "link", 0, 0, 0, 0).expect("open link"); |
| 71 | |
| 72 | check_rights!(file_fd, link_fd); |
| 73 | wasip1::fd_close(link_fd).expect("Closing link_fd"); // needed for Windows |
| 74 | wasip1::path_unlink_file(dir_fd, "link").expect("removing a link"); |
| 75 | |
| 76 | // Create a link in a different directory and compare rights |
| 77 | wasip1::path_create_directory(dir_fd, "subdir").expect("creating a subdirectory"); |
| 78 | let subdir_fd = wasip1::path_open(dir_fd, 0, "subdir", wasip1::OFLAGS_DIRECTORY, 0, 0, 0) |
| 79 | .expect("open subdir directory"); |
| 80 | wasip1::path_link(dir_fd, 0, "file", subdir_fd, "link") |
| 81 | .expect("creating a link in subdirectory"); |
| 82 | let link_fd = wasip1::path_open(subdir_fd, 0, "link", 0, 0, 0, 0).expect("open link in subdir"); |
| 83 | check_rights!(file_fd, link_fd); |
| 84 | wasip1::fd_close(link_fd).expect("Closing link_fd"); // needed for Windows |
| 85 | wasip1::path_unlink_file(subdir_fd, "link").expect("removing a link"); |
| 86 | wasip1::fd_close(subdir_fd).expect("Closing subdir_fd"); // needed for Windows |
| 87 | wasip1::path_remove_directory(dir_fd, "subdir").expect("removing a subdirectory"); |
| 88 | |
| 89 | // Create a link to a path that already exists |
| 90 | create_file(dir_fd, "link"); |
| 91 | |
| 92 | assert_errno!( |
| 93 | wasip1::path_link(dir_fd, 0, "file", dir_fd, "link") |
| 94 | .expect_err("creating a link to existing path should fail"), |
| 95 | wasip1::ERRNO_EXIST |
| 96 | ); |
| 97 | wasip1::path_unlink_file(dir_fd, "link").expect("removing a file"); |
| 98 | |
| 99 | // Create a link to itself |
| 100 | assert_errno!( |
| 101 | wasip1::path_link(dir_fd, 0, "file", dir_fd, "file") |
| 102 | .expect_err("creating a link to itself should fail"), |
| 103 | wasip1::ERRNO_EXIST |
| 104 | ); |
| 105 | |
| 106 | // Create a link where target is a directory |
| 107 | wasip1::path_create_directory(dir_fd, "link").expect("creating a dir"); |
| 108 | |
| 109 | assert_errno!( |
| 110 | wasip1::path_link(dir_fd, 0, "file", dir_fd, "link") |
| 111 | .expect_err("creating a link where target is a directory should fail"), |
| 112 | wasip1::ERRNO_EXIST |
| 113 | ); |
no test coverage detected