VirtualBox

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

Last change on this file since 96550 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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