1 | /** @file
|
---|
2 | * IPRT - Path Manipulation.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 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 |
|
---|
37 | RT_C_DECLS_BEGIN
|
---|
38 |
|
---|
39 | /** @defgroup grp_rt_path RTPath - Path Manipulation
|
---|
40 | * @ingroup grp_rt
|
---|
41 | * @{
|
---|
42 | */
|
---|
43 |
|
---|
44 |
|
---|
45 | /** @def RTPATH_SLASH
|
---|
46 | * The preferred slash character.
|
---|
47 | *
|
---|
48 | * @remark IPRT will always accept unix slashes. So, normally you would
|
---|
49 | * never have to use this define.
|
---|
50 | */
|
---|
51 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
52 | # define RTPATH_SLASH '\\'
|
---|
53 | #else
|
---|
54 | # define RTPATH_SLASH '/'
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | /** @deprecated Use '/'! */
|
---|
58 | #define RTPATH_DELIMITER RTPATH_SLASH
|
---|
59 |
|
---|
60 |
|
---|
61 | /** @def RTPATH_SLASH_STR
|
---|
62 | * The preferred slash character as a string, handy for concatenations
|
---|
63 | * with other strings.
|
---|
64 | *
|
---|
65 | * @remark IPRT will always accept unix slashes. So, normally you would
|
---|
66 | * never have to use this define.
|
---|
67 | */
|
---|
68 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
69 | # define RTPATH_SLASH_STR "\\"
|
---|
70 | #else
|
---|
71 | # define RTPATH_SLASH_STR "/"
|
---|
72 | #endif
|
---|
73 |
|
---|
74 |
|
---|
75 | /** @def RTPATH_IS_SLASH
|
---|
76 | * Checks if a character is a slash.
|
---|
77 | *
|
---|
78 | * @returns true if it's a slash and false if not.
|
---|
79 | * @returns @param ch Char to check.
|
---|
80 | */
|
---|
81 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
82 | # define RTPATH_IS_SLASH(ch) ( (ch) == '\\' || (ch) == '/' )
|
---|
83 | #else
|
---|
84 | # define RTPATH_IS_SLASH(ch) ( (ch) == '/' )
|
---|
85 | #endif
|
---|
86 |
|
---|
87 |
|
---|
88 | /** @def RTPATH_IS_VOLSEP
|
---|
89 | * Checks if a character marks the end of the volume specification.
|
---|
90 | *
|
---|
91 | * @remark This is sufficient for the drive letter concept on PC.
|
---|
92 | * However it might be insufficient on other platforms
|
---|
93 | * and even on PC a UNC volume spec won't be detected this way.
|
---|
94 | * Use the RTPath@<too be created@>() instead.
|
---|
95 | *
|
---|
96 | * @returns true if it is and false if it isn't.
|
---|
97 | * @returns @param ch Char to check.
|
---|
98 | */
|
---|
99 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
100 | # define RTPATH_IS_VOLSEP(ch) ( (ch) == ':' )
|
---|
101 | #else
|
---|
102 | # define RTPATH_IS_VOLSEP(ch) (false)
|
---|
103 | #endif
|
---|
104 |
|
---|
105 |
|
---|
106 | /** @def RTPATH_IS_SEP
|
---|
107 | * Checks if a character is path component separator
|
---|
108 | *
|
---|
109 | * @returns true if it is and false if it isn't.
|
---|
110 | * @returns @param ch Char to check.
|
---|
111 | * @
|
---|
112 | */
|
---|
113 | #define RTPATH_IS_SEP(ch) ( RTPATH_IS_SLASH(ch) || RTPATH_IS_VOLSEP(ch) )
|
---|
114 |
|
---|
115 |
|
---|
116 | /** @name Generic RTPath flags
|
---|
117 | * @{ */
|
---|
118 | /** Last component: Work on the link. */
|
---|
119 | #define RTPATH_F_ON_LINK RT_BIT_32(0)
|
---|
120 | /** Last component: Follow if link. */
|
---|
121 | #define RTPATH_F_FOLLOW_LINK RT_BIT_32(1)
|
---|
122 | /** @} */
|
---|
123 |
|
---|
124 |
|
---|
125 | /** Validates a flags parameter containing RTPATH_F_*.
|
---|
126 | * @remarks The parameters will be referenced multiple times. */
|
---|
127 | #define RTPATH_F_IS_VALID(fFlags, fIgnore) \
|
---|
128 | ( ((fFlags) & ~(uint32_t)(fIgnore)) == RTPATH_F_ON_LINK \
|
---|
129 | || ((fFlags) & ~(uint32_t)(fIgnore)) == RTPATH_F_FOLLOW_LINK )
|
---|
130 |
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Checks if the path exists.
|
---|
134 | *
|
---|
135 | * Symbolic links will all be attempted resolved and broken links means false.
|
---|
136 | *
|
---|
137 | * @returns true if it exists and false if it doesn't.
|
---|
138 | * @param pszPath The path to check.
|
---|
139 | */
|
---|
140 | RTDECL(bool) RTPathExists(const char *pszPath);
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Checks if the path exists.
|
---|
144 | *
|
---|
145 | * @returns true if it exists and false if it doesn't.
|
---|
146 | * @param pszPath The path to check.
|
---|
147 | * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
|
---|
148 | */
|
---|
149 | RTDECL(bool) RTPathExistsEx(const char *pszPath, uint32_t fFlags);
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Sets the current working directory of the process.
|
---|
153 | *
|
---|
154 | * @returns IPRT status code.
|
---|
155 | * @param pszPath The path to the new working directory.
|
---|
156 | */
|
---|
157 | RTDECL(int) RTPathSetCurrent(const char *pszPath);
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Gets the current working directory of the process.
|
---|
161 | *
|
---|
162 | * @returns IPRT status code.
|
---|
163 | * @param pszPath Where to store the path.
|
---|
164 | * @param cchPath The size of the buffer pszPath points to.
|
---|
165 | */
|
---|
166 | RTDECL(int) RTPathGetCurrent(char *pszPath, size_t cchPath);
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Get the real path (no symlinks, no . or .. components), must exist.
|
---|
170 | *
|
---|
171 | * @returns iprt status code.
|
---|
172 | * @param pszPath The path to resolve.
|
---|
173 | * @param pszRealPath Where to store the real path.
|
---|
174 | * @param cchRealPath Size of the buffer.
|
---|
175 | */
|
---|
176 | RTDECL(int) RTPathReal(const char *pszPath, char *pszRealPath, size_t cchRealPath);
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Same as RTPathReal only the result is RTStrDup()'ed.
|
---|
180 | *
|
---|
181 | * @returns Pointer to real path. Use RTStrFree() to free this string.
|
---|
182 | * @returns NULL if RTPathReal() or RTStrDup() fails.
|
---|
183 | * @param pszPath The path to resolve.
|
---|
184 | */
|
---|
185 | RTDECL(char *) RTPathRealDup(const char *pszPath);
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Get the absolute path (starts from root, no . or .. components), doesn't have
|
---|
189 | * to exist. Note that this method is designed to never perform actual file
|
---|
190 | * system access, therefore symlinks are not resolved.
|
---|
191 | *
|
---|
192 | * @returns iprt status code.
|
---|
193 | * @param pszPath The path to resolve.
|
---|
194 | * @param pszAbsPath Where to store the absolute path.
|
---|
195 | * @param cchAbsPath Size of the buffer.
|
---|
196 | */
|
---|
197 | RTDECL(int) RTPathAbs(const char *pszPath, char *pszAbsPath, size_t cchAbsPath);
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Same as RTPathAbs only the result is RTStrDup()'ed.
|
---|
201 | *
|
---|
202 | * @returns Pointer to the absolute path. Use RTStrFree() to free this string.
|
---|
203 | * @returns NULL if RTPathAbs() or RTStrDup() fails.
|
---|
204 | * @param pszPath The path to resolve.
|
---|
205 | */
|
---|
206 | RTDECL(char *) RTPathAbsDup(const char *pszPath);
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Get the absolute path (no symlinks, no . or .. components), assuming the
|
---|
210 | * given base path as the current directory. The resulting path doesn't have
|
---|
211 | * to exist.
|
---|
212 | *
|
---|
213 | * @returns iprt status code.
|
---|
214 | * @param pszBase The base path to act like a current directory.
|
---|
215 | * When NULL, the actual cwd is used (i.e. the call
|
---|
216 | * is equivalent to RTPathAbs(pszPath, ...).
|
---|
217 | * @param pszPath The path to resolve.
|
---|
218 | * @param pszAbsPath Where to store the absolute path.
|
---|
219 | * @param cchAbsPath Size of the buffer.
|
---|
220 | */
|
---|
221 | RTDECL(int) RTPathAbsEx(const char *pszBase, const char *pszPath, char *pszAbsPath, size_t cchAbsPath);
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Same as RTPathAbsEx only the result is RTStrDup()'ed.
|
---|
225 | *
|
---|
226 | * @returns Pointer to the absolute path. Use RTStrFree() to free this string.
|
---|
227 | * @returns NULL if RTPathAbsEx() or RTStrDup() fails.
|
---|
228 | * @param pszBase The base path to act like a current directory.
|
---|
229 | * When NULL, the actual cwd is used (i.e. the call
|
---|
230 | * is equivalent to RTPathAbs(pszPath, ...).
|
---|
231 | * @param pszPath The path to resolve.
|
---|
232 | */
|
---|
233 | RTDECL(char *) RTPathAbsExDup(const char *pszBase, const char *pszPath);
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Strips the filename from a path. Truncates the given string in-place by overwriting the
|
---|
237 | * last path separator character with a null byte in a platform-neutral way.
|
---|
238 | *
|
---|
239 | * @param pszPath Path from which filename should be extracted, will be truncated.
|
---|
240 | * If the string contains no path separator, it will be changed to a "." string.
|
---|
241 | */
|
---|
242 | RTDECL(void) RTPathStripFilename(char *pszPath);
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Strips the extension from a path.
|
---|
246 | *
|
---|
247 | * @param pszPath Path which extension should be stripped.
|
---|
248 | */
|
---|
249 | RTDECL(void) RTPathStripExt(char *pszPath);
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Strips the trailing slashes of a path name.
|
---|
253 | *
|
---|
254 | * Won't strip root slashes.
|
---|
255 | *
|
---|
256 | * @returns The new length of pszPath.
|
---|
257 | * @param pszPath Path to strip.
|
---|
258 | */
|
---|
259 | RTDECL(size_t) RTPathStripTrailingSlash(char *pszPath);
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Changes all the slashes in the specified path to DOS style.
|
---|
263 | *
|
---|
264 | * Unless @a fForce is set, nothing will be done when on a UNIX flavored system
|
---|
265 | * since paths wont work with DOS style slashes there.
|
---|
266 | *
|
---|
267 | * @returns @a pszPath.
|
---|
268 | * @param pszPath The path to modify.
|
---|
269 | * @param fForce Whether to force the conversion on non-DOS OSes.
|
---|
270 | */
|
---|
271 | RTDECL(char *) RTPathChangeToDosSlashes(char *pszPath, bool fForce);
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Changes all the slashes in the specified path to unix style.
|
---|
275 | *
|
---|
276 | * Unless @a fForce is set, nothing will be done when on a UNIX flavored system
|
---|
277 | * since paths wont work with DOS style slashes there.
|
---|
278 | *
|
---|
279 | * @returns @a pszPath.
|
---|
280 | * @param pszPath The path to modify.
|
---|
281 | * @param fForce Whether to force the conversion on non-DOS OSes.
|
---|
282 | */
|
---|
283 | RTDECL(char *) RTPathChangeToUnixSlashes(char *pszPath, bool fForce);
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Parses a path.
|
---|
287 | *
|
---|
288 | * It figures the length of the directory component, the offset of
|
---|
289 | * the file name and the location of the suffix dot.
|
---|
290 | *
|
---|
291 | * @returns The path length.
|
---|
292 | *
|
---|
293 | * @param pszPath Path to find filename in.
|
---|
294 | * @param pcchDir Where to put the length of the directory component. If
|
---|
295 | * no directory, this will be 0. Optional.
|
---|
296 | * @param poffName Where to store the filename offset.
|
---|
297 | * If empty string or if it's ending with a slash this
|
---|
298 | * will be set to -1. Optional.
|
---|
299 | * @param poffSuff Where to store the suffix offset (the last dot).
|
---|
300 | * If empty string or if it's ending with a slash this
|
---|
301 | * will be set to -1. Optional.
|
---|
302 | */
|
---|
303 | RTDECL(size_t) RTPathParse(const char *pszPath, size_t *pcchDir, ssize_t *poffName, ssize_t *poffSuff);
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Finds the filename in a path.
|
---|
307 | *
|
---|
308 | * @returns Pointer to filename within pszPath.
|
---|
309 | * @returns NULL if no filename (i.e. empty string or ends with a slash).
|
---|
310 | * @param pszPath Path to find filename in.
|
---|
311 | */
|
---|
312 | RTDECL(char *) RTPathFilename(const char *pszPath);
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Finds the extension part of in a path.
|
---|
316 | *
|
---|
317 | * @returns Pointer to extension within pszPath.
|
---|
318 | * @returns NULL if no extension.
|
---|
319 | * @param pszPath Path to find extension in.
|
---|
320 | */
|
---|
321 | RTDECL(char *) RTPathExt(const char *pszPath);
|
---|
322 |
|
---|
323 | /**
|
---|
324 | * Checks if a path have an extension.
|
---|
325 | *
|
---|
326 | * @returns true if extension present.
|
---|
327 | * @returns false if no extension.
|
---|
328 | * @param pszPath Path to check.
|
---|
329 | */
|
---|
330 | RTDECL(bool) RTPathHaveExt(const char *pszPath);
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * Checks if a path includes more than a filename.
|
---|
334 | *
|
---|
335 | * @returns true if path present.
|
---|
336 | * @returns false if no path.
|
---|
337 | * @param pszPath Path to check.
|
---|
338 | */
|
---|
339 | RTDECL(bool) RTPathHavePath(const char *pszPath);
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * Checks if the path starts with a root specifier or not.
|
---|
343 | *
|
---|
344 | * @returns @c true if it starts with root, @c false if not.
|
---|
345 | *
|
---|
346 | * @param pszPath Path to check.
|
---|
347 | */
|
---|
348 | RTDECL(bool) RTPathStartsWithRoot(const char *pszPath);
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Counts the components in the specified path.
|
---|
352 | *
|
---|
353 | * An empty string has zero components. A lone root slash is considered have
|
---|
354 | * one. The paths "/init" and "/bin/" are considered having two components. An
|
---|
355 | * UNC share specifier like "\\myserver\share" will be considered as one single
|
---|
356 | * component.
|
---|
357 | *
|
---|
358 | * @returns The number of path components.
|
---|
359 | * @param pszPath The path to parse.
|
---|
360 | */
|
---|
361 | RTDECL(size_t) RTPathCountComponents(const char *pszPath);
|
---|
362 |
|
---|
363 | /**
|
---|
364 | * Copies the specified number of path components from @a pszSrc and into @a
|
---|
365 | * pszDst.
|
---|
366 | *
|
---|
367 | * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW. In the latter case the buffer
|
---|
368 | * is not touched.
|
---|
369 | *
|
---|
370 | * @param pszDst The destination buffer.
|
---|
371 | * @param cbDst The size of the destination buffer.
|
---|
372 | * @param pszSrc The source path.
|
---|
373 | * @param cComponents The number of components to copy from @a pszSrc.
|
---|
374 | */
|
---|
375 | RTDECL(int) RTPathCopyComponents(char *pszDst, size_t cbDst, const char *pszSrc, size_t cComponents);
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Compares two paths.
|
---|
379 | *
|
---|
380 | * The comparison takes platform-dependent details into account,
|
---|
381 | * such as:
|
---|
382 | * <ul>
|
---|
383 | * <li>On DOS-like platforms, both separator chars (|\| and |/|) are considered
|
---|
384 | * to be equal.
|
---|
385 | * <li>On platforms with case-insensitive file systems, mismatching characters
|
---|
386 | * are uppercased and compared again.
|
---|
387 | * </ul>
|
---|
388 | *
|
---|
389 | * @returns @< 0 if the first path less than the second path.
|
---|
390 | * @returns 0 if the first path identical to the second path.
|
---|
391 | * @returns @> 0 if the first path greater than the second path.
|
---|
392 | *
|
---|
393 | * @param pszPath1 Path to compare (must be an absolute path).
|
---|
394 | * @param pszPath2 Path to compare (must be an absolute path).
|
---|
395 | *
|
---|
396 | * @remarks File system details are currently ignored. This means that you won't
|
---|
397 | * get case-insensitive compares on unix systems when a path goes into a
|
---|
398 | * case-insensitive filesystem like FAT, HPFS, HFS, NTFS, JFS, or
|
---|
399 | * similar. For NT, OS/2 and similar you'll won't get case-sensitive
|
---|
400 | * compares on a case-sensitive file system.
|
---|
401 | */
|
---|
402 | RTDECL(int) RTPathCompare(const char *pszPath1, const char *pszPath2);
|
---|
403 |
|
---|
404 | /**
|
---|
405 | * Checks if a path starts with the given parent path.
|
---|
406 | *
|
---|
407 | * This means that either the path and the parent path matches completely, or
|
---|
408 | * that the path is to some file or directory residing in the tree given by the
|
---|
409 | * parent directory.
|
---|
410 | *
|
---|
411 | * The path comparison takes platform-dependent details into account,
|
---|
412 | * see RTPathCompare() for details.
|
---|
413 | *
|
---|
414 | * @returns |true| when \a pszPath starts with \a pszParentPath (or when they
|
---|
415 | * are identical), or |false| otherwise.
|
---|
416 | *
|
---|
417 | * @param pszPath Path to check, must be an absolute path.
|
---|
418 | * @param pszParentPath Parent path, must be an absolute path.
|
---|
419 | * No trailing directory slash!
|
---|
420 | *
|
---|
421 | * @remarks This API doesn't currently handle root directory compares in a
|
---|
422 | * manner consistent with the other APIs. RTPathStartsWith(pszSomePath,
|
---|
423 | * "/") will not work if pszSomePath isn't "/".
|
---|
424 | */
|
---|
425 | RTDECL(bool) RTPathStartsWith(const char *pszPath, const char *pszParentPath);
|
---|
426 |
|
---|
427 | /**
|
---|
428 | * Appends one partial path to another.
|
---|
429 | *
|
---|
430 | * The main purpose of this function is to deal correctly with the slashes when
|
---|
431 | * concatenating the two partial paths.
|
---|
432 | *
|
---|
433 | * @retval VINF_SUCCESS on success.
|
---|
434 | * @retval VERR_BUFFER_OVERFLOW if the result is too big to fit within
|
---|
435 | * cbPathDst bytes. No changes has been made.
|
---|
436 | * @retval VERR_INVALID_PARAMETER if the string pointed to by pszPath is longer
|
---|
437 | * than cbPathDst-1 bytes (failed to find terminator). Asserted.
|
---|
438 | *
|
---|
439 | * @param pszPath The path to append pszAppend to. This serves as both
|
---|
440 | * input and output. This can be empty, in which case
|
---|
441 | * pszAppend is just copied over.
|
---|
442 | * @param cbPathDst The size of the buffer pszPath points to, terminator
|
---|
443 | * included. This should NOT be strlen(pszPath).
|
---|
444 | * @param pszAppend The partial path to append to pszPath. This can be
|
---|
445 | * NULL, in which case nothing is done.
|
---|
446 | *
|
---|
447 | * @remarks See the RTPathAppendEx remarks.
|
---|
448 | */
|
---|
449 | RTDECL(int) RTPathAppend(char *pszPath, size_t cbPathDst, const char *pszAppend);
|
---|
450 |
|
---|
451 | /**
|
---|
452 | * Appends one partial path to another.
|
---|
453 | *
|
---|
454 | * The main purpose of this function is to deal correctly with the slashes when
|
---|
455 | * concatenating the two partial paths.
|
---|
456 | *
|
---|
457 | * @retval VINF_SUCCESS on success.
|
---|
458 | * @retval VERR_BUFFER_OVERFLOW if the result is too big to fit within
|
---|
459 | * cbPathDst bytes. No changes has been made.
|
---|
460 | * @retval VERR_INVALID_PARAMETER if the string pointed to by pszPath is longer
|
---|
461 | * than cbPathDst-1 bytes (failed to find terminator). Asserted.
|
---|
462 | *
|
---|
463 | * @param pszPath The path to append pszAppend to. This serves as both
|
---|
464 | * input and output. This can be empty, in which case
|
---|
465 | * pszAppend is just copied over.
|
---|
466 | * @param cbPathDst The size of the buffer pszPath points to, terminator
|
---|
467 | * included. This should NOT be strlen(pszPath).
|
---|
468 | * @param pszAppend The partial path to append to pszPath. This can be
|
---|
469 | * NULL, in which case nothing is done.
|
---|
470 | * @param cchAppendMax The maximum number or characters to take from @a
|
---|
471 | * pszAppend. RTSTR_MAX is fine.
|
---|
472 | *
|
---|
473 | * @remarks On OS/2, Window and similar systems, concatenating a drive letter
|
---|
474 | * specifier with a slash prefixed path will result in an absolute
|
---|
475 | * path. Meaning, RTPathAppend(strcpy(szBuf, "C:"), sizeof(szBuf),
|
---|
476 | * "/bar") will result in "C:/bar". (This follows directly from the
|
---|
477 | * behavior when pszPath is empty.)
|
---|
478 | *
|
---|
479 | * On the other hand, when joining a drive letter specifier with a
|
---|
480 | * partial path that does not start with a slash, the result is not an
|
---|
481 | * absolute path. Meaning, RTPathAppend(strcpy(szBuf, "C:"),
|
---|
482 | * sizeof(szBuf), "bar") will result in "C:bar".
|
---|
483 | */
|
---|
484 | RTDECL(int) RTPathAppendEx(char *pszPath, size_t cbPathDst, const char *pszAppend, size_t cchAppendMax);
|
---|
485 |
|
---|
486 | /**
|
---|
487 | * Like RTPathAppend, but with the base path as a separate argument instead of
|
---|
488 | * in the path buffer.
|
---|
489 | *
|
---|
490 | * @retval VINF_SUCCESS on success.
|
---|
491 | * @retval VERR_BUFFER_OVERFLOW if the result is too big to fit within
|
---|
492 | * cbPathDst bytes.
|
---|
493 | * @retval VERR_INVALID_PARAMETER if the string pointed to by pszPath is longer
|
---|
494 | * than cbPathDst-1 bytes (failed to find terminator). Asserted.
|
---|
495 | *
|
---|
496 | * @param pszPathDst Where to store the resulting path.
|
---|
497 | * @param cbPathDst The size of the buffer pszPathDst points to,
|
---|
498 | * terminator included.
|
---|
499 | * @param pszPathSrc The base path to copy into @a pszPathDst before
|
---|
500 | * appending @a pszAppend.
|
---|
501 | * @param pszAppend The partial path to append to pszPathSrc. This can
|
---|
502 | * be NULL, in which case nothing is done.
|
---|
503 | *
|
---|
504 | */
|
---|
505 | RTDECL(int) RTPathJoin(char *pszPathDst, size_t cbPathDst, const char *pszPathSrc,
|
---|
506 | const char *pszAppend);
|
---|
507 |
|
---|
508 | /**
|
---|
509 | * Same as RTPathJoin, except that the output buffer is allocated.
|
---|
510 | *
|
---|
511 | * @returns Buffer containing the joined up path, call RTStrFree to free. NULL
|
---|
512 | * on allocation failure.
|
---|
513 | * @param pszPathSrc The base path to copy into @a pszPathDst before
|
---|
514 | * appending @a pszAppend.
|
---|
515 | * @param pszAppend The partial path to append to pszPathSrc. This can
|
---|
516 | * be NULL, in which case nothing is done.
|
---|
517 | *
|
---|
518 | */
|
---|
519 | RTDECL(char *) RTPathJoinA(const char *pszPathSrc, const char *pszAppend);
|
---|
520 |
|
---|
521 | /**
|
---|
522 | * Extended version of RTPathJoin, both inputs can be specified as substrings.
|
---|
523 | *
|
---|
524 | * @retval VINF_SUCCESS on success.
|
---|
525 | * @retval VERR_BUFFER_OVERFLOW if the result is too big to fit within
|
---|
526 | * cbPathDst bytes.
|
---|
527 | * @retval VERR_INVALID_PARAMETER if the string pointed to by pszPath is longer
|
---|
528 | * than cbPathDst-1 bytes (failed to find terminator). Asserted.
|
---|
529 | *
|
---|
530 | * @param pszPathDst Where to store the resulting path.
|
---|
531 | * @param cbPathDst The size of the buffer pszPathDst points to,
|
---|
532 | * terminator included.
|
---|
533 | * @param pszPathSrc The base path to copy into @a pszPathDst before
|
---|
534 | * appending @a pszAppend.
|
---|
535 | * @param cchPathSrcMax The maximum number of bytes to copy from @a
|
---|
536 | * pszPathSrc. RTSTR_MAX is find.
|
---|
537 | * @param pszAppend The partial path to append to pszPathSrc. This can
|
---|
538 | * be NULL, in which case nothing is done.
|
---|
539 | * @param cchAppendMax The maximum number of bytes to copy from @a
|
---|
540 | * pszAppend. RTSTR_MAX is find.
|
---|
541 | *
|
---|
542 | */
|
---|
543 | RTDECL(int) RTPathJoinEx(char *pszPathDst, size_t cbPathDst,
|
---|
544 | const char *pszPathSrc, size_t cchPathSrcMax,
|
---|
545 | const char *pszAppend, size_t cchAppendMax);
|
---|
546 |
|
---|
547 | /**
|
---|
548 | * Callback for RTPathTraverseList that's called for each element.
|
---|
549 | *
|
---|
550 | * @returns IPRT style status code. Return VINF_TRY_AGAIN to continue, any other
|
---|
551 | * value will abort the traversing and be returned to the caller.
|
---|
552 | *
|
---|
553 | * @param pchPath Pointer to the start of the current path. This is
|
---|
554 | * not null terminated.
|
---|
555 | * @param cchPath The length of the path.
|
---|
556 | * @param pvUser1 The first user parameter.
|
---|
557 | * @param pvUser2 The second user parameter.
|
---|
558 | */
|
---|
559 | typedef DECLCALLBACK(int) FNRTPATHTRAVERSER(char const *pchPath, size_t cchPath, void *pvUser1, void *pvUser2);
|
---|
560 | /** Pointer to a FNRTPATHTRAVERSER. */
|
---|
561 | typedef FNRTPATHTRAVERSER *PFNRTPATHTRAVERSER;
|
---|
562 |
|
---|
563 | /**
|
---|
564 | * Traverses a string that can contain multiple paths separated by a special
|
---|
565 | * character.
|
---|
566 | *
|
---|
567 | * @returns IPRT style status code from the callback or VERR_END_OF_STRING if
|
---|
568 | * the callback returned VINF_TRY_AGAIN for all paths in the string.
|
---|
569 | *
|
---|
570 | * @param pszPathList The string to traverse.
|
---|
571 | * @param chSep The separator character. Using the null terminator
|
---|
572 | * is fine, but the result will simply be that there
|
---|
573 | * will only be one callback for the entire string
|
---|
574 | * (save any leading white space).
|
---|
575 | * @param pfnCallback The callback.
|
---|
576 | * @param pvUser1 First user argument for the callback.
|
---|
577 | * @param pvUser2 Second user argument for the callback.
|
---|
578 | */
|
---|
579 | RTDECL(int) RTPathTraverseList(const char *pszPathList, char chSep, PFNRTPATHTRAVERSER pfnCallback, void *pvUser1, void *pvUser2);
|
---|
580 |
|
---|
581 |
|
---|
582 | #ifdef IN_RING3
|
---|
583 |
|
---|
584 | /**
|
---|
585 | * Gets the path to the directory containing the executable.
|
---|
586 | *
|
---|
587 | * @returns iprt status code.
|
---|
588 | * @param pszPath Buffer where to store the path.
|
---|
589 | * @param cchPath Buffer size in bytes.
|
---|
590 | */
|
---|
591 | RTDECL(int) RTPathExecDir(char *pszPath, size_t cchPath);
|
---|
592 |
|
---|
593 | /**
|
---|
594 | * Gets the user home directory.
|
---|
595 | *
|
---|
596 | * @returns iprt status code.
|
---|
597 | * @param pszPath Buffer where to store the path.
|
---|
598 | * @param cchPath Buffer size in bytes.
|
---|
599 | */
|
---|
600 | RTDECL(int) RTPathUserHome(char *pszPath, size_t cchPath);
|
---|
601 |
|
---|
602 | /**
|
---|
603 | * Gets the directory of shared libraries.
|
---|
604 | *
|
---|
605 | * This is not the same as RTPathAppPrivateArch() as Linux depends all shared
|
---|
606 | * libraries in a common global directory where ld.so can found them.
|
---|
607 | *
|
---|
608 | * Linux: /usr/lib
|
---|
609 | * Windows: @<program files directory@>/@<application@>
|
---|
610 | * Old path: same as RTPathExecDir()
|
---|
611 | *
|
---|
612 | * @returns iprt status code.
|
---|
613 | * @param pszPath Buffer where to store the path.
|
---|
614 | * @param cchPath Buffer size in bytes.
|
---|
615 | */
|
---|
616 | RTDECL(int) RTPathSharedLibs(char *pszPath, size_t cchPath);
|
---|
617 |
|
---|
618 | /**
|
---|
619 | * Gets the directory for architecture-independent application data, for
|
---|
620 | * example NLS files, module sources, ...
|
---|
621 | *
|
---|
622 | * Linux: /usr/shared/@<application@>
|
---|
623 | * Windows: @<program files directory@>/@<application@>
|
---|
624 | * Old path: same as RTPathExecDir()
|
---|
625 | *
|
---|
626 | * @returns iprt status code.
|
---|
627 | * @param pszPath Buffer where to store the path.
|
---|
628 | * @param cchPath Buffer size in bytes.
|
---|
629 | */
|
---|
630 | RTDECL(int) RTPathAppPrivateNoArch(char *pszPath, size_t cchPath);
|
---|
631 |
|
---|
632 | /**
|
---|
633 | * Gets the directory for architecture-dependent application data, for
|
---|
634 | * example modules which can be loaded at runtime.
|
---|
635 | *
|
---|
636 | * Linux: /usr/lib/@<application@>
|
---|
637 | * Windows: @<program files directory@>/@<application@>
|
---|
638 | * Old path: same as RTPathExecDir()
|
---|
639 | *
|
---|
640 | * @returns iprt status code.
|
---|
641 | * @param pszPath Buffer where to store the path.
|
---|
642 | * @param cchPath Buffer size in bytes.
|
---|
643 | */
|
---|
644 | RTDECL(int) RTPathAppPrivateArch(char *pszPath, size_t cchPath);
|
---|
645 |
|
---|
646 | /**
|
---|
647 | * Gets the directory for documentation.
|
---|
648 | *
|
---|
649 | * Linux: /usr/share/doc/@<application@>
|
---|
650 | * Windows: @<program files directory@>/@<application@>
|
---|
651 | * Old path: same as RTPathExecDir()
|
---|
652 | *
|
---|
653 | * @returns iprt status code.
|
---|
654 | * @param pszPath Buffer where to store the path.
|
---|
655 | * @param cchPath Buffer size in bytes.
|
---|
656 | */
|
---|
657 | RTDECL(int) RTPathAppDocs(char *pszPath, size_t cchPath);
|
---|
658 |
|
---|
659 | /**
|
---|
660 | * Gets the temporary directory path.
|
---|
661 | *
|
---|
662 | * @returns iprt status code.
|
---|
663 | * @param pszPath Buffer where to store the path.
|
---|
664 | * @param cchPath Buffer size in bytes.
|
---|
665 | */
|
---|
666 | RTDECL(int) RTPathTemp(char *pszPath, size_t cchPath);
|
---|
667 |
|
---|
668 | /**
|
---|
669 | * Query information about a file system object.
|
---|
670 | *
|
---|
671 | * This API will resolve NOT symbolic links in the last component (just like
|
---|
672 | * unix lstat()).
|
---|
673 | *
|
---|
674 | * @returns IPRT status code.
|
---|
675 | * @retval VINF_SUCCESS if the object exists, information returned.
|
---|
676 | * @retval VERR_PATH_NOT_FOUND if any but the last component in the specified
|
---|
677 | * path was not found or was not a directory.
|
---|
678 | * @retval VERR_FILE_NOT_FOUND if the object does not exist (but path to the
|
---|
679 | * parent directory exists).
|
---|
680 | *
|
---|
681 | * @param pszPath Path to the file system object.
|
---|
682 | * @param pObjInfo Object information structure to be filled on successful
|
---|
683 | * return.
|
---|
684 | * @param enmAdditionalAttribs
|
---|
685 | * Which set of additional attributes to request.
|
---|
686 | * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
|
---|
687 | */
|
---|
688 | RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs);
|
---|
689 |
|
---|
690 | /**
|
---|
691 | * Query information about a file system object.
|
---|
692 | *
|
---|
693 | * @returns IPRT status code.
|
---|
694 | * @retval VINF_SUCCESS if the object exists, information returned.
|
---|
695 | * @retval VERR_PATH_NOT_FOUND if any but the last component in the specified
|
---|
696 | * path was not found or was not a directory.
|
---|
697 | * @retval VERR_FILE_NOT_FOUND if the object does not exist (but path to the
|
---|
698 | * parent directory exists).
|
---|
699 | *
|
---|
700 | * @param pszPath Path to the file system object.
|
---|
701 | * @param pObjInfo Object information structure to be filled on successful return.
|
---|
702 | * @param enmAdditionalAttribs
|
---|
703 | * Which set of additional attributes to request.
|
---|
704 | * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
|
---|
705 | * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
|
---|
706 | */
|
---|
707 | RTR3DECL(int) RTPathQueryInfoEx(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags);
|
---|
708 |
|
---|
709 | /**
|
---|
710 | * Changes the mode flags of a file system object.
|
---|
711 | *
|
---|
712 | * The API requires at least one of the mode flag sets (Unix/Dos) to
|
---|
713 | * be set. The type is ignored.
|
---|
714 | *
|
---|
715 | * This API will resolve symbolic links in the last component since
|
---|
716 | * mode isn't important for symbolic links.
|
---|
717 | *
|
---|
718 | * @returns iprt status code.
|
---|
719 | * @param pszPath Path to the file system object.
|
---|
720 | * @param fMode The new file mode, see @ref grp_rt_fs for details.
|
---|
721 | */
|
---|
722 | RTR3DECL(int) RTPathSetMode(const char *pszPath, RTFMODE fMode);
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * Gets the mode flags of a file system object.
|
---|
726 | *
|
---|
727 | * @returns iprt status code.
|
---|
728 | * @param pszPath Path to the file system object.
|
---|
729 | * @param pfMode Where to store the file mode, see @ref grp_rt_fs for details.
|
---|
730 | *
|
---|
731 | * @remark This is wrapper around RTPathQueryInfoEx(RTPATH_F_FOLLOW_LINK) and
|
---|
732 | * exists to complement RTPathSetMode().
|
---|
733 | */
|
---|
734 | RTR3DECL(int) RTPathGetMode(const char *pszPath, PRTFMODE pfMode);
|
---|
735 |
|
---|
736 | /**
|
---|
737 | * Changes one or more of the timestamps associated of file system object.
|
---|
738 | *
|
---|
739 | * This API will not resolve symbolic links in the last component (just
|
---|
740 | * like unix lutimes()).
|
---|
741 | *
|
---|
742 | * @returns iprt status code.
|
---|
743 | * @param pszPath Path to the file system object.
|
---|
744 | * @param pAccessTime Pointer to the new access time.
|
---|
745 | * @param pModificationTime Pointer to the new modification time.
|
---|
746 | * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
|
---|
747 | * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
|
---|
748 | *
|
---|
749 | * @remark The file system might not implement all these time attributes,
|
---|
750 | * the API will ignore the ones which aren't supported.
|
---|
751 | *
|
---|
752 | * @remark The file system might not implement the time resolution
|
---|
753 | * employed by this interface, the time will be chopped to fit.
|
---|
754 | *
|
---|
755 | * @remark The file system may update the change time even if it's
|
---|
756 | * not specified.
|
---|
757 | *
|
---|
758 | * @remark POSIX can only set Access & Modification and will always set both.
|
---|
759 | */
|
---|
760 | RTR3DECL(int) RTPathSetTimes(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
|
---|
761 | PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime);
|
---|
762 |
|
---|
763 | /**
|
---|
764 | * Changes one or more of the timestamps associated of file system object.
|
---|
765 | *
|
---|
766 | * @returns iprt status code.
|
---|
767 | * @param pszPath Path to the file system object.
|
---|
768 | * @param pAccessTime Pointer to the new access time.
|
---|
769 | * @param pModificationTime Pointer to the new modification time.
|
---|
770 | * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
|
---|
771 | * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
|
---|
772 | * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
|
---|
773 | *
|
---|
774 | * @remark The file system might not implement all these time attributes,
|
---|
775 | * the API will ignore the ones which aren't supported.
|
---|
776 | *
|
---|
777 | * @remark The file system might not implement the time resolution
|
---|
778 | * employed by this interface, the time will be chopped to fit.
|
---|
779 | *
|
---|
780 | * @remark The file system may update the change time even if it's
|
---|
781 | * not specified.
|
---|
782 | *
|
---|
783 | * @remark POSIX can only set Access & Modification and will always set both.
|
---|
784 | */
|
---|
785 | RTR3DECL(int) RTPathSetTimesEx(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
|
---|
786 | PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime, uint32_t fFlags);
|
---|
787 |
|
---|
788 | /**
|
---|
789 | * Gets one or more of the timestamps associated of file system object.
|
---|
790 | *
|
---|
791 | * @returns iprt status code.
|
---|
792 | * @param pszPath Path to the file system object.
|
---|
793 | * @param pAccessTime Where to store the access time. NULL is ok.
|
---|
794 | * @param pModificationTime Where to store the modification time. NULL is ok.
|
---|
795 | * @param pChangeTime Where to store the change time. NULL is ok.
|
---|
796 | * @param pBirthTime Where to store the creation time. NULL is ok.
|
---|
797 | *
|
---|
798 | * @remark This is wrapper around RTPathQueryInfo() and exists to complement
|
---|
799 | * RTPathSetTimes(). If the last component is a symbolic link, it will
|
---|
800 | * not be resolved.
|
---|
801 | */
|
---|
802 | RTR3DECL(int) RTPathGetTimes(const char *pszPath, PRTTIMESPEC pAccessTime, PRTTIMESPEC pModificationTime,
|
---|
803 | PRTTIMESPEC pChangeTime, PRTTIMESPEC pBirthTime);
|
---|
804 |
|
---|
805 | /**
|
---|
806 | * Changes the owner and/or group of a file system object.
|
---|
807 | *
|
---|
808 | * This API will not resolve symbolic links in the last component (just
|
---|
809 | * like unix lchown()).
|
---|
810 | *
|
---|
811 | * @returns iprt status code.
|
---|
812 | * @param pszPath Path to the file system object.
|
---|
813 | * @param uid The new file owner user id. Pass NIL_RTUID to leave
|
---|
814 | * this unchanged.
|
---|
815 | * @param gid The new group id. Pass NIL_RTGUID to leave this
|
---|
816 | * unchanged.
|
---|
817 | */
|
---|
818 | RTR3DECL(int) RTPathSetOwner(const char *pszPath, uint32_t uid, uint32_t gid);
|
---|
819 |
|
---|
820 | /**
|
---|
821 | * Changes the owner and/or group of a file system object.
|
---|
822 | *
|
---|
823 | * @returns iprt status code.
|
---|
824 | * @param pszPath Path to the file system object.
|
---|
825 | * @param uid The new file owner user id. Pass NIL_RTUID to leave
|
---|
826 | * this unchanged.
|
---|
827 | * @param gid The new group id. Pass NIL_RTGID to leave this
|
---|
828 | * unchanged.
|
---|
829 | * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
|
---|
830 | */
|
---|
831 | RTR3DECL(int) RTPathSetOwnerEx(const char *pszPath, uint32_t uid, uint32_t gid, uint32_t fFlags);
|
---|
832 |
|
---|
833 | /**
|
---|
834 | * Gets the owner and/or group of a file system object.
|
---|
835 | *
|
---|
836 | * @returns iprt status code.
|
---|
837 | * @param pszPath Path to the file system object.
|
---|
838 | * @param pUid Where to store the owner user id. NULL is ok.
|
---|
839 | * @param pGid Where to store the group id. NULL is ok.
|
---|
840 | *
|
---|
841 | * @remark This is wrapper around RTPathQueryInfo() and exists to complement
|
---|
842 | * RTPathGetOwner(). If the last component is a symbolic link, it will
|
---|
843 | * not be resolved.
|
---|
844 | */
|
---|
845 | RTR3DECL(int) RTPathGetOwner(const char *pszPath, uint32_t *pUid, uint32_t *pGid);
|
---|
846 |
|
---|
847 |
|
---|
848 | /** @name RTPathRename, RTDirRename & RTFileRename flags.
|
---|
849 | * @{ */
|
---|
850 | /** Do not replace anything. */
|
---|
851 | #define RTPATHRENAME_FLAGS_NO_REPLACE UINT32_C(0)
|
---|
852 | /** This will replace attempt any target which isn't a directory. */
|
---|
853 | #define RTPATHRENAME_FLAGS_REPLACE RT_BIT(0)
|
---|
854 | /** @} */
|
---|
855 |
|
---|
856 | /**
|
---|
857 | * Renames a path within a filesystem.
|
---|
858 | *
|
---|
859 | * This will rename symbolic links. If RTPATHRENAME_FLAGS_REPLACE is used and
|
---|
860 | * pszDst is a symbolic link, it will be replaced and not its target.
|
---|
861 | *
|
---|
862 | * @returns IPRT status code.
|
---|
863 | * @param pszSrc The source path.
|
---|
864 | * @param pszDst The destination path.
|
---|
865 | * @param fRename Rename flags, RTPATHRENAME_FLAGS_*.
|
---|
866 | */
|
---|
867 | RTR3DECL(int) RTPathRename(const char *pszSrc, const char *pszDst, unsigned fRename);
|
---|
868 |
|
---|
869 | #endif /* IN_RING3 */
|
---|
870 |
|
---|
871 | /** @} */
|
---|
872 |
|
---|
873 | RT_C_DECLS_END
|
---|
874 |
|
---|
875 | #endif
|
---|
876 |
|
---|