| 72 | ))] |
| 73 | #[test] |
| 74 | fn test_dir_seek() { |
| 75 | use std::io::Write as _; |
| 76 | |
| 77 | let tempdir = tempfile::tempdir().unwrap(); |
| 78 | |
| 79 | // Create many files so that we exhaust the readdir buffer at least once. |
| 80 | let count = 500; |
| 81 | let prefix = "file_with_a_very_long_name_to_make_sure_that_we_fill_up_the_buffer"; |
| 82 | let test_string = "This is a test string."; |
| 83 | let mut filenames = Vec::<String>::with_capacity(count); |
| 84 | for i in 0..count { |
| 85 | let filename = format!("{}-{}.txt", prefix, i); |
| 86 | let mut file = std::fs::File::create(tempdir.path().join(&filename)).unwrap(); |
| 87 | filenames.push(filename); |
| 88 | file.write_all(test_string.as_bytes()).unwrap(); |
| 89 | } |
| 90 | |
| 91 | let t = rustix::fs::open( |
| 92 | tempdir.path(), |
| 93 | rustix::fs::OFlags::RDONLY | rustix::fs::OFlags::CLOEXEC, |
| 94 | rustix::fs::Mode::empty(), |
| 95 | ) |
| 96 | .unwrap(); |
| 97 | |
| 98 | let mut dir = rustix::fs::Dir::read_from(&t).unwrap(); |
| 99 | |
| 100 | // Read the first half of directory entries and record offset |
| 101 | for _ in 0..count / 2 { |
| 102 | dir.read().unwrap().unwrap(); |
| 103 | } |
| 104 | let offset: i64 = dir.read().unwrap().unwrap().offset(); |
| 105 | |
| 106 | // Read the rest of the directory entries and record the names |
| 107 | let mut entries = Vec::new(); |
| 108 | while let Some(entry) = dir.read() { |
| 109 | let entry = entry.unwrap(); |
| 110 | entries.push(entry.file_name().to_string_lossy().into_owned()); |
| 111 | } |
| 112 | assert!(entries.len() >= count / 2); |
| 113 | |
| 114 | // Seek to the stored position. On 64-bit platforms we can `seek`. |
| 115 | // On 32-bit platforms, `seek` isn't supported so rewind and scan. |
| 116 | #[cfg(target_pointer_width = "64")] |
| 117 | { |
| 118 | dir.seek(offset).unwrap(); |
| 119 | } |
| 120 | #[cfg(target_pointer_width = "32")] |
| 121 | { |
| 122 | dir.rewind(); |
| 123 | while let Some(entry) = dir.read() { |
| 124 | let entry = entry.unwrap(); |
| 125 | if entry.offset() == offset { |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Confirm that we're getting the same results as before |