VirtualBox

source: vbox/trunk/include/iprt/dir.h@ 20355

Last change on this file since 20355 was 20111, checked in by vboxsync, 16 years ago

IPRT: Use the generic RTDirCreateTemp implementation - made it a bit more flexible and added a testcase for it.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.1 KB
Line 
1/** @file
2 * IPRT - Directory Manipulation.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 *
25 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___iprt_dir_h
31#define ___iprt_dir_h
32
33#include <iprt/cdefs.h>
34#include <iprt/types.h>
35#ifdef IN_RING3
36# include <iprt/fs.h>
37#endif
38
39
40__BEGIN_DECLS
41
42/** @defgroup grp_rt_dir RTDir - Directory Manipulation
43 * @ingroup grp_rt
44 * @{
45 */
46
47#ifdef IN_RING3
48
49/**
50 * Check for the existence of a directory.
51 *
52 * @returns true if exist and is a directory.
53 * @returns flase if exists or isn't a directory.
54 * @param pszPath Path to the directory.
55 */
56RTDECL(bool) RTDirExists(const char *pszPath);
57
58/**
59 * Creates a directory.
60 *
61 * @returns iprt status code.
62 * @param pszPath Path to the directory to create.
63 * @param fMode The mode of the new directory.
64 */
65RTDECL(int) RTDirCreate(const char *pszPath, RTFMODE fMode);
66
67/**
68 * Creates a directory including all parent directories in the path
69 * if they don't exist.
70 *
71 * @returns iprt status code.
72 * @param pszPath Path to the directory to create.
73 * @param fMode The mode of the new directories.
74 */
75RTDECL(int) RTDirCreateFullPath(const char *pszPath, RTFMODE fMode);
76
77/**
78 * Creates a new directory with a unique name using the given template.
79 *
80 * One or more trailing X'es in the template will be replaced by random alpha
81 * numeric characters until a RTDirCreate succeeds or we run out of patience.
82 * For instance:
83 * "/tmp/myprog-XXXXXX"
84 *
85 * As an alternative to trailing X'es, it
86 * is possible to put 3 or more X'es somewhere inside the directory name. In
87 * the following string only the last bunch of X'es will be modified:
88 * "/tmp/myprog-XXX-XXX.tmp"
89 *
90 * The directory is created with mode 0700.
91 *
92 * @returns iprt status code.
93 * @param pszTemplate The directory name template on input. The actual
94 * directory name on success. Empty string on failure.
95 */
96RTDECL(int) RTDirCreateTemp(char *pszTemplate);
97
98/**
99 * Removes a directory if empty.
100 *
101 * @returns iprt status code.
102 * @param pszPath Path to the directory to remove.
103 */
104RTDECL(int) RTDirRemove(const char *pszPath);
105
106/**
107 * Removes a directory tree recursively.
108 *
109 * @returns iprt status code.
110 * @param pszPath Path to the directory to remove recursively.
111 */
112RTDECL(int) RTDirRemoveRecursive(const char *pszPath);
113
114
115/** Pointer to an open directory (sort of handle). */
116typedef struct RTDIR *PRTDIR;
117
118
119/**
120 * Filter option for RTDirOpenFiltered().
121 */
122typedef enum RTDIRFILTER
123{
124 /** The usual invalid 0 entry. */
125 RTDIRFILTER_INVALID = 0,
126 /** No filter should be applied (and none was specified). */
127 RTDIRFILTER_NONE,
128 /** The Windows NT filter.
129 * The following wildcard chars: *, ?, <, > and "
130 * The matching is done on the uppercased strings. */
131 RTDIRFILTER_WINNT,
132 /** The UNIX filter.
133 * The following wildcard chars: *, ?, [..]
134 * The matching is done on exact case. */
135 RTDIRFILTER_UNIX,
136 /** The UNIX filter, uppercased matching.
137 * Same as RTDIRFILTER_UNIX except that the strings are uppercased before comparing. */
138 RTDIRFILTER_UNIX_UPCASED,
139
140 /** The usual full 32-bit value. */
141 RTDIRFILTER_32BIT_HACK = 0x7fffffff
142} RTDIRFILTER;
143
144
145/**
146 * Directory entry type.
147 *
148 * This is the RTFS_TYPE_MASK stuff shifted down 12 bits and
149 * identical to the BSD/LINUX ABI.
150 */
151typedef enum RTDIRENTRYTYPE
152{
153 /** Unknown type (DT_UNKNOWN). */
154 RTDIRENTRYTYPE_UNKNOWN = 0,
155 /** Named pipe (fifo) (DT_FIFO). */
156 RTDIRENTRYTYPE_FIFO = 001,
157 /** Character device (DT_CHR). */
158 RTDIRENTRYTYPE_DEV_CHAR = 002,
159 /** Directory (DT_DIR). */
160 RTDIRENTRYTYPE_DIRECTORY = 004,
161 /** Block device (DT_BLK). */
162 RTDIRENTRYTYPE_DEV_BLOCK = 006,
163 /** Regular file (DT_REG). */
164 RTDIRENTRYTYPE_FILE = 010,
165 /** Symbolic link (DT_LNK). */
166 RTDIRENTRYTYPE_SYMLINK = 012,
167 /** Socket (DT_SOCK). */
168 RTDIRENTRYTYPE_SOCKET = 014,
169 /** Whiteout (DT_WHT). */
170 RTDIRENTRYTYPE_WHITEOUT = 016
171} RTDIRENTRYTYPE;
172
173
174/**
175 * Directory entry.
176 *
177 * This is inspired by the POSIX interfaces.
178 */
179#pragma pack(1)
180typedef struct RTDIRENTRY
181{
182 /** The unique identifier (within the file system) of this file system object (d_ino).
183 * Together with INodeIdDevice, this field can be used as a OS wide unique id
184 * when both their values are not 0.
185 * This field is 0 if the information is not available. */
186 RTINODE INodeId;
187 /** The entry type. (d_type)
188 * RTDIRENTRYTYPE_UNKNOWN is a legal return value here and
189 * should be expected by the user. */
190 RTDIRENTRYTYPE enmType;
191 /** The length of the filename. */
192 uint16_t cbName;
193 /** The filename. (no path)
194 * Using the pcbDirEntry parameter of RTDirRead makes this field variable in size. */
195 char szName[260];
196} RTDIRENTRY;
197#pragma pack()
198/** Pointer to a directory entry. */
199typedef RTDIRENTRY *PRTDIRENTRY;
200
201
202/**
203 * Directory entry with extended information.
204 *
205 * This is inspired by the PC interfaces.
206 */
207#pragma pack(1)
208typedef struct RTDIRENTRYEX
209{
210 /** Full information about the object. */
211 RTFSOBJINFO Info;
212 /** The length of the short field (number of RTUTF16 entries (not chars)).
213 * It is 16-bit for reasons of alignment. */
214 uint16_t cwcShortName;
215 /** The short name for 8.3 compatability.
216 * Empty string if not available.
217 * Since the length is a bit tricky for a UTF-8 encoded name, and since this
218 * is practically speaking only a windows thing, it is encoded as UCS-2. */
219 RTUTF16 wszShortName[14];
220 /** The length of the filename. */
221 uint16_t cbName;
222 /** The filename. (no path)
223 * Using the pcbDirEntry parameter of RTDirReadEx makes this field variable in size. */
224 char szName[260];
225} RTDIRENTRYEX;
226#pragma pack()
227/** Pointer to a directory entry. */
228typedef RTDIRENTRYEX *PRTDIRENTRYEX;
229
230
231/**
232 * Opens a directory.
233 *
234 * @returns iprt status code.
235 * @param ppDir Where to store the open directory pointer.
236 * @param pszPath Path to the directory to open.
237 */
238RTDECL(int) RTDirOpen(PRTDIR *ppDir, const char *pszPath);
239
240/**
241 * Opens a directory filtering the entries using dos style wildcards.
242 *
243 * @returns iprt status code.
244 * @param ppDir Where to store the open directory pointer.
245 * @param pszPath Path to the directory to search, this must include wildcards.
246 * @param enmFilter The kind of filter to apply. Setting this to RTDIRFILTER_NONE makes
247 * this function behave like RTDirOpen.
248 */
249RTDECL(int) RTDirOpenFiltered(PRTDIR *ppDir, const char *pszPath, RTDIRFILTER enmFilter);
250
251/**
252 * Closes a directory.
253 *
254 * @returns iprt status code.
255 * @param pDir Pointer to open directory returned by RTDirOpen() or RTDirOpenFiltered().
256 */
257RTDECL(int) RTDirClose(PRTDIR pDir);
258
259/**
260 * Reads the next entry in the directory.
261 *
262 * @returns VINF_SUCCESS and data in pDirEntry on success.
263 * @returns VERR_NO_MORE_FILES when the end of the directory has been reached.
264 * @returns VERR_BUFFER_OVERFLOW if the buffer is too small to contain the filename. If
265 * pcbDirEntry is specified it will be updated with the required buffer size.
266 * @returns suitable iprt status code on other errors.
267 *
268 * @param pDir Pointer to the open directory.
269 * @param pDirEntry Where to store the information about the next
270 * directory entry on success.
271 * @param pcbDirEntry Optional parameter used for variable buffer size.
272 *
273 * On input the variable pointed to contains the size of the pDirEntry
274 * structure. This must be at least OFFSET(RTDIRENTRY, szName[2]) bytes.
275 *
276 * On successful output the field is updated to
277 * OFFSET(RTDIRENTRY, szName[pDirEntry->cbName + 1]).
278 *
279 * When the data doesn't fit in the buffer and VERR_BUFFER_OVERFLOW is
280 * returned, this field contains the required buffer size.
281 *
282 * The value is unchanged in all other cases.
283 */
284RTDECL(int) RTDirRead(PRTDIR pDir, PRTDIRENTRY pDirEntry, size_t *pcbDirEntry);
285
286/**
287 * Reads the next entry in the directory returning extended information.
288 *
289 * @returns VINF_SUCCESS and data in pDirEntry on success.
290 * @returns VERR_NO_MORE_FILES when the end of the directory has been reached.
291 * @returns VERR_BUFFER_OVERFLOW if the buffer is too small to contain the filename. If
292 * pcbDirEntry is specified it will be updated with the required buffer size.
293 * @returns suitable iprt status code on other errors.
294 *
295 * @param pDir Pointer to the open directory.
296 * @param pDirEntry Where to store the information about the next
297 * directory entry on success.
298 * @param pcbDirEntry Optional parameter used for variable buffer size.
299 *
300 * On input the variable pointed to contains the size of the pDirEntry
301 * structure. This must be at least OFFSET(RTDIRENTRYEX, szName[2]) bytes.
302 *
303 * On successful output the field is updated to
304 * OFFSET(RTDIRENTRYEX, szName[pDirEntry->cbName + 1]).
305 *
306 * When the data doesn't fit in the buffer and VERR_BUFFER_OVERFLOW is
307 * returned, this field contains the required buffer size.
308 *
309 * The value is unchanged in all other cases.
310 * @param enmAdditionalAttribs
311 * Which set of additional attributes to request.
312 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
313 */
314RTDECL(int) RTDirReadEx(PRTDIR pDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAdditionalAttribs);
315
316
317/**
318 * Renames a file.
319 *
320 * Identical to RTPathRename except that it will ensure that the source is a directory.
321 *
322 * @returns IPRT status code.
323 * @returns VERR_ALREADY_EXISTS if the destination file exists.
324 *
325 * @param pszSrc The path to the source file.
326 * @param pszDst The path to the destination file.
327 * This file will be created.
328 * @param fRename See RTPathRename.
329 */
330RTDECL(int) RTDirRename(const char *pszSrc, const char *pszDst, unsigned fRename);
331
332
333/**
334 * Query information about an open directory.
335 *
336 * @returns iprt status code.
337 *
338 * @param pDir Pointer to the open directory.
339 * @param pObjInfo Object information structure to be filled on successful return.
340 * @param enmAdditionalAttribs Which set of additional attributes to request.
341 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
342 */
343RTR3DECL(int) RTDirQueryInfo(PRTDIR pDir, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs);
344
345
346/**
347 * Changes one or more of the timestamps associated of file system object.
348 *
349 * @returns iprt status code.
350 * @returns VERR_NOT_SUPPORTED is returned if the operation isn't supported by the OS.
351 *
352 * @param pDir Pointer to the open directory.
353 * @param pAccessTime Pointer to the new access time. NULL if not to be changed.
354 * @param pModificationTime Pointer to the new modifcation time. NULL if not to be changed.
355 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
356 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
357 *
358 * @remark The file system might not implement all these time attributes,
359 * the API will ignore the ones which aren't supported.
360 *
361 * @remark The file system might not implement the time resolution
362 * employed by this interface, the time will be chopped to fit.
363 *
364 * @remark The file system may update the change time even if it's
365 * not specified.
366 *
367 * @remark POSIX can only set Access & Modification and will always set both.
368 */
369RTR3DECL(int) RTDirSetTimes(PRTDIR pDir, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
370 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime);
371
372#endif /* IN_RING3 */
373/** @} */
374
375__END_DECLS
376
377#endif
378
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