25
25
26
26
#define HCAST (type , handle ) ((type)(intptr_t)handle)
27
27
28
- static const int delay [] = { 0 , 1 , 10 , 20 , 40 };
29
-
30
28
void open_in_gdb (void )
31
29
{
32
30
static struct child_process cp = CHILD_PROCESS_INIT ;
@@ -203,15 +201,12 @@ static int read_yes_no_answer(void)
203
201
return -1 ;
204
202
}
205
203
206
- static int ask_yes_no_if_possible (const char * format , ... )
204
+ static int ask_yes_no_if_possible (const char * format , va_list args )
207
205
{
208
206
char question [4096 ];
209
207
const char * retry_hook ;
210
- va_list args ;
211
208
212
- va_start (args , format );
213
209
vsnprintf (question , sizeof (question ), format , args );
214
- va_end (args );
215
210
216
211
retry_hook = mingw_getenv ("GIT_ASK_YESNO" );
217
212
if (retry_hook ) {
@@ -236,6 +231,31 @@ static int ask_yes_no_if_possible(const char *format, ...)
236
231
}
237
232
}
238
233
234
+ static int retry_ask_yes_no (int * tries , const char * format , ...)
235
+ {
236
+ static const int delay [] = { 0 , 1 , 10 , 20 , 40 };
237
+ va_list args ;
238
+ int result , saved_errno = errno ;
239
+
240
+ if ((* tries ) < ARRAY_SIZE (delay )) {
241
+ /*
242
+ * We assume that some other process had the file open at the wrong
243
+ * moment and retry. In order to give the other process a higher
244
+ * chance to complete its operation, we give up our time slice now.
245
+ * If we have to retry again, we do sleep a bit.
246
+ */
247
+ Sleep (delay [* tries ]);
248
+ (* tries )++ ;
249
+ return 1 ;
250
+ }
251
+
252
+ va_start (args , format );
253
+ result = ask_yes_no_if_possible (format , args );
254
+ va_end (args );
255
+ errno = saved_errno ;
256
+ return result ;
257
+ }
258
+
239
259
/* Windows only */
240
260
enum hide_dotfiles_type {
241
261
HIDE_DOTFILES_FALSE = 0 ,
@@ -329,34 +349,24 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf)
329
349
330
350
int mingw_unlink (const char * pathname )
331
351
{
332
- int ret , tries = 0 ;
352
+ int tries = 0 ;
333
353
wchar_t wpathname [MAX_LONG_PATH ];
334
354
if (xutftowcs_long_path (wpathname , pathname ) < 0 )
335
355
return -1 ;
336
356
337
357
if (DeleteFileW (wpathname ))
338
358
return 0 ;
339
359
340
- /* read-only files cannot be removed */
341
- _wchmod (wpathname , 0666 );
342
- while ((ret = _wunlink (wpathname )) == -1 && tries < ARRAY_SIZE (delay )) {
360
+ do {
361
+ /* read-only files cannot be removed */
362
+ _wchmod (wpathname , 0666 );
363
+ if (!_wunlink (wpathname ))
364
+ return 0 ;
343
365
if (!is_file_in_use_error (GetLastError ()))
344
366
break ;
345
- /*
346
- * We assume that some other process had the source or
347
- * destination file open at the wrong moment and retry.
348
- * In order to give the other process a higher chance to
349
- * complete its operation, we give up our time slice now.
350
- * If we have to retry again, we do sleep a bit.
351
- */
352
- Sleep (delay [tries ]);
353
- tries ++ ;
354
- }
355
- while (ret == -1 && is_file_in_use_error (GetLastError ()) &&
356
- ask_yes_no_if_possible ("Unlink of file '%s' failed. "
357
- "Should I try again?" , pathname ))
358
- ret = _wunlink (wpathname );
359
- return ret ;
367
+ } while (retry_ask_yes_no (& tries , "Unlink of file '%s' failed. "
368
+ "Should I try again?" , pathname ));
369
+ return -1 ;
360
370
}
361
371
362
372
static int is_dir_empty (const wchar_t * wpath )
@@ -383,7 +393,7 @@ static int is_dir_empty(const wchar_t *wpath)
383
393
384
394
int mingw_rmdir (const char * pathname )
385
395
{
386
- int ret , tries = 0 ;
396
+ int tries = 0 ;
387
397
wchar_t wpathname [MAX_LONG_PATH ];
388
398
struct stat st ;
389
399
@@ -409,7 +419,11 @@ int mingw_rmdir(const char *pathname)
409
419
if (xutftowcs_long_path (wpathname , pathname ) < 0 )
410
420
return -1 ;
411
421
412
- while ((ret = _wrmdir (wpathname )) == -1 && tries < ARRAY_SIZE (delay )) {
422
+ do {
423
+ if (!_wrmdir (wpathname )) {
424
+ invalidate_lstat_cache ();
425
+ return 0 ;
426
+ }
413
427
if (!is_file_in_use_error (GetLastError ()))
414
428
errno = err_win_to_posix (GetLastError ());
415
429
if (errno != EACCES )
@@ -418,23 +432,9 @@ int mingw_rmdir(const char *pathname)
418
432
errno = ENOTEMPTY ;
419
433
break ;
420
434
}
421
- /*
422
- * We assume that some other process had the source or
423
- * destination file open at the wrong moment and retry.
424
- * In order to give the other process a higher chance to
425
- * complete its operation, we give up our time slice now.
426
- * If we have to retry again, we do sleep a bit.
427
- */
428
- Sleep (delay [tries ]);
429
- tries ++ ;
430
- }
431
- while (ret == -1 && errno == EACCES && is_file_in_use_error (GetLastError ()) &&
432
- ask_yes_no_if_possible ("Deletion of directory '%s' failed. "
433
- "Should I try again?" , pathname ))
434
- ret = _wrmdir (wpathname );
435
- if (!ret )
436
- invalidate_lstat_cache ();
437
- return ret ;
435
+ } while (retry_ask_yes_no (& tries , "Deletion of directory '%s' failed. "
436
+ "Should I try again?" , pathname ));
437
+ return -1 ;
438
438
}
439
439
440
440
static inline int needs_hiding (const char * path )
@@ -2421,20 +2421,8 @@ int mingw_rename(const char *pold, const char *pnew)
2421
2421
SetFileAttributesW (wpnew , attrs );
2422
2422
}
2423
2423
}
2424
- if (tries < ARRAY_SIZE (delay ) && gle == ERROR_ACCESS_DENIED ) {
2425
- /*
2426
- * We assume that some other process had the source or
2427
- * destination file open at the wrong moment and retry.
2428
- * In order to give the other process a higher chance to
2429
- * complete its operation, we give up our time slice now.
2430
- * If we have to retry again, we do sleep a bit.
2431
- */
2432
- Sleep (delay [tries ]);
2433
- tries ++ ;
2434
- goto repeat ;
2435
- }
2436
2424
if (gle == ERROR_ACCESS_DENIED &&
2437
- ask_yes_no_if_possible ( "Rename from '%s' to '%s' failed. "
2425
+ retry_ask_yes_no ( & tries , "Rename from '%s' to '%s' failed. "
2438
2426
"Should I try again?" , pold , pnew ))
2439
2427
goto repeat ;
2440
2428
0 commit comments