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