@@ -122,6 +122,61 @@ where
122122 debug ! ( "save: data written to disk successfully" ) ;
123123}
124124
125+ pub ( crate ) fn save_in_raw < F > ( sess : & Session , path_buf : PathBuf , name : & str , encode : F )
126+ where
127+ F : FnOnce ( & mut raw:: FileEncoder ) -> raw:: FileEncodeResult ,
128+ {
129+ debug ! ( "save: storing data in {}" , path_buf. display( ) ) ;
130+
131+ // Delete the old file, if any.
132+ // Note: It's important that we actually delete the old file and not just
133+ // truncate and overwrite it, since it might be a shared hard-link, the
134+ // underlying data of which we don't want to modify.
135+ //
136+ // We have to ensure we have dropped the memory maps to this file
137+ // before performing this removal.
138+ match fs:: remove_file ( & path_buf) {
139+ Ok ( ( ) ) => {
140+ debug ! ( "save: remove old file" ) ;
141+ }
142+ Err ( err) if err. kind ( ) == io:: ErrorKind :: NotFound => ( ) ,
143+ Err ( err) => {
144+ sess. err ( & format ! (
145+ "unable to delete old {} at `{}`: {}" ,
146+ name,
147+ path_buf. display( ) ,
148+ err
149+ ) ) ;
150+ return ;
151+ }
152+ }
153+
154+ let mut encoder = match raw:: FileEncoder :: new ( & path_buf) {
155+ Ok ( encoder) => encoder,
156+ Err ( err) => {
157+ sess. err ( & format ! ( "failed to create {} at `{}`: {}" , name, path_buf. display( ) , err) ) ;
158+ return ;
159+ }
160+ } ;
161+
162+ if let Err ( err) = write_file_header_raw ( & mut encoder, sess. is_nightly_build ( ) ) {
163+ sess. err ( & format ! ( "failed to write {} header to `{}`: {}" , name, path_buf. display( ) , err) ) ;
164+ return ;
165+ }
166+
167+ if let Err ( err) = encode ( & mut encoder) {
168+ sess. err ( & format ! ( "failed to write {} to `{}`: {}" , name, path_buf. display( ) , err) ) ;
169+ return ;
170+ }
171+
172+ if let Err ( err) = encoder. flush ( ) {
173+ sess. err ( & format ! ( "failed to flush {} to `{}`: {}" , name, path_buf. display( ) , err) ) ;
174+ return ;
175+ }
176+
177+ debug ! ( "save: data written to disk successfully" ) ;
178+ }
179+
125180/// Reads the contents of a file with a file header as defined in this module.
126181///
127182/// - Returns `Ok(Some(data, pos))` if the file existed and was generated by a
0 commit comments