| 1 | use std::path::Path; |
| 2 | |
| 3 | pub fn open_workspace_directory( |
| 4 | workspace: &Path, |
| 5 | ) -> anyhow::Result<(std::path::PathBuf, std::fs::File)> { |
| 6 | open_directory(workspace, false) |
| 7 | } |
| 8 | |
| 9 | pub fn open_pinned_workspace_directory( |
| 10 | workspace: &Path, |
| 11 | ) -> anyhow::Result<(std::path::PathBuf, std::fs::File)> { |
| 12 | open_directory(workspace, true) |
| 13 | } |
| 14 | |
| 15 | fn open_directory( |
| 16 | workspace: &Path, |
| 17 | pinned: bool, |
| 18 | ) -> anyhow::Result<(std::path::PathBuf, std::fs::File)> { |
| 19 | #[cfg(not(windows))] |
| 20 | let _ = pinned; |
| 21 | let canonical = workspace.canonicalize()?; |
| 22 | let mut options = std::fs::OpenOptions::new(); |
| 23 | options.read(true); |
| 24 | #[cfg(unix)] |
| 25 | { |
| 26 | use std::os::unix::fs::OpenOptionsExt as _; |
| 27 | options.custom_flags(libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC); |
| 28 | } |
| 29 | #[cfg(windows)] |
| 30 | { |
| 31 | use std::os::windows::fs::OpenOptionsExt as _; |
| 32 | options.custom_flags(0x0200_0000 | 0x0020_0000); // BACKUP_SEMANTICS, OPEN_REPARSE_POINT |
| 33 | if pinned { |
| 34 | options.share_mode(0x1 | 0x2); |
| 35 | } |
| 36 | } |
| 37 | let directory = options.open(&canonical)?; |
| 38 | let metadata = directory.metadata()?; |
| 39 | anyhow::ensure!( |
| 40 | metadata.is_dir() && !crate::path_identity::metadata_is_link_or_reparse(&metadata), |
| 41 | "workspace is not an ordinary directory" |
| 42 | ); |
| 43 | anyhow::ensure!( |
| 44 | workspace.canonicalize()? == canonical, |
| 45 | "selected workspace changed" |
| 46 | ); |
| 47 | Ok((canonical, directory)) |
| 48 | } |
| 49 |