VirtualBox

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

Last change on this file since 4168 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

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