VirtualBox

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

Last change on this file since 12822 was 11836, checked in by vboxsync, 16 years ago

IPRT: Implemented RTR3Init*WithProgramPath. Added RTPathParse. Cleaned up the RTPathProgram and RTProcGetExecutableName implementations.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.6 KB
Line 
1/** @file
2 * IPRT - Path Manipulation.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___iprt_path_h
31#define ___iprt_path_h
32
33#include <iprt/cdefs.h>
34#include <iprt/types.h>
35#ifdef IN_RING3
36# include <iprt/fs.h>
37#endif
38
39
40
41__BEGIN_DECLS
42
43/** @defgroup grp_rt_path RTPath - Path Manipulation
44 * @ingroup grp_rt
45 * @{
46 */
47
48
49/** @def RTPATH_SLASH
50 * The prefered slash character.
51 *
52 * @remark IPRT will always accept unix slashes. So, normally you would
53 * never have to use this define.
54 */
55#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
56# define RTPATH_SLASH '\\'
57#else
58# define RTPATH_SLASH '/'
59#endif
60
61/** @deprecated Use '/'! */
62#define RTPATH_DELIMITER RTPATH_SLASH
63
64
65/** @def RTPATH_SLASH_STR
66 * The prefered slash character as a string, handy for concatenations
67 * with other strings.
68 *
69 * @remark IPRT will always accept unix slashes. So, normally you would
70 * never have to use this define.
71 */
72#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
73# define RTPATH_SLASH_STR "\\"
74#else
75# define RTPATH_SLASH_STR "/"
76#endif
77
78
79/** @def RTPATH_IS_SLASH
80 * Checks if a character is a slash.
81 *
82 * @returns true if it's a slash and false if not.
83 * @returns @param ch Char to check.
84 */
85#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
86# define RTPATH_IS_SLASH(ch) ( (ch) == '\\' || (ch) == '/' )
87#else
88# define RTPATH_IS_SLASH(ch) ( (ch) == '/' )
89#endif
90
91
92/** @def RTPATH_IS_VOLSEP
93 * Checks if a character marks the end of the volume specification.
94 *
95 * @remark This is sufficent for the drive letter consept on PC.
96 * However it might be insufficient on other platforms
97 * and even on PC a UNC volume spec won't be detected this way.
98 * Use the RTPath@<too be created@>() instead.
99 *
100 * @returns true if it is and false if it isn't.
101 * @returns @param ch Char to check.
102 */
103#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
104# define RTPATH_IS_VOLSEP(ch) ( (ch) == ':' )
105#else
106# define RTPATH_IS_VOLSEP(ch) (false)
107#endif
108
109
110/** @def RTPATH_IS_SEP
111 * Checks if a character is path component separator
112 *
113 * @returns true if it is and false if it isn't.
114 * @returns @param ch Char to check.
115 * @
116 */
117#define RTPATH_IS_SEP(ch) ( RTPATH_IS_SLASH(ch) || RTPATH_IS_VOLSEP(ch) )
118
119
120/**
121 * Checks if the path exists.
122 *
123 * Symbolic links will all be attempted resolved.
124 *
125 * @returns true if it exists and false if it doesn't.
126 * @param pszPath The path to check.
127 */
128RTDECL(bool) RTPathExists(const char *pszPath);
129
130/**
131 * Sets the current working directory of the process.
132 *
133 * @returns IPRT status code.
134 * @param pszPath The path to the new working directory.
135 */
136RTDECL(int) RTPathSetCurrent(const char *pszPath);
137
138/**
139 * Gets the current working directory of the process.
140 *
141 * @returns IPRT status code.
142 * @param pszPath Where to store the path.
143 * @param cchPath The size of the buffer pszPath points to.
144 */
145RTDECL(int) RTPathGetCurrent(char *pszPath, size_t cchPath);
146
147/**
148 * Get the real path (no symlinks, no . or .. components), must exist.
149 *
150 * @returns iprt status code.
151 * @param pszPath The path to resolve.
152 * @param pszRealPath Where to store the real path.
153 * @param cchRealPath Size of the buffer.
154 */
155RTDECL(int) RTPathReal(const char *pszPath, char *pszRealPath, unsigned cchRealPath);
156
157/**
158 * Same as RTPathReal only the result is RTStrDup()'ed.
159 *
160 * @returns Pointer to real path. Use RTStrFree() to free this string.
161 * @returns NULL if RTPathReal() or RTStrDup() fails.
162 * @param pszPath The path to resolve.
163 */
164RTDECL(char *) RTPathRealDup(const char *pszPath);
165
166/**
167 * Get the absolute path (no symlinks, no . or .. components), doesn't have to exist.
168 *
169 * @returns iprt status code.
170 * @param pszPath The path to resolve.
171 * @param pszAbsPath Where to store the absolute path.
172 * @param cchAbsPath Size of the buffer.
173 */
174RTDECL(int) RTPathAbs(const char *pszPath, char *pszAbsPath, unsigned cchAbsPath);
175
176/**
177 * Same as RTPathAbs only the result is RTStrDup()'ed.
178 *
179 * @returns Pointer to the absolute path. Use RTStrFree() to free this string.
180 * @returns NULL if RTPathAbs() or RTStrDup() fails.
181 * @param pszPath The path to resolve.
182 */
183RTDECL(char *) RTPathAbsDup(const char *pszPath);
184
185/**
186 * Get the absolute path (no symlinks, no . or .. components), assuming the
187 * given base path as the current directory. The resulting path doesn't have
188 * to exist.
189 *
190 * @returns iprt status code.
191 * @param pszBase The base path to act like a current directory.
192 * When NULL, the actual cwd is used (i.e. the call
193 * is equivalent to RTPathAbs(pszPath, ...).
194 * @param pszPath The path to resolve.
195 * @param pszAbsPath Where to store the absolute path.
196 * @param cchAbsPath Size of the buffer.
197 */
198RTDECL(int) RTPathAbsEx(const char *pszBase, const char *pszPath, char *pszAbsPath, unsigned cchAbsPath);
199
200/**
201 * Same as RTPathAbsEx only the result is RTStrDup()'ed.
202 *
203 * @returns Pointer to the absolute path. Use RTStrFree() to free this string.
204 * @returns NULL if RTPathAbsEx() or RTStrDup() fails.
205 * @param pszBase The base path to act like a current directory.
206 * When NULL, the actual cwd is used (i.e. the call
207 * is equivalent to RTPathAbs(pszPath, ...).
208 * @param pszPath The path to resolve.
209 */
210RTDECL(char *) RTPathAbsExDup(const char *pszBase, const char *pszPath);
211
212/**
213 * Strips the filename from a path.
214 *
215 * @param pszPath Path which filename should be stripped.
216 * If only filename in the string a '.' will be returned.
217 */
218RTDECL(void) RTPathStripFilename(char *pszPath);
219
220/**
221 * Strips the extension from a path.
222 *
223 * @param pszPath Path which extension should be stripped.
224 */
225RTDECL(void) RTPathStripExt(char *pszPath);
226
227/**
228 * Strips the trailing slashes of a path name.
229 *
230 * @param pszPath Path to strip.
231 */
232RTDECL(void) RTPathStripTrailingSlash(char *pszPath);
233
234/**
235 * Parses a path.
236 *
237 * It figures the length of the directory component, the offset of
238 * the file name and the location of the suffix dot.
239 *
240 * @returns The path length.
241 *
242 * @param pszPath Path to find filename in.
243 * @param pcbDir Where to put the length of the directory component.
244 * If no directory, this will be 0. Optional.
245 * @param poffName Where to store the filename offset.
246 * If empty string or if it's ending with a slash this
247 * will be set to -1. Optional.
248 * @param poffSuff Where to store the suffix offset (the last dot).
249 * If empty string or if it's ending with a slash this
250 * will be set to -1. Optional.
251 * @param pfFlags Where to set flags returning more information about
252 * the path. For the future. Optional.
253 */
254RTDECL(size_t) RTPathParse(const char *pszPath, size_t *pcchDir, ssize_t *poffName, ssize_t *poffSuff);
255
256/**
257 * Finds the filename in a path.
258 *
259 * @returns Pointer to filename within pszPath.
260 * @returns NULL if no filename (i.e. empty string or ends with a slash).
261 * @param pszPath Path to find filename in.
262 */
263RTDECL(char *) RTPathFilename(const char *pszPath);
264
265/**
266 * Finds the extension part of in a path.
267 *
268 * @returns Pointer to extension within pszPath.
269 * @returns NULL if no extension.
270 * @param pszPath Path to find extension in.
271 */
272RTDECL(char *) RTPathExt(const char *pszPath);
273
274/**
275 * Checks if a path have an extension.
276 *
277 * @returns true if extension present.
278 * @returns false if no extension.
279 * @param pszPath Path to check.
280 */
281RTDECL(bool) RTPathHaveExt(const char *pszPath);
282
283/**
284 * Checks if a path includes more than a filename.
285 *
286 * @returns true if path present.
287 * @returns false if no path.
288 * @param pszPath Path to check.
289 */
290RTDECL(bool) RTPathHavePath(const char *pszPath);
291
292/**
293 * Compares two paths.
294 *
295 * The comparison takes platform-dependent details into account,
296 * such as:
297 * <ul>
298 * <li>On DOS-like platforms, both |\| and |/| separator chars are considered
299 * to be equal.
300 * <li>On platforms with case-insensitive file systems, mismatching characters
301 * are uppercased and compared again.
302 * </ul>
303 *
304 * File system details are currently ignored. This means that you won't get
305 * case-insentive compares on unix systems when a path goes into a case-insensitive
306 * filesystem like FAT, HPFS, HFS, NTFS, JFS, or similar. For NT, OS/2 and similar
307 * you'll won't get case-sensitve compares on a case-sensitive file system.
308 *
309 * @param pszPath1 Path to compare (must be an absolute path).
310 * @param pszPath2 Path to compare (must be an absolute path).
311 *
312 * @returns @< 0 if the first path less than the second path.
313 * @returns 0 if the first path identical to the second path.
314 * @returns @> 0 if the first path greater than the second path.
315 */
316RTDECL(int) RTPathCompare(const char *pszPath1, const char *pszPath2);
317
318/**
319 * Checks if a path starts with the given parent path.
320 *
321 * This means that either the path and the parent path matches completely, or that
322 * the path is to some file or directory residing in the tree given by the parent
323 * directory.
324 *
325 * The path comparison takes platform-dependent details into account,
326 * see RTPathCompare() for details.
327 *
328 * @param pszPath Path to check, must be an absolute path.
329 * @param pszParentPath Parent path, must be an absolute path.
330 * No trailing directory slash!
331 *
332 * @returns |true| when \a pszPath starts with \a pszParentPath (or when they
333 * are identical), or |false| otherwise.
334 *
335 * @remark This API doesn't currently handle root directory compares in a manner
336 * consistant with the other APIs. RTPathStartsWith(pszSomePath, "/") will
337 * not work if pszSomePath isn't "/".
338 */
339RTDECL(bool) RTPathStartsWith(const char *pszPath, const char *pszParentPath);
340
341
342#ifdef IN_RING3
343
344/**
345 * Gets the program path.
346 *
347 * @returns iprt status code.
348 * @param pszPath Buffer where to store the path.
349 * @param cchPath Buffer size in bytes.
350 */
351RTDECL(int) RTPathProgram(char *pszPath, unsigned cchPath);
352
353/**
354 * Gets the user home directory.
355 *
356 * @returns iprt status code.
357 * @param pszPath Buffer where to store the path.
358 * @param cchPath Buffer size in bytes.
359 */
360RTDECL(int) RTPathUserHome(char *pszPath, unsigned cchPath);
361
362/**
363 * Gets the directory of shared libraries. This is not the same as
364 * RTPathAppPrivateArch() as Linux depends all shared libraries in
365 * a common global directory where ld.so can found them.
366 *
367 * Linux: /usr/lib
368 * Windows: @<program files directory@>/@<application@>
369 * Old path: same as RTPathProgram()
370 *
371 * @returns iprt status code.
372 * @param pszPath Buffer where to store the path.
373 * @param cchPath Buffer size in bytes.
374 */
375RTDECL(int) RTPathSharedLibs(char *pszPath, unsigned cchPath);
376
377/**
378 * Gets the directory for architecture-independent application data, for
379 * example NLS files, module sources, ...
380 *
381 * Linux: /usr/shared/@<application@>
382 * Windows: @<program files directory@>/@<application@>
383 * Old path: same as RTPathProgram()
384 *
385 * @returns iprt status code.
386 * @param pszPath Buffer where to store the path.
387 * @param cchPath Buffer size in bytes.
388 */
389RTDECL(int) RTPathAppPrivateNoArch(char *pszPath, unsigned cchPath);
390
391/**
392 * Gets the directory for architecture-dependent application data, for
393 * example modules which can be loaded at runtime.
394 *
395 * Linux: /usr/lib/@<application@>
396 * Windows: @<program files directory@>/@<application@>
397 * Old path: same as RTPathProgram()
398 *
399 * @returns iprt status code.
400 * @param pszPath Buffer where to store the path.
401 * @param cchPath Buffer size in bytes.
402 */
403RTDECL(int) RTPathAppPrivateArch(char *pszPath, unsigned cchPath);
404
405/**
406 * Gets the directory for documentation.
407 *
408 * Linux: /usr/share/doc/@<application@>
409 * Windows: @<program files directory@>/@<application@>
410 * Old path: same as RTPathProgram()
411 *
412 * @returns iprt status code.
413 * @param pszPath Buffer where to store the path.
414 * @param cchPath Buffer size in bytes.
415 */
416RTDECL(int) RTPathAppDocs(char *pszPath, unsigned cchPath);
417
418/**
419 * Query information about a file system object.
420 *
421 * This API will not resolve symbolic links in the last component (just
422 * like unix lstat()).
423 *
424 * @returns VINF_SUCCESS if the object exists, information returned.
425 * @returns VERR_PATH_NOT_FOUND if any but the last component in the specified
426 * path was not found or was not a directory.
427 * @returns VERR_FILE_NOT_FOUND if the object does not exist (but path to the
428 * parent directory exists).
429 * @returns some other iprt status code.
430 *
431 * @param pszPath Path to the file system object.
432 * @param pObjInfo Object information structure to be filled on successful return.
433 * @param enmAdditionalAttribs
434 * Which set of additional attributes to request.
435 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
436 */
437RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs);
438
439/**
440 * Changes the mode flags of a file system object.
441 *
442 * The API requires at least one of the mode flag sets (Unix/Dos) to
443 * be set. The type is ignored.
444 *
445 * This API will resolve symbolic links in the last component since
446 * mode isn't important for symbolic links.
447 *
448 * @returns iprt status code.
449 * @param pszPath Path to the file system object.
450 * @param fMode The new file mode, see @ref grp_rt_fs for details.
451 */
452RTR3DECL(int) RTPathSetMode(const char *pszPath, RTFMODE fMode);
453
454/**
455 * Gets the mode flags of a file system object.
456 *
457 * @returns iprt status code.
458 * @param pszPath Path to the file system object.
459 * @param pfMode Where to store the file mode, see @ref grp_rt_fs for details.
460 *
461 * @remark This is wrapper around RTPathReal() + RTPathQueryInfo()
462 * and exists to complement RTPathSetMode().
463 */
464RTR3DECL(int) RTPathGetMode(const char *pszPath, PRTFMODE pfMode);
465
466/**
467 * Changes one or more of the timestamps associated of file system object.
468 *
469 * This API will not resolve symbolic links in the last component (just
470 * like unix lutimes()).
471 *
472 * @returns iprt status code.
473 * @param pszPath Path to the file system object.
474 * @param pAccessTime Pointer to the new access time.
475 * @param pModificationTime Pointer to the new modifcation time.
476 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
477 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
478 *
479 * @remark The file system might not implement all these time attributes,
480 * the API will ignore the ones which aren't supported.
481 *
482 * @remark The file system might not implement the time resolution
483 * employed by this interface, the time will be chopped to fit.
484 *
485 * @remark The file system may update the change time even if it's
486 * not specified.
487 *
488 * @remark POSIX can only set Access & Modification and will always set both.
489 */
490RTR3DECL(int) RTPathSetTimes(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
491 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime);
492
493/**
494 * Gets one or more of the timestamps associated of file system object.
495 *
496 * @returns iprt status code.
497 * @param pszPath Path to the file system object.
498 * @param pAccessTime Where to store the access time. NULL is ok.
499 * @param pModificationTime Where to store the modifcation time. NULL is ok.
500 * @param pChangeTime Where to store the change time. NULL is ok.
501 * @param pBirthTime Where to store the creation time. NULL is ok.
502 *
503 * @remark This is wrapper around RTPathQueryInfo() and exists to complement RTPathSetTimes().
504 */
505RTR3DECL(int) RTPathGetTimes(const char *pszPath, PRTTIMESPEC pAccessTime, PRTTIMESPEC pModificationTime,
506 PRTTIMESPEC pChangeTime, PRTTIMESPEC pBirthTime);
507
508/**
509 * Changes the owner and/or group of a file system object.
510 *
511 * This API will not resolve symbolic links in the last component (just
512 * like unix lchown()).
513 *
514 * @returns iprt status code.
515 * @param pszPath Path to the file system object.
516 * @param uid The new file owner user id. Use -1 (or ~0) to leave this unchanged.
517 * @param gid The new group id. Use -1 (or ~0) to leave this unchanged.
518 */
519RTR3DECL(int) RTPathSetOwner(const char *pszPath, uint32_t uid, uint32_t gid);
520
521/**
522 * Gets the owner and/or group of a file system object.
523 *
524 * @returns iprt status code.
525 * @param pszPath Path to the file system object.
526 * @param pUid Where to store the owner user id. NULL is ok.
527 * @param pGid Where to store the group id. NULL is ok.
528 *
529 * @remark This is wrapper around RTPathQueryInfo() and exists to complement RTPathGetOwner().
530 */
531RTR3DECL(int) RTPathGetOwner(const char *pszPath, uint32_t *pUid, uint32_t *pGid);
532
533
534/** @name RTPathRename, RTDirRename & RTFileRename flags.
535 * @{ */
536/** This will replace attempt any target which isn't a directory. */
537#define RTPATHRENAME_FLAGS_REPLACE RT_BIT(0)
538/** @} */
539
540/**
541 * Renames a path within a filesystem.
542 *
543 * @returns IPRT status code.
544 * @param pszSrc The source path.
545 * @param pszDst The destination path.
546 * @param fRename Rename flags, RTPATHRENAME_FLAGS_*.
547 */
548RTR3DECL(int) RTPathRename(const char *pszSrc, const char *pszDst, unsigned fRename);
549
550#endif /* IN_RING3 */
551
552/** @} */
553
554__END_DECLS
555
556#endif
557
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