@@ -22,33 +22,15 @@ use serde::{Deserialize, Serialize};
2222use std:: collections:: HashMap ;
2323use std:: fs:: { DirBuilder , File } ;
2424use std:: io:: prelude:: * ;
25+ use std:: os:: unix:: fs:: PermissionsExt ;
2526use std:: path:: PathBuf ;
26- //use thiserror::Error;
2727use tracing:: { debug, info, trace, warn} ;
2828
2929use crate :: auth:: {
3030 authtoken:: { AuthToken , AuthTokenScope } ,
3131 AuthState ,
3232} ;
3333
34- // /// Errors which may occur when creating connection state data.
35- // #[derive(Debug, Error)]
36- // #[non_exhaustive]
37- // pub enum StateError {
38- // #[error("failed to deserialize config: {}", source)]
39- // Parse {
40- // /// The source of the error.
41- // #[from]
42- // source: config::ConfigError,
43- // },
44- // #[error("IO error: {}", source)]
45- // IO {
46- // /// The source of the error.
47- // #[from]
48- // source: std::io::Error,
49- // },
50- // }
51-
5234/// A HashMap of Scope to Token
5335#[ derive( Clone , Default , Deserialize , Serialize , Debug ) ]
5436pub ( crate ) struct ScopeAuths ( HashMap < AuthTokenScope , AuthToken > ) ;
@@ -246,37 +228,34 @@ impl State {
246228 Ok ( mut file) => {
247229 let mut contents = vec ! [ ] ;
248230 match file. read_to_end ( & mut contents) {
249- Ok ( _) => match bincode:: serde:: decode_from_slice (
250- & contents,
251- bincode:: config:: legacy ( ) ,
252- ) {
253- Ok :: < ( ScopeAuths , usize ) , _ > ( ( mut auth, _) ) => {
231+ Ok ( _) => match postcard:: from_bytes :: < ScopeAuths > ( & contents) {
232+ Ok ( mut auth) => {
254233 auth. filter_invalid_auths ( ) ;
255234 trace ! ( "Cached Auth info: {:?}" , auth) ;
256235 Some ( auth)
257236 }
258237 Err ( x) => {
259238 info ! (
260- "Corrupted cache file {} : {:?}. Removing " ,
239+ "Corrupted cache file `{}` : {:?}. Removing " ,
261240 fname. display( ) ,
262241 x
263242 ) ;
264243 let _ = std:: fs:: remove_file ( fname) ;
265244 None
266245 }
267246 } ,
268- _ => {
247+ Err ( e ) => {
269248 // Not able to read file, maybe it is corrupted. There is nothing user can
270249 // or is expected to do about it, but it make sense to make user aware of.
271- info ! ( "Error reading file {} " , fname. display( ) ) ;
250+ info ! ( "Error reading file `{}`: {:?} " , fname. display( ) , e ) ;
272251 None
273252 }
274253 }
275254 }
276- _ => {
255+ Err ( e ) => {
277256 // Not able to open file, maybe it is missing. There is nothing user can or is
278257 // expected to do about it.
279- debug ! ( "Error opening file {} " , fname. display( ) ) ;
258+ debug ! ( "Error opening file `{}`: {:?} " , fname. display( ) , e ) ;
280259 None
281260 }
282261 }
@@ -291,17 +270,25 @@ impl State {
291270
292271 let _ = state. 0 . insert ( scope. clone ( ) , data. clone ( ) ) ;
293272
294- match bincode:: serde:: encode_to_vec ( & state, bincode:: config:: legacy ( ) ) {
295- Ok ( ser_data) => match File :: create ( fname. as_path ( ) ) {
296- Ok ( mut file) => {
297- let _ = file. write_all ( & ser_data) ;
273+ match File :: create ( fname. as_path ( ) ) {
274+ Ok ( mut file) => {
275+ match file. metadata ( ) {
276+ Ok ( metadata) => {
277+ let mut permissions = metadata. permissions ( ) ;
278+ permissions. set_mode ( 0o600 ) ;
279+ let _ = file. set_permissions ( permissions) ;
280+ }
281+ Err ( _) => {
282+ warn ! ( "Cannot set permissions for the cache file" ) ;
283+ return ;
284+ }
298285 }
299- _ => {
300- warn ! ( "Error writing state file" ) ;
286+ if let Err ( e ) = postcard :: to_io ( & state , & mut file ) {
287+ warn ! ( "Error serializing state: {:?}" , e ) ;
301288 }
302- } ,
303- Err ( e ) => {
304- warn ! ( "Error serializing state, {:?}" , e ) ;
289+ }
290+ _ => {
291+ warn ! ( "Error writing state file" ) ;
305292 }
306293 }
307294 }
0 commit comments