]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_incremental/src/persist/fs.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / compiler / rustc_incremental / src / persist / fs.rs
CommitLineData
9e0c209e
SL
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
dfeec247 106use rustc_data_structures::fx::{FxHashMap, FxHashSet};
b7449926 107use rustc_data_structures::svh::Svh;
dfeec247
XL
108use rustc_data_structures::{base_n, flock};
109use rustc_fs_util::{link_or_copy, LinkOrCopy};
ba9703b0 110use rustc_session::{CrateDisambiguator, Session};
9e0c209e 111
9e0c209e
SL
112use std::fs as std_fs;
113use std::io;
114use std::mem;
115use std::path::{Path, PathBuf};
dfeec247 116use std::time::{Duration, SystemTime, UNIX_EPOCH};
abe05a73 117
dfeec247 118use rand::{thread_rng, RngCore};
9e0c209e 119
416331ca
XL
120#[cfg(test)]
121mod tests;
122
0731742a
XL
123const LOCK_FILE_EXT: &str = ".lock";
124const DEP_GRAPH_FILENAME: &str = "dep-graph.bin";
cdc7bbd5 125const STAGING_DEP_GRAPH_FILENAME: &str = "dep-graph.part.bin";
0731742a
XL
126const WORK_PRODUCTS_FILENAME: &str = "work-products.bin";
127const QUERY_CACHE_FILENAME: &str = "query-cache.bin";
9e0c209e 128
476ff2be
SL
129// We encode integers using the following base, so they are shorter than decimal
130// or hexadecimal numbers (we want short file and directory names). Since these
131// numbers will be used in file names, we choose an encoding that is not
132// case-sensitive (as opposed to base64, for example).
ff7c6d11 133const INT_ENCODE_BASE: usize = base_n::CASE_INSENSITIVE;
476ff2be 134
9e0c209e
SL
135pub fn dep_graph_path(sess: &Session) -> PathBuf {
136 in_incr_comp_dir_sess(sess, DEP_GRAPH_FILENAME)
137}
cdc7bbd5
XL
138pub fn staging_dep_graph_path(sess: &Session) -> PathBuf {
139 in_incr_comp_dir_sess(sess, STAGING_DEP_GRAPH_FILENAME)
140}
ff7c6d11
XL
141pub fn dep_graph_path_from(incr_comp_session_dir: &Path) -> PathBuf {
142 in_incr_comp_dir(incr_comp_session_dir, DEP_GRAPH_FILENAME)
143}
9e0c209e
SL
144
145pub fn work_products_path(sess: &Session) -> PathBuf {
146 in_incr_comp_dir_sess(sess, WORK_PRODUCTS_FILENAME)
147}
148
abe05a73
XL
149pub fn query_cache_path(sess: &Session) -> PathBuf {
150 in_incr_comp_dir_sess(sess, QUERY_CACHE_FILENAME)
151}
152
9e0c209e
SL
153pub fn lock_file_path(session_dir: &Path) -> PathBuf {
154 let crate_dir = session_dir.parent().unwrap();
155
156 let directory_name = session_dir.file_name().unwrap().to_string_lossy();
157 assert_no_characters_lost(&directory_name);
158
74b04a01 159 let dash_indices: Vec<_> = directory_name.match_indices('-').map(|(idx, _)| idx).collect();
9e0c209e 160 if dash_indices.len() != 3 {
dfeec247
XL
161 bug!(
162 "Encountered incremental compilation session directory with \
9e0c209e 163 malformed name: {}",
dfeec247
XL
164 session_dir.display()
165 )
9e0c209e
SL
166 }
167
dfeec247 168 crate_dir.join(&directory_name[0..dash_indices[2]]).with_extension(&LOCK_FILE_EXT[1..])
9e0c209e
SL
169}
170
171pub fn in_incr_comp_dir_sess(sess: &Session, file_name: &str) -> PathBuf {
172 in_incr_comp_dir(&sess.incr_comp_session_dir(), file_name)
173}
174
175pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBuf {
176 incr_comp_session_dir.join(file_name)
177}
178
179/// Allocates the private session directory. The boolean in the Ok() result
180/// indicates whether we should try loading a dep graph from the successfully
181/// initialized directory, or not.
182/// The post-condition of this fn is that we have a valid incremental
183/// compilation session directory, if the result is `Ok`. A valid session
184/// directory is one that contains a locked lock file. It may or may not contain
185/// a dep-graph and work products from a previous session.
186/// If the call fails, the fn may leave behind an invalid session directory.
187/// The garbage collection will take care of it.
dfeec247
XL
188pub fn prepare_session_directory(
189 sess: &Session,
190 crate_name: &str,
191 crate_disambiguator: CrateDisambiguator,
192) {
ea8adc8c 193 if sess.opts.incremental.is_none() {
dfeec247 194 return;
ea8adc8c
XL
195 }
196
dfeec247
XL
197 let _timer = sess.timer("incr_comp_prepare_session_directory");
198
9e0c209e
SL
199 debug!("prepare_session_directory");
200
201 // {incr-comp-dir}/{crate-name-and-disambiguator}
ea8adc8c 202 let crate_dir = crate_path(sess, crate_name, crate_disambiguator);
9e0c209e 203 debug!("crate-dir: {}", crate_dir.display());
ea8adc8c 204 if create_dir(sess, &crate_dir, "crate").is_err() {
dfeec247 205 return;
ea8adc8c 206 }
9e0c209e 207
476ff2be
SL
208 // Hack: canonicalize the path *after creating the directory*
209 // because, on windows, long paths can cause problems;
210 // canonicalization inserts this weird prefix that makes windows
211 // tolerate long paths.
212 let crate_dir = match crate_dir.canonicalize() {
213 Ok(v) => v,
214 Err(err) => {
dfeec247
XL
215 sess.err(&format!(
216 "incremental compilation: error canonicalizing path `{}`: {}",
217 crate_dir.display(),
218 err
219 ));
220 return;
476ff2be
SL
221 }
222 };
223
0bf4aa26 224 let mut source_directories_already_tried = FxHashSet::default();
9e0c209e
SL
225
226 loop {
227 // Generate a session directory of the form:
228 //
229 // {incr-comp-dir}/{crate-name-and-disambiguator}/s-{timestamp}-{random}-working
230 let session_dir = generate_session_dir_path(&crate_dir);
231 debug!("session-dir: {}", session_dir.display());
232
233 // Lock the new session directory. If this fails, return an
234 // error without retrying
ea8adc8c
XL
235 let (directory_lock, lock_file_path) = match lock_directory(sess, &session_dir) {
236 Ok(e) => e,
237 Err(_) => return,
238 };
9e0c209e
SL
239
240 // Now that we have the lock, we can actually create the session
241 // directory
ea8adc8c 242 if create_dir(sess, &session_dir, "session").is_err() {
dfeec247 243 return;
ea8adc8c 244 }
9e0c209e
SL
245
246 // Find a suitable source directory to copy from. Ignore those that we
247 // have already tried before.
dfeec247 248 let source_directory = find_source_directory(&crate_dir, &source_directories_already_tried);
9e0c209e
SL
249
250 let source_directory = if let Some(dir) = source_directory {
251 dir
252 } else {
253 // There's nowhere to copy from, we're done
dfeec247
XL
254 debug!(
255 "no source directory found. Continuing with empty session \
256 directory."
257 );
9e0c209e 258
ea8adc8c 259 sess.init_incr_comp_session(session_dir, directory_lock, false);
dfeec247 260 return;
9e0c209e
SL
261 };
262
dfeec247 263 debug!("attempting to copy data from source: {}", source_directory.display());
9e0c209e
SL
264
265 // Try copying over all files from the source directory
dfeec247
XL
266 if let Ok(allows_links) = copy_files(sess, &session_dir, &source_directory) {
267 debug!("successfully copied data from: {}", source_directory.display());
9e0c209e 268
c30ab7b3 269 if !allows_links {
dfeec247
XL
270 sess.warn(&format!(
271 "Hard linking files in the incremental \
c30ab7b3
SL
272 compilation cache failed. Copying files \
273 instead. Consider moving the cache \
274 directory to a file system which supports \
275 hard linking in session dir `{}`",
dfeec247
XL
276 session_dir.display()
277 ));
c30ab7b3
SL
278 }
279
ea8adc8c 280 sess.init_incr_comp_session(session_dir, directory_lock, true);
dfeec247 281 return;
9e0c209e 282 } else {
dfeec247 283 debug!("copying failed - trying next directory");
9e0c209e
SL
284
285 // Something went wrong while trying to copy/link files from the
286 // source directory. Try again with a different one.
287 source_directories_already_tried.insert(source_directory);
288
289 // Try to remove the session directory we just allocated. We don't
290 // know if there's any garbage in it from the failed copy action.
291 if let Err(err) = safe_remove_dir_all(&session_dir) {
dfeec247
XL
292 sess.warn(&format!(
293 "Failed to delete partly initialized \
ea8adc8c 294 session dir `{}`: {}",
dfeec247
XL
295 session_dir.display(),
296 err
297 ));
9e0c209e
SL
298 }
299
ea8adc8c 300 delete_session_dir_lock_file(sess, &lock_file_path);
9e0c209e
SL
301 mem::drop(directory_lock);
302 }
303 }
304}
305
9e0c209e
SL
306/// This function finalizes and thus 'publishes' the session directory by
307/// renaming it to `s-{timestamp}-{svh}` and releasing the file lock.
308/// If there have been compilation errors, however, this function will just
309/// delete the presumably invalid session directory.
310pub fn finalize_session_directory(sess: &Session, svh: Svh) {
311 if sess.opts.incremental.is_none() {
312 return;
313 }
314
dfeec247
XL
315 let _timer = sess.timer("incr_comp_finalize_session_directory");
316
9e0c209e
SL
317 let incr_comp_session_dir: PathBuf = sess.incr_comp_session_dir().clone();
318
e74abb32 319 if sess.has_errors_or_delayed_span_bugs() {
9e0c209e
SL
320 // If there have been any errors during compilation, we don't want to
321 // publish this session directory. Rather, we'll just delete it.
322
dfeec247
XL
323 debug!(
324 "finalize_session_directory() - invalidating session directory: {}",
325 incr_comp_session_dir.display()
326 );
9e0c209e
SL
327
328 if let Err(err) = safe_remove_dir_all(&*incr_comp_session_dir) {
dfeec247
XL
329 sess.warn(&format!(
330 "Error deleting incremental compilation \
9e0c209e 331 session directory `{}`: {}",
dfeec247
XL
332 incr_comp_session_dir.display(),
333 err
334 ));
9e0c209e
SL
335 }
336
337 let lock_file_path = lock_file_path(&*incr_comp_session_dir);
338 delete_session_dir_lock_file(sess, &lock_file_path);
339 sess.mark_incr_comp_session_as_invalid();
340 }
341
dfeec247 342 debug!("finalize_session_directory() - session directory: {}", incr_comp_session_dir.display());
9e0c209e 343
dfeec247 344 let old_sub_dir_name = incr_comp_session_dir.file_name().unwrap().to_string_lossy();
9e0c209e
SL
345 assert_no_characters_lost(&old_sub_dir_name);
346
347 // Keep the 's-{timestamp}-{random-number}' prefix, but replace the
348 // '-working' part with the SVH of the crate
74b04a01 349 let dash_indices: Vec<_> = old_sub_dir_name.match_indices('-').map(|(idx, _)| idx).collect();
9e0c209e 350 if dash_indices.len() != 3 {
dfeec247
XL
351 bug!(
352 "Encountered incremental compilation session directory with \
9e0c209e 353 malformed name: {}",
dfeec247
XL
354 incr_comp_session_dir.display()
355 )
9e0c209e
SL
356 }
357
358 // State: "s-{timestamp}-{random-number}-"
dfeec247 359 let mut new_sub_dir_name = String::from(&old_sub_dir_name[..=dash_indices[2]]);
9e0c209e
SL
360
361 // Append the svh
ff7c6d11 362 base_n::push_str(svh.as_u64() as u128, INT_ENCODE_BASE, &mut new_sub_dir_name);
9e0c209e
SL
363
364 // Create the full path
365 let new_path = incr_comp_session_dir.parent().unwrap().join(new_sub_dir_name);
366 debug!("finalize_session_directory() - new path: {}", new_path.display());
367
368 match std_fs::rename(&*incr_comp_session_dir, &new_path) {
369 Ok(_) => {
370 debug!("finalize_session_directory() - directory renamed successfully");
371
372 // This unlocks the directory
373 sess.finalize_incr_comp_session(new_path);
374 }
375 Err(e) => {
376 // Warn about the error. However, no need to abort compilation now.
dfeec247
XL
377 sess.warn(&format!(
378 "Error finalizing incremental compilation \
9e0c209e 379 session directory `{}`: {}",
dfeec247
XL
380 incr_comp_session_dir.display(),
381 e
382 ));
9e0c209e
SL
383
384 debug!("finalize_session_directory() - error, marking as invalid");
385 // Drop the file lock, so we can garage collect
386 sess.mark_incr_comp_session_as_invalid();
387 }
388 }
389
390 let _ = garbage_collect_session_directories(sess);
391}
392
393pub fn delete_all_session_dir_contents(sess: &Session) -> io::Result<()> {
394 let sess_dir_iterator = sess.incr_comp_session_dir().read_dir()?;
395 for entry in sess_dir_iterator {
396 let entry = entry?;
397 safe_remove_file(&entry.path())?
398 }
399 Ok(())
400}
401
dfeec247 402fn copy_files(sess: &Session, target_dir: &Path, source_dir: &Path) -> Result<bool, ()> {
9e0c209e
SL
403 // We acquire a shared lock on the lock file of the directory, so that
404 // nobody deletes it out from under us while we are reading from it.
405 let lock_file_path = lock_file_path(source_dir);
dfeec247
XL
406 let _lock = if let Ok(lock) = flock::Lock::new(
407 &lock_file_path,
408 false, // don't wait,
409 false, // don't create
410 false,
411 ) {
412 // not exclusive
9e0c209e
SL
413 lock
414 } else {
415 // Could not acquire the lock, don't try to copy from here
dfeec247 416 return Err(());
9e0c209e
SL
417 };
418
419 let source_dir_iterator = match source_dir.read_dir() {
420 Ok(it) => it,
dfeec247 421 Err(_) => return Err(()),
9e0c209e
SL
422 };
423
424 let mut files_linked = 0;
425 let mut files_copied = 0;
426
427 for entry in source_dir_iterator {
428 match entry {
429 Ok(entry) => {
430 let file_name = entry.file_name();
431
432 let target_file_path = target_dir.join(file_name);
433 let source_path = entry.path();
434
435 debug!("copying into session dir: {}", source_path.display());
b7449926 436 match link_or_copy(source_path, target_file_path) {
dfeec247
XL
437 Ok(LinkOrCopy::Link) => files_linked += 1,
438 Ok(LinkOrCopy::Copy) => files_copied += 1,
439 Err(_) => return Err(()),
9e0c209e
SL
440 }
441 }
dfeec247 442 Err(_) => return Err(()),
9e0c209e
SL
443 }
444 }
445
abe05a73 446 if sess.opts.debugging_opts.incremental_info {
6a06907d 447 eprintln!(
dfeec247
XL
448 "[incremental] session directory: \
449 {} files hard-linked",
450 files_linked
451 );
6a06907d 452 eprintln!(
dfeec247
XL
453 "[incremental] session directory: \
454 {} files copied",
455 files_copied
456 );
9e0c209e
SL
457 }
458
c30ab7b3 459 Ok(files_linked > 0 || files_copied == 0)
9e0c209e
SL
460}
461
9fa01778 462/// Generates unique directory path of the form:
9e0c209e
SL
463/// {crate_dir}/s-{timestamp}-{random-number}-working
464fn generate_session_dir_path(crate_dir: &Path) -> PathBuf {
465 let timestamp = timestamp_to_string(SystemTime::now());
466 debug!("generate_session_dir_path: timestamp = {}", timestamp);
467 let random_number = thread_rng().next_u32();
468 debug!("generate_session_dir_path: random_number = {}", random_number);
469
dfeec247
XL
470 let directory_name = format!(
471 "s-{}-{}-working",
472 timestamp,
473 base_n::encode(random_number as u128, INT_ENCODE_BASE)
474 );
9e0c209e
SL
475 debug!("generate_session_dir_path: directory_name = {}", directory_name);
476 let directory_path = crate_dir.join(directory_name);
477 debug!("generate_session_dir_path: directory_path = {}", directory_path.display());
478 directory_path
479}
480
dfeec247 481fn create_dir(sess: &Session, path: &Path, dir_tag: &str) -> Result<(), ()> {
cc61c64b 482 match std_fs::create_dir_all(path) {
9e0c209e
SL
483 Ok(()) => {
484 debug!("{} directory created successfully", dir_tag);
485 Ok(())
486 }
487 Err(err) => {
dfeec247
XL
488 sess.err(&format!(
489 "Could not create incremental compilation {} \
9e0c209e 490 directory `{}`: {}",
dfeec247
XL
491 dir_tag,
492 path.display(),
493 err
494 ));
9e0c209e
SL
495 Err(())
496 }
497 }
498}
499
a1dfa0c6 500/// Allocate the lock-file and lock it.
dfeec247 501fn lock_directory(sess: &Session, session_dir: &Path) -> Result<(flock::Lock, PathBuf), ()> {
9e0c209e
SL
502 let lock_file_path = lock_file_path(session_dir);
503 debug!("lock_directory() - lock_file: {}", lock_file_path.display());
504
dfeec247
XL
505 match flock::Lock::new(
506 &lock_file_path,
507 false, // don't wait
508 true, // create the lock file
509 true,
510 ) {
511 // the lock should be exclusive
9e0c209e
SL
512 Ok(lock) => Ok((lock, lock_file_path)),
513 Err(err) => {
dfeec247
XL
514 sess.err(&format!(
515 "incremental compilation: could not create \
516 session directory lock file: {}",
517 err
518 ));
9e0c209e
SL
519 Err(())
520 }
521 }
522}
523
dfeec247 524fn delete_session_dir_lock_file(sess: &Session, lock_file_path: &Path) {
9e0c209e 525 if let Err(err) = safe_remove_file(&lock_file_path) {
dfeec247
XL
526 sess.warn(&format!(
527 "Error deleting lock file for incremental \
9e0c209e 528 compilation session directory `{}`: {}",
dfeec247
XL
529 lock_file_path.display(),
530 err
531 ));
9e0c209e
SL
532 }
533}
534
9fa01778 535/// Finds the most recent published session directory that is not in the
9e0c209e 536/// ignore-list.
dfeec247
XL
537fn find_source_directory(
538 crate_dir: &Path,
539 source_directories_already_tried: &FxHashSet<PathBuf>,
540) -> Option<PathBuf> {
541 let iter = crate_dir
542 .read_dir()
543 .unwrap() // FIXME
544 .filter_map(|e| e.ok().map(|e| e.path()));
9e0c209e
SL
545
546 find_source_directory_in_iter(iter, source_directories_already_tried)
547}
548
dfeec247
XL
549fn find_source_directory_in_iter<I>(
550 iter: I,
551 source_directories_already_tried: &FxHashSet<PathBuf>,
552) -> Option<PathBuf>
553where
554 I: Iterator<Item = PathBuf>,
9e0c209e
SL
555{
556 let mut best_candidate = (UNIX_EPOCH, None);
557
558 for session_dir in iter {
dfeec247 559 debug!("find_source_directory_in_iter - inspecting `{}`", session_dir.display());
9e0c209e
SL
560
561 let directory_name = session_dir.file_name().unwrap().to_string_lossy();
562 assert_no_characters_lost(&directory_name);
563
dfeec247
XL
564 if source_directories_already_tried.contains(&session_dir)
565 || !is_session_directory(&directory_name)
566 || !is_finalized(&directory_name)
567 {
416331ca 568 debug!("find_source_directory_in_iter - ignoring");
dfeec247 569 continue;
9e0c209e
SL
570 }
571
dfeec247
XL
572 let timestamp = extract_timestamp_from_session_dir(&directory_name).unwrap_or_else(|_| {
573 bug!("unexpected incr-comp session dir: {}", session_dir.display())
574 });
9e0c209e
SL
575
576 if timestamp > best_candidate.0 {
577 best_candidate = (timestamp, Some(session_dir.clone()));
578 }
579 }
580
581 best_candidate.1
582}
583
584fn is_finalized(directory_name: &str) -> bool {
585 !directory_name.ends_with("-working")
586}
587
588fn is_session_directory(directory_name: &str) -> bool {
dfeec247 589 directory_name.starts_with("s-") && !directory_name.ends_with(LOCK_FILE_EXT)
9e0c209e
SL
590}
591
592fn is_session_directory_lock_file(file_name: &str) -> bool {
593 file_name.starts_with("s-") && file_name.ends_with(LOCK_FILE_EXT)
594}
595
dfeec247 596fn extract_timestamp_from_session_dir(directory_name: &str) -> Result<SystemTime, ()> {
9e0c209e 597 if !is_session_directory(directory_name) {
dfeec247 598 return Err(());
9e0c209e
SL
599 }
600
74b04a01 601 let dash_indices: Vec<_> = directory_name.match_indices('-').map(|(idx, _)| idx).collect();
9e0c209e 602 if dash_indices.len() != 3 {
dfeec247 603 return Err(());
9e0c209e
SL
604 }
605
dfeec247 606 string_to_timestamp(&directory_name[dash_indices[0] + 1..dash_indices[1]])
9e0c209e
SL
607}
608
9e0c209e
SL
609fn timestamp_to_string(timestamp: SystemTime) -> String {
610 let duration = timestamp.duration_since(UNIX_EPOCH).unwrap();
dfeec247 611 let micros = duration.as_secs() * 1_000_000 + (duration.subsec_nanos() as u64) / 1000;
ff7c6d11 612 base_n::encode(micros as u128, INT_ENCODE_BASE)
9e0c209e
SL
613}
614
615fn string_to_timestamp(s: &str) -> Result<SystemTime, ()> {
2c00a5a8 616 let micros_since_unix_epoch = u64::from_str_radix(s, INT_ENCODE_BASE as u32);
9e0c209e
SL
617
618 if micros_since_unix_epoch.is_err() {
dfeec247 619 return Err(());
9e0c209e
SL
620 }
621
622 let micros_since_unix_epoch = micros_since_unix_epoch.unwrap();
623
dfeec247
XL
624 let duration = Duration::new(
625 micros_since_unix_epoch / 1_000_000,
626 1000 * (micros_since_unix_epoch % 1_000_000) as u32,
627 );
9e0c209e
SL
628 Ok(UNIX_EPOCH + duration)
629}
630
dfeec247
XL
631fn crate_path(
632 sess: &Session,
633 crate_name: &str,
634 crate_disambiguator: CrateDisambiguator,
635) -> PathBuf {
9e0c209e
SL
636 let incr_dir = sess.opts.incremental.as_ref().unwrap().clone();
637
abe05a73 638 // The full crate disambiguator is really long. 64 bits of it should be
9e0c209e 639 // sufficient.
abe05a73 640 let crate_disambiguator = crate_disambiguator.to_fingerprint().to_smaller_hash();
dfeec247 641 let crate_disambiguator = base_n::encode(crate_disambiguator as u128, INT_ENCODE_BASE);
9e0c209e 642
abe05a73 643 let crate_name = format!("{}-{}", crate_name, crate_disambiguator);
9e0c209e
SL
644 incr_dir.join(crate_name)
645}
646
647fn assert_no_characters_lost(s: &str) {
648 if s.contains('\u{FFFD}') {
649 bug!("Could not losslessly convert '{}'.", s)
650 }
651}
652
653fn is_old_enough_to_be_collected(timestamp: SystemTime) -> bool {
654 timestamp < SystemTime::now() - Duration::from_secs(10)
655}
656
657pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
658 debug!("garbage_collect_session_directories() - begin");
659
660 let session_directory = sess.incr_comp_session_dir();
dfeec247
XL
661 debug!(
662 "garbage_collect_session_directories() - session directory: {}",
663 session_directory.display()
664 );
9e0c209e
SL
665
666 let crate_directory = session_directory.parent().unwrap();
dfeec247
XL
667 debug!(
668 "garbage_collect_session_directories() - crate directory: {}",
669 crate_directory.display()
670 );
9e0c209e
SL
671
672 // First do a pass over the crate directory, collecting lock files and
673 // session directories
0bf4aa26
XL
674 let mut session_directories = FxHashSet::default();
675 let mut lock_files = FxHashSet::default();
9e0c209e 676
a1dfa0c6 677 for dir_entry in crate_directory.read_dir()? {
9e0c209e
SL
678 let dir_entry = match dir_entry {
679 Ok(dir_entry) => dir_entry,
680 _ => {
681 // Ignore any errors
dfeec247 682 continue;
9e0c209e
SL
683 }
684 };
685
686 let entry_name = dir_entry.file_name();
687 let entry_name = entry_name.to_string_lossy();
688
689 if is_session_directory_lock_file(&entry_name) {
690 assert_no_characters_lost(&entry_name);
691 lock_files.insert(entry_name.into_owned());
692 } else if is_session_directory(&entry_name) {
693 assert_no_characters_lost(&entry_name);
694 session_directories.insert(entry_name.into_owned());
695 } else {
696 // This is something we don't know, leave it alone
697 }
698 }
699
700 // Now map from lock files to session directories
dfeec247
XL
701 let lock_file_to_session_dir: FxHashMap<String, Option<String>> = lock_files
702 .into_iter()
703 .map(|lock_file_name| {
704 assert!(lock_file_name.ends_with(LOCK_FILE_EXT));
705 let dir_prefix_end = lock_file_name.len() - LOCK_FILE_EXT.len();
706 let session_dir = {
707 let dir_prefix = &lock_file_name[0..dir_prefix_end];
708 session_directories.iter().find(|dir_name| dir_name.starts_with(dir_prefix))
709 };
710 (lock_file_name, session_dir.map(String::clone))
711 })
712 .collect();
9e0c209e
SL
713
714 // Delete all lock files, that don't have an associated directory. They must
715 // be some kind of leftover
716 for (lock_file_name, directory_name) in &lock_file_to_session_dir {
717 if directory_name.is_none() {
718 let timestamp = match extract_timestamp_from_session_dir(lock_file_name) {
719 Ok(timestamp) => timestamp,
720 Err(()) => {
dfeec247
XL
721 debug!(
722 "found lock-file with malformed timestamp: {}",
723 crate_directory.join(&lock_file_name).display()
724 );
9e0c209e 725 // Ignore it
dfeec247 726 continue;
9e0c209e
SL
727 }
728 };
729
730 let lock_file_path = crate_directory.join(&**lock_file_name);
731
732 if is_old_enough_to_be_collected(timestamp) {
dfeec247
XL
733 debug!(
734 "garbage_collect_session_directories() - deleting \
735 garbage lock file: {}",
736 lock_file_path.display()
737 );
9e0c209e
SL
738 delete_session_dir_lock_file(sess, &lock_file_path);
739 } else {
dfeec247
XL
740 debug!(
741 "garbage_collect_session_directories() - lock file with \
9e0c209e 742 no session dir not old enough to be collected: {}",
dfeec247
XL
743 lock_file_path.display()
744 );
9e0c209e
SL
745 }
746 }
747 }
748
749 // Filter out `None` directories
dfeec247
XL
750 let lock_file_to_session_dir: FxHashMap<String, String> = lock_file_to_session_dir
751 .into_iter()
752 .filter_map(|(lock_file_name, directory_name)| directory_name.map(|n| (lock_file_name, n)))
753 .collect();
9e0c209e 754
2c00a5a8
XL
755 // Delete all session directories that don't have a lock file.
756 for directory_name in session_directories {
757 if !lock_file_to_session_dir.values().any(|dir| *dir == directory_name) {
758 let path = crate_directory.join(directory_name);
759 if let Err(err) = safe_remove_dir_all(&path) {
dfeec247
XL
760 sess.warn(&format!(
761 "Failed to garbage collect invalid incremental \
2c00a5a8 762 compilation session directory `{}`: {}",
dfeec247
XL
763 path.display(),
764 err
765 ));
2c00a5a8
XL
766 }
767 }
768 }
769
770 // Now garbage collect the valid session directories.
9e0c209e 771 let mut deletion_candidates = vec![];
9e0c209e
SL
772
773 for (lock_file_name, directory_name) in &lock_file_to_session_dir {
dfeec247 774 debug!("garbage_collect_session_directories() - inspecting: {}", directory_name);
9e0c209e
SL
775
776 let timestamp = match extract_timestamp_from_session_dir(directory_name) {
777 Ok(timestamp) => timestamp,
778 Err(()) => {
dfeec247
XL
779 debug!(
780 "found session-dir with malformed timestamp: {}",
781 crate_directory.join(directory_name).display()
782 );
9e0c209e 783 // Ignore it
dfeec247 784 continue;
9e0c209e
SL
785 }
786 };
787
788 if is_finalized(directory_name) {
789 let lock_file_path = crate_directory.join(lock_file_name);
dfeec247
XL
790 match flock::Lock::new(
791 &lock_file_path,
792 false, // don't wait
793 false, // don't create the lock-file
794 true,
795 ) {
796 // get an exclusive lock
9e0c209e 797 Ok(lock) => {
dfeec247
XL
798 debug!(
799 "garbage_collect_session_directories() - \
800 successfully acquired lock"
801 );
802 debug!(
803 "garbage_collect_session_directories() - adding \
804 deletion candidate: {}",
805 directory_name
806 );
9e0c209e
SL
807
808 // Note that we are holding on to the lock
dfeec247
XL
809 deletion_candidates.push((
810 timestamp,
811 crate_directory.join(directory_name),
812 Some(lock),
813 ));
9e0c209e
SL
814 }
815 Err(_) => {
dfeec247
XL
816 debug!(
817 "garbage_collect_session_directories() - \
818 not collecting, still in use"
819 );
9e0c209e
SL
820 }
821 }
822 } else if is_old_enough_to_be_collected(timestamp) {
823 // When cleaning out "-working" session directories, i.e.
824 // session directories that might still be in use by another
825 // compiler instance, we only look a directories that are
826 // at least ten seconds old. This is supposed to reduce the
827 // chance of deleting a directory in the time window where
828 // the process has allocated the directory but has not yet
829 // acquired the file-lock on it.
830
831 // Try to acquire the directory lock. If we can't, it
832 // means that the owning process is still alive and we
833 // leave this directory alone.
834 let lock_file_path = crate_directory.join(lock_file_name);
dfeec247
XL
835 match flock::Lock::new(
836 &lock_file_path,
837 false, // don't wait
838 false, // don't create the lock-file
839 true,
840 ) {
841 // get an exclusive lock
9e0c209e 842 Ok(lock) => {
dfeec247
XL
843 debug!(
844 "garbage_collect_session_directories() - \
845 successfully acquired lock"
846 );
9e0c209e 847
29967ef6
XL
848 delete_old(sess, &crate_directory.join(directory_name));
849
850 // Let's make it explicit that the file lock is released at this point,
851 // or rather, that we held on to it until here
852 mem::drop(lock);
9e0c209e
SL
853 }
854 Err(_) => {
dfeec247
XL
855 debug!(
856 "garbage_collect_session_directories() - \
857 not collecting, still in use"
858 );
9e0c209e
SL
859 }
860 }
861 } else {
dfeec247
XL
862 debug!(
863 "garbage_collect_session_directories() - not finalized, not \
864 old enough"
865 );
9e0c209e
SL
866 }
867 }
868
869 // Delete all but the most recent of the candidates
870 for (path, lock) in all_except_most_recent(deletion_candidates) {
dfeec247 871 debug!("garbage_collect_session_directories() - deleting `{}`", path.display());
9e0c209e
SL
872
873 if let Err(err) = safe_remove_dir_all(&path) {
dfeec247
XL
874 sess.warn(&format!(
875 "Failed to garbage collect finalized incremental \
9e0c209e 876 compilation session directory `{}`: {}",
dfeec247
XL
877 path.display(),
878 err
879 ));
9e0c209e
SL
880 } else {
881 delete_session_dir_lock_file(sess, &lock_file_path(&path));
882 }
883
9e0c209e
SL
884 // Let's make it explicit that the file lock is released at this point,
885 // or rather, that we held on to it until here
886 mem::drop(lock);
887 }
888
29967ef6
XL
889 Ok(())
890}
9e0c209e 891
29967ef6
XL
892fn delete_old(sess: &Session, path: &Path) {
893 debug!("garbage_collect_session_directories() - deleting `{}`", path.display());
9e0c209e 894
29967ef6
XL
895 if let Err(err) = safe_remove_dir_all(&path) {
896 sess.warn(&format!(
897 "Failed to garbage collect incremental compilation session directory `{}`: {}",
898 path.display(),
899 err
900 ));
901 } else {
902 delete_session_dir_lock_file(sess, &lock_file_path(&path));
9e0c209e 903 }
9e0c209e
SL
904}
905
dfeec247
XL
906fn all_except_most_recent(
907 deletion_candidates: Vec<(SystemTime, PathBuf, Option<flock::Lock>)>,
908) -> FxHashMap<PathBuf, Option<flock::Lock>> {
909 let most_recent = deletion_candidates.iter().map(|&(timestamp, ..)| timestamp).max();
9e0c209e
SL
910
911 if let Some(most_recent) = most_recent {
dfeec247
XL
912 deletion_candidates
913 .into_iter()
914 .filter(|&(timestamp, ..)| timestamp != most_recent)
915 .map(|(_, path, lock)| (path, lock))
916 .collect()
9e0c209e 917 } else {
0bf4aa26 918 FxHashMap::default()
9e0c209e
SL
919 }
920}
921
922/// Since paths of artifacts within session directories can get quite long, we
923/// need to support deleting files with very long paths. The regular
924/// WinApi functions only support paths up to 260 characters, however. In order
925/// to circumvent this limitation, we canonicalize the path of the directory
926/// before passing it to std::fs::remove_dir_all(). This will convert the path
927/// into the '\\?\' format, which supports much longer paths.
928fn safe_remove_dir_all(p: &Path) -> io::Result<()> {
5869c6ff
XL
929 let canonicalized = match std_fs::canonicalize(p) {
930 Ok(canonicalized) => canonicalized,
931 Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
932 Err(err) => return Err(err),
933 };
934
935 std_fs::remove_dir_all(canonicalized)
9e0c209e
SL
936}
937
938fn safe_remove_file(p: &Path) -> io::Result<()> {
5869c6ff
XL
939 let canonicalized = match std_fs::canonicalize(p) {
940 Ok(canonicalized) => canonicalized,
941 Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
942 Err(err) => return Err(err),
943 };
944
945 match std_fs::remove_file(canonicalized) {
946 Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(()),
947 result => result,
9e0c209e
SL
948 }
949}