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