VirtualBox

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

Last change on this file since 47942 was 46014, checked in by vboxsync, 12 years ago

RTDirQueryUnknownType* adjustment.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.9 KB
Line 
1/** @file
2 * IPRT - Directory Manipulation.
3 */
4
5/*
6 * Copyright (C) 2006-2012 Oracle Corporation
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
26#ifndef ___iprt_dir_h
27#define ___iprt_dir_h
28
29#include <iprt/cdefs.h>
30#include <iprt/types.h>
31#include <iprt/fs.h>
32
33
34RT_C_DECLS_BEGIN
35
36/** @defgroup grp_rt_dir RTDir - Directory Manipulation
37 * @ingroup grp_rt
38 * @{
39 */
40
41/**
42 * Check for the existence of a directory.
43 *
44 * All symbolic links will be attemped resolved. If that is undesirable, please
45 * use RTPathQueryInfo instead.
46 *
47 * @returns true if exist and is a directory.
48 * @returns false if not exists or isn't a directory.
49 * @param pszPath Path to the directory.
50 */
51RTDECL(bool) RTDirExists(const char *pszPath);
52
53/** @name RTDirCreate flags.
54 * @{ */
55/** Don't allow symbolic links as part of the path.
56 * @remarks this flag is currently not implemented and will be ignored. */
57#define RTDIRCREATE_FLAGS_NO_SYMLINKS RT_BIT(0)
58/** @} */
59
60/**
61 * Creates a directory.
62 *
63 * @returns iprt status code.
64 * @param pszPath Path to the directory to create.
65 * @param fMode The mode of the new directory.
66 * @param fCreate Create flags, RTDIRCREATE_FLAGS_*.
67 */
68RTDECL(int) RTDirCreate(const char *pszPath, RTFMODE fMode, uint32_t fCreate);
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 * @returns iprt status code.
94 * @param pszTemplate The directory name template on input. The actual
95 * directory name on success. Empty string on failure.
96 * @param fMode The mode to create the directory with. Use 0700
97 * unless you have reason not to.
98 */
99RTDECL(int) RTDirCreateTemp(char *pszTemplate, RTFMODE fMode);
100
101/**
102 * Secure version of @a RTDirCreateTemp with a fixed mode of 0700.
103 *
104 * This function behaves in the same way as @a RTDirCreateTemp with two
105 * additional points. Firstly the mode is fixed to 0700. Secondly it will
106 * fail if it is not possible to perform the operation securely. Possible
107 * reasons include that the directory could be removed by another unprivileged
108 * user before it is used (e.g. if is created in a non-sticky /tmp directory)
109 * or that the path contains symbolic links which another unprivileged user
110 * could manipulate; however the exact criteria will be specified on a
111 * platform-by-platform basis as platform support is added.
112 * @see RTPathIsSecure for the current list of criteria.
113 * @returns iprt status code.
114 * @returns VERR_NOT_SUPPORTED if the interface can not be supported on the
115 * current platform at this time.
116 * @returns VERR_INSECURE if the directory could not be created securely.
117 * @param pszTemplate The directory name template on input. The
118 * actual directory name on success. Empty string
119 * on failure.
120 */
121RTDECL(int) RTDirCreateTempSecure(char *pszTemplate);
122
123/**
124 * Creates a new directory with a unique name by appending a number.
125 *
126 * First it is tried to create the directory without any numbers appended.
127 * When this fails a number string is appended (starting with 1) separated by
128 * the optional separator. The numbers are zero padded.
129 *
130 * On success @a pszPath contains the path created.
131 *
132 * @returns iprt status code.
133 * @param pszPath Path to the directory to create.
134 * @param cbSize The size of pszPath. Needs enough space for holding the
135 * digits and the optional separator.
136 * @param fMode The mode of the new directory.
137 * @param cchDigits How many digits should the number maximal have.
138 * @param chSep The separator used between the path and the number. Can
139 * be zero. (optional)
140 */
141RTDECL(int) RTDirCreateUniqueNumbered(char *pszPath, size_t cbSize, RTFMODE fMode, signed int cchDigits, char chSep);
142
143/**
144 * Removes a directory if empty.
145 *
146 * @returns iprt status code.
147 * @param pszPath Path to the directory to remove.
148 */
149RTDECL(int) RTDirRemove(const char *pszPath);
150
151/**
152 * Removes a directory tree recursively.
153 *
154 * @returns iprt status code.
155 * @param pszPath Path to the directory to remove recursively.
156 * @param fFlags Flags, see RTDIRRMREC_F_XXX.
157 *
158 * @remarks This will not work on a root directory.
159 */
160RTDECL(int) RTDirRemoveRecursive(const char *pszPath, uint32_t fFlags);
161
162/** @name RTDirRemoveRecursive flags.
163 * @{ */
164/** Delete the content of the directory and the directory itself. */
165#define RTDIRRMREC_F_CONTENT_AND_DIR UINT32_C(0)
166/** Only delete the content of the directory, omit the directory it self. */
167#define RTDIRRMREC_F_CONTENT_ONLY RT_BIT_32(0)
168/** Mask of valid flags. */
169#define RTDIRRMREC_F_VALID_MASK UINT32_C(0x00000001)
170/** @} */
171
172/**
173 * Flushes the specified directory.
174 *
175 * This API is not implemented on all systems. On some systems it may be
176 * unnecessary if you've already flushed the file. If you really care for your
177 * data and is entering dangerous territories, it doesn't hurt calling it after
178 * flushing and closing the file.
179 *
180 * @returns IPRT status code.
181 * @retval VERR_NOT_IMPLEMENTED must be expected.
182 * @retval VERR_NOT_SUPPORTED must be expected.
183 * @param pszPath Path to the directory.
184 */
185RTDECL(int) RTDirFlush(const char *pszPath);
186
187/**
188 * Flushes the parent directory of the specified file.
189 *
190 * This is just a wrapper around RTDirFlush.
191 *
192 * @returns IPRT status code, see RTDirFlush for details.
193 * @param pszChild Path to the file which parent should be flushed.
194 */
195RTDECL(int) RTDirFlushParent(const char *pszChild);
196
197
198/** Pointer to an open directory (sort of handle). */
199typedef struct RTDIR *PRTDIR;
200
201
202/**
203 * Filter option for RTDirOpenFiltered().
204 */
205typedef enum RTDIRFILTER
206{
207 /** The usual invalid 0 entry. */
208 RTDIRFILTER_INVALID = 0,
209 /** No filter should be applied (and none was specified). */
210 RTDIRFILTER_NONE,
211 /** The Windows NT filter.
212 * The following wildcard chars: *, ?, <, > and "
213 * The matching is done on the uppercased strings. */
214 RTDIRFILTER_WINNT,
215 /** The UNIX filter.
216 * The following wildcard chars: *, ?, [..]
217 * The matching is done on exact case. */
218 RTDIRFILTER_UNIX,
219 /** The UNIX filter, uppercased matching.
220 * Same as RTDIRFILTER_UNIX except that the strings are uppercased before comparing. */
221 RTDIRFILTER_UNIX_UPCASED,
222
223 /** The usual full 32-bit value. */
224 RTDIRFILTER_32BIT_HACK = 0x7fffffff
225} RTDIRFILTER;
226
227
228/**
229 * Directory entry type.
230 *
231 * This is the RTFS_TYPE_MASK stuff shifted down 12 bits and
232 * identical to the BSD/LINUX ABI.
233 */
234typedef enum RTDIRENTRYTYPE
235{
236 /** Unknown type (DT_UNKNOWN). */
237 RTDIRENTRYTYPE_UNKNOWN = 0,
238 /** Named pipe (fifo) (DT_FIFO). */
239 RTDIRENTRYTYPE_FIFO = 001,
240 /** Character device (DT_CHR). */
241 RTDIRENTRYTYPE_DEV_CHAR = 002,
242 /** Directory (DT_DIR). */
243 RTDIRENTRYTYPE_DIRECTORY = 004,
244 /** Block device (DT_BLK). */
245 RTDIRENTRYTYPE_DEV_BLOCK = 006,
246 /** Regular file (DT_REG). */
247 RTDIRENTRYTYPE_FILE = 010,
248 /** Symbolic link (DT_LNK). */
249 RTDIRENTRYTYPE_SYMLINK = 012,
250 /** Socket (DT_SOCK). */
251 RTDIRENTRYTYPE_SOCKET = 014,
252 /** Whiteout (DT_WHT). */
253 RTDIRENTRYTYPE_WHITEOUT = 016
254} RTDIRENTRYTYPE;
255
256
257/**
258 * Directory entry.
259 *
260 * This is inspired by the POSIX interfaces.
261 */
262#pragma pack(1)
263typedef struct RTDIRENTRY
264{
265 /** The unique identifier (within the file system) of this file system object (d_ino).
266 *
267 * Together with INodeIdDevice, this field can be used as a OS wide unique id
268 * when both their values are not 0. This field is 0 if the information is not
269 * available. */
270 RTINODE INodeId;
271 /** The entry type. (d_type)
272 * RTDIRENTRYTYPE_UNKNOWN is a common return value here since not all file
273 * systems (or Unixes) stores the type of a directory entry and instead
274 * expects the user to use stat() to get it. So, when you see this you
275 * should use RTDirQueryUnknownType or RTDirQueryUnknownTypeEx to get the type,
276 * or if if you're lazy, use RTDirReadEx. */
277 RTDIRENTRYTYPE enmType;
278 /** The length of the filename, excluding the terminating nul character. */
279 uint16_t cbName;
280 /** The filename. (no path)
281 * Using the pcbDirEntry parameter of RTDirRead makes this field variable in size. */
282 char szName[260];
283} RTDIRENTRY;
284#pragma pack()
285/** Pointer to a directory entry. */
286typedef RTDIRENTRY *PRTDIRENTRY;
287/** Pointer to a const directory entry. */
288typedef RTDIRENTRY const *PCRTDIRENTRY;
289
290
291/**
292 * Directory entry with extended information.
293 *
294 * This is inspired by the PC interfaces.
295 */
296#pragma pack(1)
297typedef struct RTDIRENTRYEX
298{
299 /** Full information about the object. */
300 RTFSOBJINFO Info;
301 /** The length of the short field (number of RTUTF16 entries (not chars)).
302 * It is 16-bit for reasons of alignment. */
303 uint16_t cwcShortName;
304 /** The short name for 8.3 compatibility.
305 * Empty string if not available.
306 * Since the length is a bit tricky for a UTF-8 encoded name, and since this
307 * is practically speaking only a windows thing, it is encoded as UCS-2. */
308 RTUTF16 wszShortName[14];
309 /** The length of the filename. */
310 uint16_t cbName;
311 /** The filename. (no path)
312 * Using the pcbDirEntry parameter of RTDirReadEx makes this field variable in size. */
313 char szName[260];
314} RTDIRENTRYEX;
315#pragma pack()
316/** Pointer to a directory entry. */
317typedef RTDIRENTRYEX *PRTDIRENTRYEX;
318/** Pointer to a const directory entry. */
319typedef RTDIRENTRYEX const *PCRTDIRENTRYEX;
320
321
322/**
323 * Opens a directory.
324 *
325 * @returns iprt status code.
326 * @param ppDir Where to store the open directory pointer.
327 * @param pszPath Path to the directory to open.
328 */
329RTDECL(int) RTDirOpen(PRTDIR *ppDir, const char *pszPath);
330
331/** @name RTDirOpenFiltered flags.
332 * @{ */
333/** Don't allow symbolic links as part of the path.
334 * @remarks this flag is currently not implemented and will be ignored. */
335#define RTDIROPEN_FLAGS_NO_SYMLINKS RT_BIT(0)
336/** @} */
337
338/**
339 * Opens a directory filtering the entries using dos style wildcards.
340 *
341 * @returns iprt status code.
342 * @param ppDir Where to store the open directory pointer.
343 * @param pszPath Path to the directory to search, this must include wildcards.
344 * @param enmFilter The kind of filter to apply. Setting this to RTDIRFILTER_NONE makes
345 * this function behave like RTDirOpen.
346 * @param fOpen Open flags, RTDIROPENFILTERED_FLAGS_*.
347 */
348RTDECL(int) RTDirOpenFiltered(PRTDIR *ppDir, const char *pszPath, RTDIRFILTER enmFilter, uint32_t fOpen);
349
350/**
351 * Closes a directory.
352 *
353 * @returns iprt status code.
354 * @param pDir Pointer to open directory returned by RTDirOpen() or RTDirOpenFiltered().
355 */
356RTDECL(int) RTDirClose(PRTDIR pDir);
357
358/**
359 * Reads the next entry in the directory.
360 *
361 * @returns VINF_SUCCESS and data in pDirEntry on success.
362 * @returns VERR_NO_MORE_FILES when the end of the directory has been reached.
363 * @returns VERR_BUFFER_OVERFLOW if the buffer is too small to contain the filename. If
364 * pcbDirEntry is specified it will be updated with the required buffer size.
365 * @returns suitable iprt status code on other errors.
366 *
367 * @param pDir Pointer to the open directory.
368 * @param pDirEntry Where to store the information about the next
369 * directory entry on success.
370 * @param pcbDirEntry Optional parameter used for variable buffer size.
371 *
372 * On input the variable pointed to contains the size of the pDirEntry
373 * structure. This must be at least OFFSET(RTDIRENTRY, szName[2]) bytes.
374 *
375 * On successful output the field is updated to
376 * OFFSET(RTDIRENTRY, szName[pDirEntry->cbName + 1]).
377 *
378 * When the data doesn't fit in the buffer and VERR_BUFFER_OVERFLOW is
379 * returned, this field contains the required buffer size.
380 *
381 * The value is unchanged in all other cases.
382 */
383RTDECL(int) RTDirRead(PRTDIR pDir, PRTDIRENTRY pDirEntry, size_t *pcbDirEntry);
384
385/**
386 * Reads the next entry in the directory returning extended information.
387 *
388 * @returns VINF_SUCCESS and data in pDirEntry on success.
389 * @returns VERR_NO_MORE_FILES when the end of the directory has been reached.
390 * @returns VERR_BUFFER_OVERFLOW if the buffer is too small to contain the filename. If
391 * pcbDirEntry is specified it will be updated with the required buffer size.
392 * @returns suitable iprt status code on other errors.
393 *
394 * @param pDir Pointer to the open directory.
395 * @param pDirEntry Where to store the information about the next
396 * directory entry on success.
397 * @param pcbDirEntry Optional parameter used for variable buffer size.
398 *
399 * On input the variable pointed to contains the size of the pDirEntry
400 * structure. This must be at least OFFSET(RTDIRENTRYEX, szName[2]) bytes.
401 *
402 * On successful output the field is updated to
403 * OFFSET(RTDIRENTRYEX, szName[pDirEntry->cbName + 1]).
404 *
405 * When the data doesn't fit in the buffer and VERR_BUFFER_OVERFLOW is
406 * returned, this field contains the required buffer size.
407 *
408 * The value is unchanged in all other cases.
409 * @param enmAdditionalAttribs
410 * Which set of additional attributes to request.
411 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
412 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
413 */
414RTDECL(int) RTDirReadEx(PRTDIR pDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags);
415
416/**
417 * Resolves RTDIRENTRYTYPE_UNKNOWN values returned by RTDirRead.
418 *
419 * @returns IPRT status code (see RTPathQueryInfo).
420 * @param pszComposedName The path to the directory entry. The caller must
421 * compose this, it's NOT sufficient to pass
422 * RTDIRENTRY::szName!
423 * @param fFollowSymlinks Whether to follow symbolic links or not.
424 * @param penmType Pointer to the RTDIRENTRY::enmType member. If this
425 * is not RTDIRENTRYTYPE_UNKNOWN and, if
426 * @a fFollowSymlinks is false, not
427 * RTDIRENTRYTYPE_SYMLINK, the function will return
428 * immediately without doing anything. Otherwise it
429 * will use RTPathQueryInfo to try figure out the
430 * correct value. On failure, this will be unchanged.
431 */
432RTDECL(int) RTDirQueryUnknownType(const char *pszComposedName, bool fFollowSymlinks, RTDIRENTRYTYPE *penmType);
433
434/**
435 * Resolves RTDIRENTRYTYPE_UNKNOWN values returned by RTDirRead, extended
436 * version.
437 *
438 * @returns IPRT status code (see RTPathQueryInfo).
439 * @param pszComposedName The path to the directory entry. The caller must
440 * compose this, it's NOT sufficient to pass
441 * RTDIRENTRY::szName!
442 * @param fFollowSymlinks Whether to follow symbolic links or not.
443 * @param penmType Pointer to the RTDIRENTRY::enmType member or
444 * similar. Will NOT be checked on input.
445 * @param pObjInfo The object info buffer to use with RTPathQueryInfo.
446 */
447RTDECL(int) RTDirQueryUnknownTypeEx(const char *pszComposedName, bool fFollowSymlinks, RTDIRENTRYTYPE *penmType, PRTFSOBJINFO pObjInfo);
448
449/**
450 * Checks if the directory entry returned by RTDirRead is '.', '..' or similar.
451 *
452 * @returns true / false.
453 * @param pDirEntry The directory entry to check.
454 */
455RTDECL(bool) RTDirEntryIsStdDotLink(PRTDIRENTRY pDirEntry);
456
457/**
458 * Checks if the directory entry returned by RTDirReadEx is '.', '..' or
459 * similar.
460 *
461 * @returns true / false.
462 * @param pDirEntryEx The extended directory entry to check.
463 */
464RTDECL(bool) RTDirEntryExIsStdDotLink(PCRTDIRENTRYEX pDirEntryEx);
465
466/**
467 * Renames a file.
468 *
469 * Identical to RTPathRename except that it will ensure that the source is a directory.
470 *
471 * @returns IPRT status code.
472 * @returns VERR_ALREADY_EXISTS if the destination file exists.
473 *
474 * @param pszSrc The path to the source file.
475 * @param pszDst The path to the destination file.
476 * This file will be created.
477 * @param fRename See RTPathRename.
478 */
479RTDECL(int) RTDirRename(const char *pszSrc, const char *pszDst, unsigned fRename);
480
481
482/**
483 * Query information about an open directory.
484 *
485 * @returns iprt status code.
486 *
487 * @param pDir Pointer to the open directory.
488 * @param pObjInfo Object information structure to be filled on successful return.
489 * @param enmAdditionalAttribs Which set of additional attributes to request.
490 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
491 */
492RTR3DECL(int) RTDirQueryInfo(PRTDIR pDir, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs);
493
494
495/**
496 * Changes one or more of the timestamps associated of file system object.
497 *
498 * @returns iprt status code.
499 * @returns VERR_NOT_SUPPORTED is returned if the operation isn't supported by the OS.
500 *
501 * @param pDir Pointer to the open directory.
502 * @param pAccessTime Pointer to the new access time. NULL if not to be changed.
503 * @param pModificationTime Pointer to the new modifcation time. NULL if not to be changed.
504 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
505 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
506 *
507 * @remark The file system might not implement all these time attributes,
508 * the API will ignore the ones which aren't supported.
509 *
510 * @remark The file system might not implement the time resolution
511 * employed by this interface, the time will be chopped to fit.
512 *
513 * @remark The file system may update the change time even if it's
514 * not specified.
515 *
516 * @remark POSIX can only set Access & Modification and will always set both.
517 */
518RTR3DECL(int) RTDirSetTimes(PRTDIR pDir, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
519 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime);
520
521/** @} */
522
523RT_C_DECLS_END
524
525#endif
526
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