@@ -178,6 +178,26 @@ GIT_INLINE(bool) last_error_retryable(void)
178178 return -1; \
179179 } while (0) \
180180
181+ static int ensure_writable (wchar_t * path )
182+ {
183+ DWORD attrs ;
184+
185+ if ((attrs = GetFileAttributesW (path )) == INVALID_FILE_ATTRIBUTES )
186+ goto on_error ;
187+
188+ if ((attrs & FILE_ATTRIBUTE_READONLY ) == 0 )
189+ return 0 ;
190+
191+ if (!SetFileAttributesW (path , (attrs & ~FILE_ATTRIBUTE_READONLY )))
192+ goto on_error ;
193+
194+ return 0 ;
195+
196+ on_error :
197+ set_errno ();
198+ return -1 ;
199+ }
200+
181201/**
182202 * Truncate or extend file.
183203 *
@@ -223,38 +243,26 @@ int p_link(const char *old, const char *new)
223243 return -1 ;
224244}
225245
246+ GIT_INLINE (int ) unlink_once (const wchar_t * path )
247+ {
248+ if (DeleteFileW (path ))
249+ return 0 ;
250+
251+ if (last_error_retryable ())
252+ return GIT_RETRY ;
253+
254+ set_errno ();
255+ return -1 ;
256+ }
257+
226258int p_unlink (const char * path )
227259{
228- git_win32_path buf ;
229- int error ;
230- int unlink_tries ;
260+ git_win32_path wpath ;
231261
232- if (git_win32_path_from_utf8 (buf , path ) < 0 )
262+ if (git_win32_path_from_utf8 (wpath , path ) < 0 )
233263 return -1 ;
234264
235- /* wait up to 50ms if file is locked by another thread or process */
236- unlink_tries = 0 ;
237- while (unlink_tries < 10 ) {
238- error = _wunlink (buf );
239-
240- /* If the file could not be deleted because it was
241- * read-only, clear the bit and try again */
242- if (error == -1 && errno == EACCES ) {
243- _wchmod (buf , 0666 );
244- error = _wunlink (buf );
245-
246- if (error == -1 && errno == EACCES ) {
247- Sleep (5 );
248- unlink_tries ++ ;
249- } else {
250- break ;
251- }
252- } else {
253- break ;
254- }
255- }
256-
257- return error ;
265+ do_with_retries (unlink_once (wpath ), ensure_writable (wpath ));
258266}
259267
260268int p_fsync (int fd )
@@ -749,26 +757,6 @@ int p_access(const char* path, mode_t mode)
749757 return _waccess (buf , mode & WIN32_MODE_MASK );
750758}
751759
752- static int ensure_writable (wchar_t * path )
753- {
754- DWORD attrs ;
755-
756- if ((attrs = GetFileAttributesW (path )) == INVALID_FILE_ATTRIBUTES )
757- goto on_error ;
758-
759- if ((attrs & FILE_ATTRIBUTE_READONLY ) == 0 )
760- return 0 ;
761-
762- if (!SetFileAttributesW (path , (attrs & ~FILE_ATTRIBUTE_READONLY )))
763- goto on_error ;
764-
765- return 0 ;
766-
767- on_error :
768- set_errno ();
769- return -1 ;
770- }
771-
772760GIT_INLINE (int ) rename_once (const wchar_t * from , const wchar_t * to )
773761{
774762 if (MoveFileExW (from , to , MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED ))
0 commit comments