@@ -4,8 +4,9 @@ use crate::utils::ErrorContext as EC;
44use anyhow:: { Context , Result } ;
55use clap:: ValueEnum ;
66use directories:: ProjectDirs ;
7+ use fs2:: FileExt ;
78use std:: collections:: BTreeSet ;
8- use std:: fs;
9+ use std:: fs:: { self , OpenOptions } ;
910use std:: io:: { IsTerminal , Write } ;
1011use std:: path:: { Path , PathBuf } ;
1112
@@ -113,36 +114,53 @@ fn confirm_trust(config_path: Option<&Path>) -> Result<()> {
113114
114115fn is_trusted ( config_path : Option < & Path > ) -> bool {
115116 canonical_config_path ( config_path)
116- . and_then ( |path| load_store ( ) . map ( |store| store. contains ( & path) ) )
117+ . and_then ( |path| {
118+ let store_path = trust_store_path ( ) ?;
119+ load_store ( & store_path) . map ( |store| store. contains ( & path) )
120+ } )
117121 . unwrap_or ( false )
118122}
119123
120124fn set_trusted ( config_path : Option < & Path > , trusted : bool ) -> Result < ( ) > {
121- let config_path = canonical_config_path ( config_path) ?;
122- let mut store = load_store ( ) ?;
125+ let config_path = if trusted {
126+ canonical_config_path ( config_path) ?
127+ } else {
128+ config_path_for_revoke ( config_path) ?
129+ } ;
130+ let store_path = trust_store_path ( ) ?;
131+ let parent = store_path. parent ( ) . expect ( "trust store path has a parent" ) ;
132+ fs:: create_dir_all ( parent) . context ( "Cannot create the ast-grep configuration directory" ) ?;
133+ let lock = OpenOptions :: new ( )
134+ . read ( true )
135+ . write ( true )
136+ . create ( true )
137+ . truncate ( false )
138+ . open ( parent. join ( "trusted-configs.lock" ) )
139+ . context ( "Cannot open the ast-grep trust store lock" ) ?;
140+ FileExt :: lock_exclusive ( & lock) . context ( "Cannot lock the ast-grep trust store" ) ?;
141+
142+ let mut store = load_store ( & store_path) ?;
123143 let changed = if trusted {
124144 store. insert ( config_path)
125145 } else {
126146 store. remove ( & config_path)
127147 } ;
128148 if changed {
129- save_store ( & store) ?;
149+ save_store ( & store_path , & store) ?;
130150 }
131151 Ok ( ( ) )
132152}
133153
134- fn load_store ( ) -> Result < BTreeSet < PathBuf > > {
135- match fs:: read ( trust_store_path ( ) ? ) {
154+ fn load_store ( path : & Path ) -> Result < BTreeSet < PathBuf > > {
155+ match fs:: read ( path ) {
136156 Ok ( json) => serde_json:: from_slice ( & json) . context ( "Cannot parse the ast-grep trust store" ) ,
137157 Err ( error) if error. kind ( ) == std:: io:: ErrorKind :: NotFound => Ok ( BTreeSet :: new ( ) ) ,
138158 Err ( error) => Err ( error) . context ( "Cannot read the ast-grep trust store" ) ,
139159 }
140160}
141161
142- fn save_store ( store : & BTreeSet < PathBuf > ) -> Result < ( ) > {
143- let path = trust_store_path ( ) ?;
162+ fn save_store ( path : & Path , store : & BTreeSet < PathBuf > ) -> Result < ( ) > {
144163 let parent = path. parent ( ) . expect ( "trust store path has a parent" ) ;
145- fs:: create_dir_all ( parent) . context ( "Cannot create the ast-grep configuration directory" ) ?;
146164 let mut temp = tempfile:: NamedTempFile :: new_in ( parent) ?;
147165 temp. write_all ( & serde_json:: to_vec_pretty ( store) ?) ?;
148166 temp. persist ( path) . map_err ( |error| error. error ) ?;
@@ -164,6 +182,29 @@ fn canonical_config_path(config_path: Option<&Path>) -> Result<PathBuf> {
164182 fs:: canonicalize ( config_path) . context ( "Cannot resolve the ast-grep configuration path" )
165183}
166184
185+ fn config_path_for_revoke ( config_path : Option < & Path > ) -> Result < PathBuf > {
186+ let config_path = find_config_path_with_default ( config_path. map ( Path :: to_path_buf) ) ?
187+ . context ( "Cannot find the ast-grep configuration file" ) ?;
188+ match fs:: canonicalize ( & config_path) {
189+ Ok ( path) => Ok ( path) ,
190+ Err ( error) if error. kind ( ) == std:: io:: ErrorKind :: NotFound => {
191+ let file_name = config_path
192+ . file_name ( )
193+ . context ( "Configuration path must have a file name" ) ?;
194+ let parent = config_path
195+ . parent ( )
196+ . filter ( |parent| !parent. as_os_str ( ) . is_empty ( ) )
197+ . unwrap_or_else ( || Path :: new ( "." ) ) ;
198+ Ok (
199+ fs:: canonicalize ( parent)
200+ . context ( "Cannot resolve the ast-grep configuration directory" ) ?
201+ . join ( file_name) ,
202+ )
203+ }
204+ Err ( error) => Err ( error) . context ( "Cannot resolve the ast-grep configuration path" ) ,
205+ }
206+ }
207+
167208#[ cfg( test) ]
168209mod test {
169210 use super :: * ;
0 commit comments