| 1 | use std::fs; |
| 2 | use std::path::Path; |
| 3 | |
| 4 | #[cfg(unix)] |
| 5 | pub fn open_bundle_file(path: &Path) -> std::io::Result<fs::File> { |
| 6 | use std::os::unix::fs::OpenOptionsExt as _; |
| 7 | use std::os::unix::io::AsRawFd as _; |
| 8 | |
| 9 | let file = fs::OpenOptions::new() |
| 10 | .read(true) |
| 11 | .custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK) |
| 12 | .open(path)?; |
| 13 | let metadata = file.metadata()?; |
| 14 | if !metadata.is_file() { |
| 15 | return Err(std::io::Error::new( |
| 16 | std::io::ErrorKind::InvalidData, |
| 17 | "plugin file is not a regular file", |
| 18 | )); |
| 19 | } |
| 20 | let fd = file.as_raw_fd(); |
| 21 | let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) }; |
| 22 | if flags < 0 { |
| 23 | return Err(std::io::Error::last_os_error()); |
| 24 | } |
| 25 | if unsafe { libc::fcntl(fd, libc::F_SETFL, flags & !libc::O_NONBLOCK) } < 0 { |
| 26 | return Err(std::io::Error::last_os_error()); |
| 27 | } |
| 28 | Ok(file) |
| 29 | } |
| 30 | |
| 31 | #[cfg(windows)] |
| 32 | pub fn open_bundle_file(path: &Path) -> std::io::Result<fs::File> { |
| 33 | open_bundle_file_with_access( |
| 34 | path, |
| 35 | windows_sys::Win32::Storage::FileSystem::FILE_GENERIC_READ, |
| 36 | ) |
| 37 | } |
| 38 | |
| 39 | /// Same protected reader, retaining the right to move this exact opened object. |
| 40 | #[cfg(windows)] |
| 41 | pub fn open_bundle_file_for_retirement(path: &Path) -> std::io::Result<fs::File> { |
| 42 | use windows_sys::Win32::Storage::FileSystem::{DELETE, FILE_GENERIC_READ}; |
| 43 | open_bundle_file_with_access(path, FILE_GENERIC_READ | DELETE) |
| 44 | } |
| 45 | |
| 46 | #[cfg(windows)] |
| 47 | fn open_bundle_file_with_access(path: &Path, access: u32) -> std::io::Result<fs::File> { |
| 48 | use std::os::windows::fs::OpenOptionsExt as _; |
| 49 | |
| 50 | const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x0000_0400; |
| 51 | let file = fs::OpenOptions::new() |
| 52 | .access_mode(access) |
| 53 | .share_mode(0x0000_0001) // deny concurrent writes and replacement |
| 54 | .custom_flags(0x0020_0000) // FILE_FLAG_OPEN_REPARSE_POINT |
| 55 | .open(path)?; |
| 56 | let metadata = file.metadata()?; |
| 57 | let identity = crate::path_identity::windows_file_identity(&file)?; |
| 58 | if !metadata.is_file() |
| 59 | || identity.attributes & FILE_ATTRIBUTE_REPARSE_POINT != 0 |
| 60 | || identity.links != 1 |
| 61 | { |
| 62 | return Err(std::io::Error::new( |
| 63 | std::io::ErrorKind::InvalidData, |
| 64 | "plugin file is a reparse point, hard link, or non-regular file", |
| 65 | )); |
| 66 | } |
| 67 | Ok(file) |
| 68 | } |
| 69 | |
| 70 | #[cfg(all(not(unix), not(windows)))] |
| 71 | pub fn open_bundle_file(path: &Path) -> std::io::Result<fs::File> { |
| 72 | fs::File::open(path) |
| 73 | } |
| 74 | |
| 75 | #[cfg(windows)] |
| 76 | pub fn open_bundle_directory(path: &Path) -> std::io::Result<fs::File> { |
| 77 | use std::os::windows::fs::OpenOptionsExt as _; |
| 78 | |
| 79 | const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x0000_0400; |
| 80 | let file = fs::OpenOptions::new() |
| 81 | .read(true) |
| 82 | .share_mode(0x0000_0001) |
| 83 | .custom_flags(0x0220_0000) // BACKUP_SEMANTICS | OPEN_REPARSE_POINT |
| 84 | .open(path)?; |
| 85 | let metadata = file.metadata()?; |
| 86 | let identity = crate::path_identity::windows_file_identity(&file)?; |
| 87 | if !metadata.is_dir() || identity.attributes & FILE_ATTRIBUTE_REPARSE_POINT != 0 { |
| 88 | return Err(std::io::Error::new( |
| 89 | std::io::ErrorKind::InvalidData, |
| 90 | "plugin directory is a reparse point or non-directory", |
| 91 | )); |
| 92 | } |
| 93 | Ok(file) |
| 94 | } |
| 95 | |
| 96 | /// Reopen a reviewed path only to compare its current handle identity with a |
| 97 | /// retained authority handle. Desired access is zero and sharing is permissive |
| 98 | /// because the retained handle remains responsible for denying writes and |
| 99 | /// replacement throughout the comparison. |
| 100 | #[cfg(windows)] |
| 101 | pub fn open_bundle_identity_probe( |
| 102 | path: &Path, |
| 103 | expect_directory: bool, |
| 104 | ) -> std::io::Result<fs::File> { |
| 105 | use std::os::windows::fs::OpenOptionsExt as _; |
| 106 | |
| 107 | const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x0000_0400; |
| 108 | const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x0200_0000; |
| 109 | const FILE_FLAG_OPEN_REPARSE_POINT: u32 = 0x0020_0000; |
| 110 | const FILE_SHARE_READ_WRITE_DELETE: u32 = 0x0000_0007; |
| 111 | let flags = FILE_FLAG_OPEN_REPARSE_POINT |
| 112 | | if expect_directory { |
| 113 | FILE_FLAG_BACKUP_SEMANTICS |
| 114 | } else { |
| 115 | 0 |
| 116 | }; |
| 117 | let file = fs::OpenOptions::new() |
| 118 | .access_mode(0) |
| 119 | .share_mode(FILE_SHARE_READ_WRITE_DELETE) |
| 120 | .custom_flags(flags) |
| 121 | .open(path)?; |
| 122 | let metadata = file.metadata()?; |
| 123 | let identity = crate::path_identity::windows_file_identity(&file)?; |
| 124 | let expected_kind = if expect_directory { |
| 125 | metadata.is_dir() |
| 126 | } else { |
| 127 | metadata.is_file() && identity.links == 1 |
| 128 | }; |
| 129 | if !expected_kind || identity.attributes & FILE_ATTRIBUTE_REPARSE_POINT != 0 { |
| 130 | return Err(std::io::Error::new( |
| 131 | std::io::ErrorKind::InvalidData, |
| 132 | "plugin identity probe found a reparse point, hard link, or unexpected object", |
| 133 | )); |
| 134 | } |
| 135 | Ok(file) |
| 136 | } |
| 137 | |
| 138 | #[cfg(test)] |
| 139 | mod tests { |
| 140 | use super::open_bundle_file; |
| 141 | |
| 142 | #[test] |
| 143 | fn open_bundle_file_rejects_directory() { |
| 144 | let dir = tempfile::tempdir().unwrap(); |
| 145 | let err = open_bundle_file(dir.path()).unwrap_err(); |
| 146 | #[cfg(unix)] |
| 147 | assert_eq!(err.kind(), std::io::ErrorKind::InvalidData); |
| 148 | #[cfg(not(unix))] |
| 149 | let _ = err; |
| 150 | } |
| 151 | |
| 152 | #[cfg(unix)] |
| 153 | #[test] |
| 154 | fn open_bundle_file_rejects_fifo_without_blocking() { |
| 155 | use std::ffi::CString; |
| 156 | use std::os::unix::ffi::OsStrExt as _; |
| 157 | |
| 158 | let dir = tempfile::tempdir().unwrap(); |
| 159 | let fifo = dir.path().join("pipe"); |
| 160 | let c_fifo = CString::new(fifo.as_os_str().as_bytes()).unwrap(); |
| 161 | let rc = unsafe { libc::mkfifo(c_fifo.as_ptr(), 0o600) }; |
| 162 | assert_eq!(rc, 0, "mkfifo failed: {}", std::io::Error::last_os_error()); |
| 163 | let err = open_bundle_file(&fifo).unwrap_err(); |
| 164 | assert_eq!(err.kind(), std::io::ErrorKind::InvalidData); |
| 165 | } |
| 166 | } |
| 167 |