@@ -438,17 +438,33 @@ int p_symlink(const char *old, const char *new)
438438 return git_futils_fake_symlink (old , new );
439439}
440440
441- GIT_INLINE (int ) open_once (const wchar_t * path , int flags , mode_t mode )
442- {
443- int ret = _wopen (path , flags , mode );
441+ GIT_INLINE (int ) open_once (
442+ const wchar_t * path ,
443+ DWORD access ,
444+ DWORD sharing ,
445+ DWORD creation_disposition ,
446+ DWORD attributes ,
447+ int osf_flags )
448+ {
449+ HANDLE handle = CreateFileW (path , access , sharing , NULL ,
450+ creation_disposition , attributes , 0 );
451+
452+ if (handle == INVALID_HANDLE_VALUE ) {
453+ if (last_error_retryable ())
454+ return GIT_RETRY ;
455+
456+ set_errno ();
457+ return -1 ;
458+ }
444459
445- return ( ret < 0 && last_error_retryable ()) ? GIT_RETRY : ret ;
460+ return _open_osfhandle (( intptr_t ) handle , osf_flags ) ;
446461}
447462
448463int p_open (const char * path , int flags , ...)
449464{
450465 git_win32_path wpath ;
451466 mode_t mode = 0 ;
467+ DWORD access , sharing , creation , attributes , osf_flags ;
452468
453469 if (git_win32_path_from_utf8 (wpath , path ) < 0 )
454470 return -1 ;
@@ -461,8 +477,44 @@ int p_open(const char *path, int flags, ...)
461477 va_end (arg_list );
462478 }
463479
480+ switch (flags & (O_WRONLY | O_RDWR )) {
481+ case O_WRONLY :
482+ access = GENERIC_WRITE ;
483+ break ;
484+ case O_RDWR :
485+ access = GENERIC_READ | GENERIC_WRITE ;
486+ break ;
487+ default :
488+ access = GENERIC_READ ;
489+ break ;
490+ }
491+
492+ sharing = FILE_SHARE_READ | FILE_SHARE_WRITE ;
493+
494+ switch (flags & (O_CREAT | O_TRUNC | O_EXCL )) {
495+ case O_CREAT | O_EXCL :
496+ case O_CREAT | O_TRUNC | O_EXCL :
497+ creation = CREATE_NEW ;
498+ break ;
499+ case O_CREAT | O_TRUNC :
500+ creation = CREATE_ALWAYS ;
501+ break ;
502+ case O_TRUNC :
503+ creation = TRUNCATE_EXISTING ;
504+ break ;
505+ case O_CREAT :
506+ creation = OPEN_ALWAYS ;
507+ break ;
508+ default :
509+ creation = OPEN_EXISTING ;
510+ break ;
511+ }
512+
513+ attributes = (mode & S_IWRITE ) ? FILE_ATTRIBUTE_NORMAL : FILE_ATTRIBUTE_READONLY ;
514+ osf_flags = flags & (O_RDONLY | O_APPEND );
515+
464516 do_with_retries (
465- open_once (wpath , flags | STANDARD_OPEN_FLAGS , mode & WIN32_MODE_MASK ),
517+ open_once (wpath , access , sharing , creation , attributes , osf_flags ),
466518 0 );
467519}
468520
0 commit comments