| 117 | |
| 118 | #[cfg(feature = "alloc")] |
| 119 | fn _readlink(path: &CStr, mut buffer: Vec<u8>) -> io::Result<CString> { |
| 120 | // This code would benefit from having a better way to read into |
| 121 | // uninitialized memory, but that requires `unsafe`. |
| 122 | buffer.clear(); |
| 123 | buffer.reserve(SMALL_PATH_BUFFER_SIZE); |
| 124 | buffer.resize(buffer.capacity(), 0_u8); |
| 125 | |
| 126 | loop { |
| 127 | let nread = backend::fs::syscalls::readlink(path, &mut buffer)?; |
| 128 | |
| 129 | let nread = nread as usize; |
| 130 | assert!(nread <= buffer.len()); |
| 131 | if nread < buffer.len() { |
| 132 | buffer.resize(nread, 0_u8); |
| 133 | return Ok(CString::new(buffer).unwrap()); |
| 134 | } |
| 135 | // Use `Vec` reallocation strategy to grow capacity exponentially. |
| 136 | buffer.reserve(1); |
| 137 | buffer.resize(buffer.capacity(), 0_u8); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /// `rename(old_path, new_path)`—Renames a file or directory. |
| 142 | /// |