VirtualBox

source: vbox/trunk/include/iprt/path.h@ 22109

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

IPRT: Added RTPathTraverseList.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.5 KB
Line 
1/** @file
2 * IPRT - Path 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_path_h
31#define ___iprt_path_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
40
41RT_C_DECLS_BEGIN
42
43/** @defgroup grp_rt_path RTPath - Path Manipulation
44 * @ingroup grp_rt
45 * @{
46 */
47
48
49/** @def RTPATH_SLASH
50 * The preferred slash character.
51 *
52 * @remark IPRT will always accept unix slashes. So, normally you would
53 * never have to use this define.
54 */
55#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
56# define RTPATH_SLASH '\\'
57#else
58# define RTPATH_SLASH '/'
59#endif
60
61/** @deprecated Use '/'! */
62#define RTPATH_DELIMITER RTPATH_SLASH
63
64
65/** @def RTPATH_SLASH_STR
66 * The preferred slash character as a string, handy for concatenations
67 * with other strings.
68 *
69 * @remark IPRT will always accept unix slashes. So, normally you would
70 * never have to use this define.
71 */
72#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
73# define RTPATH_SLASH_STR "\\"
74#else
75# define RTPATH_SLASH_STR "/"
76#endif
77
78
79/** @def RTPATH_IS_SLASH
80 * Checks if a character is a slash.
81 *
82 * @returns true if it's a slash and false if not.
83 * @returns @param ch Char to check.
84 */
85#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
86# define RTPATH_IS_SLASH(ch) ( (ch) == '\\' || (ch) == '/' )
87#else
88# define RTPATH_IS_SLASH(ch) ( (ch) == '/' )
89#endif
90
91
92/** @def RTPATH_IS_VOLSEP
93 * Checks if a character marks the end of the volume specification.
94 *
95 * @remark This is sufficient for the drive letter concept on PC.
96 * However it might be insufficient on other platforms
97 * and even on PC a UNC volume spec won't be detected this way.
98 * Use the RTPath@<too be created@>() instead.
99 *
100 * @returns true if it is and false if it isn't.
101 * @returns @param ch Char to check.
102 */
103#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
104# define RTPATH_IS_VOLSEP(ch) ( (ch) == ':' )
105#else
106# define RTPATH_IS_VOLSEP(ch) (false)
107#endif
108
109
110/** @def RTPATH_IS_SEP
111 * Checks if a character is path component separator
112 *
113 * @returns true if it is and false if it isn't.
114 * @returns @param ch Char to check.
115 * @
116 */
117#define RTPATH_IS_SEP(ch) ( RTPATH_IS_SLASH(ch) || RTPATH_IS_VOLSEP(ch) )
118
119
120/**
121 * Checks if the path exists.
122 *
123 * Symbolic links will all be attempted resolved.
124 *
125 * @returns true if it exists and false if it doesn't.
126 * @param pszPath The path to check.
127 */
128RTDECL(bool) RTPathExists(const char *pszPath);
129
130/**
131 * Sets the current working directory of the process.
132 *
133 * @returns IPRT status code.
134 * @param pszPath The path to the new working directory.
135 */
136RTDECL(int) RTPathSetCurrent(const char *pszPath);
137
138/**
139 * Gets the current working directory of the process.
140 *
141 * @returns IPRT status code.
142 * @param pszPath Where to store the path.
143 * @param cchPath The size of the buffer pszPath points to.
144 */
145RTDECL(int) RTPathGetCurrent(char *pszPath, size_t cchPath);
146
147/**
148 * Get the real path (no symlinks, no . or .. components), must exist.
149 *
150 * @returns iprt status code.
151 * @param pszPath The path to resolve.
152 * @param pszRealPath Where to store the real path.
153 * @param cchRealPath Size of the buffer.
154 */
155RTDECL(int) RTPathReal(const char *pszPath, char *pszRealPath, size_t cchRealPath);
156
157/**
158 * Same as RTPathReal only the result is RTStrDup()'ed.
159 *
160 * @returns Pointer to real path. Use RTStrFree() to free this string.
161 * @returns NULL if RTPathReal() or RTStrDup() fails.
162 * @param pszPath The path to resolve.
163 */
164RTDECL(char *) RTPathRealDup(const char *pszPath);
165
166/**
167 * Get the absolute path (starts from root, no . or .. components), doesn't have
168 * to exist. Note that this method is designed to never perform actual file
169 * system access, therefore symlinks are not resolved.
170 *
171 * @returns iprt status code.
172 * @param pszPath The path to resolve.
173 * @param pszAbsPath Where to store the absolute path.
174 * @param cchAbsPath Size of the buffer.
175 */
176RTDECL(int) RTPathAbs(const char *pszPath, char *pszAbsPath, size_t cchAbsPath);
177
178/**
179 * Same as RTPathAbs only the result is RTStrDup()'ed.
180 *
181 * @returns Pointer to the absolute path. Use RTStrFree() to free this string.
182 * @returns NULL if RTPathAbs() or RTStrDup() fails.
183 * @param pszPath The path to resolve.
184 */
185RTDECL(char *) RTPathAbsDup(const char *pszPath);
186
187/**
188 * Get the absolute path (no symlinks, no . or .. components), assuming the
189 * given base path as the current directory. The resulting path doesn't have
190 * to exist.
191 *
192 * @returns iprt status code.
193 * @param pszBase The base path to act like a current directory.
194 * When NULL, the actual cwd is used (i.e. the call
195 * is equivalent to RTPathAbs(pszPath, ...).
196 * @param pszPath The path to resolve.
197 * @param pszAbsPath Where to store the absolute path.
198 * @param cchAbsPath Size of the buffer.
199 */
200RTDECL(int) RTPathAbsEx(const char *pszBase, const char *pszPath, char *pszAbsPath, size_t cchAbsPath);
201
202/**
203 * Same as RTPathAbsEx only the result is RTStrDup()'ed.
204 *
205 * @returns Pointer to the absolute path. Use RTStrFree() to free this string.
206 * @returns NULL if RTPathAbsEx() or RTStrDup() fails.
207 * @param pszBase The base path to act like a current directory.
208 * When NULL, the actual cwd is used (i.e. the call
209 * is equivalent to RTPathAbs(pszPath, ...).
210 * @param pszPath The path to resolve.
211 */
212RTDECL(char *) RTPathAbsExDup(const char *pszBase, const char *pszPath);
213
214/**
215 * Strips the filename from a path. Truncates the given string in-place by overwriting the
216 * last path separator character with a null byte in a platform-neutral way.
217 *
218 * @param pszPath Path from which filename should be extracted, will be truncated.
219 * If the string contains no path separator, it will be changed to a "." string.
220 */
221RTDECL(void) RTPathStripFilename(char *pszPath);
222
223/**
224 * Strips the extension from a path.
225 *
226 * @param pszPath Path which extension should be stripped.
227 */
228RTDECL(void) RTPathStripExt(char *pszPath);
229
230/**
231 * Strips the trailing slashes of a path name.
232 *
233 * @param pszPath Path to strip.
234 */
235RTDECL(void) RTPathStripTrailingSlash(char *pszPath);
236
237/**
238 * Parses a path.
239 *
240 * It figures the length of the directory component, the offset of
241 * the file name and the location of the suffix dot.
242 *
243 * @returns The path length.
244 *
245 * @param pszPath Path to find filename in.
246 * @param pcbDir Where to put the length of the directory component.
247 * If no directory, this will be 0. Optional.
248 * @param poffName Where to store the filename offset.
249 * If empty string or if it's ending with a slash this
250 * will be set to -1. Optional.
251 * @param poffSuff Where to store the suffix offset (the last dot).
252 * If empty string or if it's ending with a slash this
253 * will be set to -1. Optional.
254 * @param pfFlags Where to set flags returning more information about
255 * the path. For the future. Optional.
256 */
257RTDECL(size_t) RTPathParse(const char *pszPath, size_t *pcchDir, ssize_t *poffName, ssize_t *poffSuff);
258
259/**
260 * Finds the filename in a path.
261 *
262 * @returns Pointer to filename within pszPath.
263 * @returns NULL if no filename (i.e. empty string or ends with a slash).
264 * @param pszPath Path to find filename in.
265 */
266RTDECL(char *) RTPathFilename(const char *pszPath);
267
268/**
269 * Finds the extension part of in a path.
270 *
271 * @returns Pointer to extension within pszPath.
272 * @returns NULL if no extension.
273 * @param pszPath Path to find extension in.
274 */
275RTDECL(char *) RTPathExt(const char *pszPath);
276
277/**
278 * Checks if a path have an extension.
279 *
280 * @returns true if extension present.
281 * @returns false if no extension.
282 * @param pszPath Path to check.
283 */
284RTDECL(bool) RTPathHaveExt(const char *pszPath);
285
286/**
287 * Checks if a path includes more than a filename.
288 *
289 * @returns true if path present.
290 * @returns false if no path.
291 * @param pszPath Path to check.
292 */
293RTDECL(bool) RTPathHavePath(const char *pszPath);
294
295/**
296 * Compares two paths.
297 *
298 * The comparison takes platform-dependent details into account,
299 * such as:
300 * <ul>
301 * <li>On DOS-like platforms, both separator chars (|\| and |/|) are considered
302 * to be equal.
303 * <li>On platforms with case-insensitive file systems, mismatching characters
304 * are uppercased and compared again.
305 * </ul>
306 *
307 * @returns @< 0 if the first path less than the second path.
308 * @returns 0 if the first path identical to the second path.
309 * @returns @> 0 if the first path greater than the second path.
310 *
311 * @param pszPath1 Path to compare (must be an absolute path).
312 * @param pszPath2 Path to compare (must be an absolute path).
313 *
314 * @remarks File system details are currently ignored. This means that you won't
315 * get case-insentive compares on unix systems when a path goes into a
316 * case-insensitive filesystem like FAT, HPFS, HFS, NTFS, JFS, or
317 * similar. For NT, OS/2 and similar you'll won't get case-sensitve
318 * compares on a case-sensitive file system.
319 */
320RTDECL(int) RTPathCompare(const char *pszPath1, const char *pszPath2);
321
322/**
323 * Checks if a path starts with the given parent path.
324 *
325 * This means that either the path and the parent path matches completely, or
326 * that the path is to some file or directory residing in the tree given by the
327 * parent directory.
328 *
329 * The path comparison takes platform-dependent details into account,
330 * see RTPathCompare() for details.
331 *
332 * @returns |true| when \a pszPath starts with \a pszParentPath (or when they
333 * are identical), or |false| otherwise.
334 *
335 * @param pszPath Path to check, must be an absolute path.
336 * @param pszParentPath Parent path, must be an absolute path.
337 * No trailing directory slash!
338 *
339 * @remarks This API doesn't currently handle root directory compares in a
340 * manner consistant with the other APIs. RTPathStartsWith(pszSomePath,
341 * "/") will not work if pszSomePath isn't "/".
342 */
343RTDECL(bool) RTPathStartsWith(const char *pszPath, const char *pszParentPath);
344
345/**
346 * Appends one partial path to another.
347 *
348 * The main purpose of this function is to deal correctly with the slashes when
349 * concatenating the two partial paths.
350 *
351 * @retval VINF_SUCCESS on success.
352 * @retval VERR_BUFFER_OVERFLOW if the result is too big to fit within
353 * cbPathDst bytes. No changes has been made.
354 * @retval VERR_INVALID_PARAMETER if the string pointed to by pszPath is longer
355 * than cbPathDst-1 bytes (failed to find terminator). Asserted.
356 *
357 * @param pszPath The path to append pszAppend to. This serves as both
358 * input and output. This can be empty, in which case
359 * pszAppend is just copied over.
360 * @param cbPathDst The size of the buffer pszPath points to, terminator
361 * included. This should NOT be strlen(pszPath).
362 * @param pszAppend The partial path to append to pszPath. This can be
363 * NULL, in which case nothing is done.
364 *
365 * @remarks On OS/2, Window and similar systems, concatenating a drive letter
366 * specifier with a slash prefixed path will result in an absolute
367 * path. Meaning, RTPathAppend(strcpy(szBuf, "C:"), sizeof(szBuf),
368 * "/bar") will result in "C:/bar". (This follows directly from the
369 * behavior when pszPath is empty.)
370 *
371 * On the other hand, when joining a drive letter specifier with a
372 * partial path that does not start with a slash, the result is not an
373 * absolute path. Meaning, RTPathAppend(strcpy(szBuf, "C:"),
374 * sizeof(szBuf), "bar") will result in "C:bar".
375 */
376RTDECL(int) RTPathAppend(char *pszPath, size_t cbPathDst, const char *pszAppend);
377
378/**
379 * Callback for RTPathTraverseList that's called for each element.
380 *
381 * @returns IPRT style status code. Return VINF_TRY_AGAIN to continue, any other
382 * value will abort the traversing and be returned to the caller.
383 *
384 * @param pchPath Pointer to the start of the current path. This is
385 * not null terminated.
386 * @param cchPath The length of the path.
387 * @param pvUser1 The first user parameter.
388 * @param pvUser2 The second user parameter.
389 */
390typedef DECLCALLBACK(int) FNRTPATHTRAVERSER(char const *pchPath, size_t cchPath, void *pvUser1, void *pvUser2);
391/** Pointer to a FNRTPATHTRAVERSER. */
392typedef FNRTPATHTRAVERSER *PFNRTPATHTRAVERSER;
393
394/**
395 * Traverses a string that can contain multiple paths separated by a special
396 * character.
397 *
398 * @returns IPRT style status code from the callback or VERR_END_OF_STRING if
399 * the callback returned VINF_TRY_AGAIN for all paths in the string.
400 *
401 * @param pszPathList The string to traverse.
402 * @param chSep The separator character. Using the null terminator
403 * is fine, but the result will simply be that there
404 * will only be one callback for the entire string
405 * (save any leading white space).
406 * @param pfnCallback The callback.
407 * @param pvUser1 First user argument for the callback.
408 * @param pvUser2 Second user argument for the callback.
409 */
410RTDECL(int) RTPathTraverseList(const char *pszPathList, char chSep, PFNRTPATHTRAVERSER pfnCallback, void *pvUser1, void *pvUser2);
411
412
413#ifdef IN_RING3
414
415/**
416 * Gets the path to the directory containing the executable.
417 *
418 * @returns iprt status code.
419 * @param pszPath Buffer where to store the path.
420 * @param cchPath Buffer size in bytes.
421 */
422RTDECL(int) RTPathExecDir(char *pszPath, size_t cchPath);
423
424/**
425 * Gets the user home directory.
426 *
427 * @returns iprt status code.
428 * @param pszPath Buffer where to store the path.
429 * @param cchPath Buffer size in bytes.
430 */
431RTDECL(int) RTPathUserHome(char *pszPath, size_t cchPath);
432
433/**
434 * Gets the directory of shared libraries.
435 *
436 * This is not the same as RTPathAppPrivateArch() as Linux depends all shared
437 * libraries in a common global directory where ld.so can found them.
438 *
439 * Linux: /usr/lib
440 * Windows: @<program files directory@>/@<application@>
441 * Old path: same as RTPathExecDir()
442 *
443 * @returns iprt status code.
444 * @param pszPath Buffer where to store the path.
445 * @param cchPath Buffer size in bytes.
446 */
447RTDECL(int) RTPathSharedLibs(char *pszPath, size_t cchPath);
448
449/**
450 * Gets the directory for architecture-independent application data, for
451 * example NLS files, module sources, ...
452 *
453 * Linux: /usr/shared/@<application@>
454 * Windows: @<program files directory@>/@<application@>
455 * Old path: same as RTPathExecDir()
456 *
457 * @returns iprt status code.
458 * @param pszPath Buffer where to store the path.
459 * @param cchPath Buffer size in bytes.
460 */
461RTDECL(int) RTPathAppPrivateNoArch(char *pszPath, size_t cchPath);
462
463/**
464 * Gets the directory for architecture-dependent application data, for
465 * example modules which can be loaded at runtime.
466 *
467 * Linux: /usr/lib/@<application@>
468 * Windows: @<program files directory@>/@<application@>
469 * Old path: same as RTPathExecDir()
470 *
471 * @returns iprt status code.
472 * @param pszPath Buffer where to store the path.
473 * @param cchPath Buffer size in bytes.
474 */
475RTDECL(int) RTPathAppPrivateArch(char *pszPath, size_t cchPath);
476
477/**
478 * Gets the directory for documentation.
479 *
480 * Linux: /usr/share/doc/@<application@>
481 * Windows: @<program files directory@>/@<application@>
482 * Old path: same as RTPathExecDir()
483 *
484 * @returns iprt status code.
485 * @param pszPath Buffer where to store the path.
486 * @param cchPath Buffer size in bytes.
487 */
488RTDECL(int) RTPathAppDocs(char *pszPath, size_t cchPath);
489
490/**
491 * Gets the temporary directory path.
492 *
493 * @returns iprt status code.
494 * @param pszPath Buffer where to store the path.
495 * @param cchPath Buffer size in bytes.
496 */
497RTDECL(int) RTPathTemp(char *pszPath, size_t cchPath);
498
499/**
500 * Query information about a file system object.
501 *
502 * This API will not resolve symbolic links in the last component (just
503 * like unix lstat()).
504 *
505 * @returns VINF_SUCCESS if the object exists, information returned.
506 * @returns VERR_PATH_NOT_FOUND if any but the last component in the specified
507 * path was not found or was not a directory.
508 * @returns VERR_FILE_NOT_FOUND if the object does not exist (but path to the
509 * parent directory exists).
510 * @returns some other iprt status code.
511 *
512 * @param pszPath Path to the file system object.
513 * @param pObjInfo Object information structure to be filled on successful return.
514 * @param enmAdditionalAttribs
515 * Which set of additional attributes to request.
516 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
517 */
518RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs);
519
520/**
521 * Changes the mode flags of a file system object.
522 *
523 * The API requires at least one of the mode flag sets (Unix/Dos) to
524 * be set. The type is ignored.
525 *
526 * This API will resolve symbolic links in the last component since
527 * mode isn't important for symbolic links.
528 *
529 * @returns iprt status code.
530 * @param pszPath Path to the file system object.
531 * @param fMode The new file mode, see @ref grp_rt_fs for details.
532 */
533RTR3DECL(int) RTPathSetMode(const char *pszPath, RTFMODE fMode);
534
535/**
536 * Gets the mode flags of a file system object.
537 *
538 * @returns iprt status code.
539 * @param pszPath Path to the file system object.
540 * @param pfMode Where to store the file mode, see @ref grp_rt_fs for details.
541 *
542 * @remark This is wrapper around RTPathReal() + RTPathQueryInfo()
543 * and exists to complement RTPathSetMode().
544 */
545RTR3DECL(int) RTPathGetMode(const char *pszPath, PRTFMODE pfMode);
546
547/**
548 * Changes one or more of the timestamps associated of file system object.
549 *
550 * This API will not resolve symbolic links in the last component (just
551 * like unix lutimes()).
552 *
553 * @returns iprt status code.
554 * @param pszPath Path to the file system object.
555 * @param pAccessTime Pointer to the new access time.
556 * @param pModificationTime Pointer to the new modification time.
557 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
558 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
559 *
560 * @remark The file system might not implement all these time attributes,
561 * the API will ignore the ones which aren't supported.
562 *
563 * @remark The file system might not implement the time resolution
564 * employed by this interface, the time will be chopped to fit.
565 *
566 * @remark The file system may update the change time even if it's
567 * not specified.
568 *
569 * @remark POSIX can only set Access & Modification and will always set both.
570 */
571RTR3DECL(int) RTPathSetTimes(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
572 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime);
573
574/**
575 * Gets one or more of the timestamps associated of file system object.
576 *
577 * @returns iprt status code.
578 * @param pszPath Path to the file system object.
579 * @param pAccessTime Where to store the access time. NULL is ok.
580 * @param pModificationTime Where to store the modification time. NULL is ok.
581 * @param pChangeTime Where to store the change time. NULL is ok.
582 * @param pBirthTime Where to store the creation time. NULL is ok.
583 *
584 * @remark This is wrapper around RTPathQueryInfo() and exists to complement RTPathSetTimes().
585 */
586RTR3DECL(int) RTPathGetTimes(const char *pszPath, PRTTIMESPEC pAccessTime, PRTTIMESPEC pModificationTime,
587 PRTTIMESPEC pChangeTime, PRTTIMESPEC pBirthTime);
588
589/**
590 * Changes the owner and/or group of a file system object.
591 *
592 * This API will not resolve symbolic links in the last component (just
593 * like unix lchown()).
594 *
595 * @returns iprt status code.
596 * @param pszPath Path to the file system object.
597 * @param uid The new file owner user id. Use -1 (or ~0) to leave this unchanged.
598 * @param gid The new group id. Use -1 (or ~0) to leave this unchanged.
599 */
600RTR3DECL(int) RTPathSetOwner(const char *pszPath, uint32_t uid, uint32_t gid);
601
602/**
603 * Gets the owner and/or group of a file system object.
604 *
605 * @returns iprt status code.
606 * @param pszPath Path to the file system object.
607 * @param pUid Where to store the owner user id. NULL is ok.
608 * @param pGid Where to store the group id. NULL is ok.
609 *
610 * @remark This is wrapper around RTPathQueryInfo() and exists to complement RTPathGetOwner().
611 */
612RTR3DECL(int) RTPathGetOwner(const char *pszPath, uint32_t *pUid, uint32_t *pGid);
613
614
615/** @name RTPathRename, RTDirRename & RTFileRename flags.
616 * @{ */
617/** This will replace attempt any target which isn't a directory. */
618#define RTPATHRENAME_FLAGS_REPLACE RT_BIT(0)
619/** @} */
620
621/**
622 * Renames a path within a filesystem.
623 *
624 * @returns IPRT status code.
625 * @param pszSrc The source path.
626 * @param pszDst The destination path.
627 * @param fRename Rename flags, RTPATHRENAME_FLAGS_*.
628 */
629RTR3DECL(int) RTPathRename(const char *pszSrc, const char *pszDst, unsigned fRename);
630
631#endif /* IN_RING3 */
632
633/** @} */
634
635RT_C_DECLS_END
636
637#endif
638
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