45
45
//! that are consistent with the state of the source code it was compiled
46
46
//! from, with no need to change them ever again. At this point, the compiler
47
47
//! finalizes and "publishes" its private session directory by renaming it
48
- //! from "sess -{timestamp}-{random}-working" to "sess -{timestamp}-{SVH}".
48
+ //! from "s -{timestamp}-{random}-working" to "s -{timestamp}-{SVH}".
49
49
//! 6. At this point the "old" session directory that we copied our data from
50
50
//! at the beginning of the session has become obsolete because we have just
51
51
//! published a more current version. Thus the compiler will delete it.
@@ -201,7 +201,7 @@ pub fn prepare_session_directory(tcx: TyCtxt) -> Result<bool, ()> {
201
201
loop {
202
202
// Generate a session directory of the form:
203
203
//
204
- // {incr-comp-dir}/{crate-name-and-disambiguator}/sess -{timestamp}-{random}-working
204
+ // {incr-comp-dir}/{crate-name-and-disambiguator}/s -{timestamp}-{random}-working
205
205
let session_dir = generate_session_dir_path ( & crate_dir) ;
206
206
debug ! ( "session-dir: {}" , session_dir. display( ) ) ;
207
207
@@ -265,7 +265,7 @@ pub fn prepare_session_directory(tcx: TyCtxt) -> Result<bool, ()> {
265
265
266
266
267
267
/// This function finalizes and thus 'publishes' the session directory by
268
- /// renaming it to `sess -{timestamp}-{svh}` and releasing the file lock.
268
+ /// renaming it to `s -{timestamp}-{svh}` and releasing the file lock.
269
269
/// If there have been compilation errors, however, this function will just
270
270
/// delete the presumably invalid session directory.
271
271
pub fn finalize_session_directory ( sess : & Session , svh : Svh ) {
@@ -302,7 +302,7 @@ pub fn finalize_session_directory(sess: &Session, svh: Svh) {
302
302
. to_string_lossy ( ) ;
303
303
assert_no_characters_lost ( & old_sub_dir_name) ;
304
304
305
- // Keep the 'sess -{timestamp}-{random-number}' prefix, but replace the
305
+ // Keep the 's -{timestamp}-{random-number}' prefix, but replace the
306
306
// '-working' part with the SVH of the crate
307
307
let dash_indices: Vec < _ > = old_sub_dir_name. match_indices ( "-" )
308
308
. map ( |( idx, _) | idx)
@@ -313,11 +313,11 @@ pub fn finalize_session_directory(sess: &Session, svh: Svh) {
313
313
incr_comp_session_dir. display( ) )
314
314
}
315
315
316
- // State: "sess -{timestamp}-{random-number}-"
316
+ // State: "s -{timestamp}-{random-number}-"
317
317
let mut new_sub_dir_name = String :: from ( & old_sub_dir_name[ .. dash_indices[ 2 ] + 1 ] ) ;
318
318
319
319
// Append the svh
320
- new_sub_dir_name. push_str ( & svh. to_string ( ) ) ;
320
+ new_sub_dir_name. push_str ( & encode_base_36 ( svh. as_u64 ( ) ) ) ;
321
321
322
322
// Create the full path
323
323
let new_path = incr_comp_session_dir. parent ( ) . unwrap ( ) . join ( new_sub_dir_name) ;
@@ -405,14 +405,16 @@ fn copy_files(target_dir: &Path,
405
405
}
406
406
407
407
/// Generate unique directory path of the form:
408
- /// {crate_dir}/sess -{timestamp}-{random-number}-working
408
+ /// {crate_dir}/s -{timestamp}-{random-number}-working
409
409
fn generate_session_dir_path ( crate_dir : & Path ) -> PathBuf {
410
410
let timestamp = timestamp_to_string ( SystemTime :: now ( ) ) ;
411
411
debug ! ( "generate_session_dir_path: timestamp = {}" , timestamp) ;
412
412
let random_number = thread_rng ( ) . next_u32 ( ) ;
413
413
debug ! ( "generate_session_dir_path: random_number = {}" , random_number) ;
414
414
415
- let directory_name = format ! ( "sess-{}-{:x}-working" , timestamp, random_number) ;
415
+ let directory_name = format ! ( "s-{}-{}-working" ,
416
+ timestamp,
417
+ encode_base_36( random_number as u64 ) ) ;
416
418
debug ! ( "generate_session_dir_path: directory_name = {}" , directory_name) ;
417
419
let directory_path = crate_dir. join ( directory_name) ;
418
420
debug ! ( "generate_session_dir_path: directory_path = {}" , directory_path. display( ) ) ;
@@ -517,12 +519,12 @@ fn is_finalized(directory_name: &str) -> bool {
517
519
}
518
520
519
521
fn is_session_directory ( directory_name : & str ) -> bool {
520
- directory_name. starts_with ( "sess -" ) &&
522
+ directory_name. starts_with ( "s -" ) &&
521
523
!directory_name. ends_with ( LOCK_FILE_EXT )
522
524
}
523
525
524
526
fn is_session_directory_lock_file ( file_name : & str ) -> bool {
525
- file_name. starts_with ( "sess -" ) && file_name. ends_with ( LOCK_FILE_EXT )
527
+ file_name. starts_with ( "s -" ) && file_name. ends_with ( LOCK_FILE_EXT )
526
528
}
527
529
528
530
fn extract_timestamp_from_session_dir ( directory_name : & str )
@@ -541,15 +543,31 @@ fn extract_timestamp_from_session_dir(directory_name: &str)
541
543
string_to_timestamp ( & directory_name[ dash_indices[ 0 ] +1 .. dash_indices[ 1 ] ] )
542
544
}
543
545
546
+ const BASE_36 : & ' static [ u8 ] = b"0123456789abcdefghijklmnopqrstuvwxyz" ;
547
+
548
+ fn encode_base_36 ( mut n : u64 ) -> String {
549
+ let mut s = Vec :: with_capacity ( 13 ) ;
550
+ loop {
551
+ s. push ( BASE_36 [ ( n % 36 ) as usize ] ) ;
552
+ n /= 36 ;
553
+
554
+ if n == 0 {
555
+ break ;
556
+ }
557
+ }
558
+ s. reverse ( ) ;
559
+ String :: from_utf8 ( s) . unwrap ( )
560
+ }
561
+
544
562
fn timestamp_to_string ( timestamp : SystemTime ) -> String {
545
563
let duration = timestamp. duration_since ( UNIX_EPOCH ) . unwrap ( ) ;
546
564
let micros = duration. as_secs ( ) * 1_000_000 +
547
565
( duration. subsec_nanos ( ) as u64 ) / 1000 ;
548
- format ! ( "{:x}" , micros)
566
+ encode_base_36 ( micros)
549
567
}
550
568
551
569
fn string_to_timestamp ( s : & str ) -> Result < SystemTime , ( ) > {
552
- let micros_since_unix_epoch = u64:: from_str_radix ( s, 16 ) ;
570
+ let micros_since_unix_epoch = u64:: from_str_radix ( s, 36 ) ;
553
571
554
572
if micros_since_unix_epoch. is_err ( ) {
555
573
return Err ( ( ) )
@@ -591,7 +609,8 @@ pub fn find_metadata_hashes_for(tcx: TyCtxt, cnum: ast::CrateNum) -> Option<Path
591
609
}
592
610
} ;
593
611
594
- let target_svh = tcx. sess . cstore . crate_hash ( cnum) . to_string ( ) ;
612
+ let target_svh = tcx. sess . cstore . crate_hash ( cnum) ;
613
+ let target_svh = encode_base_36 ( target_svh. as_u64 ( ) ) ;
595
614
596
615
let sub_dir = find_metadata_hashes_iter ( & target_svh, dir_entries. filter_map ( |e| {
597
616
e. ok ( ) . map ( |e| e. file_name ( ) . to_string_lossy ( ) . into_owned ( ) )
@@ -638,7 +657,7 @@ fn crate_path(sess: &Session,
638
657
let mut hasher = SipHasher :: new ( ) ;
639
658
crate_disambiguator. hash ( & mut hasher) ;
640
659
641
- let crate_name = format ! ( "{}-{:x }" , crate_name, hasher. finish( ) ) ;
660
+ let crate_name = format ! ( "{}-{}" , crate_name, encode_base_36 ( hasher. finish( ) ) ) ;
642
661
incr_dir. join ( crate_name)
643
662
}
644
663
@@ -914,27 +933,27 @@ fn test_find_source_directory_in_iter() {
914
933
915
934
// Find newest
916
935
assert_eq ! ( find_source_directory_in_iter(
917
- vec![ PathBuf :: from( "crate-dir/sess -3234-0000-svh" ) ,
918
- PathBuf :: from( "crate-dir/sess -2234-0000-svh" ) ,
919
- PathBuf :: from( "crate-dir/sess -1234-0000-svh" ) ] . into_iter( ) , & already_visited) ,
920
- Some ( PathBuf :: from( "crate-dir/sess -3234-0000-svh" ) ) ) ;
936
+ vec![ PathBuf :: from( "crate-dir/s -3234-0000-svh" ) ,
937
+ PathBuf :: from( "crate-dir/s -2234-0000-svh" ) ,
938
+ PathBuf :: from( "crate-dir/s -1234-0000-svh" ) ] . into_iter( ) , & already_visited) ,
939
+ Some ( PathBuf :: from( "crate-dir/s -3234-0000-svh" ) ) ) ;
921
940
922
941
// Filter out "-working"
923
942
assert_eq ! ( find_source_directory_in_iter(
924
- vec![ PathBuf :: from( "crate-dir/sess -3234-0000-working" ) ,
925
- PathBuf :: from( "crate-dir/sess -2234-0000-svh" ) ,
926
- PathBuf :: from( "crate-dir/sess -1234-0000-svh" ) ] . into_iter( ) , & already_visited) ,
927
- Some ( PathBuf :: from( "crate-dir/sess -2234-0000-svh" ) ) ) ;
943
+ vec![ PathBuf :: from( "crate-dir/s -3234-0000-working" ) ,
944
+ PathBuf :: from( "crate-dir/s -2234-0000-svh" ) ,
945
+ PathBuf :: from( "crate-dir/s -1234-0000-svh" ) ] . into_iter( ) , & already_visited) ,
946
+ Some ( PathBuf :: from( "crate-dir/s -2234-0000-svh" ) ) ) ;
928
947
929
948
// Handle empty
930
949
assert_eq ! ( find_source_directory_in_iter( vec![ ] . into_iter( ) , & already_visited) ,
931
950
None ) ;
932
951
933
952
// Handle only working
934
953
assert_eq ! ( find_source_directory_in_iter(
935
- vec![ PathBuf :: from( "crate-dir/sess -3234-0000-working" ) ,
936
- PathBuf :: from( "crate-dir/sess -2234-0000-working" ) ,
937
- PathBuf :: from( "crate-dir/sess -1234-0000-working" ) ] . into_iter( ) , & already_visited) ,
954
+ vec![ PathBuf :: from( "crate-dir/s -3234-0000-working" ) ,
955
+ PathBuf :: from( "crate-dir/s -2234-0000-working" ) ,
956
+ PathBuf :: from( "crate-dir/s -1234-0000-working" ) ] . into_iter( ) , & already_visited) ,
938
957
None ) ;
939
958
}
940
959
@@ -943,36 +962,36 @@ fn test_find_metadata_hashes_iter()
943
962
{
944
963
assert_eq ! ( find_metadata_hashes_iter( "testsvh2" ,
945
964
vec![
946
- String :: from( "sess -timestamp1-testsvh1" ) ,
947
- String :: from( "sess -timestamp2-testsvh2" ) ,
948
- String :: from( "sess -timestamp3-testsvh3" ) ,
965
+ String :: from( "s -timestamp1-testsvh1" ) ,
966
+ String :: from( "s -timestamp2-testsvh2" ) ,
967
+ String :: from( "s -timestamp3-testsvh3" ) ,
949
968
] . into_iter( ) ) ,
950
- Some ( OsString :: from( "sess -timestamp2-testsvh2" ) )
969
+ Some ( OsString :: from( "s -timestamp2-testsvh2" ) )
951
970
) ;
952
971
953
972
assert_eq ! ( find_metadata_hashes_iter( "testsvh2" ,
954
973
vec![
955
- String :: from( "sess -timestamp1-testsvh1" ) ,
956
- String :: from( "sess -timestamp2-testsvh2" ) ,
974
+ String :: from( "s -timestamp1-testsvh1" ) ,
975
+ String :: from( "s -timestamp2-testsvh2" ) ,
957
976
String :: from( "invalid-name" ) ,
958
977
] . into_iter( ) ) ,
959
- Some ( OsString :: from( "sess -timestamp2-testsvh2" ) )
978
+ Some ( OsString :: from( "s -timestamp2-testsvh2" ) )
960
979
) ;
961
980
962
981
assert_eq ! ( find_metadata_hashes_iter( "testsvh2" ,
963
982
vec![
964
- String :: from( "sess -timestamp1-testsvh1" ) ,
965
- String :: from( "sess -timestamp2-testsvh2-working" ) ,
966
- String :: from( "sess -timestamp3-testsvh3" ) ,
983
+ String :: from( "s -timestamp1-testsvh1" ) ,
984
+ String :: from( "s -timestamp2-testsvh2-working" ) ,
985
+ String :: from( "s -timestamp3-testsvh3" ) ,
967
986
] . into_iter( ) ) ,
968
987
None
969
988
) ;
970
989
971
990
assert_eq ! ( find_metadata_hashes_iter( "testsvh1" ,
972
991
vec![
973
- String :: from( "sess -timestamp1-random1-working" ) ,
974
- String :: from( "sess -timestamp2-random2-working" ) ,
975
- String :: from( "sess -timestamp3-random3-working" ) ,
992
+ String :: from( "s -timestamp1-random1-working" ) ,
993
+ String :: from( "s -timestamp2-random2-working" ) ,
994
+ String :: from( "s -timestamp3-random3-working" ) ,
976
995
] . into_iter( ) ) ,
977
996
None
978
997
) ;
@@ -986,3 +1005,21 @@ fn test_find_metadata_hashes_iter()
986
1005
None
987
1006
) ;
988
1007
}
1008
+
1009
+ #[ test]
1010
+ fn test_encode_base_36 ( ) {
1011
+ fn test ( n : u64 ) {
1012
+ assert_eq ! ( Ok ( n) , u64 :: from_str_radix( & encode_base_36( n) [ ..] , 36 ) ) ;
1013
+ }
1014
+
1015
+ test ( 0 ) ;
1016
+ test ( 1 ) ;
1017
+ test ( 35 ) ;
1018
+ test ( 36 ) ;
1019
+ test ( 37 ) ;
1020
+ test ( u64:: max_value ( ) ) ;
1021
+
1022
+ for i in 0 .. 1_000 {
1023
+ test ( i * 983 ) ;
1024
+ }
1025
+ }
0 commit comments