| 279 | } |
| 280 | |
| 281 | static bool watcher_windows_git_candidate(const wchar_t *entry, size_t entry_length, |
| 282 | char output[CBM_SZ_4K]) { |
| 283 | while (entry_length > 0U && (entry[0] == L' ' || entry[0] == L'\t')) { |
| 284 | entry++; |
| 285 | entry_length--; |
| 286 | } |
| 287 | while (entry_length > 0U && |
| 288 | (entry[entry_length - 1U] == L' ' || entry[entry_length - 1U] == L'\t')) { |
| 289 | entry_length--; |
| 290 | } |
| 291 | if (entry_length >= 2U && entry[0] == L'"' && entry[entry_length - 1U] == L'"') { |
| 292 | entry++; |
| 293 | entry_length -= 2U; |
| 294 | } |
| 295 | while (entry_length > 0U && (entry[0] == L' ' || entry[0] == L'\t')) { |
| 296 | entry++; |
| 297 | entry_length--; |
| 298 | } |
| 299 | while (entry_length > 0U && |
| 300 | (entry[entry_length - 1U] == L' ' || entry[entry_length - 1U] == L'\t')) { |
| 301 | entry_length--; |
| 302 | } |
| 303 | if (entry_length == 0U || entry_length >= CBM_SZ_4K) { |
| 304 | return false; |
| 305 | } |
| 306 | wchar_t directory[CBM_SZ_4K]; |
| 307 | memcpy(directory, entry, entry_length * sizeof(*directory)); |
| 308 | directory[entry_length] = L'\0'; |
| 309 | if (!watcher_windows_path_absolute(directory) || wcschr(directory, L'"') != NULL) { |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | bool separator = directory[entry_length - 1U] == L'\\' || directory[entry_length - 1U] == L'/'; |
| 314 | wchar_t candidate[CBM_SZ_4K]; |
| 315 | int written = |
| 316 | swprintf(candidate, CBM_SZ_4K, separator ? L"%lsgit.exe" : L"%ls\\git.exe", directory); |
| 317 | if (written <= 0 || written >= CBM_SZ_4K) { |
| 318 | return false; |
| 319 | } |
| 320 | DWORD required = GetFullPathNameW(candidate, 0U, NULL, NULL); |
| 321 | wchar_t *normalized = |
| 322 | required > 0U ? malloc(((size_t)required + 1U) * sizeof(*normalized)) : NULL; |
| 323 | DWORD normalized_length = |
| 324 | normalized ? GetFullPathNameW(candidate, required + 1U, normalized, NULL) : 0U; |
| 325 | if (!normalized || normalized_length == 0U || normalized_length > required || |
| 326 | !watcher_windows_path_absolute(normalized)) { |
| 327 | free(normalized); |
| 328 | return false; |
| 329 | } |
| 330 | HANDLE file = CreateFileW(normalized, GENERIC_READ | FILE_READ_ATTRIBUTES, |
| 331 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 332 | OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 333 | BY_HANDLE_FILE_INFORMATION information; |
| 334 | bool regular = file != INVALID_HANDLE_VALUE && GetFileType(file) == FILE_TYPE_DISK && |
| 335 | GetFileInformationByHandle(file, &information) != 0 && |
| 336 | (information.dwFileAttributes & |
| 337 | (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)) == 0; |
| 338 | if (file != INVALID_HANDLE_VALUE) { |
no test coverage detected