VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstDir.cpp@ 26517

Last change on this file since 26517 was 25292, checked in by vboxsync, 15 years ago

RTDirReadEx parameter to resolve symlinks.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.6 KB
Line 
1/* $Id: tstDir.cpp 25292 2009-12-10 10:29:57Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Directory listing.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31#include <iprt/dir.h>
32#include <iprt/initterm.h>
33#include <iprt/stream.h>
34#include <iprt/err.h>
35#include <iprt/path.h>
36//#include <iprt/
37
38int main(int argc, char **argv)
39{
40 int rcRet = 0;
41 RTR3Init();
42
43 /*
44 * Iterate arguments.
45 */
46 bool fLong = false;
47 bool fShortName = 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 's':
60 fShortName = true;
61 break;
62 default:
63 RTPrintf("Unknown option '%c' ignored!\n", argv[i][j]);
64 break;
65 }
66 }
67 }
68 else
69 {
70 /* open */
71 PRTDIR pDir;
72 int rc = RTDirOpen(&pDir, argv[i]);
73 if (RT_SUCCESS(rc))
74 {
75 /* list */
76 if (!fLong)
77 {
78 for (;;)
79 {
80 RTDIRENTRY DirEntry;
81 rc = RTDirRead(pDir, &DirEntry, NULL);
82 if (RT_FAILURE(rc))
83 break;
84 switch (DirEntry.enmType)
85 {
86 case RTDIRENTRYTYPE_UNKNOWN: RTPrintf("u"); break;
87 case RTDIRENTRYTYPE_FIFO: RTPrintf("f"); break;
88 case RTDIRENTRYTYPE_DEV_CHAR: RTPrintf("c"); break;
89 case RTDIRENTRYTYPE_DIRECTORY: RTPrintf("d"); break;
90 case RTDIRENTRYTYPE_DEV_BLOCK: RTPrintf("b"); break;
91 case RTDIRENTRYTYPE_FILE: RTPrintf("-"); break;
92 case RTDIRENTRYTYPE_SYMLINK: RTPrintf("l"); break;
93 case RTDIRENTRYTYPE_SOCKET: RTPrintf("s"); break;
94 case RTDIRENTRYTYPE_WHITEOUT: RTPrintf("w"); break;
95 default:
96 rcRet = 1;
97 RTPrintf("?");
98 break;
99 }
100 RTPrintf(" %#18llx %3d %s\n", (uint64_t)DirEntry.INodeId,
101 DirEntry.cbName, DirEntry.szName);
102 }
103 }
104 else
105 {
106 for (;;)
107 {
108 RTDIRENTRYEX DirEntry;
109 rc = RTDirReadEx(pDir, &DirEntry, NULL, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
110 if (RT_FAILURE(rc))
111 break;
112
113 RTFMODE fMode = DirEntry.Info.Attr.fMode;
114 switch (fMode & RTFS_TYPE_MASK)
115 {
116 case RTFS_TYPE_FIFO: RTPrintf("f"); break;
117 case RTFS_TYPE_DEV_CHAR: RTPrintf("c"); break;
118 case RTFS_TYPE_DIRECTORY: RTPrintf("d"); break;
119 case RTFS_TYPE_DEV_BLOCK: RTPrintf("b"); break;
120 case RTFS_TYPE_FILE: RTPrintf("-"); break;
121 case RTFS_TYPE_SYMLINK: RTPrintf("l"); break;
122 case RTFS_TYPE_SOCKET: RTPrintf("s"); break;
123 case RTFS_TYPE_WHITEOUT: RTPrintf("w"); break;
124 default:
125 rcRet = 1;
126 RTPrintf("?");
127 break;
128 }
129 /** @todo sticy bits++ */
130 RTPrintf("%c%c%c",
131 fMode & RTFS_UNIX_IRUSR ? 'r' : '-',
132 fMode & RTFS_UNIX_IWUSR ? 'w' : '-',
133 fMode & RTFS_UNIX_IXUSR ? 'x' : '-');
134 RTPrintf("%c%c%c",
135 fMode & RTFS_UNIX_IRGRP ? 'r' : '-',
136 fMode & RTFS_UNIX_IWGRP ? 'w' : '-',
137 fMode & RTFS_UNIX_IXGRP ? 'x' : '-');
138 RTPrintf("%c%c%c",
139 fMode & RTFS_UNIX_IROTH ? 'r' : '-',
140 fMode & RTFS_UNIX_IWOTH ? 'w' : '-',
141 fMode & RTFS_UNIX_IXOTH ? 'x' : '-');
142 RTPrintf(" %c%c%c%c%c%c%c%c%c%c%c%c%c%c",
143 fMode & RTFS_DOS_READONLY ? 'R' : '-',
144 fMode & RTFS_DOS_HIDDEN ? 'H' : '-',
145 fMode & RTFS_DOS_SYSTEM ? 'S' : '-',
146 fMode & RTFS_DOS_DIRECTORY ? 'D' : '-',
147 fMode & RTFS_DOS_ARCHIVED ? 'A' : '-',
148 fMode & RTFS_DOS_NT_DEVICE ? 'd' : '-',
149 fMode & RTFS_DOS_NT_NORMAL ? 'N' : '-',
150 fMode & RTFS_DOS_NT_TEMPORARY ? 'T' : '-',
151 fMode & RTFS_DOS_NT_SPARSE_FILE ? 'P' : '-',
152 fMode & RTFS_DOS_NT_REPARSE_POINT ? 'J' : '-',
153 fMode & RTFS_DOS_NT_COMPRESSED ? 'C' : '-',
154 fMode & RTFS_DOS_NT_OFFLINE ? 'O' : '-',
155 fMode & RTFS_DOS_NT_NOT_CONTENT_INDEXED ? 'I' : '-',
156 fMode & RTFS_DOS_NT_ENCRYPTED ? 'E' : '-');
157 RTPrintf(" %d %4d %4d %10lld %10lld %#llx %#llx %#llx %#llx",
158 DirEntry.Info.Attr.u.Unix.cHardlinks,
159 DirEntry.Info.Attr.u.Unix.uid,
160 DirEntry.Info.Attr.u.Unix.gid,
161 DirEntry.Info.cbObject,
162 DirEntry.Info.cbAllocated,
163 DirEntry.Info.BirthTime,
164 DirEntry.Info.ChangeTime,
165 DirEntry.Info.ModificationTime,
166 DirEntry.Info.AccessTime);
167 if (fShortName && DirEntry.cwcShortName)
168 RTPrintf(" %2d %lS\n", DirEntry.cwcShortName, DirEntry.wszShortName);
169 else
170 RTPrintf(" %2d %s\n", DirEntry.cbName, DirEntry.szName);
171 if (rc != VINF_SUCCESS)
172 RTPrintf("^^ %Rrc\n", rc);
173 }
174 }
175
176 if (rc != VERR_NO_MORE_FILES)
177 {
178 RTPrintf("tstDir: Enumeration failed! rc=%Rrc\n", rc);
179 rcRet = 1;
180 }
181
182 /* close up */
183 rc = RTDirClose(pDir);
184 if (RT_FAILURE(rc))
185 {
186 RTPrintf("tstDir: Failed to close dir! rc=%Rrc\n", rc);
187 rcRet = 1;
188 }
189 }
190 else
191 {
192 RTPrintf("tstDir: Failed to open '%s', rc=%Rrc\n", argv[i], rc);
193 rcRet = 1;
194 }
195 }
196 }
197
198 return rcRet;
199}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette