VirtualBox

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

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

InnoTek -> innotek: all the headers and comments.

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