Debian Codegraph @

rust/hg-core/src/checkexec.rs

raw
1 use std::fs;
2 use std::fs::Metadata;
3 use std::io;
4 use std::os::unix::fs::MetadataExt;
5 use std::os::unix::fs::PermissionsExt;
6 use std::path::Path;
7
8 const EXECFLAGS: u32 = 0o111;
9
10 pub fn is_executable(metadata: &Metadata) -> bool {
11 metadata.mode() & EXECFLAGS != 0
12 }
13
14 fn is_path_executable(path: impl AsRef<Path>) -> Result<bool, io::Error> {
15 let metadata = fs::metadata(path)?;
16 Ok(is_executable(&metadata))
17 }
18
19 fn make_executable(path: impl AsRef<Path>) -> Result<(), io::Error> {
20 let mode = fs::metadata(path.as_ref())?.mode();
21 fs::set_permissions(
22 path,
23 fs::Permissions::from_mode((mode & 0o777) | EXECFLAGS),
24 )?;
25 Ok(())
26 }
27
28 fn copy_mode(
29 src: impl AsRef<Path>,
30 dst: impl AsRef<Path>,
31 ) -> Result<(), io::Error> {
32 let mode = match fs::symlink_metadata(src) {
33 Ok(metadata) => metadata.mode(),
34 Err(e) if e.kind() == io::ErrorKind::NotFound =>
35 // copymode in python has a more complicated handling of FileNotFound
36 // error, which we don't need because all it does is applying
37 // umask, which the OS already does when we mkdir.
38 {
39 return Ok(())
40 }
41 Err(e) => return Err(e),
42 };
43 fs::set_permissions(dst, fs::Permissions::from_mode(mode))?;
44 Ok(())
45 }
46
47 fn check_exec_impl(path: impl AsRef<Path>) -> Result<bool, io::Error> {
48 let basedir = path.as_ref().join(".hg");
49 let cachedir = basedir.join("wcache");
50 let storedir = basedir.join("store");
51
52 if !cachedir.exists() {
53 // we want to create the 'cache' directory, not the '.hg' one.
54 // Automatically creating '.hg' directory could silently spawn
55 // invalid Mercurial repositories. That seems like a bad idea.
56 fs::create_dir(&cachedir)
57 .and_then(|()| {
58 if storedir.exists() {
59 copy_mode(&storedir, &cachedir)
60 } else {
61 copy_mode(&basedir, &cachedir)
62 }
63 })
64 .ok();
65 }
66
67 let leave_file: bool;
68 let checkdir: &Path;
69 let checkisexec = cachedir.join("checkisexec");
70 let checknoexec = cachedir.join("checknoexec");
71 if cachedir.is_dir() {
72 // Check if both files already exist in cache and have correct
73 // permissions. if so, we assume that permissions work.
74 // If not, we delete the files and try again.
75 match is_path_executable(&checkisexec) {
76 Err(e) if e.kind() == io::ErrorKind::NotFound => (),
77 Err(e) => return Err(e),
78 Ok(is_exec) => {
79 if is_exec {
80 let noexec_is_exec = match is_path_executable(&checknoexec)
81 {
82 Err(e) if e.kind() == io::ErrorKind::NotFound => {
83 fs::write(&checknoexec, "")?;
84 is_path_executable(&checknoexec)?
85 }
86 Err(e) => return Err(e),
87 Ok(exec) => exec,
88 };
89 if !noexec_is_exec {
90 // check-exec is exec and check-no-exec is not exec
91 return Ok(true);
92 }
93 fs::remove_file(&checknoexec)?;
94 }
95 fs::remove_file(&checkisexec)?;
96 }
97 }
98 checkdir = &cachedir;
99 leave_file = true;
100 } else {
101 // no cache directory (probably because .hg doesn't exist):
102 // check directly in `path` and don't leave the temp file behind
103 checkdir = path.as_ref();
104 leave_file = false;
105 };
106
107 let tmp_file = tempfile::Builder::new()
108 .permissions(std::fs::Permissions::from_mode(0o666))
109 .tempfile_in(checkdir)?;
110
111 if !is_path_executable(tmp_file.path())? {
112 make_executable(tmp_file.path())?;
113 if is_path_executable(tmp_file.path())? {
114 if leave_file {
115 tmp_file.persist(checkisexec).ok();
116 }
117 return Ok(true);
118 }
119 }
120
121 Ok(false)
122 }
123
124 /// This function is a Rust rewrite of the `checkexec` function from
125 /// `posix.py`.
126 ///
127 /// Returns `true` if the filesystem supports execute permissions.
128 pub fn check_exec(path: impl AsRef<Path>) -> bool {
129 check_exec_impl(path).unwrap_or(false)
130 }