2727
2828#define HCAST (type , handle ) ((type)(intptr_t)handle)
2929
30- static const int delay [] = { 0 , 1 , 10 , 20 , 40 };
31-
3230void open_in_gdb (void )
3331{
3432 static struct child_process cp = CHILD_PROCESS_INIT ;
@@ -204,15 +202,12 @@ static int read_yes_no_answer(void)
204202 return -1 ;
205203}
206204
207- static int ask_yes_no_if_possible (const char * format , ... )
205+ static int ask_yes_no_if_possible (const char * format , va_list args )
208206{
209207 char question [4096 ];
210208 const char * retry_hook ;
211- va_list args ;
212209
213- va_start (args , format );
214210 vsnprintf (question , sizeof (question ), format , args );
215- va_end (args );
216211
217212 retry_hook = mingw_getenv ("GIT_ASK_YESNO" );
218213 if (retry_hook ) {
@@ -237,6 +232,31 @@ static int ask_yes_no_if_possible(const char *format, ...)
237232 }
238233}
239234
235+ static int retry_ask_yes_no (int * tries , const char * format , ...)
236+ {
237+ static const int delay [] = { 0 , 1 , 10 , 20 , 40 };
238+ va_list args ;
239+ int result , saved_errno = errno ;
240+
241+ if ((* tries ) < ARRAY_SIZE (delay )) {
242+ /*
243+ * We assume that some other process had the file open at the wrong
244+ * moment and retry. In order to give the other process a higher
245+ * chance to complete its operation, we give up our time slice now.
246+ * If we have to retry again, we do sleep a bit.
247+ */
248+ Sleep (delay [* tries ]);
249+ (* tries )++ ;
250+ return 1 ;
251+ }
252+
253+ va_start (args , format );
254+ result = ask_yes_no_if_possible (format , args );
255+ va_end (args );
256+ errno = saved_errno ;
257+ return result ;
258+ }
259+
240260/* Windows only */
241261enum hide_dotfiles_type {
242262 HIDE_DOTFILES_FALSE = 0 ,
@@ -329,34 +349,24 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf)
329349
330350int mingw_unlink (const char * pathname )
331351{
332- int ret , tries = 0 ;
352+ int tries = 0 ;
333353 wchar_t wpathname [MAX_LONG_PATH ];
334354 if (xutftowcs_long_path (wpathname , pathname ) < 0 )
335355 return -1 ;
336356
337357 if (DeleteFileW (wpathname ))
338358 return 0 ;
339359
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 ;
343365 if (!is_file_in_use_error (GetLastError ()))
344366 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 ;
360370}
361371
362372static int is_dir_empty (const wchar_t * wpath )
@@ -383,7 +393,7 @@ static int is_dir_empty(const wchar_t *wpath)
383393
384394int mingw_rmdir (const char * pathname )
385395{
386- int ret , tries = 0 ;
396+ int tries = 0 ;
387397 wchar_t wpathname [MAX_LONG_PATH ];
388398 struct stat st ;
389399
@@ -409,7 +419,11 @@ int mingw_rmdir(const char *pathname)
409419 if (xutftowcs_long_path (wpathname , pathname ) < 0 )
410420 return -1 ;
411421
412- while ((ret = _wrmdir (wpathname )) == -1 && tries < ARRAY_SIZE (delay )) {
422+ do {
423+ if (!_wrmdir (wpathname )) {
424+ invalidate_lstat_cache ();
425+ return 0 ;
426+ }
413427 if (!is_file_in_use_error (GetLastError ()))
414428 errno = err_win_to_posix (GetLastError ());
415429 if (errno != EACCES )
@@ -418,23 +432,9 @@ int mingw_rmdir(const char *pathname)
418432 errno = ENOTEMPTY ;
419433 break ;
420434 }
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 ;
438438}
439439
440440static inline int needs_hiding (const char * path )
@@ -2625,20 +2625,8 @@ int mingw_rename(const char *pold, const char *pnew)
26252625 SetFileAttributesW (wpnew , attrs );
26262626 }
26272627 }
2628- if (tries < ARRAY_SIZE (delay ) && gle == ERROR_ACCESS_DENIED ) {
2629- /*
2630- * We assume that some other process had the source or
2631- * destination file open at the wrong moment and retry.
2632- * In order to give the other process a higher chance to
2633- * complete its operation, we give up our time slice now.
2634- * If we have to retry again, we do sleep a bit.
2635- */
2636- Sleep (delay [tries ]);
2637- tries ++ ;
2638- goto repeat ;
2639- }
26402628 if (gle == ERROR_ACCESS_DENIED &&
2641- ask_yes_no_if_possible ( "Rename from '%s' to '%s' failed. "
2629+ retry_ask_yes_no ( & tries , "Rename from '%s' to '%s' failed. "
26422630 "Should I try again?" , pold , pnew ))
26432631 goto repeat ;
26442632
0 commit comments