2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
8 #include "../fileops.h"
12 #include "repository.h"
21 #ifndef FILE_NAME_NORMALIZED
22 # define FILE_NAME_NORMALIZED 0
25 #ifndef IO_REPARSE_TAG_SYMLINK
26 #define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
29 /* Options which we always provide to _wopen.
31 * _O_BINARY - Raw access; no translation of CR or LF characters
32 * _O_NOINHERIT - Do not mark the created handle as inheritable by child processes.
33 * The Windows default is 'not inheritable', but the CRT's default (following
34 * POSIX convention) is 'inheritable'. We have no desire for our handles to be
35 * inheritable on Windows, so specify the flag to get default behavior back. */
36 #define STANDARD_OPEN_FLAGS (_O_BINARY | _O_NOINHERIT)
38 /* Allowable mode bits on Win32. Using mode bits that are not supported on
39 * Win32 (eg S_IRWXU) is generally ignored, but Wine warns loudly about it
40 * so we simply remove them.
42 #define WIN32_MODE_MASK (_S_IREAD | _S_IWRITE)
44 /* GetFinalPathNameByHandleW signature */
45 typedef DWORD(WINAPI
*PFGetFinalPathNameByHandleW
)(HANDLE
, LPWSTR
, DWORD
, DWORD
);
48 * Truncate or extend file.
50 * We now take a "git_off_t" rather than "long" because
51 * files may be longer than 2Gb.
53 int p_ftruncate(int fd
, git_off_t size
)
60 #if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API)
61 return ((_chsize_s(fd
, size
) == 0) ? 0 : -1);
63 /* TODO MINGW32 Find a replacement for _chsize() that handles big files. */
64 if (size
> INT32_MAX
) {
68 return _chsize(fd
, (long)size
);
72 int p_mkdir(const char *path
, mode_t mode
)
78 if (git_win32_path_from_utf8(buf
, path
) < 0)
84 int p_link(const char *old
, const char *new)
92 int p_unlink(const char *path
)
97 if (git_win32_path_from_utf8(buf
, path
) < 0)
100 error
= _wunlink(buf
);
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
) {
106 error
= _wunlink(buf
);
114 HANDLE fh
= (HANDLE
)_get_osfhandle(fd
);
116 if (fh
== INVALID_HANDLE_VALUE
) {
121 if (!FlushFileBuffers(fh
)) {
122 DWORD code
= GetLastError();
124 if (code
== ERROR_INVALID_HANDLE
)
135 #define WIN32_IS_WSEP(CH) ((CH) == L'/' || (CH) == L'\\')
142 WIN32_FILE_ATTRIBUTE_DATA fdata
;
144 if (GetFileAttributesExW(path
, GetFileExInfoStandard
, &fdata
)) {
148 return git_win32__file_attribute_to_stat(buf
, &fdata
, path
);
151 switch (GetLastError()) {
152 case ERROR_ACCESS_DENIED
:
160 /* To match POSIX behavior, set ENOTDIR when any of the folders in the
161 * file path is a regular file, otherwise set ENOENT.
163 if (errno
== ENOENT
&& posix_enotdir
) {
164 size_t path_len
= wcslen(path
);
166 /* scan up path until we find an existing item */
170 /* remove last directory component */
171 for (path_len
--; path_len
> 0 && !WIN32_IS_WSEP(path
[path_len
]); path_len
--);
176 path
[path_len
] = L
'\0';
177 attrs
= GetFileAttributesW(path
);
179 if (attrs
!= INVALID_FILE_ATTRIBUTES
) {
180 if (!(attrs
& FILE_ATTRIBUTE_DIRECTORY
))
190 static int do_lstat(const char *path
, struct stat
*buf
, bool posixly_correct
)
192 git_win32_path path_w
;
195 if ((len
= git_win32_path_from_utf8(path_w
, path
)) < 0)
198 git_win32__path_trim_end(path_w
, len
);
200 return lstat_w(path_w
, buf
, posixly_correct
);
203 int p_lstat(const char *filename
, struct stat
*buf
)
205 return do_lstat(filename
, buf
, false);
208 int p_lstat_posixly(const char *filename
, struct stat
*buf
)
210 return do_lstat(filename
, buf
, true);
213 int p_utimes(const char *filename
, const struct p_timeval times
[2])
217 if ((fd
= p_open(filename
, O_RDWR
)) < 0)
220 error
= p_futimes(fd
, times
);
226 int p_futimes(int fd
, const struct p_timeval times
[2])
229 FILETIME atime
= {0}, mtime
= {0};
235 SystemTimeToFileTime(&st
, &atime
);
236 SystemTimeToFileTime(&st
, &mtime
);
238 git_win32__timeval_to_filetime(&atime
, times
[0]);
239 git_win32__timeval_to_filetime(&mtime
, times
[1]);
242 if ((handle
= (HANDLE
)_get_osfhandle(fd
)) == INVALID_HANDLE_VALUE
)
245 if (SetFileTime(handle
, NULL
, &atime
, &mtime
) == 0)
251 int p_readlink(const char *path
, char *buf
, size_t bufsiz
)
253 git_win32_path path_w
, target_w
;
254 git_win32_utf8_path target
;
257 /* readlink(2) does not NULL-terminate the string written
258 * to the target buffer. Furthermore, the target buffer need
259 * not be large enough to hold the entire result. A truncated
260 * result should be written in this case. Since this truncation
261 * could occur in the middle of the encoding of a code point,
262 * we need to buffer the result on the stack. */
264 if (git_win32_path_from_utf8(path_w
, path
) < 0 ||
265 git_win32_path_readlink_w(target_w
, path_w
) < 0 ||
266 (len
= git_win32_path_to_utf8(target
, target_w
)) < 0)
269 bufsiz
= min((size_t)len
, bufsiz
);
270 memcpy(buf
, target
, bufsiz
);
275 int p_symlink(const char *old
, const char *new)
277 /* Real symlinks on NTFS require admin privileges. Until this changes,
278 * libgit2 just creates a text file with the link target in the contents.
280 return git_futils_fake_symlink(old
, new);
283 int p_open(const char *path
, int flags
, ...)
288 if (git_win32_path_from_utf8(buf
, path
) < 0)
291 if (flags
& O_CREAT
) {
294 va_start(arg_list
, flags
);
295 mode
= (mode_t
)va_arg(arg_list
, int);
299 return _wopen(buf
, flags
| STANDARD_OPEN_FLAGS
, mode
& WIN32_MODE_MASK
);
302 int p_creat(const char *path
, mode_t mode
)
306 if (git_win32_path_from_utf8(buf
, path
) < 0)
310 _O_WRONLY
| _O_CREAT
| _O_TRUNC
| STANDARD_OPEN_FLAGS
,
311 mode
& WIN32_MODE_MASK
);
314 int p_getcwd(char *buffer_out
, size_t size
)
317 wchar_t *cwd
= _wgetcwd(buf
, GIT_WIN_PATH_UTF16
);
322 /* Convert the working directory back to UTF-8 */
323 if (git__utf16_to_8(buffer_out
, size
, cwd
) < 0) {
324 DWORD code
= GetLastError();
326 if (code
== ERROR_INSUFFICIENT_BUFFER
)
338 * Returns the address of the GetFinalPathNameByHandleW function.
339 * This function is available on Windows Vista and higher.
341 static PFGetFinalPathNameByHandleW
get_fpnbyhandle(void)
343 static PFGetFinalPathNameByHandleW pFunc
= NULL
;
344 PFGetFinalPathNameByHandleW toReturn
= pFunc
;
347 HMODULE hModule
= GetModuleHandleW(L
"kernel32");
350 toReturn
= (PFGetFinalPathNameByHandleW
)GetProcAddress(hModule
, "GetFinalPathNameByHandleW");
360 static int getfinalpath_w(
364 PFGetFinalPathNameByHandleW pgfp
= get_fpnbyhandle();
371 /* Use FILE_FLAG_BACKUP_SEMANTICS so we can open a directory. Do not
372 * specify FILE_FLAG_OPEN_REPARSE_POINT; we want to open a handle to the
373 * target of the link. */
374 hFile
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_DELETE
,
375 NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
377 if (INVALID_HANDLE_VALUE
== hFile
)
380 /* Call GetFinalPathNameByHandle */
381 dwChars
= pgfp(hFile
, dest
, GIT_WIN_PATH_UTF16
, FILE_NAME_NORMALIZED
);
384 if (!dwChars
|| dwChars
>= GIT_WIN_PATH_UTF16
)
387 /* The path may be delivered to us with a prefix; canonicalize */
388 return (int)git_win32__canonicalize_path(dest
, dwChars
);
391 static int follow_and_lstat_link(git_win32_path path
, struct stat
* buf
)
393 git_win32_path target_w
;
395 if (getfinalpath_w(target_w
, path
) < 0)
398 return lstat_w(target_w
, buf
, false);
401 int p_fstat(int fd
, struct stat
*buf
)
403 BY_HANDLE_FILE_INFORMATION fhInfo
;
405 HANDLE fh
= (HANDLE
)_get_osfhandle(fd
);
407 if (fh
== INVALID_HANDLE_VALUE
||
408 !GetFileInformationByHandle(fh
, &fhInfo
)) {
413 git_win32__file_information_to_stat(buf
, &fhInfo
);
417 int p_stat(const char* path
, struct stat
* buf
)
419 git_win32_path path_w
;
422 if ((len
= git_win32_path_from_utf8(path_w
, path
)) < 0 ||
423 lstat_w(path_w
, buf
, false) < 0)
426 /* The item is a symbolic link or mount point. No need to iterate
427 * to follow multiple links; use GetFinalPathNameFromHandle. */
428 if (S_ISLNK(buf
->st_mode
))
429 return follow_and_lstat_link(path_w
, buf
);
434 int p_chdir(const char* path
)
438 if (git_win32_path_from_utf8(buf
, path
) < 0)
444 int p_chmod(const char* path
, mode_t mode
)
448 if (git_win32_path_from_utf8(buf
, path
) < 0)
451 return _wchmod(buf
, mode
);
454 int p_rmdir(const char* path
)
459 if (git_win32_path_from_utf8(buf
, path
) < 0)
462 error
= _wrmdir(buf
);
465 switch (GetLastError()) {
466 /* _wrmdir() is documented to return EACCES if "A program has an open
467 * handle to the directory." This sounds like what everybody else calls
468 * EBUSY. Let's convert appropriate error codes.
470 case ERROR_SHARING_VIOLATION
:
474 /* This error can be returned when trying to rmdir an extant file. */
475 case ERROR_DIRECTORY
:
484 char *p_realpath(const char *orig_path
, char *buffer
)
486 git_win32_path orig_path_w
, buffer_w
;
488 if (git_win32_path_from_utf8(orig_path_w
, orig_path
) < 0)
491 /* Note that if the path provided is a relative path, then the current directory
492 * is used to resolve the path -- which is a concurrency issue because the current
493 * directory is a process-wide variable. */
494 if (!GetFullPathNameW(orig_path_w
, GIT_WIN_PATH_UTF16
, buffer_w
, NULL
)) {
495 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
496 errno
= ENAMETOOLONG
;
503 /* The path must exist. */
504 if (GetFileAttributesW(buffer_w
) == INVALID_FILE_ATTRIBUTES
) {
509 if (!buffer
&& !(buffer
= git__malloc(GIT_WIN_PATH_UTF8
))) {
514 /* Convert the path to UTF-8. If the caller provided a buffer, then it
515 * is assumed to be GIT_WIN_PATH_UTF8 characters in size. If it isn't,
516 * then we may overflow. */
517 if (git_win32_path_to_utf8(buffer
, buffer_w
) < 0)
520 git_path_mkposix(buffer
);
525 int p_vsnprintf(char *buffer
, size_t count
, const char *format
, va_list argptr
)
527 #if defined(_MSC_VER)
531 return _vscprintf(format
, argptr
);
534 len
= _vsnprintf_s(buffer
, count
, _TRUNCATE
, format
, argptr
);
536 len
= _vsnprintf(buffer
, count
, format
, argptr
);
540 return _vscprintf(format
, argptr
);
544 return vsnprintf(buffer
, count
, format
, argptr
);
548 int p_snprintf(char *buffer
, size_t count
, const char *format
, ...)
553 va_start(va
, format
);
554 r
= p_vsnprintf(buffer
, count
, format
, va
);
561 int p_mkstemp(char *tmp_path
)
563 #if defined(_MSC_VER) && _MSC_VER >= 1500
564 if (_mktemp_s(tmp_path
, strlen(tmp_path
) + 1) != 0)
567 if (_mktemp(tmp_path
) == NULL
)
571 return p_open(tmp_path
, O_RDWR
| O_CREAT
| O_EXCL
, 0744); //-V536
574 int p_access(const char* path
, mode_t mode
)
578 if (git_win32_path_from_utf8(buf
, path
) < 0)
581 return _waccess(buf
, mode
& WIN32_MODE_MASK
);
584 static int ensure_writable(wchar_t *fpath
)
588 attrs
= GetFileAttributesW(fpath
);
589 if (attrs
== INVALID_FILE_ATTRIBUTES
) {
590 if (GetLastError() == ERROR_FILE_NOT_FOUND
)
593 giterr_set(GITERR_OS
, "failed to get attributes");
597 if (!(attrs
& FILE_ATTRIBUTE_READONLY
))
600 attrs
&= ~FILE_ATTRIBUTE_READONLY
;
601 if (!SetFileAttributesW(fpath
, attrs
)) {
602 giterr_set(GITERR_OS
, "failed to set attributes");
609 int p_rename(const char *from
, const char *to
)
611 git_win32_path wfrom
;
614 int rename_succeeded
;
617 if (git_win32_path_from_utf8(wfrom
, from
) < 0 ||
618 git_win32_path_from_utf8(wto
, to
) < 0)
621 /* wait up to 50ms if file is locked by another thread or process */
623 rename_succeeded
= 0;
624 while (rename_tries
< 10) {
625 if (ensure_writable(wto
) == 0 &&
626 MoveFileExW(wfrom
, wto
, MOVEFILE_REPLACE_EXISTING
| MOVEFILE_COPY_ALLOWED
) != 0) {
627 rename_succeeded
= 1;
631 error
= GetLastError();
632 if (error
== ERROR_SHARING_VIOLATION
|| error
== ERROR_ACCESS_DENIED
) {
639 return rename_succeeded
? 0 : -1;
642 int p_recv(GIT_SOCKET socket
, void *buffer
, size_t length
, int flags
)
644 if ((size_t)((int)length
) != length
)
645 return -1; /* giterr_set will be done by caller */
647 return recv(socket
, buffer
, (int)length
, flags
);
650 int p_send(GIT_SOCKET socket
, const void *buffer
, size_t length
, int flags
)
652 if ((size_t)((int)length
) != length
)
653 return -1; /* giterr_set will be done by caller */
655 return send(socket
, buffer
, (int)length
, flags
);
659 * Borrowed from http://old.nabble.com/Porting-localtime_r-and-gmtime_r-td15282276.html
660 * On Win32, `gmtime_r` doesn't exist but `gmtime` is threadsafe, so we can use that
663 p_localtime_r (const time_t *timer
, struct tm
*result
)
665 struct tm
*local_result
;
666 local_result
= localtime (timer
);
668 if (local_result
== NULL
|| result
== NULL
)
671 memcpy (result
, local_result
, sizeof (struct tm
));
675 p_gmtime_r (const time_t *timer
, struct tm
*result
)
677 struct tm
*local_result
;
678 local_result
= gmtime (timer
);
680 if (local_result
== NULL
|| result
== NULL
)
683 memcpy (result
, local_result
, sizeof (struct tm
));
687 int p_inet_pton(int af
, const char *src
, void *dst
)
689 struct sockaddr_storage sin
;
691 int sin_len
= sizeof(struct sockaddr_storage
), addr_len
;
695 addr
= &((struct sockaddr_in
*)&sin
)->sin_addr
;
696 addr_len
= sizeof(struct in_addr
);
697 } else if (af
== AF_INET6
) {
698 addr
= &((struct sockaddr_in6
*)&sin
)->sin6_addr
;
699 addr_len
= sizeof(struct in6_addr
);
701 errno
= EAFNOSUPPORT
;
705 if ((error
= WSAStringToAddressA((LPSTR
)src
, af
, NULL
, (LPSOCKADDR
)&sin
, &sin_len
)) == 0) {
706 memcpy(dst
, addr
, addr_len
);
710 switch(WSAGetLastError()) {
716 case WSA_NOT_ENOUGH_MEMORY
: