VirtualBox

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

Last change on this file since 95897 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 64.3 KB
Line 
1/** @file
2 * IPRT - Path Manipulation.
3 */
4
5/*
6 * Copyright (C) 2006-2022 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_INCLUDED_path_h
27#define IPRT_INCLUDED_path_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/cdefs.h>
33#include <iprt/types.h>
34#ifdef IN_RING3
35# include <iprt/fs.h>
36#endif
37
38
39
40RT_C_DECLS_BEGIN
41
42/** @defgroup grp_rt_path RTPath - Path Manipulation
43 * @ingroup grp_rt
44 * @{
45 */
46
47/**
48 * Host max path (the reasonable value).
49 * @remarks defined both by iprt/param.h and iprt/path.h.
50 */
51#if !defined(IPRT_INCLUDED_param_h) || defined(DOXYGEN_RUNNING)
52# define RTPATH_MAX (4096 + 4) /* (PATH_MAX + 1) on linux w/ some alignment */
53#endif
54
55/**
56 * The absolute max host path length we are willing to support.
57 * @note Not really suitable for stack buffers.
58 */
59#define RTPATH_BIG_MAX (_64K)
60
61/** @def RTPATH_TAG
62 * The default allocation tag used by the RTPath allocation APIs.
63 *
64 * When not defined before the inclusion of iprt/string.h, this will default to
65 * the pointer to the current file name. The string API will make of use of
66 * this as pointer to a volatile but read-only string.
67 */
68#ifndef RTPATH_TAG
69# define RTPATH_TAG (__FILE__)
70#endif
71
72
73/** @name RTPATH_F_XXX - Generic flags for APIs working on the file system.
74 * @{ */
75/** Last component: Work on the link. */
76#define RTPATH_F_ON_LINK RT_BIT_32(0)
77/** Last component: Follow if link. */
78#define RTPATH_F_FOLLOW_LINK RT_BIT_32(1)
79/** Don't allow symbolic links as part of the path.
80 * @remarks this flag is currently not implemented and will be ignored. */
81#define RTPATH_F_NO_SYMLINKS RT_BIT_32(2)
82/** Current RTPATH_F_XXX flag mask. */
83#define RTPATH_F_MASK UINT32_C(0x00000007)
84/** @} */
85
86/** Validates a flags parameter containing RTPATH_F_*.
87 * @remarks The parameters will be referenced multiple times. */
88#define RTPATH_F_IS_VALID(a_fFlags, a_fIgnore) \
89 ( ((a_fFlags) & ~(uint32_t)((a_fIgnore) | RTPATH_F_NO_SYMLINKS)) == RTPATH_F_ON_LINK \
90 || ((a_fFlags) & ~(uint32_t)((a_fIgnore) | RTPATH_F_NO_SYMLINKS)) == RTPATH_F_FOLLOW_LINK )
91
92
93/** @name RTPATH_STR_F_XXX - Generic flags for APIs working with path strings.
94 * @{
95 */
96/** Host OS path style (default 0 value). */
97#define RTPATH_STR_F_STYLE_HOST UINT32_C(0x00000000)
98/** DOS, OS/2 and Windows path style. */
99#define RTPATH_STR_F_STYLE_DOS UINT32_C(0x00000001)
100/** Unix path style. */
101#define RTPATH_STR_F_STYLE_UNIX UINT32_C(0x00000002)
102/** Reserved path style. */
103#define RTPATH_STR_F_STYLE_RESERVED UINT32_C(0x00000003)
104/** The path style mask. */
105#define RTPATH_STR_F_STYLE_MASK UINT32_C(0x00000003)
106/** Partial path - no start.
107 * This causes the API to skip the root specification parsing. */
108#define RTPATH_STR_F_NO_START UINT32_C(0x00000010)
109/** Partial path - no end.
110 * This causes the API to skip the filename and dir-slash parsing. */
111#define RTPATH_STR_F_NO_END UINT32_C(0x00000020)
112/** Partial path - no start and no end. */
113#define RTPATH_STR_F_MIDDLE (RTPATH_STR_F_NO_START | RTPATH_STR_F_NO_END)
114
115/** Reserved for future use. */
116#define RTPATH_STR_F_RESERVED_MASK UINT32_C(0x0000ffcc)
117/** @} */
118
119/** Validates a flags parameter containing RTPATH_FSTR_.
120 * @remarks The parameters will be references multiple times. */
121#define RTPATH_STR_F_IS_VALID(a_fFlags, a_fIgnore) \
122 ( ((a_fFlags) & ~((uint32_t)(a_fIgnore) | RTPATH_STR_F_STYLE_MASK | RTPATH_STR_F_MIDDLE)) == 0 \
123 && ((a_fFlags) & RTPATH_STR_F_STYLE_MASK) != RTPATH_STR_F_STYLE_RESERVED \
124 && ((a_fFlags) & RTPATH_STR_F_RESERVED_MASK) == 0 )
125
126
127/** @def RTPATH_STYLE
128 * The host path style. This is set to RTPATH_STR_F_STYLE_DOS,
129 * RTPATH_STR_F_STYLE_UNIX, or other future styles. */
130#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
131# define RTPATH_STYLE RTPATH_STR_F_STYLE_DOS
132#else
133# define RTPATH_STYLE RTPATH_STR_F_STYLE_UNIX
134#endif
135
136
137/** @def RTPATH_SLASH
138 * The preferred slash character.
139 *
140 * @remark IPRT will always accept unix slashes. So, normally you would
141 * never have to use this define.
142 */
143#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
144# define RTPATH_SLASH '\\'
145#elif RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX
146# define RTPATH_SLASH '/'
147#else
148# error "Unsupported RTPATH_STYLE value."
149#endif
150
151/** @deprecated Use '/'! */
152#define RTPATH_DELIMITER RTPATH_SLASH
153
154
155/** @def RTPATH_SLASH_STR
156 * The preferred slash character as a string, handy for concatenations
157 * with other strings.
158 *
159 * @remark IPRT will always accept unix slashes. So, normally you would
160 * never have to use this define.
161 */
162#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
163# define RTPATH_SLASH_STR "\\"
164#elif RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX
165# define RTPATH_SLASH_STR "/"
166#else
167# error "Unsupported RTPATH_STYLE value."
168#endif
169
170
171/** @def RTPATH_IS_SLASH
172 * Checks if a character is a slash.
173 *
174 * @returns true if it's a slash and false if not.
175 * @returns @param a_ch Char to check.
176 */
177#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
178# define RTPATH_IS_SLASH(a_ch) ( (a_ch) == '\\' || (a_ch) == '/' )
179#elif RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX
180# define RTPATH_IS_SLASH(a_ch) ( (a_ch) == '/' )
181#else
182# error "Unsupported RTPATH_STYLE value."
183#endif
184
185
186/** @def RTPATH_IS_VOLSEP
187 * Checks if a character marks the end of the volume specification.
188 *
189 * @remark This is sufficient for the drive letter concept on PC.
190 * However it might be insufficient on other platforms
191 * and even on PC a UNC volume spec won't be detected this way.
192 * Use the RTPath@<too be created@>() instead.
193 *
194 * @returns true if it is and false if it isn't.
195 * @returns @param a_ch Char to check.
196 */
197#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
198# define RTPATH_IS_VOLSEP(a_ch) ( (a_ch) == ':' )
199#elif RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX
200# define RTPATH_IS_VOLSEP(a_ch) (false)
201#else
202# error "Unsupported RTPATH_STYLE value."
203#endif
204
205
206/** @def RTPATH_IS_SEP
207 * Checks if a character is path component separator
208 *
209 * @returns true if it is and false if it isn't.
210 * @returns @param a_ch Char to check.
211 * @
212 */
213#define RTPATH_IS_SEP(a_ch) ( RTPATH_IS_SLASH(a_ch) || RTPATH_IS_VOLSEP(a_ch) )
214
215#if defined(RT_OS_WINDOWS) || defined(DOXYGEN_RUNNING)
216/** @def RTPATH_NT_PASSTHRU_PREFIX
217 * Prefix used to access the NT namespace directly.
218 * This forms an invalid UNC name. */
219# define RTPATH_NT_PASSTHRU_PREFIX "\\\\:iprtnt:\\"
220#endif
221
222/**
223 * Checks if the path exists.
224 *
225 * Symbolic links will all be attempted resolved and broken links means false.
226 *
227 * @returns true if it exists and false if it doesn't.
228 * @param pszPath The path to check.
229 */
230RTDECL(bool) RTPathExists(const char *pszPath);
231
232/**
233 * Checks if the path exists.
234 *
235 * @returns true if it exists and false if it doesn't.
236 * @param pszPath The path to check.
237 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
238 */
239RTDECL(bool) RTPathExistsEx(const char *pszPath, uint32_t fFlags);
240
241/**
242 * Sets the current working directory of the process.
243 *
244 * @returns IPRT status code.
245 * @param pszPath The path to the new working directory.
246 */
247RTDECL(int) RTPathSetCurrent(const char *pszPath);
248
249/**
250 * Gets the current working directory of the process.
251 *
252 * @returns IPRT status code.
253 * @param pszPath Where to store the path.
254 * @param cchPath The size of the buffer pszPath points to.
255 */
256RTDECL(int) RTPathGetCurrent(char *pszPath, size_t cchPath);
257
258/**
259 * Gets the current working directory on the specified drive.
260 *
261 * On systems without drive letters, the root slash will be returned.
262 *
263 * @returns IPRT status code.
264 * @param chDrive The drive we're querying the driver letter on.
265 * @param pszPath Where to store the working directroy path.
266 * @param cbPath The size of the buffer pszPath points to.
267 */
268RTDECL(int) RTPathGetCurrentOnDrive(char chDrive, char *pszPath, size_t cbPath);
269
270/**
271 * Gets the current working drive of the process.
272 *
273 * Normally drive letter and colon will be returned, never trailing a root
274 * slash. If the current directory is on a UNC share, the root of the share
275 * will be returned. On systems without drive letters, an empty string is
276 * returned for consistency.
277 *
278 * @returns IPRT status code.
279 * @param pszPath Where to store the working drive or UNC root.
280 * @param cbPath The size of the buffer pszPath points to.
281 */
282RTDECL(int) RTPathGetCurrentDrive(char *pszPath, size_t cbPath);
283
284/**
285 * Get the real path (no symlinks, no . or .. components), must exist.
286 *
287 * @returns iprt status code.
288 * @param pszPath The path to resolve.
289 * @param pszRealPath Where to store the real path.
290 * @param cchRealPath Size of the buffer.
291 */
292RTDECL(int) RTPathReal(const char *pszPath, char *pszRealPath, size_t cchRealPath);
293
294/**
295 * Same as RTPathReal only the result is RTStrDup()'ed.
296 *
297 * @returns Pointer to real path. Use RTStrFree() to free this string.
298 * @returns NULL if RTPathReal() or RTStrDup() fails.
299 * @param pszPath The path to resolve.
300 */
301RTDECL(char *) RTPathRealDup(const char *pszPath);
302
303/**
304 * Get the absolute path (starts from root, no . or .. components), doesn't have
305 * to exist.
306 *
307 * Note that this method is designed to never perform actual file system access,
308 * therefore symlinks are not resolved.
309 *
310 * @returns iprt status code.
311 * @param pszPath The path to resolve.
312 * @param pszAbsPath Where to store the absolute path.
313 * @param cbAbsPath Size of the buffer.
314 *
315 * @note Current implementation is buggy and will remove trailing slashes
316 * that would normally specify a directory. Don't depend on this.
317 */
318RTDECL(int) RTPathAbs(const char *pszPath, char *pszAbsPath, size_t cbAbsPath);
319
320/**
321 * Same as RTPathAbs only the result is RTStrDup()'ed.
322 *
323 * @returns Pointer to the absolute path. Use RTStrFree() to free this string.
324 * @returns NULL if RTPathAbs() or RTStrDup() fails.
325 * @param pszPath The path to resolve.
326 *
327 * @note Current implementation is buggy and will remove trailing slashes
328 * that would normally specify a directory. Don't depend on this.
329 */
330RTDECL(char *) RTPathAbsDup(const char *pszPath);
331
332/**
333 * Get the absolute path (no symlinks, no . or .. components), assuming the
334 * given base path as the current directory.
335 *
336 * The resulting path doesn't have to exist.
337 *
338 * @returns iprt status code.
339 * @param pszBase The base path to act like a current directory.
340 * When NULL, the actual cwd is used (i.e. the call
341 * is equivalent to RTPathAbs(pszPath, ...).
342 * @param pszPath The path to resolve.
343 * @param fFlags One of the RTPATH_STR_F_STYLE_XXX flags combined
344 * with any of the RTPATHABS_F_XXX ones. Most
345 * users will pass RTPATH_STR_F_STYLE_HOST (0).
346 * @param pszAbsPath Where to store the absolute path.
347 * @param pcbAbsPath Hold the size of the buffer when called. The return
348 * value is the string length on success, and the
349 * required (or slightly more in some case) buffer
350 * size, including terminator, on VERR_BUFFER_OVERFLOW
351 * failures.
352 */
353RTDECL(int) RTPathAbsEx(const char *pszBase, const char *pszPath, uint32_t fFlags, char *pszAbsPath, size_t *pcbAbsPath);
354
355/** @name RTPATHABS_F_XXX - Flags for RTPathAbsEx.
356 * @note The RTPATH_F_STR_XXX style flags also applies.
357 * @{ */
358/** Treat specified base directory as a root that cannot be ascended beyond. */
359#define RTPATHABS_F_STOP_AT_BASE RT_BIT_32(16)
360/** Treat CWD as a root that cannot be ascended beyond. */
361#define RTPATHABS_F_STOP_AT_CWD RT_BIT_32(17)
362/** Ensure trailing slash in the result. */
363#define RTPATHABS_F_ENSURE_TRAILING_SLASH RT_BIT_32(18)
364/** @} */
365
366/**
367 * Same as RTPathAbsEx only the result is RTStrDup()'ed.
368 *
369 * @returns Pointer to the absolute path. Use RTStrFree() to free this string.
370 * @retval NULL if RTPathAbsEx() or RTStrDup() fails.
371 *
372 * @param pszBase The base path to act like a current directory.
373 * When NULL, the actual cwd is used (i.e. the call
374 * is equivalent to RTPathAbs(pszPath, ...).
375 * @param pszPath The path to resolve.
376 * @param fFlags One of the RTPATH_STR_F_STYLE_XXX flags combined
377 * with any of the RTPATHABS_F_XXX ones. Most
378 * users will pass RTPATH_STR_F_STYLE_HOST (0).
379 */
380RTDECL(char *) RTPathAbsExDup(const char *pszBase, const char *pszPath, uint32_t fFlags);
381
382/**
383 * Strips the filename from a path. Truncates the given string in-place by overwriting the
384 * last path separator character with a null byte in a platform-neutral way.
385 *
386 * @param pszPath Path from which filename should be extracted, will be truncated.
387 * If the string contains no path separator, it will be changed to a "." string.
388 */
389RTDECL(void) RTPathStripFilename(char *pszPath);
390
391/**
392 * Strips the last suffix from a path.
393 *
394 * @param pszPath Path which suffix should be stripped.
395 */
396RTDECL(void) RTPathStripSuffix(char *pszPath);
397
398/**
399 * Strips the trailing slashes of a path name.
400 *
401 * Won't strip root slashes.
402 *
403 * @returns The new length of pszPath.
404 * @param pszPath Path to strip.
405 */
406RTDECL(size_t) RTPathStripTrailingSlash(char *pszPath);
407
408/**
409 * Skips the root specification, if present.
410 *
411 * @return Pointer to the first char after the root specification. This can be
412 * pointing to the terminator, if the path is only a root
413 * specification.
414 * @param pszPath The path to skip ahead in.
415 */
416RTDECL(char *) RTPathSkipRootSpec(const char *pszPath);
417
418/**
419 * Ensures that the path has a trailing path separator such that file names can
420 * be appended without further work.
421 *
422 * This can be helpful when preparing for efficiently combining a directory path
423 * with the filenames returned by RTDirRead. The return value gives you the
424 * position at which you copy the RTDIRENTRY::szName to construct a valid path
425 * to it.
426 *
427 * @returns The length of the path, 0 on buffer overflow.
428 * @param pszPath The path.
429 * @param cbPath The length of the path buffer @a pszPath points to.
430 */
431RTDECL(size_t) RTPathEnsureTrailingSeparator(char *pszPath, size_t cbPath);
432
433/**
434 * Same as RTPathEnsureTrailingSeparator but with selectable path style.
435 *
436 * @returns The length of the path, 0 on buffer overflow.
437 * @param pszPath The path.
438 * @param cbPath The length of the path buffer @a pszPath points to.
439 * @param fFlags The path style, RTPATH_STR_F_STYLE_XXX.
440 * @sa RTPathEnsureTrailingSeparator
441 */
442RTDECL(size_t) RTPathEnsureTrailingSeparatorEx(char *pszPath, size_t cbPath, uint32_t fFlags);
443
444/**
445 * Changes all the slashes in the specified path to DOS style.
446 *
447 * Unless @a fForce is set, nothing will be done when on a UNIX flavored system
448 * since paths wont work with DOS style slashes there.
449 *
450 * @returns @a pszPath.
451 * @param pszPath The path to modify.
452 * @param fForce Whether to force the conversion on non-DOS OSes.
453 */
454RTDECL(char *) RTPathChangeToDosSlashes(char *pszPath, bool fForce);
455
456/**
457 * Changes all the slashes in the specified path to unix style.
458 *
459 * Unless @a fForce is set, nothing will be done when on a UNIX flavored system
460 * since paths wont work with DOS style slashes there.
461 *
462 * @returns @a pszPath.
463 * @param pszPath The path to modify.
464 * @param fForce Whether to force the conversion on non-DOS OSes.
465 */
466RTDECL(char *) RTPathChangeToUnixSlashes(char *pszPath, bool fForce);
467
468/**
469 * Purges a string so it can be used as a file according to fFlags.
470 *
471 * Illegal filename characters are replaced by '_'.
472 *
473 * @returns pszString
474 * @param pszString The string to purge.
475 * @param fFlags One of the RTPATH_STR_F_STYLE_XXX flags. Most users
476 * will pass RTPATH_STR_F_STYLE_HOST (0).
477 */
478RTDECL(char *) RTPathPurgeFilename(char *pszString, uint32_t fFlags);
479
480/**
481 * Simple parsing of the a path.
482 *
483 * It figures the length of the directory component, the offset of
484 * the file name and the location of the suffix dot.
485 *
486 * @returns The path length.
487 *
488 * @param pszPath Path to find filename in.
489 * @param pcchDir Where to put the length of the directory component. If
490 * no directory, this will be 0. Optional.
491 * @param poffName Where to store the filename offset.
492 * If empty string or if it's ending with a slash this
493 * will be set to -1. Optional.
494 * @param poffSuff Where to store the suffix offset (the last dot).
495 * If empty string or if it's ending with a slash this
496 * will be set to -1. Optional.
497 */
498RTDECL(size_t) RTPathParseSimple(const char *pszPath, size_t *pcchDir, ssize_t *poffName, ssize_t *poffSuff);
499
500/**
501 * Finds the filename in a path.
502 *
503 * @returns Pointer to filename within pszPath.
504 * @returns NULL if no filename (i.e. empty string or ends with a slash).
505 * @param pszPath Path to find filename in.
506 */
507RTDECL(char *) RTPathFilename(const char *pszPath);
508RTDECL(PRTUTF16) RTPathFilenameUtf16(PCRTUTF16 pwszPath);
509
510/**
511 * Finds the filename in a path, extended version.
512 *
513 * @returns Pointer to filename within pszPath.
514 * @returns NULL if no filename (i.e. empty string or ends with a slash).
515 * @param pszPath Path to find filename in.
516 * @param fFlags RTPATH_STR_F_STYLE_XXX. Other RTPATH_STR_F_XXX flags
517 * will be ignored.
518 */
519RTDECL(char *) RTPathFilenameEx(const char *pszPath, uint32_t fFlags);
520RTDECL(PRTUTF16) RTPathFilenameExUtf16(PCRTUTF16 pwszPath, uint32_t fFlags);
521
522/**
523 * Finds the common path in a given set of paths.
524 *
525 * The paths are not made absolute or real, they are taken as given.
526 *
527 * @returns The common path length as represented by \a papszPaths[0], 0 if not
528 * found or invalid input.
529 * @param cPaths Number of paths in \a papszPaths.
530 * @param papszPaths Array of paths to find common path for. The paths must
531 * not contains ".." sequences, as that's too complicated
532 * to handle.
533 */
534RTDECL(size_t) RTPathFindCommon(size_t cPaths, const char * const *papszPaths);
535
536/**
537 * Finds the common path in a given set of paths, extended version.
538 *
539 * The paths are not made absolute or real, they are taken as given.
540 *
541 * @returns The common path length as represented by \a papszPaths[0], 0 if not
542 * found or invalid input.
543 * @param cPaths Number of paths in \a papszPaths.
544 * @param papszPaths Array of paths to find common path for. The paths must
545 * not contains ".." sequences, as that's too complicated
546 * to handle.
547 * @param fFlags RTPATH_STR_F_STYLE_XXX, RTPATH_STR_F_NO_START, and
548 * RTPATHFINDCOMMON_F_IGNORE_DOTDOT as desired. Other
549 * RTPATH_STR_F_XXX flags will be ignored.
550 */
551RTDECL(size_t) RTPathFindCommonEx(size_t cPaths, const char * const *papszPaths, uint32_t fFlags);
552
553/** @name RTPATHFINDCOMMON_F_XXX - Flags for RTPathFindCommonEx.
554 * @{ */
555/** Ignore the dangers of '..' components. */
556#define RTPATHFINDCOMMON_F_IGNORE_DOTDOT RT_BIT_32(16)
557/** @} */
558
559/**
560 * Finds the suffix part of in a path (last dot and onwards).
561 *
562 * @returns Pointer to suffix within pszPath.
563 * @returns NULL if no suffix
564 * @param pszPath Path to find suffix in.
565 *
566 * @remarks IPRT terminology: A suffix includes the dot, the extension starts
567 * after the dot. For instance suffix '.txt' and extension 'txt'.
568 */
569RTDECL(char *) RTPathSuffix(const char *pszPath);
570
571/**
572 * Checks if a path has an extension / suffix.
573 *
574 * @returns true if extension / suffix present.
575 * @returns false if no extension / suffix.
576 * @param pszPath Path to check.
577 */
578RTDECL(bool) RTPathHasSuffix(const char *pszPath);
579/** Same thing, different name. */
580#define RTPathHasExt RTPathHasSuffix
581
582/**
583 * Checks if a path includes more than a filename.
584 *
585 * @returns true if path present.
586 * @returns false if no path.
587 * @param pszPath Path to check.
588 */
589RTDECL(bool) RTPathHasPath(const char *pszPath);
590/** Misspelled, don't use. */
591#define RTPathHavePath RTPathHasPath
592
593/**
594 * Checks if the path starts with a root specifier or not.
595 *
596 * @returns @c true if it starts with root, @c false if not.
597 *
598 * @param pszPath Path to check.
599 */
600RTDECL(bool) RTPathStartsWithRoot(const char *pszPath);
601
602/**
603 * Determins the length of the parent part of the given path.
604 *
605 * @returns The length of the parent section of the path, including the final
606 * path separator. Returns 0 if only filename or empty path.
607 * @param pszPath The path to evaluate.
608 *
609 * @note Will stop at the server for UNC paths, so given "//server/share/"
610 * the parent length will be 9.
611 */
612RTDECL(size_t) RTPathParentLength(const char *pszPath);
613
614/**
615 * Determins the length of the parent part of the given path, extended variant.
616 *
617 * @returns The length of the parent section of the path, including the final
618 * path separator. Returns 0 if only filename or empty path.
619 * @param pszPath The path to evaluate.
620 * @param fFlags RTPATH_STR_F_STYLE_XXX and RTPATH_STR_F_NO_START.
621 * Asserts and ignores RTPATH_STR_F_NO_END.
622 *
623 * @note Will stop at the server for UNC paths, so given "//server/share/"
624 * the parent length will be 9.
625 */
626RTDECL(size_t) RTPathParentLengthEx(const char *pszPath, uint32_t fFlags);
627
628/**
629 * Counts the components in the specified path.
630 *
631 * An empty string has zero components. A lone root slash is considered have
632 * one. The paths "/init" and "/bin/" are considered having two components. An
633 * UNC share specifier like "\\myserver\share" will be considered as one single
634 * component.
635 *
636 * @returns The number of path components.
637 * @param pszPath The path to parse.
638 */
639RTDECL(size_t) RTPathCountComponents(const char *pszPath);
640
641/**
642 * Copies the specified number of path components from @a pszSrc and into @a
643 * pszDst.
644 *
645 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW. In the latter case the buffer
646 * is not touched.
647 *
648 * @param pszDst The destination buffer.
649 * @param cbDst The size of the destination buffer.
650 * @param pszSrc The source path.
651 * @param cComponents The number of components to copy from @a pszSrc.
652 */
653RTDECL(int) RTPathCopyComponents(char *pszDst, size_t cbDst, const char *pszSrc, size_t cComponents);
654
655/** @name Path properties returned by RTPathParse and RTPathSplit.
656 * @{ */
657
658/** Indicates that there is a filename.
659 * If not set, either a lone root spec was given (RTPATH_PROP_UNC,
660 * RTPATH_PROP_ROOT_SLASH, or RTPATH_PROP_VOLUME) or the final component had a
661 * trailing slash (RTPATH_PROP_DIR_SLASH). */
662#define RTPATH_PROP_FILENAME UINT16_C(0x0001)
663/** Indicates that a directory was specified using a trailing slash.
664 * @note This is not set for lone root specifications (RTPATH_PROP_UNC,
665 * RTPATH_PROP_ROOT_SLASH, or RTPATH_PROP_VOLUME).
666 * @note The slash is not counted into the last component. However, it is
667 * counted into cchPath. */
668#define RTPATH_PROP_DIR_SLASH UINT16_C(0x0002)
669
670/** The filename has a suffix (extension). */
671#define RTPATH_PROP_SUFFIX UINT16_C(0x0004)
672/** Indicates that this is an UNC path (Windows and OS/2 only).
673 *
674 * UNC = Universal Naming Convention. It is on the form '//Computer/',
675 * '//Namespace/', '//ComputerName/Resource' and '//Namespace/Resource'.
676 * RTPathParse, RTPathSplit and friends does not consider the 'Resource' as
677 * part of the UNC root specifier. Thus the root specs for the above examples
678 * would be '//ComputerName/' or '//Namespace/'.
679 *
680 * Please note that '//something' is not a UNC path, there must be a slash
681 * following the computer or namespace.
682 */
683#define RTPATH_PROP_UNC UINT16_C(0x0010)
684/** A root slash was specified (unix style root).
685 * (While the path must relative if not set, this being set doesn't make it
686 * absolute.)
687 *
688 * This will be set in the following examples: '/', '/bin', 'C:/', 'C:/Windows',
689 * '//./', '//./PhysicalDisk0', '//example.org/', and '//example.org/share'.
690 *
691 * It will not be set for the following examples: '.', 'bin/ls', 'C:', and
692 * 'C:Windows'.
693 */
694#define RTPATH_PROP_ROOT_SLASH UINT16_C(0x0020)
695/** A volume is specified (Windows, DOS and OS/2).
696 * For examples: 'C:', 'C:/', and 'A:/AutoExec.bat'. */
697#define RTPATH_PROP_VOLUME UINT16_C(0x0040)
698/** The path is absolute, i.e. has a root specifier (root-slash,
699 * volume or UNC) and contains no winding '..' bits, though it may contain
700 * unnecessary slashes (RTPATH_PROP_EXTRA_SLASHES) and '.' components
701 * (RTPATH_PROP_DOT_REFS).
702 *
703 * On systems without volumes and UNC (unix style) it will be set for '/',
704 * '/bin/ls', and '/bin//./ls', but not for 'bin/ls', /bin/../usr/bin/env',
705 * '/./bin/ls' or '/.'.
706 *
707 * On systems with volumes, it will be set for 'C:/', C:/Windows', and
708 * 'C:/./Windows//', but not for 'C:', 'C:Windows', or 'C:/Windows/../boot.ini'.
709 *
710 * On systems with UNC paths, it will be set for '//localhost/',
711 * '//localhost/C$', '//localhost/C$/Windows/System32', '//localhost/.', and
712 * '//localhost/C$//./AutoExec.bat', but not for
713 * '//localhost/C$/Windows/../AutoExec.bat'.
714 *
715 * @note For the RTPathAbs definition, this flag needs to be set while both
716 * RTPATH_PROP_EXTRA_SLASHES and RTPATH_PROP_DOT_REFS must be cleared.
717 */
718#define RTPATH_PROP_ABSOLUTE UINT16_C(0x0100)
719/** Relative path. Inverse of RTPATH_PROP_ABSOLUTE. */
720#define RTPATH_PROP_RELATIVE UINT16_C(0x0200)
721/** The path contains unnecessary slashes. Meaning, that if */
722#define RTPATH_PROP_EXTRA_SLASHES UINT16_C(0x0400)
723/** The path contains references to the special '.' (dot) directory link. */
724#define RTPATH_PROP_DOT_REFS UINT16_C(0x0800)
725/** The path contains references to the special '..' (dot) directory link.
726 * RTPATH_PROP_RELATIVE will always be set together with this. */
727#define RTPATH_PROP_DOTDOT_REFS UINT16_C(0x1000)
728/** Special UNC root.
729 * The share name is not sacred when this is set. */
730#define RTPATH_PROP_SPECIAL_UNC UINT16_C(0x2000)
731
732
733/** Macro to determin whether to insert a slash after the first component when
734 * joining it with something else.
735 * (All other components in a split or parsed path requies slashes added.) */
736#define RTPATH_PROP_FIRST_NEEDS_NO_SLASH(a_fProps) \
737 RT_BOOL( (a_fProps) & (RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_VOLUME | RTPATH_PROP_UNC) )
738
739/** Macro to determin whether there is a root specification of any kind
740 * (unix, volumes, unc). */
741#define RTPATH_PROP_HAS_ROOT_SPEC(a_fProps) \
742 RT_BOOL( (a_fProps) & (RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_VOLUME | RTPATH_PROP_UNC) )
743
744/** @} */
745
746
747/**
748 * Parsed path.
749 *
750 * The first component is the root, volume or UNC specifier, if present. Use
751 * RTPATH_PROP_HAS_ROOT_SPEC() on RTPATHPARSED::fProps to determine its
752 * presence.
753 *
754 * Other than the root component, no component will include directory separators
755 * (slashes).
756 */
757typedef struct RTPATHPARSED
758{
759 /** Number of path components.
760 * This will always be set on VERR_BUFFER_OVERFLOW returns from RTPathParsed
761 * so the caller can calculate the required buffer size. */
762 uint16_t cComps;
763 /** Path property flags, RTPATH_PROP_XXX */
764 uint16_t fProps;
765 /** On success this is the length of the described path, i.e. sum of all
766 * component lengths and necessary separators.
767 * Do NOT use this to index in the source path in case it contains
768 * unnecessary slashes that RTPathParsed has ignored here. */
769 uint16_t cchPath;
770 /** Reserved for future use. */
771 uint16_t u16Reserved;
772 /** The offset of the filename suffix, offset of the NUL char if none. */
773 uint16_t offSuffix;
774 /** The length of the suffix. */
775 uint16_t cchSuffix;
776 /** Array of component descriptors (variable size).
777 * @note Don't try figure the end of the input path by adding up off and cch
778 * of the last component. If RTPATH_PROP_DIR_SLASH is set, there may
779 * be one or more trailing slashes that are unaccounted for! */
780 RT_FLEXIBLE_ARRAY_EXTENSION
781 struct
782 {
783 /** The offset of the component. */
784 uint16_t off;
785 /** The length of the component. */
786 uint16_t cch;
787 } aComps[RT_FLEXIBLE_ARRAY];
788} RTPATHPARSED;
789/** Pointer to to a parsed path result. */
790typedef RTPATHPARSED *PRTPATHPARSED;
791/** Pointer to to a const parsed path result. */
792typedef RTPATHPARSED *PCRTPATHPARSED;
793
794/** Stupid hack for MSC and flexible arrays. */
795#define RTPATHPARSED_MIN_SIZE (sizeof(uint16_t) * (6 + 4))
796
797
798/**
799 * Parses the path.
800 *
801 * @returns IPRT status code.
802 * @retval VERR_INVALID_POINTER if pParsed or pszPath is an invalid pointer.
803 * @retval VERR_INVALID_PARAMETER if cbOutput is less than the RTPATHPARSED
804 * strucuture. No output. (asserted)
805 * @retval VERR_BUFFER_OVERFLOW there are more components in the path than
806 * there is space in aComps. The required amount of space can be
807 * determined from the pParsed->cComps:
808 * @code
809 * RT_OFFSETOF(RTPATHPARSED, aComps[pParsed->cComps])
810 * @endcode
811 * @retval VERR_PATH_ZERO_LENGTH if the path is empty.
812 *
813 * @param pszPath The path to parse.
814 * @param pParsed Where to store the details of the parsed path.
815 * @param cbParsed The size of the buffer. Must be at least the
816 * size of RTPATHPARSED.
817 * @param fFlags Combination of RTPATH_STR_F_XXX flags.
818 * Most users will pass 0.
819 * @sa RTPathSplit, RTPathSplitA.
820 */
821RTDECL(int) RTPathParse(const char *pszPath, PRTPATHPARSED pParsed, size_t cbParsed, uint32_t fFlags);
822
823/**
824 * Reassembles a path parsed by RTPathParse.
825 *
826 * This will be more useful as more APIs manipulating the RTPATHPARSED output
827 * are added.
828 *
829 * @returns IPRT status code.
830 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small.
831 * The necessary length is @a pParsed->cchPath + 1 (updated).
832 *
833 * @param pszSrcPath The source path.
834 * @param pParsed The parser output for @a pszSrcPath. Caller may
835 * eliminate elements by setting their length to
836 * zero. The cchPath member is updated.
837 * @param fFlags Combination of RTPATH_STR_F_STYLE_XXX.
838 * Most users will pass 0.
839 * @param pszDstPath Pointer to the buffer where the path is to be
840 * reassembled.
841 * @param cbDstPath The size of the output buffer.
842 */
843RTDECL(int) RTPathParsedReassemble(const char *pszSrcPath, PRTPATHPARSED pParsed, uint32_t fFlags,
844 char *pszDstPath, size_t cbDstPath);
845
846
847/**
848 * Output buffer for RTPathSplit and RTPathSplitA.
849 */
850typedef struct RTPATHSPLIT
851{
852 /** Number of path components.
853 * This will always be set on VERR_BUFFER_OVERFLOW returns from RTPathParsed
854 * so the caller can calculate the required buffer size. */
855 uint16_t cComps;
856 /** Path property flags, RTPATH_PROP_XXX */
857 uint16_t fProps;
858 /** On success this is the length of the described path, i.e. sum of all
859 * component lengths and necessary separators.
860 * Do NOT use this to index in the source path in case it contains
861 * unnecessary slashes that RTPathSplit has ignored here. */
862 uint16_t cchPath;
863 /** Reserved (internal use). */
864 uint16_t u16Reserved;
865 /** The amount of memory used (on success) or required (on
866 * VERR_BUFFER_OVERFLOW) of this structure and it's strings. */
867 uint32_t cbNeeded;
868 /** Pointer to the filename suffix (the dot), if any. Points to the NUL
869 * character of the last component if none or if RTPATH_PROP_DIR_SLASH is
870 * present. */
871 const char *pszSuffix;
872 /** Array of component strings (variable size). */
873 RT_FLEXIBLE_ARRAY_EXTENSION
874 char *apszComps[RT_FLEXIBLE_ARRAY];
875} RTPATHSPLIT;
876/** Pointer to a split path buffer. */
877typedef RTPATHSPLIT *PRTPATHSPLIT;
878/** Pointer to a const split path buffer. */
879typedef RTPATHSPLIT const *PCRTPATHSPLIT;
880
881/**
882 * Splits the path into individual component strings, carved from user supplied
883 * the given buffer block.
884 *
885 * @returns IPRT status code.
886 * @retval VERR_INVALID_POINTER if pParsed or pszPath is an invalid pointer.
887 * @retval VERR_INVALID_PARAMETER if cbOutput is less than the RTPATHSPLIT
888 * strucuture. No output. (asserted)
889 * @retval VERR_BUFFER_OVERFLOW there are more components in the path than
890 * there is space in aComps. The required amount of space can be
891 * determined from the pParsed->cComps:
892 * @code
893 * RT_OFFSETOF(RTPATHPARSED, aComps[pParsed->cComps])
894 * @endcode
895 * @retval VERR_PATH_ZERO_LENGTH if the path is empty.
896 * @retval VERR_FILENAME_TOO_LONG if the filename is too long (close to 64 KB).
897 *
898 * @param pszPath The path to parse.
899 * @param pSplit Where to store the details of the parsed path.
900 * @param cbSplit The size of the buffer pointed to by @a pSplit
901 * (variable sized array at the end). Must be at
902 * least the size of RTPATHSPLIT.
903 * @param fFlags Combination of RTPATH_STR_F_XXX flags.
904 * Most users will pass 0.
905 *
906 * @sa RTPathSplitA, RTPathParse.
907 */
908RTDECL(int) RTPathSplit(const char *pszPath, PRTPATHSPLIT pSplit, size_t cbSplit, uint32_t fFlags);
909
910/**
911 * Splits the path into individual component strings, allocating the buffer on
912 * the default thread heap.
913 *
914 * @returns IPRT status code.
915 * @retval VERR_INVALID_POINTER if pParsed or pszPath is an invalid pointer.
916 * @retval VERR_PATH_ZERO_LENGTH if the path is empty.
917 *
918 * @param pszPath The path to parse.
919 * @param ppSplit Where to return the pointer to the output on
920 * success. This must be freed by calling
921 * RTPathSplitFree().
922 * @param fFlags Combination of RTPATH_STR_F_XXX flags.
923 * Most users will pass 0.
924 * @sa RTPathSplitFree, RTPathSplit, RTPathParse.
925 */
926#define RTPathSplitA(pszPath, ppSplit, fFlags) RTPathSplitATag(pszPath, ppSplit, fFlags, RTPATH_TAG)
927
928/**
929 * Splits the path into individual component strings, allocating the buffer on
930 * the default thread heap.
931 *
932 * @returns IPRT status code.
933 * @retval VERR_INVALID_POINTER if pParsed or pszPath is an invalid pointer.
934 * @retval VERR_PATH_ZERO_LENGTH if the path is empty.
935 *
936 * @param pszPath The path to parse.
937 * @param ppSplit Where to return the pointer to the output on
938 * success. This must be freed by calling
939 * RTPathSplitFree().
940 * @param fFlags Combination of RTPATH_STR_F_XXX flags.
941 * Most users will pass 0.
942 * @param pszTag Allocation tag used for statistics and such.
943 * @sa RTPathSplitFree, RTPathSplit, RTPathParse.
944 */
945RTDECL(int) RTPathSplitATag(const char *pszPath, PRTPATHSPLIT *ppSplit, uint32_t fFlags, const char *pszTag);
946
947/**
948 * Frees buffer returned by RTPathSplitA.
949 *
950 * @param pSplit What RTPathSplitA returned.
951 * @sa RTPathSplitA
952 */
953RTDECL(void) RTPathSplitFree(PRTPATHSPLIT pSplit);
954
955/**
956 * Reassembles a path parsed by RTPathSplit.
957 *
958 * This will be more useful as more APIs manipulating the RTPATHSPLIT output are
959 * added.
960 *
961 * @returns IPRT status code.
962 * @retval VERR_BUFFER_OVERFLOW if @a cbDstPath is less than or equal to
963 * RTPATHSPLIT::cchPath.
964 *
965 * @param pSplit A split path (see RTPathSplit, RTPathSplitA).
966 * @param fFlags Combination of RTPATH_STR_F_STYLE_XXX.
967 * Most users will pass 0.
968 * @param pszDstPath Pointer to the buffer where the path is to be
969 * reassembled.
970 * @param cbDstPath The size of the output buffer.
971 */
972RTDECL(int) RTPathSplitReassemble(PRTPATHSPLIT pSplit, uint32_t fFlags, char *pszDstPath, size_t cbDstPath);
973
974/**
975 * Checks if the two paths leads to the file system object.
976 *
977 * If the objects exist, we'll query attributes for them. If that's not
978 * conclusive (some OSes) or one of them doesn't exist, we'll use a combination
979 * of RTPathAbs and RTPathCompare to determine the result.
980 *
981 * @returns true, false, or VERR_FILENAME_TOO_LONG.
982 * @param pszPath1 The first path.
983 * @param pszPath2 The seoncd path.
984 */
985RTDECL(int) RTPathIsSame(const char *pszPath1, const char *pszPath2);
986
987
988/**
989 * Compares two paths.
990 *
991 * The comparison takes platform-dependent details into account,
992 * such as:
993 * <ul>
994 * <li>On DOS-like platforms, both separator chars (|\| and |/|) are considered
995 * to be equal.
996 * <li>On platforms with case-insensitive file systems, mismatching characters
997 * are uppercased and compared again.
998 * </ul>
999 *
1000 * @returns @< 0 if the first path less than the second path.
1001 * @returns 0 if the first path identical to the second path.
1002 * @returns @> 0 if the first path greater than the second path.
1003 *
1004 * @param pszPath1 Path to compare (must be an absolute path).
1005 * @param pszPath2 Path to compare (must be an absolute path).
1006 *
1007 * @remarks File system details are currently ignored. This means that you won't
1008 * get case-insensitive compares on unix systems when a path goes into a
1009 * case-insensitive filesystem like FAT, HPFS, HFS, NTFS, JFS, or
1010 * similar. For NT, OS/2 and similar you'll won't get case-sensitive
1011 * compares on a case-sensitive file system.
1012 */
1013RTDECL(int) RTPathCompare(const char *pszPath1, const char *pszPath2);
1014
1015/**
1016 * Checks if a path starts with the given parent path.
1017 *
1018 * This means that either the path and the parent path matches completely, or
1019 * that the path is to some file or directory residing in the tree given by the
1020 * parent directory.
1021 *
1022 * The path comparison takes platform-dependent details into account,
1023 * see RTPathCompare() for details.
1024 *
1025 * @returns |true| when \a pszPath starts with \a pszParentPath (or when they
1026 * are identical), or |false| otherwise.
1027 *
1028 * @param pszPath Path to check, must be an absolute path.
1029 * @param pszParentPath Parent path, must be an absolute path.
1030 * No trailing directory slash!
1031 *
1032 * @remarks This API doesn't currently handle root directory compares in a
1033 * manner consistent with the other APIs. RTPathStartsWith(pszSomePath,
1034 * "/") will not work if pszSomePath isn't "/".
1035 */
1036RTDECL(bool) RTPathStartsWith(const char *pszPath, const char *pszParentPath);
1037
1038/**
1039 * Appends one partial path to another.
1040 *
1041 * The main purpose of this function is to deal correctly with the slashes when
1042 * concatenating the two partial paths.
1043 *
1044 * @retval VINF_SUCCESS on success.
1045 * @retval VERR_BUFFER_OVERFLOW if the result is too big to fit within
1046 * cbPathDst bytes. No changes has been made.
1047 * @retval VERR_INVALID_PARAMETER if the string pointed to by pszPath is longer
1048 * than cbPathDst-1 bytes (failed to find terminator). Asserted.
1049 *
1050 * @param pszPath The path to append pszAppend to. This serves as both
1051 * input and output. This can be empty, in which case
1052 * pszAppend is just copied over.
1053 * @param cbPathDst The size of the buffer pszPath points to, terminator
1054 * included. This should NOT be strlen(pszPath).
1055 * @param pszAppend The partial path to append to pszPath. This can be
1056 * NULL, in which case nothing is done.
1057 *
1058 * @remarks See the RTPathAppendEx remarks.
1059 */
1060RTDECL(int) RTPathAppend(char *pszPath, size_t cbPathDst, const char *pszAppend);
1061
1062/**
1063 * Appends one partial path to another.
1064 *
1065 * The main purpose of this function is to deal correctly with the slashes when
1066 * concatenating the two partial paths.
1067 *
1068 * @retval VINF_SUCCESS on success.
1069 * @retval VERR_BUFFER_OVERFLOW if the result is too big to fit within
1070 * cbPathDst bytes. No changes has been made.
1071 * @retval VERR_INVALID_PARAMETER if the string pointed to by pszPath is longer
1072 * than cbPathDst-1 bytes (failed to find terminator). Asserted.
1073 *
1074 * @param pszPath The path to append pszAppend to. This serves as both
1075 * input and output. This can be empty, in which case
1076 * pszAppend is just copied over.
1077 * @param cbPathDst The size of the buffer pszPath points to, terminator
1078 * included. This should NOT be strlen(pszPath).
1079 * @param pszAppend The partial path to append to pszPath. This can be
1080 * NULL, in which case nothing is done.
1081 * @param cchAppendMax The maximum number or characters to take from @a
1082 * pszAppend. RTSTR_MAX is fine.
1083 *
1084 * @remarks On OS/2, Window and similar systems, concatenating a drive letter
1085 * specifier with a slash prefixed path will result in an absolute
1086 * path. Meaning, RTPathAppend(strcpy(szBuf, "C:"), sizeof(szBuf),
1087 * "/bar") will result in "C:/bar". (This follows directly from the
1088 * behavior when pszPath is empty.)
1089 *
1090 * On the other hand, when joining a drive letter specifier with a
1091 * partial path that does not start with a slash, the result is not an
1092 * absolute path. Meaning, RTPathAppend(strcpy(szBuf, "C:"),
1093 * sizeof(szBuf), "bar") will result in "C:bar".
1094 */
1095RTDECL(int) RTPathAppendEx(char *pszPath, size_t cbPathDst, const char *pszAppend, size_t cchAppendMax);
1096
1097/**
1098 * Like RTPathAppend, but with the base path as a separate argument instead of
1099 * in the path buffer.
1100 *
1101 * @retval VINF_SUCCESS on success.
1102 * @retval VERR_BUFFER_OVERFLOW if the result is too big to fit within
1103 * cbPathDst bytes.
1104 * @retval VERR_INVALID_PARAMETER if the string pointed to by pszPath is longer
1105 * than cbPathDst-1 bytes (failed to find terminator). Asserted.
1106 *
1107 * @param pszPathDst Where to store the resulting path.
1108 * @param cbPathDst The size of the buffer pszPathDst points to,
1109 * terminator included.
1110 * @param pszPathSrc The base path to copy into @a pszPathDst before
1111 * appending @a pszAppend.
1112 * @param pszAppend The partial path to append to pszPathSrc. This can
1113 * be NULL, in which case nothing is done.
1114 *
1115 */
1116RTDECL(int) RTPathJoin(char *pszPathDst, size_t cbPathDst, const char *pszPathSrc,
1117 const char *pszAppend);
1118
1119/**
1120 * Same as RTPathJoin, except that the output buffer is allocated.
1121 *
1122 * @returns Buffer containing the joined up path, call RTStrFree to free. NULL
1123 * on allocation failure.
1124 * @param pszPathSrc The base path to copy into @a pszPathDst before
1125 * appending @a pszAppend.
1126 * @param pszAppend The partial path to append to pszPathSrc. This can
1127 * be NULL, in which case nothing is done.
1128 *
1129 */
1130RTDECL(char *) RTPathJoinA(const char *pszPathSrc, const char *pszAppend);
1131
1132/**
1133 * Extended version of RTPathJoin, both inputs can be specified as substrings.
1134 *
1135 * @retval VINF_SUCCESS on success.
1136 * @retval VERR_BUFFER_OVERFLOW if the result is too big to fit within
1137 * cbPathDst bytes.
1138 * @retval VERR_INVALID_PARAMETER if the string pointed to by pszPath is longer
1139 * than cbPathDst-1 bytes (failed to find terminator). Asserted.
1140 *
1141 * @param pszPathDst Where to store the resulting path.
1142 * @param cbPathDst The size of the buffer pszPathDst points to,
1143 * terminator included.
1144 * @param pszPathSrc The base path to copy into @a pszPathDst before
1145 * appending @a pszAppend.
1146 * @param cchPathSrcMax The maximum number of bytes to copy from @a
1147 * pszPathSrc. RTSTR_MAX is find.
1148 * @param pszAppend The partial path to append to pszPathSrc. This can
1149 * be NULL, in which case nothing is done.
1150 * @param cchAppendMax The maximum number of bytes to copy from @a
1151 * pszAppend. RTSTR_MAX is find.
1152 *
1153 */
1154RTDECL(int) RTPathJoinEx(char *pszPathDst, size_t cbPathDst,
1155 const char *pszPathSrc, size_t cchPathSrcMax,
1156 const char *pszAppend, size_t cchAppendMax);
1157
1158/**
1159 * Callback for RTPathTraverseList that's called for each element.
1160 *
1161 * @returns IPRT style status code. Return VERR_TRY_AGAIN to continue, any other
1162 * value will abort the traversing and be returned to the caller.
1163 *
1164 * @param pchPath Pointer to the start of the current path. This is
1165 * not null terminated.
1166 * @param cchPath The length of the path.
1167 * @param pvUser1 The first user parameter.
1168 * @param pvUser2 The second user parameter.
1169 */
1170typedef DECLCALLBACKTYPE(int, FNRTPATHTRAVERSER,(char const *pchPath, size_t cchPath, void *pvUser1, void *pvUser2));
1171/** Pointer to a FNRTPATHTRAVERSER. */
1172typedef FNRTPATHTRAVERSER *PFNRTPATHTRAVERSER;
1173
1174/**
1175 * Traverses a string that can contain multiple paths separated by a special
1176 * character.
1177 *
1178 * @returns IPRT style status code from the callback or VERR_END_OF_STRING if
1179 * the callback returned VERR_TRY_AGAIN for all paths in the string.
1180 *
1181 * @param pszPathList The string to traverse.
1182 * @param chSep The separator character. Using the null terminator
1183 * is fine, but the result will simply be that there
1184 * will only be one callback for the entire string
1185 * (save any leading white space).
1186 * @param pfnCallback The callback.
1187 * @param pvUser1 First user argument for the callback.
1188 * @param pvUser2 Second user argument for the callback.
1189 */
1190RTDECL(int) RTPathTraverseList(const char *pszPathList, char chSep, PFNRTPATHTRAVERSER pfnCallback, void *pvUser1, void *pvUser2);
1191
1192
1193/**
1194 * Calculate a relative path between the two given paths.
1195 *
1196 * @returns IPRT status code.
1197 * @retval VINF_SUCCESS on success.
1198 * @retval VERR_BUFFER_OVERFLOW if the result is too big to fit within
1199 * cbPathDst bytes.
1200 * @retval VERR_NOT_SUPPORTED if both paths start with different volume specifiers.
1201 * @param pszPathDst Where to store the resulting path.
1202 * @param cbPathDst The size of the buffer pszPathDst points to,
1203 * terminator included.
1204 * @param pszPathFrom The path to start from creating the relative path.
1205 * @param fFromFile Whether @a pszPathFrom is a file and we should work
1206 * relative to it's parent directory (@c true), or if
1207 * we should assume @a pszPathFrom is a directory and
1208 * work relative to it.
1209 * @param pszPathTo The path to reach with the created relative path.
1210 */
1211RTDECL(int) RTPathCalcRelative(char *pszPathDst, size_t cbPathDst, const char *pszPathFrom, bool fFromFile, const char *pszPathTo);
1212
1213#ifdef IN_RING3
1214
1215/**
1216 * Gets the path to the directory containing the executable.
1217 *
1218 * @returns iprt status code.
1219 * @param pszPath Buffer where to store the path.
1220 * @param cchPath Buffer size in bytes.
1221 */
1222RTDECL(int) RTPathExecDir(char *pszPath, size_t cchPath);
1223
1224/**
1225 * Gets the user home directory.
1226 *
1227 * @returns iprt status code.
1228 * @param pszPath Buffer where to store the path.
1229 * @param cchPath Buffer size in bytes.
1230 */
1231RTDECL(int) RTPathUserHome(char *pszPath, size_t cchPath);
1232
1233/**
1234 * Gets the user documents directory.
1235 *
1236 * The returned path isn't guaranteed to exist.
1237 *
1238 * @returns iprt status code.
1239 * @param pszPath Buffer where to store the path.
1240 * @param cchPath Buffer size in bytes.
1241 */
1242RTDECL(int) RTPathUserDocuments(char *pszPath, size_t cchPath);
1243
1244/**
1245 * Gets the directory of shared libraries.
1246 *
1247 * This is not the same as RTPathAppPrivateArch() as Linux depends all shared
1248 * libraries in a common global directory where ld.so can find them.
1249 *
1250 * Linux: /usr/lib
1251 * Solaris: /opt/@<application@>/@<arch>@ or something
1252 * Windows: @<program files directory@>/@<application@>
1253 * Old path: same as RTPathExecDir()
1254 *
1255 * @returns iprt status code.
1256 * @param pszPath Buffer where to store the path.
1257 * @param cchPath Buffer size in bytes.
1258 */
1259RTDECL(int) RTPathSharedLibs(char *pszPath, size_t cchPath);
1260
1261/**
1262 * Gets the directory for architecture-independent application data, for
1263 * example NLS files, module sources, ...
1264 *
1265 * Linux: /usr/shared/@<application@>
1266 * Solaris: /opt/@<application@>
1267 * Windows: @<program files directory@>/@<application@>
1268 * Old path: same as RTPathExecDir()
1269 *
1270 * @returns iprt status code.
1271 * @param pszPath Buffer where to store the path.
1272 * @param cchPath Buffer size in bytes.
1273 */
1274RTDECL(int) RTPathAppPrivateNoArch(char *pszPath, size_t cchPath);
1275
1276/**
1277 * Gets the directory for architecture-dependent application data, for
1278 * example modules which can be loaded at runtime.
1279 *
1280 * Linux: /usr/lib/@<application@>
1281 * Solaris: /opt/@<application@>/@<arch>@ or something
1282 * Windows: @<program files directory@>/@<application@>
1283 * Old path: same as RTPathExecDir()
1284 *
1285 * @returns iprt status code.
1286 * @param pszPath Buffer where to store the path.
1287 * @param cchPath Buffer size in bytes.
1288 */
1289RTDECL(int) RTPathAppPrivateArch(char *pszPath, size_t cchPath);
1290
1291/**
1292 * Gets the toplevel directory for architecture-dependent application data.
1293 *
1294 * This differs from RTPathAppPrivateArch on Solaris only where it will work
1295 * around the /opt/@<application@>/amd64 and /opt/@<application@>/i386 multi
1296 * architecture installation style.
1297 *
1298 * Linux: /usr/lib/@<application@>
1299 * Solaris: /opt/@<application@>
1300 * Windows: @<program files directory@>/@<application@>
1301 * Old path: same as RTPathExecDir()
1302 *
1303 * @returns iprt status code.
1304 * @param pszPath Buffer where to store the path.
1305 * @param cchPath Buffer size in bytes.
1306 */
1307RTDECL(int) RTPathAppPrivateArchTop(char *pszPath, size_t cchPath);
1308
1309/**
1310 * Gets the directory for documentation.
1311 *
1312 * Linux: /usr/share/doc/@<application@>
1313 * Solaris: /opt/@<application@>
1314 * Windows: @<program files directory@>/@<application@>
1315 * Old path: same as RTPathExecDir()
1316 *
1317 * @returns iprt status code.
1318 * @param pszPath Buffer where to store the path.
1319 * @param cchPath Buffer size in bytes.
1320 */
1321RTDECL(int) RTPathAppDocs(char *pszPath, size_t cchPath);
1322
1323/**
1324 * Gets the temporary directory path.
1325 *
1326 * @returns iprt status code.
1327 * @param pszPath Buffer where to store the path.
1328 * @param cchPath Buffer size in bytes.
1329 */
1330RTDECL(int) RTPathTemp(char *pszPath, size_t cchPath);
1331
1332
1333/**
1334 * RTPathGlobl result entry.
1335 */
1336typedef struct RTPATHGLOBENTRY
1337{
1338 /** List entry. */
1339 struct RTPATHGLOBENTRY *pNext;
1340 /** RTDIRENTRYTYPE value. */
1341 uint8_t uType;
1342 /** Unused explicit padding. */
1343 uint8_t bUnused;
1344 /** The length of the path. */
1345 uint16_t cchPath;
1346 /** The path to the file (variable length). */
1347 char szPath[1];
1348} RTPATHGLOBENTRY;
1349/** Pointer to a GLOB result entry. */
1350typedef RTPATHGLOBENTRY *PRTPATHGLOBENTRY;
1351/** Pointer to a const GLOB result entry. */
1352typedef RTPATHGLOBENTRY const *PCRTPATHGLOBENTRY;
1353/** Pointer to a GLOB result entry pointer. */
1354typedef PCRTPATHGLOBENTRY *PPCRTPATHGLOBENTRY;
1355
1356/**
1357 * Performs wildcard expansion on a path pattern.
1358 *
1359 * @returns IPRT status code.
1360 *
1361 * @param pszPattern The pattern to expand.
1362 * @param fFlags RTPATHGLOB_F_XXX.
1363 * @param ppHead Where to return the head of the result list. This
1364 * is always set to NULL on failure.
1365 * @param pcResults Where to return the number of the result. Optional.
1366 */
1367RTDECL(int) RTPathGlob(const char *pszPattern, uint32_t fFlags, PPCRTPATHGLOBENTRY ppHead, uint32_t *pcResults);
1368
1369/** @name RTPATHGLOB_F_XXX - RTPathGlob flags
1370 * @{ */
1371/** Case insensitive. */
1372#define RTPATHGLOB_F_IGNORE_CASE RT_BIT_32(0)
1373/** Do not expand \${EnvOrSpecialVariable} in the pattern. */
1374#define RTPATHGLOB_F_NO_VARIABLES RT_BIT_32(1)
1375/** Do not interpret a leading tilde as a home directory reference. */
1376#define RTPATHGLOB_F_NO_TILDE RT_BIT_32(2)
1377/** Only return the first match. */
1378#define RTPATHGLOB_F_FIRST_ONLY RT_BIT_32(3)
1379/** Only match directories (implied if pattern ends with slash). */
1380#define RTPATHGLOB_F_ONLY_DIRS RT_BIT_32(4)
1381/** Do not match directories. (Can't be used with RTPATHGLOB_F_ONLY_DIRS or
1382 * patterns containing a trailing slash.) */
1383#define RTPATHGLOB_F_NO_DIRS RT_BIT_32(5)
1384/** Disables the '**' wildcard pattern for matching zero or more subdirs. */
1385#define RTPATHGLOB_F_NO_STARSTAR RT_BIT_32(6)
1386/** Mask of valid flags. */
1387#define RTPATHGLOB_F_MASK UINT32_C(0x0000007f)
1388/** @} */
1389
1390/**
1391 * Frees the results produced by RTPathGlob.
1392 *
1393 * @param pHead What RTPathGlob returned. NULL ignored.
1394 */
1395RTDECL(void) RTPathGlobFree(PCRTPATHGLOBENTRY pHead);
1396
1397
1398/**
1399 * Query information about a file system object.
1400 *
1401 * This API will resolve NOT symbolic links in the last component (just like
1402 * unix lstat()).
1403 *
1404 * @returns IPRT status code.
1405 * @retval VINF_SUCCESS if the object exists, information returned.
1406 * @retval VERR_PATH_NOT_FOUND if any but the last component in the specified
1407 * path was not found or was not a directory.
1408 * @retval VERR_FILE_NOT_FOUND if the object does not exist (but path to the
1409 * parent directory exists).
1410 *
1411 * @param pszPath Path to the file system object.
1412 * @param pObjInfo Object information structure to be filled on successful
1413 * return.
1414 * @param enmAdditionalAttribs
1415 * Which set of additional attributes to request.
1416 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
1417 */
1418RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs);
1419
1420/**
1421 * Query information about a file system object.
1422 *
1423 * @returns IPRT status code.
1424 * @retval VINF_SUCCESS if the object exists, information returned.
1425 * @retval VERR_PATH_NOT_FOUND if any but the last component in the specified
1426 * path was not found or was not a directory.
1427 * @retval VERR_FILE_NOT_FOUND if the object does not exist (but path to the
1428 * parent directory exists).
1429 *
1430 * @param pszPath Path to the file system object.
1431 * @param pObjInfo Object information structure to be filled on successful return.
1432 * @param enmAdditionalAttribs
1433 * Which set of additional attributes to request.
1434 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
1435 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
1436 */
1437RTR3DECL(int) RTPathQueryInfoEx(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags);
1438
1439/**
1440 * Changes the mode flags of a file system object.
1441 *
1442 * The API requires at least one of the mode flag sets (Unix/Dos) to
1443 * be set. The type is ignored.
1444 *
1445 * This API will resolve symbolic links in the last component since
1446 * mode isn't important for symbolic links.
1447 *
1448 * @returns iprt status code.
1449 * @param pszPath Path to the file system object.
1450 * @param fMode The new file mode, see @ref grp_rt_fs for details.
1451 */
1452RTR3DECL(int) RTPathSetMode(const char *pszPath, RTFMODE fMode);
1453
1454/**
1455 * Gets the mode flags of a file system object.
1456 *
1457 * @returns iprt status code.
1458 * @param pszPath Path to the file system object.
1459 * @param pfMode Where to store the file mode, see @ref grp_rt_fs for details.
1460 *
1461 * @remark This is wrapper around RTPathQueryInfoEx(RTPATH_F_FOLLOW_LINK) and
1462 * exists to complement RTPathSetMode().
1463 */
1464RTR3DECL(int) RTPathGetMode(const char *pszPath, PRTFMODE pfMode);
1465
1466/**
1467 * Changes one or more of the timestamps associated of file system object.
1468 *
1469 * This API will not resolve symbolic links in the last component (just
1470 * like unix lutimes()).
1471 *
1472 * @returns iprt status code.
1473 * @param pszPath Path to the file system object.
1474 * @param pAccessTime Pointer to the new access time.
1475 * @param pModificationTime Pointer to the new modification time.
1476 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
1477 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
1478 *
1479 * @remark The file system might not implement all these time attributes,
1480 * the API will ignore the ones which aren't supported.
1481 *
1482 * @remark The file system might not implement the time resolution
1483 * employed by this interface, the time will be chopped to fit.
1484 *
1485 * @remark The file system may update the change time even if it's
1486 * not specified.
1487 *
1488 * @remark POSIX can only set Access & Modification and will always set both.
1489 */
1490RTR3DECL(int) RTPathSetTimes(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
1491 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime);
1492
1493/**
1494 * Changes one or more of the timestamps associated of file system object.
1495 *
1496 * @returns iprt status code.
1497 * @param pszPath Path to the file system object.
1498 * @param pAccessTime Pointer to the new access time.
1499 * @param pModificationTime Pointer to the new modification time.
1500 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
1501 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
1502 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
1503 *
1504 * @remark The file system might not implement all these time attributes,
1505 * the API will ignore the ones which aren't supported.
1506 *
1507 * @remark The file system might not implement the time resolution
1508 * employed by this interface, the time will be chopped to fit.
1509 *
1510 * @remark The file system may update the change time even if it's
1511 * not specified.
1512 *
1513 * @remark POSIX can only set Access & Modification and will always set both.
1514 */
1515RTR3DECL(int) RTPathSetTimesEx(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
1516 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime, uint32_t fFlags);
1517
1518/**
1519 * Gets one or more of the timestamps associated of file system object.
1520 *
1521 * @returns iprt status code.
1522 * @param pszPath Path to the file system object.
1523 * @param pAccessTime Where to store the access time. NULL is ok.
1524 * @param pModificationTime Where to store the modification time. NULL is ok.
1525 * @param pChangeTime Where to store the change time. NULL is ok.
1526 * @param pBirthTime Where to store the creation time. NULL is ok.
1527 *
1528 * @remark This is wrapper around RTPathQueryInfo() and exists to complement
1529 * RTPathSetTimes(). If the last component is a symbolic link, it will
1530 * not be resolved.
1531 */
1532RTR3DECL(int) RTPathGetTimes(const char *pszPath, PRTTIMESPEC pAccessTime, PRTTIMESPEC pModificationTime,
1533 PRTTIMESPEC pChangeTime, PRTTIMESPEC pBirthTime);
1534
1535/**
1536 * Changes the owner and/or group of a file system object.
1537 *
1538 * This API will not resolve symbolic links in the last component (just
1539 * like unix lchown()).
1540 *
1541 * @returns iprt status code.
1542 * @param pszPath Path to the file system object.
1543 * @param uid The new file owner user id. Pass NIL_RTUID to leave
1544 * this unchanged.
1545 * @param gid The new group id. Pass NIL_RTGUID to leave this
1546 * unchanged.
1547 */
1548RTR3DECL(int) RTPathSetOwner(const char *pszPath, uint32_t uid, uint32_t gid);
1549
1550/**
1551 * Changes the owner and/or group of a file system object.
1552 *
1553 * @returns iprt status code.
1554 * @param pszPath Path to the file system object.
1555 * @param uid The new file owner user id. Pass NIL_RTUID to leave
1556 * this unchanged.
1557 * @param gid The new group id. Pass NIL_RTGID to leave this
1558 * unchanged.
1559 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
1560 */
1561RTR3DECL(int) RTPathSetOwnerEx(const char *pszPath, uint32_t uid, uint32_t gid, uint32_t fFlags);
1562
1563/**
1564 * Gets the owner and/or group of a file system object.
1565 *
1566 * @returns iprt status code.
1567 * @param pszPath Path to the file system object.
1568 * @param pUid Where to store the owner user id. NULL is ok.
1569 * @param pGid Where to store the group id. NULL is ok.
1570 *
1571 * @remark This is wrapper around RTPathQueryInfo() and exists to complement
1572 * RTPathGetOwner(). If the last component is a symbolic link, it will
1573 * not be resolved.
1574 */
1575RTR3DECL(int) RTPathGetOwner(const char *pszPath, uint32_t *pUid, uint32_t *pGid);
1576
1577
1578/** @name RTPathRename, RTDirRename & RTFileRename flags.
1579 * @{ */
1580/** Do not replace anything. */
1581#define RTPATHRENAME_FLAGS_NO_REPLACE UINT32_C(0)
1582/** This will replace attempt any target which isn't a directory. */
1583#define RTPATHRENAME_FLAGS_REPLACE RT_BIT(0)
1584/** Don't allow symbolic links as part of the path.
1585 * @remarks this flag is currently not implemented and will be ignored. */
1586#define RTPATHRENAME_FLAGS_NO_SYMLINKS RT_BIT(1)
1587/** @} */
1588
1589/**
1590 * Renames a path within a filesystem.
1591 *
1592 * This will rename symbolic links. If RTPATHRENAME_FLAGS_REPLACE is used and
1593 * pszDst is a symbolic link, it will be replaced and not its target.
1594 *
1595 * @returns IPRT status code.
1596 * @param pszSrc The source path.
1597 * @param pszDst The destination path.
1598 * @param fRename Rename flags, RTPATHRENAME_FLAGS_*.
1599 */
1600RTR3DECL(int) RTPathRename(const char *pszSrc, const char *pszDst, unsigned fRename);
1601
1602/** @name RTPathUnlink flags.
1603 * @{ */
1604/** Don't allow symbolic links as part of the path.
1605 * @remarks this flag is currently not implemented and will be ignored. */
1606#define RTPATHUNLINK_FLAGS_NO_SYMLINKS RT_BIT(0)
1607/** @} */
1608
1609/**
1610 * Removes the last component of the path.
1611 *
1612 * @returns IPRT status code.
1613 * @param pszPath The path.
1614 * @param fUnlink Unlink flags, RTPATHUNLINK_FLAGS_*.
1615 */
1616RTR3DECL(int) RTPathUnlink(const char *pszPath, uint32_t fUnlink);
1617
1618/**
1619 * A /bin/rm tool.
1620 *
1621 * @returns Program exit code.
1622 *
1623 * @param cArgs The number of arguments.
1624 * @param papszArgs The argument vector. (Note that this may be
1625 * reordered, so the memory must be writable.)
1626 */
1627RTDECL(RTEXITCODE) RTPathRmCmd(unsigned cArgs, char **papszArgs);
1628
1629# ifdef RT_OS_WINDOWS
1630
1631/**
1632 * Converts the given UTF-8 path into a native windows path.
1633 *
1634 * @returns IPRT status code.
1635 * @param ppwszPath Where to return the path. This will always be
1636 * set to NULL on failure. Use RTPathWinFree to
1637 * free it when done.
1638 * @param pszPath The UTF-8 path to convert.
1639 * @param fFlags MBZ, reserved for future hacks.
1640 * @sa RTPathWinFree, RTNtPathFromWinUtf8, RTNtPathRelativeFromUtf8.
1641 */
1642RTDECL(int) RTPathWinFromUtf8(PRTUTF16 *ppwszPath, const char *pszPath, uint32_t fFlags);
1643
1644/**
1645 * Frees a native windows path returned by RTPathWinFromUtf8
1646 *
1647 * @param pwszPath The path to free. NULL is ignored.
1648 */
1649RTDECL(void) RTPathWinFree(PRTUTF16 pwszPath);
1650
1651# endif /* RT_OS_WINDOWS */
1652
1653#endif /* IN_RING3 */
1654
1655/** @} */
1656
1657RT_C_DECLS_END
1658
1659#endif /* !IPRT_INCLUDED_path_h */
1660
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