VirtualBox

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

Last change on this file since 78048 was 78048, checked in by vboxsync, 6 years ago

IPRT: Working on new RTPathAbsEx implementation, temporarily called RTPathAbsExEx. This should fix most of the bugs in the current RTPathAbs/Ex code and do away with the fixed path buffer limitations. Also introduces a RTPATH_BIG_MAX, given that RTPATH_MAX is just a reasonable and not absolute MAX value. The new one is more or less absolute and must never be used for stack buffers. bugref:9172

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