VirtualBox

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

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

RTDirRemoveRecursive: Fixed memory leak, made it deal with UNKNOWN and thus work on Linux, optimized stack usage, added flag for only removing the content of the specified directory. TXS: clean up the scratch directory.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.9 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
40RT_C_DECLS_BEGIN
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 * All symbolic links will be attemped resolved. If that is undesirable, please
53 * use RTPathQueryInfo instead.
54 *
55 * @returns true if exist and is a directory.
56 * @returns false if not exists or isn't a directory.
57 * @param pszPath Path to the directory.
58 */
59RTDECL(bool) RTDirExists(const char *pszPath);
60
61/**
62 * Creates a directory.
63 *
64 * @returns iprt status code.
65 * @param pszPath Path to the directory to create.
66 * @param fMode The mode of the new directory.
67 */
68RTDECL(int) RTDirCreate(const char *pszPath, RTFMODE fMode);
69
70/**
71 * Creates a directory including all parent directories in the path
72 * if they don't exist.
73 *
74 * @returns iprt status code.
75 * @param pszPath Path to the directory to create.
76 * @param fMode The mode of the new directories.
77 */
78RTDECL(int) RTDirCreateFullPath(const char *pszPath, RTFMODE fMode);
79
80/**
81 * Creates a new directory with a unique name using the given template.
82 *
83 * One or more trailing X'es in the template will be replaced by random alpha
84 * numeric characters until a RTDirCreate succeeds or we run out of patience.
85 * For instance:
86 * "/tmp/myprog-XXXXXX"
87 *
88 * As an alternative to trailing X'es, it
89 * is possible to put 3 or more X'es somewhere inside the directory name. In
90 * the following string only the last bunch of X'es will be modified:
91 * "/tmp/myprog-XXX-XXX.tmp"
92 *
93 * The directory is created with mode 0700.
94 *
95 * @returns iprt status code.
96 * @param pszTemplate The directory name template on input. The actual
97 * directory name on success. Empty string on failure.
98 */
99RTDECL(int) RTDirCreateTemp(char *pszTemplate);
100
101/**
102 * Removes a directory if empty.
103 *
104 * @returns iprt status code.
105 * @param pszPath Path to the directory to remove.
106 */
107RTDECL(int) RTDirRemove(const char *pszPath);
108
109/**
110 * Removes a directory tree recursively.
111 *
112 * @returns iprt status code.
113 * @param pszPath Path to the directory to remove recursively.
114 * @param fFlags Flags, see RTDIRRMREC_F_XXX.
115 */
116RTDECL(int) RTDirRemoveRecursive(const char *pszPath, uint32_t fFlags);
117
118/** @name RTDirRemoveRecursive flags.
119 * @{ */
120/** Only delete the content of the directory, omit the directory it self. */
121#define RTDIRRMREC_F_CONTENT_ONLY RT_BIT_32(0)
122/** Mask of valid flags. */
123#define RTDIRRMREC_F_VALID_MASK UINT32_C(0x00000001)
124/** @} */
125
126
127/** Pointer to an open directory (sort of handle). */
128typedef struct RTDIR *PRTDIR;
129
130
131/**
132 * Filter option for RTDirOpenFiltered().
133 */
134typedef enum RTDIRFILTER
135{
136 /** The usual invalid 0 entry. */
137 RTDIRFILTER_INVALID = 0,
138 /** No filter should be applied (and none was specified). */
139 RTDIRFILTER_NONE,
140 /** The Windows NT filter.
141 * The following wildcard chars: *, ?, <, > and "
142 * The matching is done on the uppercased strings. */
143 RTDIRFILTER_WINNT,
144 /** The UNIX filter.
145 * The following wildcard chars: *, ?, [..]
146 * The matching is done on exact case. */
147 RTDIRFILTER_UNIX,
148 /** The UNIX filter, uppercased matching.
149 * Same as RTDIRFILTER_UNIX except that the strings are uppercased before comparing. */
150 RTDIRFILTER_UNIX_UPCASED,
151
152 /** The usual full 32-bit value. */
153 RTDIRFILTER_32BIT_HACK = 0x7fffffff
154} RTDIRFILTER;
155
156
157/**
158 * Directory entry type.
159 *
160 * This is the RTFS_TYPE_MASK stuff shifted down 12 bits and
161 * identical to the BSD/LINUX ABI.
162 */
163typedef enum RTDIRENTRYTYPE
164{
165 /** Unknown type (DT_UNKNOWN). */
166 RTDIRENTRYTYPE_UNKNOWN = 0,
167 /** Named pipe (fifo) (DT_FIFO). */
168 RTDIRENTRYTYPE_FIFO = 001,
169 /** Character device (DT_CHR). */
170 RTDIRENTRYTYPE_DEV_CHAR = 002,
171 /** Directory (DT_DIR). */
172 RTDIRENTRYTYPE_DIRECTORY = 004,
173 /** Block device (DT_BLK). */
174 RTDIRENTRYTYPE_DEV_BLOCK = 006,
175 /** Regular file (DT_REG). */
176 RTDIRENTRYTYPE_FILE = 010,
177 /** Symbolic link (DT_LNK). */
178 RTDIRENTRYTYPE_SYMLINK = 012,
179 /** Socket (DT_SOCK). */
180 RTDIRENTRYTYPE_SOCKET = 014,
181 /** Whiteout (DT_WHT). */
182 RTDIRENTRYTYPE_WHITEOUT = 016
183} RTDIRENTRYTYPE;
184
185
186/**
187 * Directory entry.
188 *
189 * This is inspired by the POSIX interfaces.
190 */
191#pragma pack(1)
192typedef struct RTDIRENTRY
193{
194 /** The unique identifier (within the file system) of this file system object (d_ino).
195 *
196 * Together with INodeIdDevice, this field can be used as a OS wide unique id
197 * when both their values are not 0. This field is 0 if the information is not
198 * available. */
199 RTINODE INodeId;
200 /** The entry type. (d_type)
201 * RTDIRENTRYTYPE_UNKNOWN is a common return value here since not all file
202 * systems (or Unixes) stores the type of a directory entry and instead
203 * expects the user to use stat() to get it. So, when you see this you
204 * should use RTPathQueryInfo to get the type, or if if you're lazy, use
205 * RTDirReadEx. */
206 RTDIRENTRYTYPE enmType;
207 /** The length of the filename, excluding the terminating nul character. */
208 uint16_t cbName;
209 /** The filename. (no path)
210 * Using the pcbDirEntry parameter of RTDirRead makes this field variable in size. */
211 char szName[260];
212} RTDIRENTRY;
213#pragma pack()
214/** Pointer to a directory entry. */
215typedef RTDIRENTRY *PRTDIRENTRY;
216
217
218/**
219 * Directory entry with extended information.
220 *
221 * This is inspired by the PC interfaces.
222 */
223#pragma pack(1)
224typedef struct RTDIRENTRYEX
225{
226 /** Full information about the object. */
227 RTFSOBJINFO Info;
228 /** The length of the short field (number of RTUTF16 entries (not chars)).
229 * It is 16-bit for reasons of alignment. */
230 uint16_t cwcShortName;
231 /** The short name for 8.3 compatability.
232 * Empty string if not available.
233 * Since the length is a bit tricky for a UTF-8 encoded name, and since this
234 * is practically speaking only a windows thing, it is encoded as UCS-2. */
235 RTUTF16 wszShortName[14];
236 /** The length of the filename. */
237 uint16_t cbName;
238 /** The filename. (no path)
239 * Using the pcbDirEntry parameter of RTDirReadEx makes this field variable in size. */
240 char szName[260];
241} RTDIRENTRYEX;
242#pragma pack()
243/** Pointer to a directory entry. */
244typedef RTDIRENTRYEX *PRTDIRENTRYEX;
245
246
247/**
248 * Opens a directory.
249 *
250 * @returns iprt status code.
251 * @param ppDir Where to store the open directory pointer.
252 * @param pszPath Path to the directory to open.
253 */
254RTDECL(int) RTDirOpen(PRTDIR *ppDir, const char *pszPath);
255
256/**
257 * Opens a directory filtering the entries using dos style wildcards.
258 *
259 * @returns iprt status code.
260 * @param ppDir Where to store the open directory pointer.
261 * @param pszPath Path to the directory to search, this must include wildcards.
262 * @param enmFilter The kind of filter to apply. Setting this to RTDIRFILTER_NONE makes
263 * this function behave like RTDirOpen.
264 */
265RTDECL(int) RTDirOpenFiltered(PRTDIR *ppDir, const char *pszPath, RTDIRFILTER enmFilter);
266
267/**
268 * Closes a directory.
269 *
270 * @returns iprt status code.
271 * @param pDir Pointer to open directory returned by RTDirOpen() or RTDirOpenFiltered().
272 */
273RTDECL(int) RTDirClose(PRTDIR pDir);
274
275/**
276 * Reads the next entry in the directory.
277 *
278 * @returns VINF_SUCCESS and data in pDirEntry on success.
279 * @returns VERR_NO_MORE_FILES when the end of the directory has been reached.
280 * @returns VERR_BUFFER_OVERFLOW if the buffer is too small to contain the filename. If
281 * pcbDirEntry is specified it will be updated with the required buffer size.
282 * @returns suitable iprt status code on other errors.
283 *
284 * @param pDir Pointer to the open directory.
285 * @param pDirEntry Where to store the information about the next
286 * directory entry on success.
287 * @param pcbDirEntry Optional parameter used for variable buffer size.
288 *
289 * On input the variable pointed to contains the size of the pDirEntry
290 * structure. This must be at least OFFSET(RTDIRENTRY, szName[2]) bytes.
291 *
292 * On successful output the field is updated to
293 * OFFSET(RTDIRENTRY, szName[pDirEntry->cbName + 1]).
294 *
295 * When the data doesn't fit in the buffer and VERR_BUFFER_OVERFLOW is
296 * returned, this field contains the required buffer size.
297 *
298 * The value is unchanged in all other cases.
299 */
300RTDECL(int) RTDirRead(PRTDIR pDir, PRTDIRENTRY pDirEntry, size_t *pcbDirEntry);
301
302/**
303 * Reads the next entry in the directory returning extended information.
304 *
305 * @returns VINF_SUCCESS and data in pDirEntry on success.
306 * @returns VERR_NO_MORE_FILES when the end of the directory has been reached.
307 * @returns VERR_BUFFER_OVERFLOW if the buffer is too small to contain the filename. If
308 * pcbDirEntry is specified it will be updated with the required buffer size.
309 * @returns suitable iprt status code on other errors.
310 *
311 * @param pDir Pointer to the open directory.
312 * @param pDirEntry Where to store the information about the next
313 * directory entry on success.
314 * @param pcbDirEntry Optional parameter used for variable buffer size.
315 *
316 * On input the variable pointed to contains the size of the pDirEntry
317 * structure. This must be at least OFFSET(RTDIRENTRYEX, szName[2]) bytes.
318 *
319 * On successful output the field is updated to
320 * OFFSET(RTDIRENTRYEX, szName[pDirEntry->cbName + 1]).
321 *
322 * When the data doesn't fit in the buffer and VERR_BUFFER_OVERFLOW is
323 * returned, this field contains the required buffer size.
324 *
325 * The value is unchanged in all other cases.
326 * @param enmAdditionalAttribs
327 * Which set of additional attributes to request.
328 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
329 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
330 */
331RTDECL(int) RTDirReadEx(PRTDIR pDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags);
332
333
334/**
335 * Renames a file.
336 *
337 * Identical to RTPathRename except that it will ensure that the source is a directory.
338 *
339 * @returns IPRT status code.
340 * @returns VERR_ALREADY_EXISTS if the destination file exists.
341 *
342 * @param pszSrc The path to the source file.
343 * @param pszDst The path to the destination file.
344 * This file will be created.
345 * @param fRename See RTPathRename.
346 */
347RTDECL(int) RTDirRename(const char *pszSrc, const char *pszDst, unsigned fRename);
348
349
350/**
351 * Query information about an open directory.
352 *
353 * @returns iprt status code.
354 *
355 * @param pDir Pointer to the open directory.
356 * @param pObjInfo Object information structure to be filled on successful return.
357 * @param enmAdditionalAttribs Which set of additional attributes to request.
358 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
359 */
360RTR3DECL(int) RTDirQueryInfo(PRTDIR pDir, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs);
361
362
363/**
364 * Changes one or more of the timestamps associated of file system object.
365 *
366 * @returns iprt status code.
367 * @returns VERR_NOT_SUPPORTED is returned if the operation isn't supported by the OS.
368 *
369 * @param pDir Pointer to the open directory.
370 * @param pAccessTime Pointer to the new access time. NULL if not to be changed.
371 * @param pModificationTime Pointer to the new modifcation time. NULL if not to be changed.
372 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
373 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
374 *
375 * @remark The file system might not implement all these time attributes,
376 * the API will ignore the ones which aren't supported.
377 *
378 * @remark The file system might not implement the time resolution
379 * employed by this interface, the time will be chopped to fit.
380 *
381 * @remark The file system may update the change time even if it's
382 * not specified.
383 *
384 * @remark POSIX can only set Access & Modification and will always set both.
385 */
386RTR3DECL(int) RTDirSetTimes(PRTDIR pDir, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
387 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime);
388
389#endif /* IN_RING3 */
390/** @} */
391
392RT_C_DECLS_END
393
394#endif
395
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