Reads a file's bytes, absorbing transient Windows file locks. On Windows, antivirus scanners and the search indexer briefly open freshly written files with exclusive access; an unlucky open during that window fails with a sharing violation or "Access is denied" (os error 5) even though the file is readable milliseconds later. Callers treat read errors as "skip this file" or fail the whole sync, s
(path: &Path)
| 53 | /// giving up. Other platforms read directly: `PermissionDenied` there is a |
| 54 | /// real ACL problem that retrying cannot fix. |
| 55 | fn read_file_bytes(path: &Path) -> std::io::Result<Vec<u8>> { |
| 56 | const RETRY_DELAYS_MS: [u64; 4] = [10, 20, 40, 80]; |
| 57 | if !cfg!(windows) { |
| 58 | return std::fs::read(path); |
| 59 | } |
| 60 | let mut delays = RETRY_DELAYS_MS.iter(); |
| 61 | loop { |
| 62 | match std::fs::read(path) { |
| 63 | Err(err) if is_transient_windows_file_lock(&err) => match delays.next() { |
| 64 | Some(&delay_ms) => { |
| 65 | std::thread::sleep(std::time::Duration::from_millis(delay_ms)); |
| 66 | } |
| 67 | None => return Err(err), |
| 68 | }, |
| 69 | result => return result, |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// True for Windows errors that indicate another process is briefly holding |
| 75 | /// the file: `ERROR_SHARING_VIOLATION` (32) and `ERROR_LOCK_VIOLATION` (33) |
no test coverage detected