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