@@ -10,6 +10,7 @@ use std::{io, process};
1010
1111use crate :: common:: SudoPath ;
1212use crate :: exec:: ExitReason ;
13+ use crate :: gettext:: xlat;
1314use crate :: log:: { user_error, user_info} ;
1415use crate :: system:: file:: { create_temporary_dir, FileLock } ;
1516use crate :: system:: wait:: { Wait , WaitError , WaitOptions } ;
@@ -42,25 +43,43 @@ pub(super) fn edit_files(
4243 let metadata = file. metadata ( ) . map_err ( |e| {
4344 io:: Error :: new (
4445 e. kind ( ) ,
45- format ! ( "Failed to read metadata for {}: {e}" , path. display( ) ) ,
46+ xlat ! (
47+ "failed to read metadata for {path}: {error}" ,
48+ path = path. display( ) ,
49+ error = e
50+ ) ,
4651 )
4752 } ) ?;
4853 if !metadata. is_file ( ) {
4954 return Err ( io:: Error :: new (
5055 io:: ErrorKind :: Other ,
51- format ! ( "File { } is not a regular file" , path. display( ) ) ,
56+ xlat ! ( "file {path } is not a regular file" , path = path. display( ) ) ,
5257 ) ) ;
5358 }
5459
5560 // Take file lock
5661 let lock = FileLock :: exclusive ( & file, true ) . map_err ( |e| {
57- io:: Error :: new ( e. kind ( ) , format ! ( "Failed to lock {}: {e}" , path. display( ) ) )
62+ io:: Error :: new (
63+ e. kind ( ) ,
64+ xlat ! (
65+ "failed to lock {path}: {error}" ,
66+ path = path. display( ) ,
67+ error = e
68+ ) ,
69+ )
5870 } ) ?;
5971
6072 // Read file
6173 let mut old_data = Vec :: new ( ) ;
6274 file. read_to_end ( & mut old_data) . map_err ( |e| {
63- io:: Error :: new ( e. kind ( ) , format ! ( "Failed to read {}: {e}" , path. display( ) ) )
75+ io:: Error :: new (
76+ e. kind ( ) ,
77+ xlat ! (
78+ "failed to read {path}: {error}" ,
79+ path = path. display( ) ,
80+ error = e
81+ ) ,
82+ )
6483 } ) ?;
6584
6685 // Create socket
@@ -142,7 +161,11 @@ pub(super) fn edit_files(
142161 . map_err ( |e| {
143162 io:: Error :: new (
144163 e. kind ( ) ,
145- format ! ( "Failed to write {}: {e}" , file. path. display( ) ) ,
164+ xlat ! (
165+ "failed to write {path}: {error}" ,
166+ path = file. path. display( ) ,
167+ error = e
168+ ) ,
146169 )
147170 } ) ?;
148171
@@ -186,16 +209,18 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
186209 }
187210
188211 let tempdir = TempDirDropGuard (
189- create_temporary_dir ( ) . map_err ( |e| format ! ( "failed to create temporary directory: {e}" ) ) ?,
212+ create_temporary_dir ( )
213+ . map_err ( |e| xlat ! ( "failed to create temporary directory: {error}" , error = e) ) ?,
190214 ) ;
191215
192216 for ( i, file) in files. iter_mut ( ) . enumerate ( ) {
193217 // Create temp file
194218 let dir = tempdir. 0 . join ( format ! ( "{i}" ) ) ;
195219 std:: fs:: create_dir ( & dir) . map_err ( |e| {
196- format ! (
197- "failed to create temporary directory {}: {e}" ,
198- dir. display( ) ,
220+ xlat ! (
221+ "failed to create temporary directory {path}: {error}" ,
222+ path = dir. display( ) ,
223+ error = e
199224 )
200225 } ) ?;
201226 let tempfile_path = dir. join ( file. path . file_name ( ) . expect ( "file must have filename" ) ) ;
@@ -206,17 +231,19 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
206231 . mode ( 0o600 )
207232 . open ( & tempfile_path)
208233 . map_err ( |e| {
209- format ! (
210- "failed to create temporary file {}: {e}" ,
211- tempfile_path. display( ) ,
234+ xlat ! (
235+ "failed to create temporary file {path}: {error}" ,
236+ path = tempfile_path. display( ) ,
237+ error = e
212238 )
213239 } ) ?;
214240
215241 // Write to temp file
216242 tempfile. write_all ( & file. old_data ) . map_err ( |e| {
217- format ! (
218- "failed to write to temporary file {}: {e}" ,
219- tempfile_path. display( ) ,
243+ xlat ! (
244+ "failed to write to temporary file {path}: {error}" ,
245+ path = tempfile_path. display( ) ,
246+ error = e
220247 )
221248 } ) ?;
222249 drop ( tempfile) ;
@@ -231,7 +258,13 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
231258 . map ( |file| file. tempfile_path . as_ref ( ) . expect ( "filled in above" ) ) ,
232259 )
233260 . status ( )
234- . map_err ( |e| format ! ( "failed to run editor {}: {e}" , editor. display( ) ) ) ?;
261+ . map_err ( |e| {
262+ xlat ! (
263+ "failed to run editor {path}: {error}" ,
264+ path = editor. display( ) ,
265+ error = e
266+ )
267+ } ) ?;
235268
236269 if !status. success ( ) {
237270 drop ( tempdir) ;
@@ -247,26 +280,28 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
247280
248281 // Read from temp file
249282 let new_data = std:: fs:: read ( tempfile_path) . map_err ( |e| {
250- format ! (
251- "failed to read from temporary file {}: {e}" ,
252- tempfile_path. display( ) ,
283+ xlat ! (
284+ "failed to read from temporary file {path}: {error}" ,
285+ path = tempfile_path. display( ) ,
286+ error = e
253287 )
254288 } ) ?;
255289
256290 // FIXME preserve temporary file if the original couldn't be written to
257291 std:: fs:: remove_file ( tempfile_path) . map_err ( |e| {
258- format ! (
259- "failed to remove temporary file {}: {e}" ,
260- tempfile_path. display( ) ,
292+ xlat ! (
293+ "failed to remove temporary file {path}: {error}" ,
294+ path = tempfile_path. display( ) ,
295+ error = e
261296 )
262297 } ) ?;
263298
264299 // If the file has been changed to be empty, ask the user what to do.
265300 if new_data. is_empty ( ) && new_data != file. old_data {
266301 match crate :: visudo:: ask_response (
267- format ! (
268- "sudoedit: truncate {} to zero? (y/n) [n] " ,
269- file. path. display( )
302+ xlat ! (
303+ "sudoedit: truncate {path } to zero? (y/n) [n] " ,
304+ path = file. path. display( )
270305 )
271306 . as_bytes ( ) ,
272307 b"yn" ,
@@ -277,7 +312,7 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
277312
278313 // Parent ignores write when new data matches old data
279314 write_stream ( & mut file. new_data_tx , & file. old_data )
280- . map_err ( |e| format ! ( "failed to write data to parent: {e}" ) ) ?;
315+ . map_err ( |e| xlat ! ( "failed to write data to parent: {error}" , error = e ) ) ?;
281316
282317 continue ;
283318 }
@@ -286,7 +321,7 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
286321
287322 // Write to socket
288323 write_stream ( & mut file. new_data_tx , & new_data)
289- . map_err ( |e| format ! ( "failed to write data to parent: {e}" ) ) ?;
324+ . map_err ( |e| xlat ! ( "failed to write data to parent: {error}" , error = e ) ) ?;
290325 }
291326
292327 process:: exit ( 0 ) ;
0 commit comments