1 | /* $Id: tstDir-3.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Directory listing & filtering (no parameters needed).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <iprt/dir.h>
|
---|
38 | #include <iprt/path.h>
|
---|
39 | #include <iprt/stream.h>
|
---|
40 | #include <iprt/err.h>
|
---|
41 | #include <iprt/initterm.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 |
|
---|
44 | static int tstDirOpenFiltered(const char *pszFilter, unsigned *pcFilesMatch, int *pRc)
|
---|
45 | {
|
---|
46 | int rcRet = 0;
|
---|
47 | unsigned cFilesMatch = 0;
|
---|
48 | RTDIR hDir;
|
---|
49 | int rc = RTDirOpenFiltered(&hDir, pszFilter, RTDIRFILTER_WINNT, 0 /*fFlags*/);
|
---|
50 | if (RT_SUCCESS(rc))
|
---|
51 | {
|
---|
52 | for (;;)
|
---|
53 | {
|
---|
54 | RTDIRENTRY DirEntry;
|
---|
55 | rc = RTDirRead(hDir, &DirEntry, NULL);
|
---|
56 | if (RT_FAILURE(rc))
|
---|
57 | break;
|
---|
58 | cFilesMatch++;
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (rc != VERR_NO_MORE_FILES)
|
---|
62 | {
|
---|
63 | RTPrintf("tstDir-3: Enumeration '%s' failed! rc=%Rrc\n", pszFilter, rc);
|
---|
64 | rcRet = 1;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /* close up */
|
---|
68 | rc = RTDirClose(hDir);
|
---|
69 | if (RT_FAILURE(rc))
|
---|
70 | {
|
---|
71 | RTPrintf("tstDir-3: Failed to close dir '%s'! rc=%Rrc\n", pszFilter, rc);
|
---|
72 | rcRet = 1;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | else
|
---|
76 | {
|
---|
77 | RTPrintf("tstDir-3: Failed to open '%s', rc=%Rrc\n", pszFilter, rc);
|
---|
78 | rcRet = 1;
|
---|
79 | }
|
---|
80 |
|
---|
81 | *pcFilesMatch = cFilesMatch;
|
---|
82 | *pRc = rc;
|
---|
83 | return rcRet;
|
---|
84 | }
|
---|
85 |
|
---|
86 | int main(int argc, char **argv)
|
---|
87 | {
|
---|
88 | int rcRet = 0;
|
---|
89 | int rcRet2;
|
---|
90 | int rc;
|
---|
91 | unsigned cMatch;
|
---|
92 | RTR3InitExe(argc, &argv, 0);
|
---|
93 |
|
---|
94 | const char *pszTestDir = ".";
|
---|
95 |
|
---|
96 | char *pszFilter1 = RTPathJoinA(pszTestDir, "xyxzxq*");
|
---|
97 | if (!pszFilter1)
|
---|
98 | {
|
---|
99 | RTPrintf("tstDir-3: cannot create non-match filter!\n");
|
---|
100 | return 1;
|
---|
101 | }
|
---|
102 |
|
---|
103 | char *pszFilter2 = RTPathJoinA(pszTestDir, "*");
|
---|
104 | if (!pszFilter2)
|
---|
105 | {
|
---|
106 | RTPrintf("tstDir-3: cannot create match filter!\n");
|
---|
107 | RTStrFree(pszFilter1);
|
---|
108 | return 1;
|
---|
109 | }
|
---|
110 |
|
---|
111 | rcRet2 = tstDirOpenFiltered(pszFilter1, &cMatch, &rc);
|
---|
112 | if (rcRet2)
|
---|
113 | rcRet = rcRet2;
|
---|
114 | if (RT_FAILURE(rc))
|
---|
115 | RTPrintf("tstDir-3: filter '%s' failed! rc=%Rrc\n", pszFilter1, rc);
|
---|
116 | if (cMatch)
|
---|
117 | RTPrintf("tstDir-3: filter '%s' gave wrong result count! cMatch=%u\n", pszFilter1, cMatch);
|
---|
118 |
|
---|
119 | rcRet2 = tstDirOpenFiltered(pszFilter2, &cMatch, &rc);
|
---|
120 | if (rcRet2)
|
---|
121 | rcRet = rcRet2;
|
---|
122 | if (RT_FAILURE(rc))
|
---|
123 | RTPrintf("tstDir-3: filter '%s' failed! rc=%Rrc\n", pszFilter2, rc);
|
---|
124 | if (!cMatch)
|
---|
125 | RTPrintf("tstDir-3: filter '%s' gave wrong result count! cMatch=%u\n", pszFilter2, cMatch);
|
---|
126 |
|
---|
127 | RTStrFree(pszFilter2);
|
---|
128 | RTStrFree(pszFilter1);
|
---|
129 |
|
---|
130 | if (!rcRet)
|
---|
131 | RTPrintf("tstDir-3: OK\n");
|
---|
132 | return rcRet;
|
---|
133 | }
|
---|