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