@@ -93,17 +93,31 @@ int p_unlink(const char *path)
9393{
9494 git_win32_path buf ;
9595 int error ;
96+ int unlink_tries ;
9697
9798 if (git_win32_path_from_utf8 (buf , path ) < 0 )
9899 return -1 ;
99100
100- error = _wunlink (buf );
101-
102- /* If the file could not be deleted because it was
103- * read-only, clear the bit and try again */
104- if (error == -1 && errno == EACCES ) {
105- _wchmod (buf , 0666 );
101+ /* wait up to 50ms if file is locked by another thread or process */
102+ unlink_tries = 0 ;
103+ while (unlink_tries < 10 ) {
106104 error = _wunlink (buf );
105+
106+ /* If the file could not be deleted because it was
107+ * read-only, clear the bit and try again */
108+ if (error == -1 && errno == EACCES ) {
109+ _wchmod (buf , 0666 );
110+ error = _wunlink (buf );
111+
112+ if (error == -1 && errno == EACCES ) {
113+ Sleep (5 );
114+ unlink_tries ++ ;
115+ } else {
116+ break ;
117+ }
118+ } else {
119+ break ;
120+ }
107121 }
108122
109123 return error ;
@@ -286,6 +300,8 @@ int p_open(const char *path, int flags, ...)
286300{
287301 git_win32_path buf ;
288302 mode_t mode = 0 ;
303+ int open_tries ;
304+ int handle ;
289305
290306 if (git_win32_path_from_utf8 (buf , path ) < 0 )
291307 return -1 ;
@@ -298,7 +314,23 @@ int p_open(const char *path, int flags, ...)
298314 va_end (arg_list );
299315 }
300316
301- return _wopen (buf , flags | STANDARD_OPEN_FLAGS , mode & WIN32_MODE_MASK );
317+ /* wait up to 50ms if file is locked by another thread or process */
318+ open_tries = 0 ;
319+ while (open_tries < 10 ) {
320+ handle = _wopen (buf , flags | STANDARD_OPEN_FLAGS , mode & WIN32_MODE_MASK );
321+ if (handle != -1 ) {
322+ break ;
323+ }
324+
325+ if (errno == EACCES ) {
326+ Sleep (5 );
327+ open_tries ++ ;
328+ } else {
329+ break ;
330+ }
331+ }
332+
333+ return handle ;
302334}
303335
304336int p_creat (const char * path , mode_t mode )
0 commit comments