VirtualBox

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

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

IPRT: Added RTPathFilenameUtf16[Ex].

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