1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7
8 #ifndef INCLUDE_win32_w32_util_h__
9 #define INCLUDE_win32_w32_util_h__
10
11 #include "common.h"
12
13 #include "utf-conv.h"
14 #include "posix.h"
15 #include "path_w32.h"
16
17 /*
18
19 #include "common.h"
20 #include "path.h"
21 #include "path_w32.h"
22 #include "utf-conv.h"
23 #include "posix.h"
24 #include "reparse.h"
25 #include "dir.h"
26 */
27
28
git_win32__isalpha(wchar_t c)29 GIT_INLINE(bool) git_win32__isalpha(wchar_t c)
30 {
31 return ((c >= L'A' && c <= L'Z') || (c >= L'a' && c <= L'z'));
32 }
33
34 /**
35 * Creates a FindFirstFile(Ex) filter string from a UTF-8 path.
36 * The filter string enumerates all items in the directory.
37 *
38 * @param dest The buffer to receive the filter string.
39 * @param src The UTF-8 path of the directory to enumerate.
40 * @return True if the filter string was created successfully; false otherwise
41 */
42 bool git_win32__findfirstfile_filter(git_win32_path dest, const char *src);
43
44 /**
45 * Ensures the given path (file or folder) has the +H (hidden) attribute set
46 * or unset.
47 *
48 * @param path The path that should receive the +H bit.
49 * @param hidden true to set +H, false to unset it
50 * @return 0 on success; -1 on failure
51 */
52 extern int git_win32__set_hidden(const char *path, bool hidden);
53
54 /**
55 * Determines if the given file or folder has the hidden attribute set.
56 * @param hidden pointer to store hidden value
57 * @param path The path that should be queried for hiddenness.
58 * @return 0 on success or an error code.
59 */
60 extern int git_win32__hidden(bool *hidden, const char *path);
61
62 extern int git_win32__file_attribute_to_stat(
63 struct stat *st,
64 const WIN32_FILE_ATTRIBUTE_DATA *attrdata,
65 const wchar_t *path);
66
67 /**
68 * Converts a FILETIME structure to a struct timespec.
69 *
70 * @param FILETIME A pointer to a FILETIME
71 * @param ts A pointer to the timespec structure to fill in
72 */
git_win32__filetime_to_timespec(const FILETIME * ft,struct timespec * ts)73 GIT_INLINE(void) git_win32__filetime_to_timespec(
74 const FILETIME *ft,
75 struct timespec *ts)
76 {
77 int64_t winTime = ((int64_t)ft->dwHighDateTime << 32) + ft->dwLowDateTime;
78 winTime -= INT64_C(116444736000000000); /* Windows to Unix Epoch conversion */
79 ts->tv_sec = (time_t)(winTime / 10000000);
80 #ifdef GIT_USE_NSEC
81 ts->tv_nsec = (winTime % 10000000) * 100;
82 #else
83 ts->tv_nsec = 0;
84 #endif
85 }
86
git_win32__timeval_to_filetime(FILETIME * ft,const struct p_timeval tv)87 GIT_INLINE(void) git_win32__timeval_to_filetime(
88 FILETIME *ft, const struct p_timeval tv)
89 {
90 int64_t ticks = (tv.tv_sec * INT64_C(10000000)) +
91 (tv.tv_usec * INT64_C(10)) + INT64_C(116444736000000000);
92
93 ft->dwHighDateTime = ((ticks >> 32) & INT64_C(0xffffffff));
94 ft->dwLowDateTime = (ticks & INT64_C(0xffffffff));
95 }
96
git_win32__stat_init(struct stat * st,DWORD dwFileAttributes,DWORD nFileSizeHigh,DWORD nFileSizeLow,FILETIME ftCreationTime,FILETIME ftLastAccessTime,FILETIME ftLastWriteTime)97 GIT_INLINE(void) git_win32__stat_init(
98 struct stat *st,
99 DWORD dwFileAttributes,
100 DWORD nFileSizeHigh,
101 DWORD nFileSizeLow,
102 FILETIME ftCreationTime,
103 FILETIME ftLastAccessTime,
104 FILETIME ftLastWriteTime)
105 {
106 mode_t mode = S_IREAD;
107
108 memset(st, 0, sizeof(struct stat));
109
110 if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
111 mode |= S_IFDIR;
112 else
113 mode |= S_IFREG;
114
115 if ((dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
116 mode |= S_IWRITE;
117
118 st->st_ino = 0;
119 st->st_gid = 0;
120 st->st_uid = 0;
121 st->st_nlink = 1;
122 st->st_mode = mode;
123 st->st_size = ((int64_t)nFileSizeHigh << 32) + nFileSizeLow;
124 st->st_dev = _getdrive() - 1;
125 st->st_rdev = st->st_dev;
126 git_win32__filetime_to_timespec(&ftLastAccessTime, &(st->st_atim));
127 git_win32__filetime_to_timespec(&ftLastWriteTime, &(st->st_mtim));
128 git_win32__filetime_to_timespec(&ftCreationTime, &(st->st_ctim));
129 }
130
git_win32__file_information_to_stat(struct stat * st,const BY_HANDLE_FILE_INFORMATION * fileinfo)131 GIT_INLINE(void) git_win32__file_information_to_stat(
132 struct stat *st,
133 const BY_HANDLE_FILE_INFORMATION *fileinfo)
134 {
135 git_win32__stat_init(st,
136 fileinfo->dwFileAttributes,
137 fileinfo->nFileSizeHigh,
138 fileinfo->nFileSizeLow,
139 fileinfo->ftCreationTime,
140 fileinfo->ftLastAccessTime,
141 fileinfo->ftLastWriteTime);
142 }
143
144 #endif
145