junction_verbatim/
lib.rs

1/*!
2Library for working with NTFS junctions.
3
4# ⚠️ DEPRECATED
5
6**This crate is deprecated. Please use the upstream [`junction`](https://crates.io/crates/junction) crate instead.**
7
8The verbatim prefix fix that this fork provided has been merged and published in the upstream crate.
9Simply replace `junction-verbatim` with `junction` in your `Cargo.toml` - the API is identical.
10
11---
12
13Junction Points are a little known NTFS v5+ feature roughly equivalent to Unix
14directory symbolic links.
15
16They are supported in Windows 2000 and onwards, where a directory
17serves as a symbolic link to another directory on the computer. For example,
18if the directory `D:\SYMLINK` specified `C:\WINNT\SYSTEM32` as its target, then
19an application accessing `D:\SYMLINK\DRIVERS` would in reality be accessing
20`C:\WINNT\SYSTEM32\DRIVERS`.
21*/
22#![deprecated(
23    since = "1.3.1",
24    note = "This crate is deprecated. Use the upstream `junction` v1.4.1 crate instead - the verbatim prefix fix has been merged."
25)]
26#![doc(html_root_url = "https://docs.rs/junction-verbatim/1.3")]
27#![cfg(windows)]
28#![deny(rust_2021_compatibility)]
29
30mod internals;
31
32#[cfg(test)]
33mod tests;
34
35use std::io;
36use std::path::{Path, PathBuf};
37
38/// Creates a junction point from the specified directory to the specified target directory.
39///
40/// N.B. Only works on NTFS.
41///
42/// # Error
43///
44/// This function may error if the `junction` path already exists.
45///
46/// # Example
47///
48/// ```rust
49/// use std::io;
50/// use std::path::Path;
51/// # use std::fs;
52/// # use junction_verbatim::create;
53/// fn main() -> io::Result<()> {
54///     let tmpdir = tempfile::tempdir()?;
55///     let target = tmpdir.path().join("target");
56///     let junction = tmpdir.path().join("junction");
57///     # fs::create_dir_all(&target)?;
58///     create(&target, &junction)
59/// }
60/// ```
61pub fn create<P, Q>(target: P, junction: Q) -> io::Result<()>
62where
63    P: AsRef<Path>,
64    Q: AsRef<Path>,
65{
66    internals::create(target.as_ref(), junction.as_ref())
67}
68
69/// Deletes a `junction` reparse point from the specified file or directory.
70///
71/// N.B. Only works on NTFS.
72///
73/// This function delete the junction point only, leaving the target directory
74/// and its content as is. It does nothing if the `junction` point does not exist.
75///
76/// # Example
77///
78/// ```rust
79/// use std::io;
80/// use std::path::Path;
81/// # use std::fs;
82/// # use junction_verbatim::{create, delete};
83/// fn main() -> io::Result<()> {
84///     let tmpdir = tempfile::tempdir()?;
85///     let target = tmpdir.path().join("target");
86///     let junction = tmpdir.path().join("junction");
87///     # fs::create_dir_all(&target)?;
88///     create(&target, &junction)?;
89///     delete(&junction)
90/// }
91/// ```
92pub fn delete<P: AsRef<Path>>(junction: P) -> io::Result<()> {
93    internals::delete(junction.as_ref())
94}
95
96/// Determines whether the specified path exists and refers to a junction point.
97///
98/// # Example
99///
100/// ```rust
101/// use std::io;
102/// # use junction_verbatim::exists;
103/// fn main() -> io::Result<()> {
104///     # #[cfg(feature = "unstable_admin")]
105///     assert!(exists(r"C:\Users\Default User")?);
106///     Ok(())
107/// }
108/// ```
109pub fn exists<P: AsRef<Path>>(junction: P) -> io::Result<bool> {
110    internals::exists(junction.as_ref())
111}
112
113/// Gets the target of the specified junction point.
114///
115/// N.B. Only works on NTFS.
116///
117/// # Example
118///
119/// ```rust
120/// use std::io;
121/// # use junction_verbatim::get_target;
122/// fn main() -> io::Result<()> {
123///     # #[cfg(feature = "unstable_admin")]
124///     assert_eq!(get_target(r"C:\Users\Default User")?.to_str(), Some(r"C:\Users\Default"));
125///     Ok(())
126/// }
127/// ```
128pub fn get_target<P: AsRef<Path>>(junction: P) -> io::Result<PathBuf> {
129    internals::get_target(junction.as_ref())
130}