]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_incremental/src/persist/fs.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / compiler / rustc_incremental / src / persist / fs.rs
1 //! This module manages how the incremental compilation cache is represented in
2 //! the file system.
3 //!
4 //! Incremental compilation caches are managed according to a copy-on-write
5 //! strategy: Once a complete, consistent cache version is finalized, it is
6 //! never modified. Instead, when a subsequent compilation session is started,
7 //! the compiler will allocate a new version of the cache that starts out as
8 //! a copy of the previous version. Then only this new copy is modified and it
9 //! will not be visible to other processes until it is finalized. This ensures
10 //! that multiple compiler processes can be executed concurrently for the same
11 //! crate without interfering with each other or blocking each other.
12 //!
13 //! More concretely this is implemented via the following protocol:
14 //!
15 //! 1. For a newly started compilation session, the compiler allocates a
16 //! new `session` directory within the incremental compilation directory.
17 //! This session directory will have a unique name that ends with the suffix
18 //! "-working" and that contains a creation timestamp.
19 //! 2. Next, the compiler looks for the newest finalized session directory,
20 //! that is, a session directory from a previous compilation session that
21 //! has been marked as valid and consistent. A session directory is
22 //! considered finalized if the "-working" suffix in the directory name has
23 //! been replaced by the SVH of the crate.
24 //! 3. Once the compiler has found a valid, finalized session directory, it will
25 //! hard-link/copy its contents into the new "-working" directory. If all
26 //! goes well, it will have its own, private copy of the source directory and
27 //! subsequently not have to worry about synchronizing with other compiler
28 //! processes.
29 //! 4. Now the compiler can do its normal compilation process, which involves
30 //! reading and updating its private session directory.
31 //! 5. When compilation finishes without errors, the private session directory
32 //! will be in a state where it can be used as input for other compilation
33 //! sessions. That is, it will contain a dependency graph and cache artifacts
34 //! that are consistent with the state of the source code it was compiled
35 //! from, with no need to change them ever again. At this point, the compiler
36 //! finalizes and "publishes" its private session directory by renaming it
37 //! from "s-{timestamp}-{random}-working" to "s-{timestamp}-{SVH}".
38 //! 6. At this point the "old" session directory that we copied our data from
39 //! at the beginning of the session has become obsolete because we have just
40 //! published a more current version. Thus the compiler will delete it.
41 //!
42 //! ## Garbage Collection
43 //!
44 //! Naively following the above protocol might lead to old session directories
45 //! piling up if a compiler instance crashes for some reason before its able to
46 //! remove its private session directory. In order to avoid wasting disk space,
47 //! the compiler also does some garbage collection each time it is started in
48 //! incremental compilation mode. Specifically, it will scan the incremental
49 //! compilation directory for private session directories that are not in use
50 //! any more and will delete those. It will also delete any finalized session
51 //! directories for a given crate except for the most recent one.
52 //!
53 //! ## Synchronization
54 //!
55 //! There is some synchronization needed in order for the compiler to be able to
56 //! determine whether a given private session directory is not in used any more.
57 //! This is done by creating a lock file for each session directory and
58 //! locking it while the directory is still being used. Since file locks have
59 //! operating system support, we can rely on the lock being released if the
60 //! compiler process dies for some unexpected reason. Thus, when garbage
61 //! collecting private session directories, the collecting process can determine
62 //! whether the directory is still in use by trying to acquire a lock on the
63 //! file. If locking the file fails, the original process must still be alive.
64 //! If locking the file succeeds, we know that the owning process is not alive
65 //! any more and we can safely delete the directory.
66 //! There is still a small time window between the original process creating the
67 //! lock file and actually locking it. In order to minimize the chance that
68 //! another process tries to acquire the lock in just that instance, only
69 //! session directories that are older than a few seconds are considered for
70 //! garbage collection.
71 //!
72 //! Another case that has to be considered is what happens if one process
73 //! deletes a finalized session directory that another process is currently
74 //! trying to copy from. This case is also handled via the lock file. Before
75 //! a process starts copying a finalized session directory, it will acquire a
76 //! shared lock on the directory's lock file. Any garbage collecting process,
77 //! on the other hand, will acquire an exclusive lock on the lock file.
78 //! Thus, if a directory is being collected, any reader process will fail
79 //! acquiring the shared lock and will leave the directory alone. Conversely,
80 //! if a collecting process can't acquire the exclusive lock because the
81 //! directory is currently being read from, it will leave collecting that
82 //! directory to another process at a later point in time.
83 //! The exact same scheme is also used when reading the metadata hashes file
84 //! from an extern crate. When a crate is compiled, the hash values of its
85 //! metadata are stored in a file in its session directory. When the
86 //! compilation session of another crate imports the first crate's metadata,
87 //! it also has to read in the accompanying metadata hashes. It thus will access
88 //! the finalized session directory of all crates it links to and while doing
89 //! so, it will also place a read lock on that the respective session directory
90 //! so that it won't be deleted while the metadata hashes are loaded.
91 //!
92 //! ## Preconditions
93 //!
94 //! This system relies on two features being available in the file system in
95 //! order to work really well: file locking and hard linking.
96 //! If hard linking is not available (like on FAT) the data in the cache
97 //! actually has to be copied at the beginning of each session.
98 //! If file locking does not work reliably (like on NFS), some of the
99 //! synchronization will go haywire.
100 //! In both cases we recommend to locate the incremental compilation directory
101 //! on a file system that supports these things.
102 //! It might be a good idea though to try and detect whether we are on an
103 //! unsupported file system and emit a warning in that case. This is not yet
104 //! implemented.
105
106 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
107 use rustc_data_structures::svh::Svh;
108 use rustc_data_structures::{base_n, flock};
109 use rustc_errors::ErrorGuaranteed;
110 use rustc_fs_util::{link_or_copy, LinkOrCopy};
111 use rustc_session::{Session, StableCrateId};
112
113 use std::fs as std_fs;
114 use std::io::{self, ErrorKind};
115 use std::mem;
116 use std::path::{Path, PathBuf};
117 use std::time::{Duration, SystemTime, UNIX_EPOCH};
118
119 use rand::{thread_rng, RngCore};
120
121 #[cfg(test)]
122 mod tests;
123
124 const LOCK_FILE_EXT: &str = ".lock";
125 const DEP_GRAPH_FILENAME: &str = "dep-graph.bin";
126 const STAGING_DEP_GRAPH_FILENAME: &str = "dep-graph.part.bin";
127 const WORK_PRODUCTS_FILENAME: &str = "work-products.bin";
128 const QUERY_CACHE_FILENAME: &str = "query-cache.bin";
129
130 // We encode integers using the following base, so they are shorter than decimal
131 // or hexadecimal numbers (we want short file and directory names). Since these
132 // numbers will be used in file names, we choose an encoding that is not
133 // case-sensitive (as opposed to base64, for example).
134 const INT_ENCODE_BASE: usize = base_n::CASE_INSENSITIVE;
135
136 /// Returns the path to a session's dependency graph.
137 pub fn dep_graph_path(sess: &Session) -> PathBuf {
138 in_incr_comp_dir_sess(sess, DEP_GRAPH_FILENAME)
139 }
140 /// Returns the path to a session's staging dependency graph.
141 ///
142 /// On the difference between dep-graph and staging dep-graph,
143 /// see `build_dep_graph`.
144 pub fn staging_dep_graph_path(sess: &Session) -> PathBuf {
145 in_incr_comp_dir_sess(sess, STAGING_DEP_GRAPH_FILENAME)
146 }
147 pub fn work_products_path(sess: &Session) -> PathBuf {
148 in_incr_comp_dir_sess(sess, WORK_PRODUCTS_FILENAME)
149 }
150 /// Returns the path to a session's query cache.
151 pub fn query_cache_path(sess: &Session) -> PathBuf {
152 in_incr_comp_dir_sess(sess, QUERY_CACHE_FILENAME)
153 }
154
155 /// Locks a given session directory.
156 pub fn lock_file_path(session_dir: &Path) -> PathBuf {
157 let crate_dir = session_dir.parent().unwrap();
158
159 let directory_name = session_dir.file_name().unwrap().to_string_lossy();
160 assert_no_characters_lost(&directory_name);
161
162 let dash_indices: Vec<_> = directory_name.match_indices('-').map(|(idx, _)| idx).collect();
163 if dash_indices.len() != 3 {
164 bug!(
165 "Encountered incremental compilation session directory with \
166 malformed name: {}",
167 session_dir.display()
168 )
169 }
170
171 crate_dir.join(&directory_name[0..dash_indices[2]]).with_extension(&LOCK_FILE_EXT[1..])
172 }
173
174 /// Returns the path for a given filename within the incremental compilation directory
175 /// in the current session.
176 pub fn in_incr_comp_dir_sess(sess: &Session, file_name: &str) -> PathBuf {
177 in_incr_comp_dir(&sess.incr_comp_session_dir(), file_name)
178 }
179
180 /// Returns the path for a given filename within the incremental compilation directory,
181 /// not necessarily from the current session.
182 ///
183 /// To ensure the file is part of the current session, use [`in_incr_comp_dir_sess`].
184 pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBuf {
185 incr_comp_session_dir.join(file_name)
186 }
187
188 /// Allocates the private session directory.
189 ///
190 /// If the result of this function is `Ok`, we have a valid incremental
191 /// compilation session directory. A valid session
192 /// directory is one that contains a locked lock file. It may or may not contain
193 /// a dep-graph and work products from a previous session.
194 ///
195 /// This always attempts to load a dep-graph from the directory.
196 /// If loading fails for some reason, we fallback to a disabled `DepGraph`.
197 /// See [`rustc_interface::queries::dep_graph`].
198 ///
199 /// If this function returns an error, it may leave behind an invalid session directory.
200 /// The garbage collection will take care of it.
201 ///
202 /// [`rustc_interface::queries::dep_graph`]: ../../rustc_interface/struct.Queries.html#structfield.dep_graph
203 pub fn prepare_session_directory(
204 sess: &Session,
205 crate_name: &str,
206 stable_crate_id: StableCrateId,
207 ) -> Result<(), ErrorGuaranteed> {
208 if sess.opts.incremental.is_none() {
209 return Ok(());
210 }
211
212 let _timer = sess.timer("incr_comp_prepare_session_directory");
213
214 debug!("prepare_session_directory");
215
216 // {incr-comp-dir}/{crate-name-and-disambiguator}
217 let crate_dir = crate_path(sess, crate_name, stable_crate_id);
218 debug!("crate-dir: {}", crate_dir.display());
219 create_dir(sess, &crate_dir, "crate")?;
220
221 // Hack: canonicalize the path *after creating the directory*
222 // because, on windows, long paths can cause problems;
223 // canonicalization inserts this weird prefix that makes windows
224 // tolerate long paths.
225 let crate_dir = match crate_dir.canonicalize() {
226 Ok(v) => v,
227 Err(err) => {
228 let reported = sess.err(&format!(
229 "incremental compilation: error canonicalizing path `{}`: {}",
230 crate_dir.display(),
231 err
232 ));
233 return Err(reported);
234 }
235 };
236
237 let mut source_directories_already_tried = FxHashSet::default();
238
239 loop {
240 // Generate a session directory of the form:
241 //
242 // {incr-comp-dir}/{crate-name-and-disambiguator}/s-{timestamp}-{random}-working
243 let session_dir = generate_session_dir_path(&crate_dir);
244 debug!("session-dir: {}", session_dir.display());
245
246 // Lock the new session directory. If this fails, return an
247 // error without retrying
248 let (directory_lock, lock_file_path) = lock_directory(sess, &session_dir)?;
249
250 // Now that we have the lock, we can actually create the session
251 // directory
252 create_dir(sess, &session_dir, "session")?;
253
254 // Find a suitable source directory to copy from. Ignore those that we
255 // have already tried before.
256 let source_directory = find_source_directory(&crate_dir, &source_directories_already_tried);
257
258 let Some(source_directory) = source_directory else {
259 // There's nowhere to copy from, we're done
260 debug!(
261 "no source directory found. Continuing with empty session \
262 directory."
263 );
264
265 sess.init_incr_comp_session(session_dir, directory_lock, false);
266 return Ok(());
267 };
268
269 debug!("attempting to copy data from source: {}", source_directory.display());
270
271 // Try copying over all files from the source directory
272 if let Ok(allows_links) = copy_files(sess, &session_dir, &source_directory) {
273 debug!("successfully copied data from: {}", source_directory.display());
274
275 if !allows_links {
276 sess.warn(&format!(
277 "Hard linking files in the incremental \
278 compilation cache failed. Copying files \
279 instead. Consider moving the cache \
280 directory to a file system which supports \
281 hard linking in session dir `{}`",
282 session_dir.display()
283 ));
284 }
285
286 sess.init_incr_comp_session(session_dir, directory_lock, true);
287 return Ok(());
288 } else {
289 debug!("copying failed - trying next directory");
290
291 // Something went wrong while trying to copy/link files from the
292 // source directory. Try again with a different one.
293 source_directories_already_tried.insert(source_directory);
294
295 // Try to remove the session directory we just allocated. We don't
296 // know if there's any garbage in it from the failed copy action.
297 if let Err(err) = safe_remove_dir_all(&session_dir) {
298 sess.warn(&format!(
299 "Failed to delete partly initialized \
300 session dir `{}`: {}",
301 session_dir.display(),
302 err
303 ));
304 }
305
306 delete_session_dir_lock_file(sess, &lock_file_path);
307 mem::drop(directory_lock);
308 }
309 }
310 }
311
312 /// This function finalizes and thus 'publishes' the session directory by
313 /// renaming it to `s-{timestamp}-{svh}` and releasing the file lock.
314 /// If there have been compilation errors, however, this function will just
315 /// delete the presumably invalid session directory.
316 pub fn finalize_session_directory(sess: &Session, svh: Svh) {
317 if sess.opts.incremental.is_none() {
318 return;
319 }
320
321 let _timer = sess.timer("incr_comp_finalize_session_directory");
322
323 let incr_comp_session_dir: PathBuf = sess.incr_comp_session_dir().clone();
324
325 if sess.has_errors_or_delayed_span_bugs() {
326 // If there have been any errors during compilation, we don't want to
327 // publish this session directory. Rather, we'll just delete it.
328
329 debug!(
330 "finalize_session_directory() - invalidating session directory: {}",
331 incr_comp_session_dir.display()
332 );
333
334 if let Err(err) = safe_remove_dir_all(&*incr_comp_session_dir) {
335 sess.warn(&format!(
336 "Error deleting incremental compilation \
337 session directory `{}`: {}",
338 incr_comp_session_dir.display(),
339 err
340 ));
341 }
342
343 let lock_file_path = lock_file_path(&*incr_comp_session_dir);
344 delete_session_dir_lock_file(sess, &lock_file_path);
345 sess.mark_incr_comp_session_as_invalid();
346 }
347
348 debug!("finalize_session_directory() - session directory: {}", incr_comp_session_dir.display());
349
350 let old_sub_dir_name = incr_comp_session_dir.file_name().unwrap().to_string_lossy();
351 assert_no_characters_lost(&old_sub_dir_name);
352
353 // Keep the 's-{timestamp}-{random-number}' prefix, but replace the
354 // '-working' part with the SVH of the crate
355 let dash_indices: Vec<_> = old_sub_dir_name.match_indices('-').map(|(idx, _)| idx).collect();
356 if dash_indices.len() != 3 {
357 bug!(
358 "Encountered incremental compilation session directory with \
359 malformed name: {}",
360 incr_comp_session_dir.display()
361 )
362 }
363
364 // State: "s-{timestamp}-{random-number}-"
365 let mut new_sub_dir_name = String::from(&old_sub_dir_name[..=dash_indices[2]]);
366
367 // Append the svh
368 base_n::push_str(svh.as_u64() as u128, INT_ENCODE_BASE, &mut new_sub_dir_name);
369
370 // Create the full path
371 let new_path = incr_comp_session_dir.parent().unwrap().join(new_sub_dir_name);
372 debug!("finalize_session_directory() - new path: {}", new_path.display());
373
374 match rename_path_with_retry(&*incr_comp_session_dir, &new_path, 3) {
375 Ok(_) => {
376 debug!("finalize_session_directory() - directory renamed successfully");
377
378 // This unlocks the directory
379 sess.finalize_incr_comp_session(new_path);
380 }
381 Err(e) => {
382 // Warn about the error. However, no need to abort compilation now.
383 sess.warn(&format!(
384 "Error finalizing incremental compilation \
385 session directory `{}`: {}",
386 incr_comp_session_dir.display(),
387 e
388 ));
389
390 debug!("finalize_session_directory() - error, marking as invalid");
391 // Drop the file lock, so we can garage collect
392 sess.mark_incr_comp_session_as_invalid();
393 }
394 }
395
396 let _ = garbage_collect_session_directories(sess);
397 }
398
399 pub fn delete_all_session_dir_contents(sess: &Session) -> io::Result<()> {
400 let sess_dir_iterator = sess.incr_comp_session_dir().read_dir()?;
401 for entry in sess_dir_iterator {
402 let entry = entry?;
403 safe_remove_file(&entry.path())?
404 }
405 Ok(())
406 }
407
408 fn copy_files(sess: &Session, target_dir: &Path, source_dir: &Path) -> Result<bool, ()> {
409 // We acquire a shared lock on the lock file of the directory, so that
410 // nobody deletes it out from under us while we are reading from it.
411 let lock_file_path = lock_file_path(source_dir);
412
413 // not exclusive
414 let Ok(_lock) = flock::Lock::new(
415 &lock_file_path,
416 false, // don't wait,
417 false, // don't create
418 false,
419 ) else {
420 // Could not acquire the lock, don't try to copy from here
421 return Err(());
422 };
423
424 let Ok(source_dir_iterator) = source_dir.read_dir() else {
425 return Err(());
426 };
427
428 let mut files_linked = 0;
429 let mut files_copied = 0;
430
431 for entry in source_dir_iterator {
432 match entry {
433 Ok(entry) => {
434 let file_name = entry.file_name();
435
436 let target_file_path = target_dir.join(file_name);
437 let source_path = entry.path();
438
439 debug!("copying into session dir: {}", source_path.display());
440 match link_or_copy(source_path, target_file_path) {
441 Ok(LinkOrCopy::Link) => files_linked += 1,
442 Ok(LinkOrCopy::Copy) => files_copied += 1,
443 Err(_) => return Err(()),
444 }
445 }
446 Err(_) => return Err(()),
447 }
448 }
449
450 if sess.opts.debugging_opts.incremental_info {
451 eprintln!(
452 "[incremental] session directory: \
453 {} files hard-linked",
454 files_linked
455 );
456 eprintln!(
457 "[incremental] session directory: \
458 {} files copied",
459 files_copied
460 );
461 }
462
463 Ok(files_linked > 0 || files_copied == 0)
464 }
465
466 /// Generates unique directory path of the form:
467 /// {crate_dir}/s-{timestamp}-{random-number}-working
468 fn generate_session_dir_path(crate_dir: &Path) -> PathBuf {
469 let timestamp = timestamp_to_string(SystemTime::now());
470 debug!("generate_session_dir_path: timestamp = {}", timestamp);
471 let random_number = thread_rng().next_u32();
472 debug!("generate_session_dir_path: random_number = {}", random_number);
473
474 let directory_name = format!(
475 "s-{}-{}-working",
476 timestamp,
477 base_n::encode(random_number as u128, INT_ENCODE_BASE)
478 );
479 debug!("generate_session_dir_path: directory_name = {}", directory_name);
480 let directory_path = crate_dir.join(directory_name);
481 debug!("generate_session_dir_path: directory_path = {}", directory_path.display());
482 directory_path
483 }
484
485 fn create_dir(sess: &Session, path: &Path, dir_tag: &str) -> Result<(), ErrorGuaranteed> {
486 match std_fs::create_dir_all(path) {
487 Ok(()) => {
488 debug!("{} directory created successfully", dir_tag);
489 Ok(())
490 }
491 Err(err) => {
492 let reported = sess.err(&format!(
493 "Could not create incremental compilation {} \
494 directory `{}`: {}",
495 dir_tag,
496 path.display(),
497 err
498 ));
499 Err(reported)
500 }
501 }
502 }
503
504 /// Allocate the lock-file and lock it.
505 fn lock_directory(
506 sess: &Session,
507 session_dir: &Path,
508 ) -> Result<(flock::Lock, PathBuf), ErrorGuaranteed> {
509 let lock_file_path = lock_file_path(session_dir);
510 debug!("lock_directory() - lock_file: {}", lock_file_path.display());
511
512 match flock::Lock::new(
513 &lock_file_path,
514 false, // don't wait
515 true, // create the lock file
516 true,
517 ) {
518 // the lock should be exclusive
519 Ok(lock) => Ok((lock, lock_file_path)),
520 Err(lock_err) => {
521 let mut err = sess.struct_err(&format!(
522 "incremental compilation: could not create \
523 session directory lock file: {}",
524 lock_err
525 ));
526 if flock::Lock::error_unsupported(&lock_err) {
527 err.note(&format!(
528 "the filesystem for the incremental path at {} \
529 does not appear to support locking, consider changing the \
530 incremental path to a filesystem that supports locking \
531 or disable incremental compilation",
532 session_dir.display()
533 ));
534 if std::env::var_os("CARGO").is_some() {
535 err.help(
536 "incremental compilation can be disabled by setting the \
537 environment variable CARGO_INCREMENTAL=0 (see \
538 https://doc.rust-lang.org/cargo/reference/profiles.html#incremental)",
539 );
540 err.help(
541 "the entire build directory can be changed to a different \
542 filesystem by setting the environment variable CARGO_TARGET_DIR \
543 to a different path (see \
544 https://doc.rust-lang.org/cargo/reference/config.html#buildtarget-dir)",
545 );
546 }
547 }
548 Err(err.emit())
549 }
550 }
551 }
552
553 fn delete_session_dir_lock_file(sess: &Session, lock_file_path: &Path) {
554 if let Err(err) = safe_remove_file(&lock_file_path) {
555 sess.warn(&format!(
556 "Error deleting lock file for incremental \
557 compilation session directory `{}`: {}",
558 lock_file_path.display(),
559 err
560 ));
561 }
562 }
563
564 /// Finds the most recent published session directory that is not in the
565 /// ignore-list.
566 fn find_source_directory(
567 crate_dir: &Path,
568 source_directories_already_tried: &FxHashSet<PathBuf>,
569 ) -> Option<PathBuf> {
570 let iter = crate_dir
571 .read_dir()
572 .unwrap() // FIXME
573 .filter_map(|e| e.ok().map(|e| e.path()));
574
575 find_source_directory_in_iter(iter, source_directories_already_tried)
576 }
577
578 fn find_source_directory_in_iter<I>(
579 iter: I,
580 source_directories_already_tried: &FxHashSet<PathBuf>,
581 ) -> Option<PathBuf>
582 where
583 I: Iterator<Item = PathBuf>,
584 {
585 let mut best_candidate = (UNIX_EPOCH, None);
586
587 for session_dir in iter {
588 debug!("find_source_directory_in_iter - inspecting `{}`", session_dir.display());
589
590 let directory_name = session_dir.file_name().unwrap().to_string_lossy();
591 assert_no_characters_lost(&directory_name);
592
593 if source_directories_already_tried.contains(&session_dir)
594 || !is_session_directory(&directory_name)
595 || !is_finalized(&directory_name)
596 {
597 debug!("find_source_directory_in_iter - ignoring");
598 continue;
599 }
600
601 let timestamp = extract_timestamp_from_session_dir(&directory_name).unwrap_or_else(|_| {
602 bug!("unexpected incr-comp session dir: {}", session_dir.display())
603 });
604
605 if timestamp > best_candidate.0 {
606 best_candidate = (timestamp, Some(session_dir.clone()));
607 }
608 }
609
610 best_candidate.1
611 }
612
613 fn is_finalized(directory_name: &str) -> bool {
614 !directory_name.ends_with("-working")
615 }
616
617 fn is_session_directory(directory_name: &str) -> bool {
618 directory_name.starts_with("s-") && !directory_name.ends_with(LOCK_FILE_EXT)
619 }
620
621 fn is_session_directory_lock_file(file_name: &str) -> bool {
622 file_name.starts_with("s-") && file_name.ends_with(LOCK_FILE_EXT)
623 }
624
625 fn extract_timestamp_from_session_dir(directory_name: &str) -> Result<SystemTime, ()> {
626 if !is_session_directory(directory_name) {
627 return Err(());
628 }
629
630 let dash_indices: Vec<_> = directory_name.match_indices('-').map(|(idx, _)| idx).collect();
631 if dash_indices.len() != 3 {
632 return Err(());
633 }
634
635 string_to_timestamp(&directory_name[dash_indices[0] + 1..dash_indices[1]])
636 }
637
638 fn timestamp_to_string(timestamp: SystemTime) -> String {
639 let duration = timestamp.duration_since(UNIX_EPOCH).unwrap();
640 let micros = duration.as_secs() * 1_000_000 + (duration.subsec_nanos() as u64) / 1000;
641 base_n::encode(micros as u128, INT_ENCODE_BASE)
642 }
643
644 fn string_to_timestamp(s: &str) -> Result<SystemTime, ()> {
645 let micros_since_unix_epoch = u64::from_str_radix(s, INT_ENCODE_BASE as u32);
646
647 if micros_since_unix_epoch.is_err() {
648 return Err(());
649 }
650
651 let micros_since_unix_epoch = micros_since_unix_epoch.unwrap();
652
653 let duration = Duration::new(
654 micros_since_unix_epoch / 1_000_000,
655 1000 * (micros_since_unix_epoch % 1_000_000) as u32,
656 );
657 Ok(UNIX_EPOCH + duration)
658 }
659
660 fn crate_path(sess: &Session, crate_name: &str, stable_crate_id: StableCrateId) -> PathBuf {
661 let incr_dir = sess.opts.incremental.as_ref().unwrap().clone();
662
663 let stable_crate_id = base_n::encode(stable_crate_id.to_u64() as u128, INT_ENCODE_BASE);
664
665 let crate_name = format!("{}-{}", crate_name, stable_crate_id);
666 incr_dir.join(crate_name)
667 }
668
669 fn assert_no_characters_lost(s: &str) {
670 if s.contains('\u{FFFD}') {
671 bug!("Could not losslessly convert '{}'.", s)
672 }
673 }
674
675 fn is_old_enough_to_be_collected(timestamp: SystemTime) -> bool {
676 timestamp < SystemTime::now() - Duration::from_secs(10)
677 }
678
679 /// Runs garbage collection for the current session.
680 pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
681 debug!("garbage_collect_session_directories() - begin");
682
683 let session_directory = sess.incr_comp_session_dir();
684 debug!(
685 "garbage_collect_session_directories() - session directory: {}",
686 session_directory.display()
687 );
688
689 let crate_directory = session_directory.parent().unwrap();
690 debug!(
691 "garbage_collect_session_directories() - crate directory: {}",
692 crate_directory.display()
693 );
694
695 // First do a pass over the crate directory, collecting lock files and
696 // session directories
697 let mut session_directories = FxHashSet::default();
698 let mut lock_files = FxHashSet::default();
699
700 for dir_entry in crate_directory.read_dir()? {
701 let Ok(dir_entry) = dir_entry else {
702 // Ignore any errors
703 continue;
704 };
705
706 let entry_name = dir_entry.file_name();
707 let entry_name = entry_name.to_string_lossy();
708
709 if is_session_directory_lock_file(&entry_name) {
710 assert_no_characters_lost(&entry_name);
711 lock_files.insert(entry_name.into_owned());
712 } else if is_session_directory(&entry_name) {
713 assert_no_characters_lost(&entry_name);
714 session_directories.insert(entry_name.into_owned());
715 } else {
716 // This is something we don't know, leave it alone
717 }
718 }
719
720 // Now map from lock files to session directories
721 let lock_file_to_session_dir: FxHashMap<String, Option<String>> = lock_files
722 .into_iter()
723 .map(|lock_file_name| {
724 assert!(lock_file_name.ends_with(LOCK_FILE_EXT));
725 let dir_prefix_end = lock_file_name.len() - LOCK_FILE_EXT.len();
726 let session_dir = {
727 let dir_prefix = &lock_file_name[0..dir_prefix_end];
728 session_directories.iter().find(|dir_name| dir_name.starts_with(dir_prefix))
729 };
730 (lock_file_name, session_dir.map(String::clone))
731 })
732 .collect();
733
734 // Delete all lock files, that don't have an associated directory. They must
735 // be some kind of leftover
736 for (lock_file_name, directory_name) in &lock_file_to_session_dir {
737 if directory_name.is_none() {
738 let Ok(timestamp) = extract_timestamp_from_session_dir(lock_file_name) else {
739 debug!(
740 "found lock-file with malformed timestamp: {}",
741 crate_directory.join(&lock_file_name).display()
742 );
743 // Ignore it
744 continue;
745 };
746
747 let lock_file_path = crate_directory.join(&**lock_file_name);
748
749 if is_old_enough_to_be_collected(timestamp) {
750 debug!(
751 "garbage_collect_session_directories() - deleting \
752 garbage lock file: {}",
753 lock_file_path.display()
754 );
755 delete_session_dir_lock_file(sess, &lock_file_path);
756 } else {
757 debug!(
758 "garbage_collect_session_directories() - lock file with \
759 no session dir not old enough to be collected: {}",
760 lock_file_path.display()
761 );
762 }
763 }
764 }
765
766 // Filter out `None` directories
767 let lock_file_to_session_dir: FxHashMap<String, String> = lock_file_to_session_dir
768 .into_iter()
769 .filter_map(|(lock_file_name, directory_name)| directory_name.map(|n| (lock_file_name, n)))
770 .collect();
771
772 // Delete all session directories that don't have a lock file.
773 for directory_name in session_directories {
774 if !lock_file_to_session_dir.values().any(|dir| *dir == directory_name) {
775 let path = crate_directory.join(directory_name);
776 if let Err(err) = safe_remove_dir_all(&path) {
777 sess.warn(&format!(
778 "Failed to garbage collect invalid incremental \
779 compilation session directory `{}`: {}",
780 path.display(),
781 err
782 ));
783 }
784 }
785 }
786
787 // Now garbage collect the valid session directories.
788 let mut deletion_candidates = vec![];
789
790 for (lock_file_name, directory_name) in &lock_file_to_session_dir {
791 debug!("garbage_collect_session_directories() - inspecting: {}", directory_name);
792
793 let Ok(timestamp) = extract_timestamp_from_session_dir(directory_name) else {
794 debug!(
795 "found session-dir with malformed timestamp: {}",
796 crate_directory.join(directory_name).display()
797 );
798 // Ignore it
799 continue;
800 };
801
802 if is_finalized(directory_name) {
803 let lock_file_path = crate_directory.join(lock_file_name);
804 match flock::Lock::new(
805 &lock_file_path,
806 false, // don't wait
807 false, // don't create the lock-file
808 true,
809 ) {
810 // get an exclusive lock
811 Ok(lock) => {
812 debug!(
813 "garbage_collect_session_directories() - \
814 successfully acquired lock"
815 );
816 debug!(
817 "garbage_collect_session_directories() - adding \
818 deletion candidate: {}",
819 directory_name
820 );
821
822 // Note that we are holding on to the lock
823 deletion_candidates.push((
824 timestamp,
825 crate_directory.join(directory_name),
826 Some(lock),
827 ));
828 }
829 Err(_) => {
830 debug!(
831 "garbage_collect_session_directories() - \
832 not collecting, still in use"
833 );
834 }
835 }
836 } else if is_old_enough_to_be_collected(timestamp) {
837 // When cleaning out "-working" session directories, i.e.
838 // session directories that might still be in use by another
839 // compiler instance, we only look a directories that are
840 // at least ten seconds old. This is supposed to reduce the
841 // chance of deleting a directory in the time window where
842 // the process has allocated the directory but has not yet
843 // acquired the file-lock on it.
844
845 // Try to acquire the directory lock. If we can't, it
846 // means that the owning process is still alive and we
847 // leave this directory alone.
848 let lock_file_path = crate_directory.join(lock_file_name);
849 match flock::Lock::new(
850 &lock_file_path,
851 false, // don't wait
852 false, // don't create the lock-file
853 true,
854 ) {
855 // get an exclusive lock
856 Ok(lock) => {
857 debug!(
858 "garbage_collect_session_directories() - \
859 successfully acquired lock"
860 );
861
862 delete_old(sess, &crate_directory.join(directory_name));
863
864 // Let's make it explicit that the file lock is released at this point,
865 // or rather, that we held on to it until here
866 mem::drop(lock);
867 }
868 Err(_) => {
869 debug!(
870 "garbage_collect_session_directories() - \
871 not collecting, still in use"
872 );
873 }
874 }
875 } else {
876 debug!(
877 "garbage_collect_session_directories() - not finalized, not \
878 old enough"
879 );
880 }
881 }
882
883 // Delete all but the most recent of the candidates
884 for (path, lock) in all_except_most_recent(deletion_candidates) {
885 debug!("garbage_collect_session_directories() - deleting `{}`", path.display());
886
887 if let Err(err) = safe_remove_dir_all(&path) {
888 sess.warn(&format!(
889 "Failed to garbage collect finalized incremental \
890 compilation session directory `{}`: {}",
891 path.display(),
892 err
893 ));
894 } else {
895 delete_session_dir_lock_file(sess, &lock_file_path(&path));
896 }
897
898 // Let's make it explicit that the file lock is released at this point,
899 // or rather, that we held on to it until here
900 mem::drop(lock);
901 }
902
903 Ok(())
904 }
905
906 fn delete_old(sess: &Session, path: &Path) {
907 debug!("garbage_collect_session_directories() - deleting `{}`", path.display());
908
909 if let Err(err) = safe_remove_dir_all(&path) {
910 sess.warn(&format!(
911 "Failed to garbage collect incremental compilation session directory `{}`: {}",
912 path.display(),
913 err
914 ));
915 } else {
916 delete_session_dir_lock_file(sess, &lock_file_path(&path));
917 }
918 }
919
920 fn all_except_most_recent(
921 deletion_candidates: Vec<(SystemTime, PathBuf, Option<flock::Lock>)>,
922 ) -> FxHashMap<PathBuf, Option<flock::Lock>> {
923 let most_recent = deletion_candidates.iter().map(|&(timestamp, ..)| timestamp).max();
924
925 if let Some(most_recent) = most_recent {
926 deletion_candidates
927 .into_iter()
928 .filter(|&(timestamp, ..)| timestamp != most_recent)
929 .map(|(_, path, lock)| (path, lock))
930 .collect()
931 } else {
932 FxHashMap::default()
933 }
934 }
935
936 /// Since paths of artifacts within session directories can get quite long, we
937 /// need to support deleting files with very long paths. The regular
938 /// WinApi functions only support paths up to 260 characters, however. In order
939 /// to circumvent this limitation, we canonicalize the path of the directory
940 /// before passing it to std::fs::remove_dir_all(). This will convert the path
941 /// into the '\\?\' format, which supports much longer paths.
942 fn safe_remove_dir_all(p: &Path) -> io::Result<()> {
943 let canonicalized = match std_fs::canonicalize(p) {
944 Ok(canonicalized) => canonicalized,
945 Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
946 Err(err) => return Err(err),
947 };
948
949 std_fs::remove_dir_all(canonicalized)
950 }
951
952 fn safe_remove_file(p: &Path) -> io::Result<()> {
953 let canonicalized = match std_fs::canonicalize(p) {
954 Ok(canonicalized) => canonicalized,
955 Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
956 Err(err) => return Err(err),
957 };
958
959 match std_fs::remove_file(canonicalized) {
960 Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(()),
961 result => result,
962 }
963 }
964
965 // On Windows the compiler would sometimes fail to rename the session directory because
966 // the OS thought something was still being accessed in it. So we retry a few times to give
967 // the OS time to catch up.
968 // See https://github.com/rust-lang/rust/issues/86929.
969 fn rename_path_with_retry(from: &Path, to: &Path, mut retries_left: usize) -> std::io::Result<()> {
970 loop {
971 match std_fs::rename(from, to) {
972 Ok(()) => return Ok(()),
973 Err(e) => {
974 if retries_left > 0 && e.kind() == ErrorKind::PermissionDenied {
975 // Try again after a short waiting period.
976 std::thread::sleep(Duration::from_millis(50));
977 retries_left -= 1;
978 } else {
979 return Err(e);
980 }
981 }
982 }
983 }
984 }