|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "win_dirent.h" |
|
8 #include <errno.h> |
|
9 #include <string.h> |
|
10 |
|
11 // This file implements the minimum set of dirent APIs used by updater.cpp on |
|
12 // Windows. If updater.cpp is modified to use more of this API, we need to |
|
13 // implement those parts here too. |
|
14 |
|
15 static dirent gDirEnt; |
|
16 |
|
17 DIR::DIR(const WCHAR* path) |
|
18 : findHandle(INVALID_HANDLE_VALUE) |
|
19 { |
|
20 memset(name, 0, sizeof(name)); |
|
21 wcsncpy(name, path, sizeof(name)/sizeof(name[0])); |
|
22 wcsncat(name, L"\\*", sizeof(name)/sizeof(name[0]) - wcslen(name) - 1); |
|
23 } |
|
24 |
|
25 DIR::~DIR() |
|
26 { |
|
27 if (findHandle != INVALID_HANDLE_VALUE) { |
|
28 FindClose(findHandle); |
|
29 } |
|
30 } |
|
31 |
|
32 dirent::dirent() |
|
33 { |
|
34 d_name[0] = L'\0'; |
|
35 } |
|
36 |
|
37 DIR* |
|
38 opendir(const WCHAR* path) |
|
39 { |
|
40 return new DIR(path); |
|
41 } |
|
42 |
|
43 int |
|
44 closedir(DIR* dir) |
|
45 { |
|
46 delete dir; |
|
47 return 0; |
|
48 } |
|
49 |
|
50 dirent* readdir(DIR* dir) |
|
51 { |
|
52 WIN32_FIND_DATAW data; |
|
53 if (dir->findHandle != INVALID_HANDLE_VALUE) { |
|
54 BOOL result = FindNextFileW(dir->findHandle, &data); |
|
55 if (!result) { |
|
56 if (GetLastError() != ERROR_FILE_NOT_FOUND) { |
|
57 errno = ENOENT; |
|
58 } |
|
59 return 0; |
|
60 } |
|
61 } else { |
|
62 // Reading the first directory entry |
|
63 dir->findHandle = FindFirstFileW(dir->name, &data); |
|
64 if (dir->findHandle == INVALID_HANDLE_VALUE) { |
|
65 if (GetLastError() == ERROR_FILE_NOT_FOUND) { |
|
66 errno = ENOENT; |
|
67 } else { |
|
68 errno = EBADF; |
|
69 } |
|
70 return 0; |
|
71 } |
|
72 } |
|
73 memset(gDirEnt.d_name, 0, sizeof(gDirEnt.d_name)); |
|
74 wcsncpy(gDirEnt.d_name, data.cFileName, |
|
75 sizeof(gDirEnt.d_name)/sizeof(gDirEnt.d_name[0])); |
|
76 return &gDirEnt; |
|
77 } |
|
78 |