VirtualBox

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

Last change on this file since 43248 was 42712, checked in by vboxsync, 12 years ago

Runtime: add RTFileCreateTemp, RTFileCreateTempSecure and RTDirCreateTempSecure (the last two are not yet implemented).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.5 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 * @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 RTPathQueryInfo to get the type, or if if you're lazy, use
276 * 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
288
289/**
290 * Directory entry with extended information.
291 *
292 * This is inspired by the PC interfaces.
293 */
294#pragma pack(1)
295typedef struct RTDIRENTRYEX
296{
297 /** Full information about the object. */
298 RTFSOBJINFO Info;
299 /** The length of the short field (number of RTUTF16 entries (not chars)).
300 * It is 16-bit for reasons of alignment. */
301 uint16_t cwcShortName;
302 /** The short name for 8.3 compatibility.
303 * Empty string if not available.
304 * Since the length is a bit tricky for a UTF-8 encoded name, and since this
305 * is practically speaking only a windows thing, it is encoded as UCS-2. */
306 RTUTF16 wszShortName[14];
307 /** The length of the filename. */
308 uint16_t cbName;
309 /** The filename. (no path)
310 * Using the pcbDirEntry parameter of RTDirReadEx makes this field variable in size. */
311 char szName[260];
312} RTDIRENTRYEX;
313#pragma pack()
314/** Pointer to a directory entry. */
315typedef RTDIRENTRYEX *PRTDIRENTRYEX;
316
317
318/**
319 * Opens a directory.
320 *
321 * @returns iprt status code.
322 * @param ppDir Where to store the open directory pointer.
323 * @param pszPath Path to the directory to open.
324 */
325RTDECL(int) RTDirOpen(PRTDIR *ppDir, const char *pszPath);
326
327/** @name RTDirOpenFiltered flags.
328 * @{ */
329/** Don't allow symbolic links as part of the path.
330 * @remarks this flag is currently not implemented and will be ignored. */
331#define RTDIROPEN_FLAGS_NO_SYMLINKS RT_BIT(0)
332/** @} */
333
334/**
335 * Opens a directory filtering the entries using dos style wildcards.
336 *
337 * @returns iprt status code.
338 * @param ppDir Where to store the open directory pointer.
339 * @param pszPath Path to the directory to search, this must include wildcards.
340 * @param enmFilter The kind of filter to apply. Setting this to RTDIRFILTER_NONE makes
341 * this function behave like RTDirOpen.
342 * @param fOpen Open flags, RTDIROPENFILTERED_FLAGS_*.
343 */
344RTDECL(int) RTDirOpenFiltered(PRTDIR *ppDir, const char *pszPath, RTDIRFILTER enmFilter, uint32_t fOpen);
345
346/**
347 * Closes a directory.
348 *
349 * @returns iprt status code.
350 * @param pDir Pointer to open directory returned by RTDirOpen() or RTDirOpenFiltered().
351 */
352RTDECL(int) RTDirClose(PRTDIR pDir);
353
354/**
355 * Reads the next entry in the directory.
356 *
357 * @returns VINF_SUCCESS and data in pDirEntry on success.
358 * @returns VERR_NO_MORE_FILES when the end of the directory has been reached.
359 * @returns VERR_BUFFER_OVERFLOW if the buffer is too small to contain the filename. If
360 * pcbDirEntry is specified it will be updated with the required buffer size.
361 * @returns suitable iprt status code on other errors.
362 *
363 * @param pDir Pointer to the open directory.
364 * @param pDirEntry Where to store the information about the next
365 * directory entry on success.
366 * @param pcbDirEntry Optional parameter used for variable buffer size.
367 *
368 * On input the variable pointed to contains the size of the pDirEntry
369 * structure. This must be at least OFFSET(RTDIRENTRY, szName[2]) bytes.
370 *
371 * On successful output the field is updated to
372 * OFFSET(RTDIRENTRY, szName[pDirEntry->cbName + 1]).
373 *
374 * When the data doesn't fit in the buffer and VERR_BUFFER_OVERFLOW is
375 * returned, this field contains the required buffer size.
376 *
377 * The value is unchanged in all other cases.
378 */
379RTDECL(int) RTDirRead(PRTDIR pDir, PRTDIRENTRY pDirEntry, size_t *pcbDirEntry);
380
381/**
382 * Reads the next entry in the directory returning extended information.
383 *
384 * @returns VINF_SUCCESS and data in pDirEntry on success.
385 * @returns VERR_NO_MORE_FILES when the end of the directory has been reached.
386 * @returns VERR_BUFFER_OVERFLOW if the buffer is too small to contain the filename. If
387 * pcbDirEntry is specified it will be updated with the required buffer size.
388 * @returns suitable iprt status code on other errors.
389 *
390 * @param pDir Pointer to the open directory.
391 * @param pDirEntry Where to store the information about the next
392 * directory entry on success.
393 * @param pcbDirEntry Optional parameter used for variable buffer size.
394 *
395 * On input the variable pointed to contains the size of the pDirEntry
396 * structure. This must be at least OFFSET(RTDIRENTRYEX, szName[2]) bytes.
397 *
398 * On successful output the field is updated to
399 * OFFSET(RTDIRENTRYEX, szName[pDirEntry->cbName + 1]).
400 *
401 * When the data doesn't fit in the buffer and VERR_BUFFER_OVERFLOW is
402 * returned, this field contains the required buffer size.
403 *
404 * The value is unchanged in all other cases.
405 * @param enmAdditionalAttribs
406 * Which set of additional attributes to request.
407 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
408 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
409 */
410RTDECL(int) RTDirReadEx(PRTDIR pDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags);
411
412
413/**
414 * Renames a file.
415 *
416 * Identical to RTPathRename except that it will ensure that the source is a directory.
417 *
418 * @returns IPRT status code.
419 * @returns VERR_ALREADY_EXISTS if the destination file exists.
420 *
421 * @param pszSrc The path to the source file.
422 * @param pszDst The path to the destination file.
423 * This file will be created.
424 * @param fRename See RTPathRename.
425 */
426RTDECL(int) RTDirRename(const char *pszSrc, const char *pszDst, unsigned fRename);
427
428
429/**
430 * Query information about an open directory.
431 *
432 * @returns iprt status code.
433 *
434 * @param pDir Pointer to the open directory.
435 * @param pObjInfo Object information structure to be filled on successful return.
436 * @param enmAdditionalAttribs Which set of additional attributes to request.
437 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
438 */
439RTR3DECL(int) RTDirQueryInfo(PRTDIR pDir, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs);
440
441
442/**
443 * Changes one or more of the timestamps associated of file system object.
444 *
445 * @returns iprt status code.
446 * @returns VERR_NOT_SUPPORTED is returned if the operation isn't supported by the OS.
447 *
448 * @param pDir Pointer to the open directory.
449 * @param pAccessTime Pointer to the new access time. NULL if not to be changed.
450 * @param pModificationTime Pointer to the new modifcation time. NULL if not to be changed.
451 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
452 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
453 *
454 * @remark The file system might not implement all these time attributes,
455 * the API will ignore the ones which aren't supported.
456 *
457 * @remark The file system might not implement the time resolution
458 * employed by this interface, the time will be chopped to fit.
459 *
460 * @remark The file system may update the change time even if it's
461 * not specified.
462 *
463 * @remark POSIX can only set Access & Modification and will always set both.
464 */
465RTR3DECL(int) RTDirSetTimes(PRTDIR pDir, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
466 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime);
467
468/** @} */
469
470RT_C_DECLS_END
471
472#endif
473
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