VirtualBox

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

Last change on this file since 62589 was 62473, checked in by vboxsync, 8 years ago

(C) 2016

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