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