VirtualBox

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

Last change on this file since 42419 was 39641, checked in by vboxsync, 13 years ago

IPRT headers: warning

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.2 KB
Line 
1/** @file
2 * IPRT - Directory Manipulation.
3 */
4
5/*
6 * Copyright (C) 2006-2010 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 * 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 * Creates a new directory with a unique name by appending a number.
103 *
104 * First it is tried to create the directory without any numbers appended.
105 * When this fails a number string is appended (starting with 1) separated by
106 * the optional separator. The numbers are zero padded.
107 *
108 * On success @a pszPath contains the path created.
109 *
110 * @returns iprt status code.
111 * @param pszPath Path to the directory to create.
112 * @param cbSize The size of pszPath. Needs enough space for holding the
113 * digits and the optional separator.
114 * @param fMode The mode of the new directory.
115 * @param cchDigits How many digits should the number maximal have.
116 * @param chSep The separator used between the path and the number. Can
117 * be zero. (optional)
118 */
119RTDECL(int) RTDirCreateUniqueNumbered(char *pszPath, size_t cbSize, RTFMODE fMode, signed int cchDigits, char chSep);
120
121/**
122 * Removes a directory if empty.
123 *
124 * @returns iprt status code.
125 * @param pszPath Path to the directory to remove.
126 */
127RTDECL(int) RTDirRemove(const char *pszPath);
128
129/**
130 * Removes a directory tree recursively.
131 *
132 * @returns iprt status code.
133 * @param pszPath Path to the directory to remove recursively.
134 * @param fFlags Flags, see RTDIRRMREC_F_XXX.
135 *
136 * @remarks This will not work on a root directory.
137 */
138RTDECL(int) RTDirRemoveRecursive(const char *pszPath, uint32_t fFlags);
139
140/** @name RTDirRemoveRecursive flags.
141 * @{ */
142/** Delete the content of the directory and the directory itself. */
143#define RTDIRRMREC_F_CONTENT_AND_DIR UINT32_C(0)
144/** Only delete the content of the directory, omit the directory it self. */
145#define RTDIRRMREC_F_CONTENT_ONLY RT_BIT_32(0)
146/** Mask of valid flags. */
147#define RTDIRRMREC_F_VALID_MASK UINT32_C(0x00000001)
148/** @} */
149
150/**
151 * Flushes the specified directory.
152 *
153 * This API is not implemented on all systems. On some systems it may be
154 * unnecessary if you've already flushed the file. If you really care for your
155 * data and is entering dangerous territories, it doesn't hurt calling it after
156 * flushing and closing the file.
157 *
158 * @returns IPRT status code.
159 * @retval VERR_NOT_IMPLEMENTED must be expected.
160 * @retval VERR_NOT_SUPPORTED must be expected.
161 * @param pszPath Path to the directory.
162 */
163RTDECL(int) RTDirFlush(const char *pszPath);
164
165/**
166 * Flushes the parent directory of the specified file.
167 *
168 * This is just a wrapper around RTDirFlush.
169 *
170 * @returns IPRT status code, see RTDirFlush for details.
171 * @param pszChild Path to the file which parent should be flushed.
172 */
173RTDECL(int) RTDirFlushParent(const char *pszChild);
174
175
176/** Pointer to an open directory (sort of handle). */
177typedef struct RTDIR *PRTDIR;
178
179
180/**
181 * Filter option for RTDirOpenFiltered().
182 */
183typedef enum RTDIRFILTER
184{
185 /** The usual invalid 0 entry. */
186 RTDIRFILTER_INVALID = 0,
187 /** No filter should be applied (and none was specified). */
188 RTDIRFILTER_NONE,
189 /** The Windows NT filter.
190 * The following wildcard chars: *, ?, <, > and "
191 * The matching is done on the uppercased strings. */
192 RTDIRFILTER_WINNT,
193 /** The UNIX filter.
194 * The following wildcard chars: *, ?, [..]
195 * The matching is done on exact case. */
196 RTDIRFILTER_UNIX,
197 /** The UNIX filter, uppercased matching.
198 * Same as RTDIRFILTER_UNIX except that the strings are uppercased before comparing. */
199 RTDIRFILTER_UNIX_UPCASED,
200
201 /** The usual full 32-bit value. */
202 RTDIRFILTER_32BIT_HACK = 0x7fffffff
203} RTDIRFILTER;
204
205
206/**
207 * Directory entry type.
208 *
209 * This is the RTFS_TYPE_MASK stuff shifted down 12 bits and
210 * identical to the BSD/LINUX ABI.
211 */
212typedef enum RTDIRENTRYTYPE
213{
214 /** Unknown type (DT_UNKNOWN). */
215 RTDIRENTRYTYPE_UNKNOWN = 0,
216 /** Named pipe (fifo) (DT_FIFO). */
217 RTDIRENTRYTYPE_FIFO = 001,
218 /** Character device (DT_CHR). */
219 RTDIRENTRYTYPE_DEV_CHAR = 002,
220 /** Directory (DT_DIR). */
221 RTDIRENTRYTYPE_DIRECTORY = 004,
222 /** Block device (DT_BLK). */
223 RTDIRENTRYTYPE_DEV_BLOCK = 006,
224 /** Regular file (DT_REG). */
225 RTDIRENTRYTYPE_FILE = 010,
226 /** Symbolic link (DT_LNK). */
227 RTDIRENTRYTYPE_SYMLINK = 012,
228 /** Socket (DT_SOCK). */
229 RTDIRENTRYTYPE_SOCKET = 014,
230 /** Whiteout (DT_WHT). */
231 RTDIRENTRYTYPE_WHITEOUT = 016
232} RTDIRENTRYTYPE;
233
234
235/**
236 * Directory entry.
237 *
238 * This is inspired by the POSIX interfaces.
239 */
240#pragma pack(1)
241typedef struct RTDIRENTRY
242{
243 /** The unique identifier (within the file system) of this file system object (d_ino).
244 *
245 * Together with INodeIdDevice, this field can be used as a OS wide unique id
246 * when both their values are not 0. This field is 0 if the information is not
247 * available. */
248 RTINODE INodeId;
249 /** The entry type. (d_type)
250 * RTDIRENTRYTYPE_UNKNOWN is a common return value here since not all file
251 * systems (or Unixes) stores the type of a directory entry and instead
252 * expects the user to use stat() to get it. So, when you see this you
253 * should use RTPathQueryInfo to get the type, or if if you're lazy, use
254 * RTDirReadEx. */
255 RTDIRENTRYTYPE enmType;
256 /** The length of the filename, excluding the terminating nul character. */
257 uint16_t cbName;
258 /** The filename. (no path)
259 * Using the pcbDirEntry parameter of RTDirRead makes this field variable in size. */
260 char szName[260];
261} RTDIRENTRY;
262#pragma pack()
263/** Pointer to a directory entry. */
264typedef RTDIRENTRY *PRTDIRENTRY;
265
266
267/**
268 * Directory entry with extended information.
269 *
270 * This is inspired by the PC interfaces.
271 */
272#pragma pack(1)
273typedef struct RTDIRENTRYEX
274{
275 /** Full information about the object. */
276 RTFSOBJINFO Info;
277 /** The length of the short field (number of RTUTF16 entries (not chars)).
278 * It is 16-bit for reasons of alignment. */
279 uint16_t cwcShortName;
280 /** The short name for 8.3 compatibility.
281 * Empty string if not available.
282 * Since the length is a bit tricky for a UTF-8 encoded name, and since this
283 * is practically speaking only a windows thing, it is encoded as UCS-2. */
284 RTUTF16 wszShortName[14];
285 /** The length of the filename. */
286 uint16_t cbName;
287 /** The filename. (no path)
288 * Using the pcbDirEntry parameter of RTDirReadEx makes this field variable in size. */
289 char szName[260];
290} RTDIRENTRYEX;
291#pragma pack()
292/** Pointer to a directory entry. */
293typedef RTDIRENTRYEX *PRTDIRENTRYEX;
294
295
296/**
297 * Opens a directory.
298 *
299 * @returns iprt status code.
300 * @param ppDir Where to store the open directory pointer.
301 * @param pszPath Path to the directory to open.
302 */
303RTDECL(int) RTDirOpen(PRTDIR *ppDir, const char *pszPath);
304
305/** @name RTDirOpenFiltered flags.
306 * @{ */
307/** Don't allow symbolic links as part of the path.
308 * @remarks this flag is currently not implemented and will be ignored. */
309#define RTDIROPEN_FLAGS_NO_SYMLINKS RT_BIT(0)
310/** @} */
311
312/**
313 * Opens a directory filtering the entries using dos style wildcards.
314 *
315 * @returns iprt status code.
316 * @param ppDir Where to store the open directory pointer.
317 * @param pszPath Path to the directory to search, this must include wildcards.
318 * @param enmFilter The kind of filter to apply. Setting this to RTDIRFILTER_NONE makes
319 * this function behave like RTDirOpen.
320 * @param fOpen Open flags, RTDIROPENFILTERED_FLAGS_*.
321 */
322RTDECL(int) RTDirOpenFiltered(PRTDIR *ppDir, const char *pszPath, RTDIRFILTER enmFilter, uint32_t fOpen);
323
324/**
325 * Closes a directory.
326 *
327 * @returns iprt status code.
328 * @param pDir Pointer to open directory returned by RTDirOpen() or RTDirOpenFiltered().
329 */
330RTDECL(int) RTDirClose(PRTDIR pDir);
331
332/**
333 * Reads the next entry in the directory.
334 *
335 * @returns VINF_SUCCESS and data in pDirEntry on success.
336 * @returns VERR_NO_MORE_FILES when the end of the directory has been reached.
337 * @returns VERR_BUFFER_OVERFLOW if the buffer is too small to contain the filename. If
338 * pcbDirEntry is specified it will be updated with the required buffer size.
339 * @returns suitable iprt status code on other errors.
340 *
341 * @param pDir Pointer to the open directory.
342 * @param pDirEntry Where to store the information about the next
343 * directory entry on success.
344 * @param pcbDirEntry Optional parameter used for variable buffer size.
345 *
346 * On input the variable pointed to contains the size of the pDirEntry
347 * structure. This must be at least OFFSET(RTDIRENTRY, szName[2]) bytes.
348 *
349 * On successful output the field is updated to
350 * OFFSET(RTDIRENTRY, szName[pDirEntry->cbName + 1]).
351 *
352 * When the data doesn't fit in the buffer and VERR_BUFFER_OVERFLOW is
353 * returned, this field contains the required buffer size.
354 *
355 * The value is unchanged in all other cases.
356 */
357RTDECL(int) RTDirRead(PRTDIR pDir, PRTDIRENTRY pDirEntry, size_t *pcbDirEntry);
358
359/**
360 * Reads the next entry in the directory returning extended information.
361 *
362 * @returns VINF_SUCCESS and data in pDirEntry on success.
363 * @returns VERR_NO_MORE_FILES when the end of the directory has been reached.
364 * @returns VERR_BUFFER_OVERFLOW if the buffer is too small to contain the filename. If
365 * pcbDirEntry is specified it will be updated with the required buffer size.
366 * @returns suitable iprt status code on other errors.
367 *
368 * @param pDir Pointer to the open directory.
369 * @param pDirEntry Where to store the information about the next
370 * directory entry on success.
371 * @param pcbDirEntry Optional parameter used for variable buffer size.
372 *
373 * On input the variable pointed to contains the size of the pDirEntry
374 * structure. This must be at least OFFSET(RTDIRENTRYEX, szName[2]) bytes.
375 *
376 * On successful output the field is updated to
377 * OFFSET(RTDIRENTRYEX, szName[pDirEntry->cbName + 1]).
378 *
379 * When the data doesn't fit in the buffer and VERR_BUFFER_OVERFLOW is
380 * returned, this field contains the required buffer size.
381 *
382 * The value is unchanged in all other cases.
383 * @param enmAdditionalAttribs
384 * Which set of additional attributes to request.
385 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
386 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
387 */
388RTDECL(int) RTDirReadEx(PRTDIR pDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags);
389
390
391/**
392 * Renames a file.
393 *
394 * Identical to RTPathRename except that it will ensure that the source is a directory.
395 *
396 * @returns IPRT status code.
397 * @returns VERR_ALREADY_EXISTS if the destination file exists.
398 *
399 * @param pszSrc The path to the source file.
400 * @param pszDst The path to the destination file.
401 * This file will be created.
402 * @param fRename See RTPathRename.
403 */
404RTDECL(int) RTDirRename(const char *pszSrc, const char *pszDst, unsigned fRename);
405
406
407/**
408 * Query information about an open directory.
409 *
410 * @returns iprt status code.
411 *
412 * @param pDir Pointer to the open directory.
413 * @param pObjInfo Object information structure to be filled on successful return.
414 * @param enmAdditionalAttribs Which set of additional attributes to request.
415 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
416 */
417RTR3DECL(int) RTDirQueryInfo(PRTDIR pDir, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs);
418
419
420/**
421 * Changes one or more of the timestamps associated of file system object.
422 *
423 * @returns iprt status code.
424 * @returns VERR_NOT_SUPPORTED is returned if the operation isn't supported by the OS.
425 *
426 * @param pDir Pointer to the open directory.
427 * @param pAccessTime Pointer to the new access time. NULL if not to be changed.
428 * @param pModificationTime Pointer to the new modifcation time. NULL if not to be changed.
429 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
430 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
431 *
432 * @remark The file system might not implement all these time attributes,
433 * the API will ignore the ones which aren't supported.
434 *
435 * @remark The file system might not implement the time resolution
436 * employed by this interface, the time will be chopped to fit.
437 *
438 * @remark The file system may update the change time even if it's
439 * not specified.
440 *
441 * @remark POSIX can only set Access & Modification and will always set both.
442 */
443RTR3DECL(int) RTDirSetTimes(PRTDIR pDir, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
444 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime);
445
446/** @} */
447
448RT_C_DECLS_END
449
450#endif
451
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