1
- use rustc_incremental:: in_incr_comp_dir_sess;
1
+ use std:: path:: Path ;
2
+
2
3
use rustc_middle:: ty:: TyCtxt ;
3
4
use rustc_serialize:: { Decodable , Encodable } ;
4
5
@@ -7,25 +8,21 @@ pub use self::adapted_encoding::{CacheDecoder, CacheEncoder};
7
8
// Adapted from: compiler/rustc_incremental/src/persist/save.rs:27
8
9
pub fn save_to_file < ' tcx > (
9
10
tcx : TyCtxt < ' tcx > ,
10
- file_name : & str ,
11
11
name : & str ,
12
+ path : & Path ,
12
13
item : & impl for < ' a > Encodable < CacheEncoder < ' a , ' tcx > > ,
13
14
) {
14
- let path = in_incr_comp_dir_sess ( tcx. sess , file_name) ;
15
-
16
- adapted_io:: save_in ( tcx. sess , path, name, |e| {
15
+ adapted_io:: save_in ( tcx. sess , path. to_path_buf ( ) , name, |e| {
17
16
adapted_encoding:: save_to_encoder ( tcx, e, item)
18
17
} ) ;
19
18
}
20
19
21
20
// Adapted from: compiler/rustc_incremental/src/persist/load.rs:176
22
- pub fn try_load_from_file < ' tcx , D > ( tcx : TyCtxt < ' tcx > , file_name : & str , name : & str ) -> Option < D >
21
+ pub fn try_load_from_file < ' tcx , D > ( tcx : TyCtxt < ' tcx > , name : & str , path : & Path ) -> Option < D >
23
22
where
24
23
D : for < ' a > Decodable < CacheDecoder < ' a , ' tcx > > ,
25
24
{
26
- let path = in_incr_comp_dir_sess ( tcx. sess , file_name) ;
27
-
28
- match adapted_io:: read_file ( & path) {
25
+ match adapted_io:: read_file ( path) {
29
26
Ok ( Some ( ( mmap, start_pos) ) ) => {
30
27
Some ( adapted_encoding:: load_from_decoder ( tcx, mmap, start_pos) )
31
28
}
@@ -54,19 +51,7 @@ mod adapted_io {
54
51
} ;
55
52
use rustc_session:: Session ;
56
53
57
- /// The first few bytes of files generated by incremental compilation.
58
- const FILE_MAGIC : & [ u8 ] = b"AUTOKEN-v1" ;
59
-
60
- /// Change this if the header format changes.
61
- const HEADER_FORMAT_VERSION : u16 = 0 ;
62
-
63
- fn write_file_header ( stream : & mut FileEncoder ) {
64
- stream. emit_raw_bytes ( FILE_MAGIC ) ;
65
- stream. emit_raw_bytes ( & [
66
- ( HEADER_FORMAT_VERSION ) as u8 ,
67
- ( HEADER_FORMAT_VERSION >> 8 ) as u8 ,
68
- ] ) ;
69
- }
54
+ const FILE_MAGIC : & [ u8 ] = b"autoken-1" ;
70
55
71
56
pub fn save_in < F > ( sess : & Session , path_buf : PathBuf , name : & str , encode : F )
72
57
where
@@ -90,7 +75,7 @@ mod adapted_io {
90
75
}
91
76
} ;
92
77
93
- write_file_header ( & mut encoder) ;
78
+ encoder. emit_raw_bytes ( FILE_MAGIC ) ;
94
79
95
80
match encode ( encoder) {
96
81
Ok ( _) => { }
@@ -100,27 +85,14 @@ mod adapted_io {
100
85
}
101
86
}
102
87
103
- /// Reads the contents of a file with a file header as defined in this module.
104
- ///
105
- /// - Returns `Ok(Some(data, pos))` if the file existed and was generated by a
106
- /// compatible compiler version. `data` is the entire contents of the file
107
- /// and `pos` points to the first byte after the header.
108
- /// - Returns `Ok(None)` if the file did not exist or was generated by an
109
- /// incompatible version of the compiler.
110
- /// - Returns `Err(..)` if some kind of IO error occurred while reading the
111
- /// file.
112
88
pub fn read_file ( path : & Path ) -> io:: Result < Option < ( Mmap , usize ) > > {
89
+ // Open file
113
90
let file = match fs:: File :: open ( path) {
114
91
Ok ( file) => file,
115
92
Err ( err) if err. kind ( ) == io:: ErrorKind :: NotFound => return Ok ( None ) ,
116
93
Err ( err) => return Err ( err) ,
117
94
} ;
118
- // SAFETY: This process must not modify nor remove the backing file while the memory map lives.
119
- // For the dep-graph and the work product index, it is as soon as the decoding is done.
120
- // For the query result cache, the memory map is dropped in save_dep_graph before calling
121
- // save_in and trying to remove the backing file.
122
- //
123
- // There is no way to prevent another process from modifying this file.
95
+
124
96
let mmap = unsafe { Mmap :: map ( file) } ?;
125
97
126
98
let mut file = io:: Cursor :: new ( & * mmap) ;
@@ -315,7 +287,7 @@ mod adapted_encoding {
315
287
} ;
316
288
317
289
// Adapted from compiler/rustc_middle/src/query/on_disk_cache.rs:156
318
- let pos = AbsoluteBytePos ( 0 ) ;
290
+ let pos = AbsoluteBytePos ( start_pos as u64 ) ;
319
291
320
292
let alloc_decoding_session = AllocDecodingState :: new ( footer. interpret_alloc_index ) ;
321
293
0 commit comments