]>
Commit | Line | Data |
---|---|---|
a7813a04 XL |
1 | // Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | //! Tidy check to ensure that there are no binaries checked into the source tree | |
12 | //! by accident. | |
13 | //! | |
14 | //! In the past we've accidentally checked in test binaries and such which add a | |
15 | //! huge amount of bloat to the git history, so it's good to just ensure we | |
16 | //! don't do that again :) | |
17 | ||
18 | use std::path::Path; | |
19 | ||
20 | // All files are executable on Windows, so just check on Unix | |
21 | #[cfg(windows)] | |
22 | pub fn check(_path: &Path, _bad: &mut bool) {} | |
23 | ||
24 | #[cfg(unix)] | |
25 | pub fn check(path: &Path, bad: &mut bool) { | |
26 | use std::fs; | |
9e0c209e SL |
27 | use std::io::Read; |
28 | use std::process::{Command, Stdio}; | |
a7813a04 XL |
29 | use std::os::unix::prelude::*; |
30 | ||
9e0c209e SL |
31 | if let Ok(mut file) = fs::File::open("/proc/version") { |
32 | let mut contents = String::new(); | |
33 | file.read_to_string(&mut contents).unwrap(); | |
abe05a73 XL |
34 | // Probably on Windows Linux Subsystem or Docker via VirtualBox, |
35 | // all files will be marked as executable, so skip checking. | |
36 | if contents.contains("Microsoft") || contents.contains("boot2docker") { | |
9e0c209e SL |
37 | return; |
38 | } | |
39 | } | |
40 | ||
a7813a04 XL |
41 | super::walk(path, |
42 | &mut |path| super::filter_dirs(path) || path.ends_with("src/etc"), | |
43 | &mut |file| { | |
44 | let filename = file.file_name().unwrap().to_string_lossy(); | |
45 | let extensions = [".py", ".sh"]; | |
46 | if extensions.iter().any(|e| filename.ends_with(e)) { | |
c30ab7b3 | 47 | return; |
a7813a04 XL |
48 | } |
49 | ||
50 | let metadata = t!(fs::symlink_metadata(&file), &file); | |
51 | if metadata.mode() & 0o111 != 0 { | |
9e0c209e SL |
52 | let rel_path = file.strip_prefix(path).unwrap(); |
53 | let git_friendly_path = rel_path.to_str().unwrap().replace("\\", "/"); | |
c30ab7b3 SL |
54 | let output = Command::new("git") |
55 | .arg("ls-files") | |
56 | .arg(&git_friendly_path) | |
57 | .current_dir(path) | |
58 | .stderr(Stdio::null()) | |
59 | .output() | |
60 | .unwrap_or_else(|e| { | |
61 | panic!("could not run git ls-files: {}", e); | |
62 | }); | |
63 | let path_bytes = rel_path.as_os_str().as_bytes(); | |
64 | if output.status.success() && output.stdout.starts_with(path_bytes) { | |
cc61c64b | 65 | tidy_error!(bad, "binary checked into source: {}", file.display()); |
9e0c209e | 66 | } |
a7813a04 XL |
67 | } |
68 | }) | |
69 | } |