
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1203 · main · 9 min 56 s
1Étoiles
0Forks
0Issues
| 1 | //! Filesystem operations for `std/file`: blocking externs, run on the |
| 2 | //! blocking pool while their actor is parked (see `blocking.rs`). |
| 3 | //! |
| 4 | //! Every failure is an `Error` value with the OS's message: a blocking |
| 5 | //! extern must not panic. |
| 6 | //! |
| 7 | //! Open files (`std/file/stream`, ADR 0087) are pinned resources over the |
| 8 | //! kernel shared with the interpreter (`louise_rt_prims::file`): a file |
| 9 | //! stays with the actor that opened it, and is closed by `close` or when |
| 10 | //! its resource is dropped (the actor ended), whichever comes first. |
| 11 | |
| 12 | use louise_rt_prims::file::{Handle, Mode, sync_dir}; |
| 13 | |
| 14 | use crate::abi::{Ctx, UNIT, Value, int}; |
| 15 | use crate::int::saturating; |
| 16 | use crate::prims::boolean; |
| 17 | use crate::resource::{tl_resource_get, tl_resource_new}; |
| 18 | use crate::values::{bytes_from, bytes_of, error, list, ok, string, text}; |
| 19 | |
| 20 | fn create_dir(path: &str, recursive: bool) -> std::io::Result<()> { |
| 21 | let mut builder = std::fs::DirBuilder::new(); |
| 22 | builder.recursive(recursive); |
| 23 | #[cfg(unix)] |
| 24 | { |
| 25 | use std::os::unix::fs::DirBuilderExt; |
| 26 | builder.mode(0o700); |
| 27 | } |
| 28 | builder.create(path) |
| 29 | } |
| 30 | |
| 31 | fn remove_path(path: &str, recursive: bool) -> std::io::Result<()> { |
| 32 | let file_type = std::fs::symlink_metadata(path)?.file_type(); |
| 33 | if file_type.is_dir() { |
| 34 | if recursive { |
| 35 | std::fs::remove_dir_all(path) |
| 36 | } else { |
| 37 | std::fs::remove_dir(path) |
| 38 | } |
| 39 | } else { |
| 40 | std::fs::remove_file(path) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | fn unit_result(ctx: *mut Ctx, result: std::io::Result<()>) -> Value { |
| 45 | match result { |
| 46 | Ok(()) => ok(ctx, UNIT), |
| 47 | Err(e) => error(ctx, &e.to_string()), |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /// `read_bytes(path)`: the file's contents, or `Error` with the OS's |
| 52 | /// message. |
| 53 | /// |
| 54 | /// # Safety |
| 55 | /// `path` must be a live string; `ctx` the calling actor's context. The |
| 56 | /// result is built with the allocation functions of `louise_rt.h`, which |
| 57 | /// work on any thread, as a blocking extern requires. |
| 58 | #[unsafe(no_mangle)] |
| 59 | pub unsafe extern "C" fn tl_file_read(ctx: *mut Ctx, path: Value) -> Value { |
| 60 | // SAFETY: caller guarantees a live string. |
| 61 | match std::fs::read(unsafe { text(path) }) { |
| 62 | Ok(bytes) => { |
| 63 | let b = bytes_from(ctx, &bytes); |
| 64 | ok(ctx, b) |
| 65 | } |
| 66 | Err(e) => error(ctx, &e.to_string()), |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /// `write(path, content)`: the file created or truncated, or `Error` with |
| 71 | /// the OS's message. |
| 72 | /// |
| 73 | /// # Safety |
| 74 | /// `path` must be a live string and `content` live `Bytes`; `ctx` the |
| 75 | /// calling actor's context. |
| 76 | #[unsafe(no_mangle)] |
| 77 | pub unsafe extern "C" fn tl_file_write(ctx: *mut Ctx, path: Value, content: Value) -> Value { |
| 78 | // SAFETY: caller guarantees a live string and live Bytes. |
| 79 | let (path, content) = unsafe { (text(path), bytes_of(content)) }; |
| 80 | match std::fs::write(path, content) { |
| 81 | Ok(()) => ok(ctx, UNIT), |
| 82 | Err(e) => error(ctx, &e.to_string()), |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// Whether a file (not a directory) exists at `path`. |
| 87 | /// |
| 88 | /// # Safety |
| 89 | /// `path` must be a live string. |
| 90 | #[unsafe(no_mangle)] |
| 91 | pub unsafe extern "C" fn tl_file_exists(_ctx: *mut Ctx, path: Value) -> Value { |
| 92 | // SAFETY: caller guarantees a live string. |
| 93 | boolean(std::path::Path::is_file(std::path::Path::new(unsafe { |
| 94 | text(path) |
| 95 | }))) |
| 96 | } |
| 97 | |
| 98 | /// The file's size in bytes, or `Error` with the OS's message. |
| 99 | /// |
| 100 | /// # Safety |
| 101 | /// `path` must be a live string; `ctx` the calling actor's context. |
| 102 | #[unsafe(no_mangle)] |
| 103 | pub unsafe extern "C" fn tl_file_size(ctx: *mut Ctx, path: Value) -> Value { |
| 104 | // SAFETY: caller guarantees a live string. |
| 105 | match std::fs::metadata(unsafe { text(path) }) { |
| 106 | Ok(meta) => ok(ctx, int(i64::try_from(meta.len()).unwrap_or(i64::MAX))), |
| 107 | Err(e) => error(ctx, &e.to_string()), |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// The names of the directory's entries (not full paths, never `.` or |
| 112 | /// `..`, in the OS's order), or `Error` with the OS's message. A name |
| 113 | /// that is not valid UTF-8 is lossy-decoded. |
| 114 | /// |
| 115 | /// # Safety |
| 116 | /// `path` must be a live string; `ctx` the calling actor's context. |
| 117 | #[unsafe(no_mangle)] |
| 118 | pub unsafe extern "C" fn tl_file_list_dir(ctx: *mut Ctx, path: Value) -> Value { |
| 119 | // SAFETY: caller guarantees a live string. |
| 120 | let entries = match std::fs::read_dir(unsafe { text(path) }) { |
| 121 | Ok(entries) => entries, |
| 122 | Err(e) => return error(ctx, &e.to_string()), |
| 123 | }; |
| 124 | let mut names = Vec::new(); |
| 125 | for entry in entries { |
| 126 | match entry { |
| 127 | Ok(e) => names.push(string(ctx, &e.file_name().to_string_lossy())), |
| 128 | Err(e) => return error(ctx, &e.to_string()), |
| 129 | } |
| 130 | } |
| 131 | ok(ctx, list(ctx, names.into_iter())) |
| 132 | } |
| 133 | |
| 134 | /// `make_dir(path)`: a private directory, without creating its parents. |
| 135 | /// |
| 136 | /// # Safety |
| 137 | /// `path` must be a live string; `ctx` the calling actor's context. |
| 138 | #[unsafe(no_mangle)] |
| 139 | pub unsafe extern "C" fn tl_file_make_dir(ctx: *mut Ctx, path: Value) -> Value { |
| 140 | // SAFETY: caller guarantees a live string. |
| 141 | unit_result(ctx, create_dir(unsafe { text(path) }, false)) |
| 142 | } |
| 143 | |
| 144 | /// `make_dirs(path)`: a private directory and any missing parents. |
| 145 | /// |
| 146 | /// # Safety |
| 147 | /// `path` must be a live string; `ctx` the calling actor's context. |
| 148 | #[unsafe(no_mangle)] |
| 149 | pub unsafe extern "C" fn tl_file_make_dirs(ctx: *mut Ctx, path: Value) -> Value { |
| 150 | // SAFETY: caller guarantees a live string. |
| 151 | unit_result(ctx, create_dir(unsafe { text(path) }, true)) |
| 152 | } |
| 153 | |
| 154 | /// `remove(path)`: a file, symbolic link, or empty directory removed. |
| 155 | /// |
| 156 | /// # Safety |
| 157 | /// `path` must be a live string; `ctx` the calling actor's context. |
| 158 | #[unsafe(no_mangle)] |
| 159 | pub unsafe extern "C" fn tl_file_remove(ctx: *mut Ctx, path: Value) -> Value { |
| 160 | // SAFETY: caller guarantees a live string. |
| 161 | unit_result(ctx, remove_path(unsafe { text(path) }, false)) |
| 162 | } |
| 163 | |
| 164 | /// `remove_all(path)`: a file, symbolic link, or directory tree removed, |
| 165 | /// without following symbolic links. |
| 166 | /// |
| 167 | /// # Safety |
| 168 | /// `path` must be a live string; `ctx` the calling actor's context. |
| 169 | #[unsafe(no_mangle)] |
| 170 | pub unsafe extern "C" fn tl_file_remove_all(ctx: *mut Ctx, path: Value) -> Value { |
| 171 | // SAFETY: caller guarantees a live string. |
| 172 | unit_result(ctx, remove_path(unsafe { text(path) }, true)) |
| 173 | } |
| 174 | |
| 175 | /// `rename(from, to)`: `from` atomically renamed over `to` within one |
| 176 | /// filesystem. |
| 177 | /// |
| 178 | /// # Safety |
| 179 | /// `from` and `to` must be live strings; `ctx` the calling actor's context. |
| 180 | #[unsafe(no_mangle)] |
| 181 | pub unsafe extern "C" fn tl_file_rename(ctx: *mut Ctx, from: Value, to: Value) -> Value { |
| 182 | // SAFETY: caller guarantees live strings. |
| 183 | let (from, to) = unsafe { (text(from), text(to)) }; |
| 184 | unit_result(ctx, std::fs::rename(from, to)) |
| 185 | } |
| 186 | |
| 187 | /// The kernel of an open file (`louise_rt_prims::file`), owned by its |
| 188 | /// resource: dropping the resource closes the file if `close` did not. |
| 189 | unsafe extern "C" fn finalize_handle(p: *mut std::ffi::c_void) { |
| 190 | // SAFETY: made by `opened`, finalized once. Finalizers never run under |
| 191 | // the scheduler lock; closing a file does not touch Louise values. |
| 192 | unsafe { drop(Box::from_raw(p.cast::<Handle>())) } |
| 193 | } |
| 194 | |
| 195 | /// `Ok(file)`, a pinned resource (it stays with the actor that opened |
| 196 | /// it), or `Error` with the message. |
| 197 | fn opened(ctx: *mut Ctx, handle: Result<Handle, String>) -> Value { |
| 198 | match handle { |
| 199 | Ok(h) => { |
| 200 | let h = Box::into_raw(Box::new(h)); |
| 201 | let r = tl_resource_new(ctx, h.cast(), Some(finalize_handle), false); |
| 202 | ok(ctx, r) |
| 203 | } |
| 204 | Err(e) => error(ctx, &e), |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /// # Safety |
| 209 | /// `r` must be a live resource made by [`opened`]. |
| 210 | unsafe fn handle<'a>(r: Value) -> &'a Handle { |
| 211 | // SAFETY: caller guarantees a file resource. |
| 212 | unsafe { &*tl_resource_get(r).cast::<Handle>() } |
| 213 | } |
| 214 | |
| 215 | fn bytes_result(ctx: *mut Ctx, result: Result<Vec<u8>, String>) -> Value { |
| 216 | match result { |
| 217 | Ok(data) => { |
| 218 | let b = bytes_from(ctx, &data); |
| 219 | ok(ctx, b) |
| 220 | } |
| 221 | Err(e) => error(ctx, &e), |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | fn done(ctx: *mut Ctx, result: Result<(), String>) -> Value { |
| 226 | match result { |
| 227 | Ok(()) => ok(ctx, UNIT), |
| 228 | Err(e) => error(ctx, &e), |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /// `open_read(path)`: the file open for reading (ADR 0087). |
| 233 | /// |
| 234 | /// # Safety |
| 235 | /// `path` must be a live string; `ctx` the calling actor's context. |
| 236 | #[unsafe(no_mangle)] |
| 237 | pub unsafe extern "C" fn tl_file_open_read(ctx: *mut Ctx, path: Value) -> Value { |
| 238 | // SAFETY: caller guarantees a live string. |
| 239 | opened(ctx, Handle::open_read(unsafe { text(path) })) |
| 240 | } |
| 241 | |
| 242 | /// `open_write(path, mode)`: the file open for writing, `mode` being the |
| 243 | /// index of `Mode`'s constructor. |
| 244 | /// |
| 245 | /// # Safety |
| 246 | /// `path` must be a live string and `mode` a live Int; `ctx` the calling |
| 247 | /// actor's context. |
| 248 | #[unsafe(no_mangle)] |
| 249 | pub unsafe extern "C" fn tl_file_open_write(ctx: *mut Ctx, path: Value, mode: Value) -> Value { |
| 250 | // SAFETY: caller guarantees a live string and a live Int. |
| 251 | let (path, mode) = unsafe { (text(path), saturating(mode)) }; |
| 252 | match Mode::from_index(mode) { |
| 253 | Some(mode) => opened(ctx, Handle::open_write(path, mode)), |
| 254 | None => error(ctx, &format!("no such mode: {mode}")), |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /// `read(file, max)`: up to `max` bytes from the position. |
| 259 | /// |
| 260 | /// # Safety |
| 261 | /// `file` must be a live file resource and `max` a live Int; `ctx` the |
| 262 | /// calling actor's context. |
| 263 | #[unsafe(no_mangle)] |
| 264 | pub unsafe extern "C" fn tl_file_handle_read(ctx: *mut Ctx, file: Value, max: Value) -> Value { |
| 265 | // SAFETY: caller guarantees a live file and a live Int. |
| 266 | let read = unsafe { handle(file).read(saturating(max)) }; |
| 267 | bytes_result(ctx, read) |
| 268 | } |
| 269 | |
| 270 | /// `read_at(file, offset, max)`: up to `max` bytes from `offset`. |
| 271 | /// |
| 272 | /// # Safety |
| 273 | /// `file` must be a live file resource, `offset` and `max` live Ints; |
| 274 | /// `ctx` the calling actor's context. |
| 275 | #[unsafe(no_mangle)] |
| 276 | pub unsafe extern "C" fn tl_file_handle_read_at( |
| 277 | ctx: *mut Ctx, |
| 278 | file: Value, |
| 279 | offset: Value, |
| 280 | max: Value, |
| 281 | ) -> Value { |
| 282 | // SAFETY: caller guarantees a live file and live Ints. |
| 283 | let read = unsafe { handle(file).read_at(saturating(offset), saturating(max)) }; |
| 284 | bytes_result(ctx, read) |
| 285 | } |
| 286 | |
| 287 | /// `write(file, data)`: all of `data`. |
| 288 | /// |
| 289 | /// # Safety |
| 290 | /// `file` must be a live file resource and `data` live `Bytes`; `ctx` the |
| 291 | /// calling actor's context. |
| 292 | #[unsafe(no_mangle)] |
| 293 | pub unsafe extern "C" fn tl_file_handle_write(ctx: *mut Ctx, file: Value, data: Value) -> Value { |
| 294 | // SAFETY: caller guarantees a live file and live Bytes. |
| 295 | let wrote = unsafe { handle(file).write(bytes_of(data)) }; |
| 296 | done(ctx, wrote) |
| 297 | } |
| 298 | |
| 299 | /// `seek(file, offset)`: the position moved to `offset`. |
| 300 | /// |
| 301 | /// # Safety |
| 302 | /// `file` must be a live file resource and `offset` a live Int; `ctx` the |
| 303 | /// calling actor's context. |
| 304 | #[unsafe(no_mangle)] |
| 305 | pub unsafe extern "C" fn tl_file_handle_seek(ctx: *mut Ctx, file: Value, offset: Value) -> Value { |
| 306 | // SAFETY: caller guarantees a live file and a live Int. |
| 307 | let moved = unsafe { handle(file).seek(saturating(offset)) }; |
| 308 | done(ctx, moved) |
| 309 | } |
| 310 | |
| 311 | /// `size(file)`: the open file's size in bytes. |
| 312 | /// |
| 313 | /// # Safety |
| 314 | /// `file` must be a live file resource; `ctx` the calling actor's context. |
| 315 | #[unsafe(no_mangle)] |
| 316 | pub unsafe extern "C" fn tl_file_handle_size(ctx: *mut Ctx, file: Value) -> Value { |
| 317 | // SAFETY: caller guarantees a live file. |
| 318 | match unsafe { handle(file).size() } { |
| 319 | Ok(n) => ok(ctx, int(n)), |
| 320 | Err(e) => error(ctx, &e), |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /// `sync(file)`: the file flushed to the device. |
| 325 | /// |
| 326 | /// # Safety |
| 327 | /// `file` must be a live file resource; `ctx` the calling actor's context. |
| 328 | #[unsafe(no_mangle)] |
| 329 | pub unsafe extern "C" fn tl_file_handle_sync(ctx: *mut Ctx, file: Value) -> Value { |
| 330 | // SAFETY: caller guarantees a live file. |
| 331 | let synced = unsafe { handle(file).sync() }; |
| 332 | done(ctx, synced) |
| 333 | } |
| 334 | |
| 335 | /// `close(file)`: the file closed; later calls fail. |
| 336 | /// |
| 337 | /// # Safety |
| 338 | /// `file` must be a live file resource. |
| 339 | #[unsafe(no_mangle)] |
| 340 | pub unsafe extern "C" fn tl_file_handle_close(_ctx: *mut Ctx, file: Value) -> Value { |
| 341 | // SAFETY: caller guarantees a live file. |
| 342 | unsafe { handle(file).close() }; |
| 343 | UNIT |
| 344 | } |
| 345 | |
| 346 | /// `sync_dir(path)`: the directory flushed to the device. |
| 347 | /// |
| 348 | /// # Safety |
| 349 | /// `path` must be a live string; `ctx` the calling actor's context. |
| 350 | #[unsafe(no_mangle)] |
| 351 | pub unsafe extern "C" fn tl_file_sync_dir(ctx: *mut Ctx, path: Value) -> Value { |
| 352 | // SAFETY: caller guarantees a live string. |
| 353 | done(ctx, sync_dir(unsafe { text(path) })) |
| 354 | } |
| 355 | |
| 356 | #[cfg(test)] |
| 357 | mod tests { |
| 358 | use super::*; |
| 359 | use crate::abi::{Block, int_get}; |
| 360 | use crate::heap::drop_value; |
| 361 | use crate::values::{list_items, string, string_bytes, text}; |
| 362 | |
| 363 | /// (tag, first field) of a constructor value (`Ok` is tag 0). |
| 364 | unsafe fn ctor(v: Value) -> (u16, Value) { |
| 365 | // SAFETY: a live constructor cell. |
| 366 | unsafe { |
| 367 | let b = v as *mut Block; |
| 368 | ((*b).header.tag, Block::fields_ptr(b).read()) |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | extern "C" fn roundtrip(ctx: *mut Ctx) -> Value { |
| 373 | // SAFETY: the test follows the generated-code convention: fresh |
| 374 | // values are dropped once, arguments of the externs are borrowed. |
| 375 | unsafe { |
| 376 | let path = std::env::temp_dir().join("louise_rt_fs_test.bin"); |
| 377 | let path = path.to_str().unwrap(); |
| 378 | let p = string(ctx, path); |
| 379 | std::fs::remove_file(path).ok(); |
| 380 | assert_eq!(tl_file_exists(ctx, p), int(0)); |
| 381 | |
| 382 | let content = bytes_from(ctx, b"hello"); |
| 383 | let wrote = tl_file_write(ctx, p, content); |
| 384 | assert_eq!(ctor(wrote).0, 0, "Ok expected"); |
| 385 | assert_eq!(tl_file_exists(ctx, p), int(1)); |
| 386 | |
| 387 | let size = tl_file_size(ctx, p); |
| 388 | let (tag, n) = ctor(size); |
| 389 | assert_eq!(tag, 0, "Ok expected"); |
| 390 | assert_eq!(int_get(n), 5); |
| 391 | |
| 392 | let read = tl_file_read(ctx, p); |
| 393 | let (tag, bytes) = ctor(read); |
| 394 | assert_eq!(tag, 0, "Ok expected"); |
| 395 | assert_eq!(string_bytes(bytes), b"hello"); |
| 396 | |
| 397 | // A missing file is an `Error`, not a panic. |
| 398 | std::fs::remove_file(path).unwrap(); |
| 399 | let missing = tl_file_read(ctx, p); |
| 400 | assert_eq!(ctor(missing).0, 1, "Error expected"); |
| 401 | |
| 402 | for v in [wrote, size, read, missing, content, p] { |
| 403 | drop_value(ctx, v); |
| 404 | } |
| 405 | assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 406 | UNIT |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | #[test] |
| 411 | fn write_read_exists_size_and_errors() { |
| 412 | // SAFETY: the test actor follows the generated-code convention. |
| 413 | assert_eq!(unsafe { crate::sched::run_test_actor(roundtrip) }, 0); |
| 414 | } |
| 415 | |
| 416 | extern "C" fn lists_dir(ctx: *mut Ctx) -> Value { |
| 417 | // SAFETY: the test follows the generated-code convention: fresh |
| 418 | // values are dropped once, arguments of the externs are borrowed. |
| 419 | unsafe { |
| 420 | let p = string(ctx, "."); |
| 421 | let listed = tl_file_list_dir(ctx, p); |
| 422 | let (tag, entries) = ctor(listed); |
| 423 | assert_eq!(tag, 0, "Ok expected"); |
| 424 | let names: Vec<&str> = list_items(entries).map(|e| text(e)).collect(); |
| 425 | assert!(names.contains(&"Cargo.toml"), "{names:?}"); |
| 426 | assert!(!names.contains(&"."), "{names:?}"); |
| 427 | |
| 428 | // A missing directory is an `Error`, not a panic. |
| 429 | let np = string(ctx, "/definitely/not/here"); |
| 430 | let missing = tl_file_list_dir(ctx, np); |
| 431 | assert_eq!(ctor(missing).0, 1, "Error expected"); |
| 432 | |
| 433 | for v in [listed, missing, p, np] { |
| 434 | drop_value(ctx, v); |
| 435 | } |
| 436 | assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 437 | UNIT |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | #[test] |
| 442 | fn list_dir_lists_and_errors() { |
| 443 | // SAFETY: the test actor follows the generated-code convention. |
| 444 | assert_eq!(unsafe { crate::sched::run_test_actor(lists_dir) }, 0); |
| 445 | } |
| 446 | |
| 447 | /// Whether a descriptor of this process is open on `path`. |
| 448 | fn open_on(path: &std::path::Path) -> bool { |
| 449 | std::fs::read_dir("/proc/self/fd") |
| 450 | .unwrap() |
| 451 | .filter_map(|e| std::fs::read_link(e.ok()?.path()).ok()) |
| 452 | .any(|target| target == path) |
| 453 | } |
| 454 | |
| 455 | fn handle_path(name: &str) -> std::path::PathBuf { |
| 456 | std::env::temp_dir().join(format!("louise_rt_fs_{name}_{}", std::process::id())) |
| 457 | } |
| 458 | |
| 459 | extern "C" fn streams_through_a_handle(ctx: *mut Ctx) -> Value { |
| 460 | // SAFETY: the test follows the generated-code convention: fresh |
| 461 | // values are dropped once, arguments of the externs are borrowed. |
| 462 | unsafe { |
| 463 | let path = handle_path("handle_roundtrip"); |
| 464 | let p = string(ctx, path.to_str().unwrap()); |
| 465 | let truncate = int(0); |
| 466 | let opened = tl_file_open_write(ctx, p, truncate); |
| 467 | let (tag, w) = ctor(opened); |
| 468 | assert_eq!(tag, 0, "Ok expected"); |
| 469 | let data = bytes_from(ctx, b"hello world"); |
| 470 | let wrote = tl_file_handle_write(ctx, w, data); |
| 471 | assert_eq!(ctor(wrote).0, 0, "Ok expected"); |
| 472 | let synced = tl_file_handle_sync(ctx, w); |
| 473 | assert_eq!(ctor(synced).0, 0, "Ok expected"); |
| 474 | assert_eq!(tl_file_handle_close(ctx, w), UNIT); |
| 475 | assert!(!open_on(&path)); |
| 476 | let after = tl_file_handle_write(ctx, w, data); |
| 477 | let (tag, message) = ctor(after); |
| 478 | assert_eq!(tag, 1, "Error expected"); |
| 479 | assert_eq!(text(message), louise_rt_prims::file::CLOSED); |
| 480 | |
| 481 | let reading = tl_file_open_read(ctx, p); |
| 482 | let (tag, r) = ctor(reading); |
| 483 | assert_eq!(tag, 0, "Ok expected"); |
| 484 | let (offset, max) = (int(6), int(100)); |
| 485 | let range = tl_file_handle_read_at(ctx, r, offset, max); |
| 486 | assert_eq!(string_bytes(ctor(range).1), b"world"); |
| 487 | let size = tl_file_handle_size(ctx, r); |
| 488 | assert_eq!(int_get(ctor(size).1), 11); |
| 489 | let bad_mode = int(7); |
| 490 | let refused = tl_file_open_write(ctx, p, bad_mode); |
| 491 | assert_eq!(ctor(refused).0, 1, "Error expected"); |
| 492 | |
| 493 | for v in [ |
| 494 | opened, data, wrote, synced, after, reading, range, size, refused, p, |
| 495 | ] { |
| 496 | drop_value(ctx, v); |
| 497 | } |
| 498 | std::fs::remove_file(&path).unwrap(); |
| 499 | assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 500 | UNIT |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | #[test] |
| 505 | fn a_handle_writes_reads_and_refuses_use_after_close() { |
| 506 | // SAFETY: the test actor follows the generated-code convention. |
| 507 | let result = unsafe { crate::sched::run_test_actor(streams_through_a_handle) }; |
| 508 | assert_eq!(result, 0); |
| 509 | } |
| 510 | |
| 511 | extern "C" fn drops_an_open_handle(ctx: *mut Ctx) -> Value { |
| 512 | // SAFETY: the test follows the generated-code convention. |
| 513 | unsafe { |
| 514 | let path = handle_path("handle_dropped"); |
| 515 | let p = string(ctx, path.to_str().unwrap()); |
| 516 | let append = int(2); |
| 517 | let opened = tl_file_open_write(ctx, p, append); |
| 518 | assert_eq!(ctor(opened).0, 0, "Ok expected"); |
| 519 | assert!(open_on(&path)); |
| 520 | drop_value(ctx, opened); |
| 521 | assert!(!open_on(&path), "the finalizer closes the file"); |
| 522 | drop_value(ctx, p); |
| 523 | std::fs::remove_file(&path).unwrap(); |
| 524 | assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 525 | UNIT |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | #[test] |
| 530 | fn dropping_an_open_handle_closes_its_file() { |
| 531 | // SAFETY: the test actor follows the generated-code convention. |
| 532 | let result = unsafe { crate::sched::run_test_actor(drops_an_open_handle) }; |
| 533 | assert_eq!(result, 0); |
| 534 | } |
| 535 | |
| 536 | #[cfg(unix)] |
| 537 | #[test] |
| 538 | fn new_directories_are_private() { |
| 539 | use std::os::unix::fs::PermissionsExt; |
| 540 | |
| 541 | let base = |
| 542 | std::env::temp_dir().join(format!("louise_rt_fs_private_dirs_{}", std::process::id())); |
| 543 | let _ = std::fs::remove_dir_all(&base); |
| 544 | std::fs::create_dir(&base).unwrap(); |
| 545 | let (single, nested) = (base.join("single"), base.join("parent/child")); |
| 546 | create_dir(single.to_str().unwrap(), false).unwrap(); |
| 547 | create_dir(nested.to_str().unwrap(), true).unwrap(); |
| 548 | create_dir(nested.to_str().unwrap(), true).unwrap(); |
| 549 | assert!(create_dir(single.to_str().unwrap(), false).is_err()); |
| 550 | for path in [single.as_path(), nested.as_path(), nested.parent().unwrap()] { |
| 551 | let mode = std::fs::metadata(path).unwrap().permissions().mode(); |
| 552 | assert_eq!( |
| 553 | mode & 0o077, |
| 554 | 0, |
| 555 | "{} is group/other accessible", |
| 556 | path.display() |
| 557 | ); |
| 558 | } |
| 559 | std::fs::remove_dir_all(base).unwrap(); |
| 560 | } |
| 561 | |
| 562 | #[cfg(unix)] |
| 563 | extern "C" fn removes_without_following_symlinks(ctx: *mut Ctx) -> Value { |
| 564 | // SAFETY: the test follows the generated-code convention: fresh |
| 565 | // values are dropped once, arguments of the externs are borrowed. |
| 566 | unsafe { |
| 567 | let base = std::env::temp_dir() |
| 568 | .join(format!("louise_rt_fs_symlink_test_{}", std::process::id())); |
| 569 | let _ = std::fs::remove_dir_all(&base); |
| 570 | let (tree, outside) = (base.join("tree"), base.join("outside")); |
| 571 | std::fs::create_dir_all(tree.join("nested")).unwrap(); |
| 572 | std::fs::create_dir(&outside).unwrap(); |
| 573 | let sentinel = outside.join("keep.txt"); |
| 574 | std::fs::write(&sentinel, "untouched").unwrap(); |
| 575 | std::os::unix::fs::symlink(&outside, tree.join("link")).unwrap(); |
| 576 | |
| 577 | let p = string(ctx, tree.to_str().unwrap()); |
| 578 | let removed = tl_file_remove_all(ctx, p); |
| 579 | assert_eq!(ctor(removed).0, 0, "Ok expected"); |
| 580 | assert!(!tree.exists()); |
| 581 | assert!(sentinel.is_file()); |
| 582 | |
| 583 | // A symlink at the root is removed itself, just like a nested one. |
| 584 | std::os::unix::fs::symlink(&outside, &tree).unwrap(); |
| 585 | let removed_link = tl_file_remove_all(ctx, p); |
| 586 | assert_eq!(ctor(removed_link).0, 0, "Ok expected"); |
| 587 | assert!(std::fs::symlink_metadata(&tree).is_err()); |
| 588 | assert_eq!(std::fs::read_to_string(&sentinel).unwrap(), "untouched"); |
| 589 | |
| 590 | for v in [removed, removed_link, p] { |
| 591 | drop_value(ctx, v); |
| 592 | } |
| 593 | assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 594 | std::fs::remove_dir_all(base).unwrap(); |
| 595 | UNIT |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | #[cfg(unix)] |
| 600 | #[test] |
| 601 | fn remove_all_does_not_follow_symlinks() { |
| 602 | // SAFETY: the test actor follows the generated-code convention. |
| 603 | let result = unsafe { crate::sched::run_test_actor(removes_without_following_symlinks) }; |
| 604 | assert_eq!(result, 0); |
| 605 | } |
| 606 | } |