1 | /* $Id: tstDir.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Directory listing.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 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/initterm.h>
|
---|
39 | #include <iprt/stream.h>
|
---|
40 | #include <iprt/err.h>
|
---|
41 | #include <iprt/path.h>
|
---|
42 | //#include <iprt/
|
---|
43 |
|
---|
44 | int main(int argc, char **argv)
|
---|
45 | {
|
---|
46 | int rcRet = 0;
|
---|
47 | RTR3InitExe(argc, &argv, 0);
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * Iterate arguments.
|
---|
51 | */
|
---|
52 | bool fLong = false;
|
---|
53 | bool fTimes = false;
|
---|
54 | bool fInode = false;
|
---|
55 | bool fShortName = false;
|
---|
56 | bool fFiltered = false;
|
---|
57 | bool fQuiet = false;
|
---|
58 | bool fNoFollow = false;
|
---|
59 | for (int i = 1; i < argc; i++)
|
---|
60 | {
|
---|
61 | if (argv[i][0] == '-')
|
---|
62 | {
|
---|
63 | for (int j = 1; argv[i][j]; j++)
|
---|
64 | {
|
---|
65 | switch (argv[i][j])
|
---|
66 | {
|
---|
67 | case 'l':
|
---|
68 | fLong = true;
|
---|
69 | break;
|
---|
70 | case 'i':
|
---|
71 | fLong = fInode = true;
|
---|
72 | break;
|
---|
73 | case 't':
|
---|
74 | fLong = fTimes = true;
|
---|
75 | break;
|
---|
76 | case 's':
|
---|
77 | fLong = fShortName = true;
|
---|
78 | break;
|
---|
79 | case 'f':
|
---|
80 | fFiltered = true;
|
---|
81 | break;
|
---|
82 | case 'q':
|
---|
83 | fQuiet = true;
|
---|
84 | break;
|
---|
85 | case 'H':
|
---|
86 | fNoFollow = true;
|
---|
87 | break;
|
---|
88 | default:
|
---|
89 | RTPrintf("Unknown option '%c' ignored!\n", argv[i][j]);
|
---|
90 | break;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | else
|
---|
95 | {
|
---|
96 | /* open */
|
---|
97 | RTDIR hDir;
|
---|
98 | int rc;
|
---|
99 | if (!fFiltered && !fNoFollow)
|
---|
100 | rc = RTDirOpen(&hDir, argv[i]);
|
---|
101 | else
|
---|
102 | rc = RTDirOpenFiltered(&hDir, argv[i], fFiltered ? RTDIRFILTER_WINNT : RTDIRFILTER_NONE,
|
---|
103 | fNoFollow ? RTDIR_F_NO_FOLLOW : 0);
|
---|
104 | if (RT_SUCCESS(rc))
|
---|
105 | {
|
---|
106 | /* list */
|
---|
107 | if (!fLong)
|
---|
108 | {
|
---|
109 | for (;;)
|
---|
110 | {
|
---|
111 | RTDIRENTRY DirEntry;
|
---|
112 | rc = RTDirRead(hDir, &DirEntry, NULL);
|
---|
113 | if (RT_FAILURE(rc))
|
---|
114 | break;
|
---|
115 | if (!fQuiet)
|
---|
116 | {
|
---|
117 | switch (DirEntry.enmType)
|
---|
118 | {
|
---|
119 | case RTDIRENTRYTYPE_UNKNOWN: RTPrintf("u"); break;
|
---|
120 | case RTDIRENTRYTYPE_FIFO: RTPrintf("f"); break;
|
---|
121 | case RTDIRENTRYTYPE_DEV_CHAR: RTPrintf("c"); break;
|
---|
122 | case RTDIRENTRYTYPE_DIRECTORY: RTPrintf("d"); break;
|
---|
123 | case RTDIRENTRYTYPE_DEV_BLOCK: RTPrintf("b"); break;
|
---|
124 | case RTDIRENTRYTYPE_FILE: RTPrintf("-"); break;
|
---|
125 | case RTDIRENTRYTYPE_SYMLINK: RTPrintf("l"); break;
|
---|
126 | case RTDIRENTRYTYPE_SOCKET: RTPrintf("s"); break;
|
---|
127 | case RTDIRENTRYTYPE_WHITEOUT: RTPrintf("w"); break;
|
---|
128 | default:
|
---|
129 | rcRet = 1;
|
---|
130 | RTPrintf("?");
|
---|
131 | break;
|
---|
132 | }
|
---|
133 | RTPrintf(" %#18llx %3d %s\n", (uint64_t)DirEntry.INodeId,
|
---|
134 | DirEntry.cbName, DirEntry.szName);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 | else
|
---|
139 | {
|
---|
140 | for (;;)
|
---|
141 | {
|
---|
142 | RTDIRENTRYEX DirEntry;
|
---|
143 | rc = RTDirReadEx(hDir, &DirEntry, NULL, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
|
---|
144 | if (RT_FAILURE(rc))
|
---|
145 | break;
|
---|
146 |
|
---|
147 | if (!fQuiet)
|
---|
148 | {
|
---|
149 | RTFMODE fMode = DirEntry.Info.Attr.fMode;
|
---|
150 | switch (fMode & RTFS_TYPE_MASK)
|
---|
151 | {
|
---|
152 | case RTFS_TYPE_FIFO: RTPrintf("f"); break;
|
---|
153 | case RTFS_TYPE_DEV_CHAR: RTPrintf("c"); break;
|
---|
154 | case RTFS_TYPE_DIRECTORY: RTPrintf("d"); break;
|
---|
155 | case RTFS_TYPE_DEV_BLOCK: RTPrintf("b"); break;
|
---|
156 | case RTFS_TYPE_FILE: RTPrintf("-"); break;
|
---|
157 | case RTFS_TYPE_SYMLINK: RTPrintf("l"); break;
|
---|
158 | case RTFS_TYPE_SOCKET: RTPrintf("s"); break;
|
---|
159 | case RTFS_TYPE_WHITEOUT: RTPrintf("w"); break;
|
---|
160 | default:
|
---|
161 | rcRet = 1;
|
---|
162 | RTPrintf("?");
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | /** @todo sticy bits++ */
|
---|
166 | RTPrintf("%c%c%c",
|
---|
167 | fMode & RTFS_UNIX_IRUSR ? 'r' : '-',
|
---|
168 | fMode & RTFS_UNIX_IWUSR ? 'w' : '-',
|
---|
169 | fMode & RTFS_UNIX_IXUSR ? 'x' : '-');
|
---|
170 | RTPrintf("%c%c%c",
|
---|
171 | fMode & RTFS_UNIX_IRGRP ? 'r' : '-',
|
---|
172 | fMode & RTFS_UNIX_IWGRP ? 'w' : '-',
|
---|
173 | fMode & RTFS_UNIX_IXGRP ? 'x' : '-');
|
---|
174 | RTPrintf("%c%c%c",
|
---|
175 | fMode & RTFS_UNIX_IROTH ? 'r' : '-',
|
---|
176 | fMode & RTFS_UNIX_IWOTH ? 'w' : '-',
|
---|
177 | fMode & RTFS_UNIX_IXOTH ? 'x' : '-');
|
---|
178 | RTPrintf(" %c%c%c%c%c%c%c%c%c%c%c%c%c%c",
|
---|
179 | fMode & RTFS_DOS_READONLY ? 'R' : '-',
|
---|
180 | fMode & RTFS_DOS_HIDDEN ? 'H' : '-',
|
---|
181 | fMode & RTFS_DOS_SYSTEM ? 'S' : '-',
|
---|
182 | fMode & RTFS_DOS_DIRECTORY ? 'D' : '-',
|
---|
183 | fMode & RTFS_DOS_ARCHIVED ? 'A' : '-',
|
---|
184 | fMode & RTFS_DOS_NT_DEVICE ? 'd' : '-',
|
---|
185 | fMode & RTFS_DOS_NT_NORMAL ? 'N' : '-',
|
---|
186 | fMode & RTFS_DOS_NT_TEMPORARY ? 'T' : '-',
|
---|
187 | fMode & RTFS_DOS_NT_SPARSE_FILE ? 'P' : '-',
|
---|
188 | fMode & RTFS_DOS_NT_REPARSE_POINT ? 'J' : '-',
|
---|
189 | fMode & RTFS_DOS_NT_COMPRESSED ? 'C' : '-',
|
---|
190 | fMode & RTFS_DOS_NT_OFFLINE ? 'O' : '-',
|
---|
191 | fMode & RTFS_DOS_NT_NOT_CONTENT_INDEXED ? 'I' : '-',
|
---|
192 | fMode & RTFS_DOS_NT_ENCRYPTED ? 'E' : '-');
|
---|
193 | RTPrintf(" %d %4d %4d %10lld %10lld",
|
---|
194 | DirEntry.Info.Attr.u.Unix.cHardlinks,
|
---|
195 | DirEntry.Info.Attr.u.Unix.uid,
|
---|
196 | DirEntry.Info.Attr.u.Unix.gid,
|
---|
197 | DirEntry.Info.cbObject,
|
---|
198 | DirEntry.Info.cbAllocated);
|
---|
199 | if (fTimes)
|
---|
200 | RTPrintf(" %#llx %#llx %#llx %#llx",
|
---|
201 | DirEntry.Info.BirthTime,
|
---|
202 | DirEntry.Info.ChangeTime,
|
---|
203 | DirEntry.Info.ModificationTime,
|
---|
204 | DirEntry.Info.AccessTime);
|
---|
205 |
|
---|
206 | if (fInode)
|
---|
207 | RTPrintf(" %#x:%#018llx",
|
---|
208 | DirEntry.Info.Attr.u.Unix.INodeIdDevice, DirEntry.Info.Attr.u.Unix.INodeId);
|
---|
209 | if (fShortName)
|
---|
210 | RTPrintf(" %2d %-12ls ", DirEntry.cwcShortName, DirEntry.wszShortName);
|
---|
211 | RTPrintf(" %2d %s\n", DirEntry.cbName, DirEntry.szName);
|
---|
212 | }
|
---|
213 | if (rc != VINF_SUCCESS)
|
---|
214 | RTPrintf("^^ %Rrc\n", rc);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | if (rc != VERR_NO_MORE_FILES)
|
---|
219 | {
|
---|
220 | RTPrintf("tstDir: Enumeration failed! rc=%Rrc\n", rc);
|
---|
221 | rcRet = 1;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /* close up */
|
---|
225 | rc = RTDirClose(hDir);
|
---|
226 | if (RT_FAILURE(rc))
|
---|
227 | {
|
---|
228 | RTPrintf("tstDir: Failed to close dir! rc=%Rrc\n", rc);
|
---|
229 | rcRet = 1;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | else
|
---|
233 | {
|
---|
234 | RTPrintf("tstDir: Failed to open '%s', rc=%Rrc\n", argv[i], rc);
|
---|
235 | rcRet = 1;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | return rcRet;
|
---|
241 | }
|
---|