rustc_metadata/
fs.rs

1use std::path::{Path, PathBuf};
2use std::{fs, io};
3
4use rustc_data_structures::temp_dir::MaybeTempDir;
5use rustc_fs_util::TempDirBuilder;
6use rustc_middle::ty::TyCtxt;
7use rustc_session::Session;
8use rustc_session::config::{CrateType, OutFileName, OutputType};
9use rustc_session::output::filename_for_metadata;
10
11use crate::errors::{
12    BinaryOutputToTty, FailedCopyToStdout, FailedCreateEncodedMetadata, FailedCreateFile,
13    FailedCreateTempdir, FailedWriteError,
14};
15use crate::{EncodedMetadata, encode_metadata};
16
17// FIXME(eddyb) maybe include the crate name in this?
18pub const METADATA_FILENAME: &str = "lib.rmeta";
19
20/// We use a temp directory here to avoid races between concurrent rustc processes,
21/// such as builds in the same directory using the same filename for metadata while
22/// building an `.rlib` (stomping over one another), or writing an `.rmeta` into a
23/// directory being searched for `extern crate` (observing an incomplete file).
24/// The returned path is the temporary file containing the complete metadata.
25pub fn emit_wrapper_file(sess: &Session, data: &[u8], tmpdir: &Path, name: &str) -> PathBuf {
26    let out_filename = tmpdir.join(name);
27    let result = fs::write(&out_filename, data);
28
29    if let Err(err) = result {
30        sess.dcx().emit_fatal(FailedWriteError { filename: out_filename, err });
31    }
32
33    out_filename
34}
35
36pub fn encode_and_write_metadata(tcx: TyCtxt<'_>) -> EncodedMetadata {
37    let out_filename = filename_for_metadata(tcx.sess, tcx.output_filenames(()));
38    // To avoid races with another rustc process scanning the output directory,
39    // we need to write the file somewhere else and atomically move it to its
40    // final destination, with an `fs::rename` call. In order for the rename to
41    // always succeed, the temporary file needs to be on the same filesystem,
42    // which is why we create it inside the output directory specifically.
43    let metadata_tmpdir = TempDirBuilder::new()
44        .prefix("rmeta")
45        .tempdir_in(out_filename.parent().unwrap_or_else(|| Path::new("")))
46        .unwrap_or_else(|err| tcx.dcx().emit_fatal(FailedCreateTempdir { err }));
47    let metadata_tmpdir = MaybeTempDir::new(metadata_tmpdir, tcx.sess.opts.cg.save_temps);
48    let metadata_filename = metadata_tmpdir.as_ref().join("full.rmeta");
49    let metadata_stub_filename = if !tcx.sess.opts.unstable_opts.embed_metadata
50        && !tcx.crate_types().contains(&CrateType::ProcMacro)
51    {
52        Some(metadata_tmpdir.as_ref().join("stub.rmeta"))
53    } else {
54        None
55    };
56
57    if tcx.needs_metadata() {
58        encode_metadata(tcx, &metadata_filename, metadata_stub_filename.as_deref());
59    } else {
60        // Always create a file at `metadata_filename`, even if we have nothing to write to it.
61        // This simplifies the creation of the output `out_filename` when requested.
62        std::fs::File::create(&metadata_filename).unwrap_or_else(|err| {
63            tcx.dcx().emit_fatal(FailedCreateFile { filename: &metadata_filename, err });
64        });
65        if let Some(metadata_stub_filename) = &metadata_stub_filename {
66            std::fs::File::create(metadata_stub_filename).unwrap_or_else(|err| {
67                tcx.dcx().emit_fatal(FailedCreateFile { filename: &metadata_stub_filename, err });
68            });
69        }
70    }
71
72    let _prof_timer = tcx.sess.prof.generic_activity("write_crate_metadata");
73
74    // If the user requests metadata as output, rename `metadata_filename`
75    // to the expected output `out_filename`. The match above should ensure
76    // this file always exists.
77    let need_metadata_file = tcx.sess.opts.output_types.contains_key(&OutputType::Metadata);
78    let (metadata_filename, metadata_tmpdir) = if need_metadata_file {
79        let filename = match out_filename {
80            OutFileName::Real(ref path) => {
81                if let Err(err) = non_durable_rename(&metadata_filename, path) {
82                    tcx.dcx().emit_fatal(FailedWriteError { filename: path.to_path_buf(), err });
83                }
84                path.clone()
85            }
86            OutFileName::Stdout => {
87                if out_filename.is_tty() {
88                    tcx.dcx().emit_err(BinaryOutputToTty);
89                } else if let Err(err) = copy_to_stdout(&metadata_filename) {
90                    tcx.dcx()
91                        .emit_err(FailedCopyToStdout { filename: metadata_filename.clone(), err });
92                }
93                metadata_filename
94            }
95        };
96        if tcx.sess.opts.json_artifact_notifications {
97            tcx.dcx().emit_artifact_notification(out_filename.as_path(), "metadata");
98        }
99        (filename, None)
100    } else {
101        (metadata_filename, Some(metadata_tmpdir))
102    };
103
104    // Load metadata back to memory: codegen may need to include it in object files.
105    let metadata =
106        EncodedMetadata::from_path(metadata_filename, metadata_stub_filename, metadata_tmpdir)
107            .unwrap_or_else(|err| {
108                tcx.dcx().emit_fatal(FailedCreateEncodedMetadata { err });
109            });
110
111    metadata
112}
113
114#[cfg(not(target_os = "linux"))]
115pub fn non_durable_rename(src: &Path, dst: &Path) -> std::io::Result<()> {
116    std::fs::rename(src, dst)
117}
118
119/// This function attempts to bypass the auto_da_alloc heuristic implemented by some filesystems
120/// such as btrfs and ext4. When renaming over a file that already exists then they will "helpfully"
121/// write back the source file before committing the rename in case a developer forgot some of
122/// the fsyncs in the open/write/fsync(file)/rename/fsync(dir) dance for atomic file updates.
123///
124/// To avoid triggering this heuristic we delete the destination first, if it exists.
125/// The cost of an extra syscall is much lower than getting descheduled for the sync IO.
126#[cfg(target_os = "linux")]
127pub fn non_durable_rename(src: &Path, dst: &Path) -> std::io::Result<()> {
128    let _ = std::fs::remove_file(dst);
129    std::fs::rename(src, dst)
130}
131
132pub fn copy_to_stdout(from: &Path) -> io::Result<()> {
133    let mut reader = fs::File::open_buffered(from)?;
134    let mut stdout = io::stdout();
135    io::copy(&mut reader, &mut stdout)?;
136    Ok(())
137}