1 | /** @file
|
---|
2 | * InnoTek Portable Runtime - Path Manipulation.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006 InnoTek Systemberatung 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 | * Get the real path (no symlinks, no . or .. components), must exist.
|
---|
113 | *
|
---|
114 | * @returns iprt status code.
|
---|
115 | * @param pszPath The path to resolve.
|
---|
116 | * @param pszRealPath Where to store the real path.
|
---|
117 | * @param cchRealPath Size of the buffer.
|
---|
118 | */
|
---|
119 | RTDECL(int) RTPathReal(const char *pszPath, char *pszRealPath, unsigned cchRealPath);
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Same as RTPathReal only the result is RTStrDup()'ed.
|
---|
123 | *
|
---|
124 | * @returns Pointer to real path. Use RTStrFree() to free this string.
|
---|
125 | * @returns NULL if RTPathReal() or RTStrDup() fails.
|
---|
126 | * @param pszPath The path to resolve.
|
---|
127 | */
|
---|
128 | RTDECL(char *) RTPathRealDup(const char *pszPath);
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Get the absolute path (no symlinks, no . or .. components), doesn't have to exist.
|
---|
132 | *
|
---|
133 | * @returns iprt status code.
|
---|
134 | * @param pszPath The path to resolve.
|
---|
135 | * @param pszAbsPath Where to store the absolute path.
|
---|
136 | * @param cchAbsPath Size of the buffer.
|
---|
137 | */
|
---|
138 | RTDECL(int) RTPathAbs(const char *pszPath, char *pszAbsPath, unsigned cchAbsPath);
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Same as RTPathAbs only the result is RTStrDup()'ed.
|
---|
142 | *
|
---|
143 | * @returns Pointer to the absolute path. Use RTStrFree() to free this string.
|
---|
144 | * @returns NULL if RTPathAbs() or RTStrDup() fails.
|
---|
145 | * @param pszPath The path to resolve.
|
---|
146 | */
|
---|
147 | RTDECL(char *) RTPathAbsDup(const char *pszPath);
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Get the absolute path (no symlinks, no . or .. components), assuming the
|
---|
151 | * given base path as the current directory. The resulting path doesn't have
|
---|
152 | * to exist.
|
---|
153 | *
|
---|
154 | * @returns iprt status code.
|
---|
155 | * @param pszBase The base path to act like a current directory.
|
---|
156 | * When NULL, the actual cwd is used (i.e. the call
|
---|
157 | * is equivalent to RTPathAbs(pszPath, ...).
|
---|
158 | * @param pszPath The path to resolve.
|
---|
159 | * @param pszAbsPath Where to store the absolute path.
|
---|
160 | * @param cchAbsPath Size of the buffer.
|
---|
161 | */
|
---|
162 | RTDECL(int) RTPathAbsEx(const char *pszBase, const char *pszPath, char *pszAbsPath, unsigned cchAbsPath);
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Same as RTPathAbsEx only the result is RTStrDup()'ed.
|
---|
166 | *
|
---|
167 | * @returns Pointer to the absolute path. Use RTStrFree() to free this string.
|
---|
168 | * @returns NULL if RTPathAbsEx() or RTStrDup() fails.
|
---|
169 | * @param pszBase The base path to act like a current directory.
|
---|
170 | * When NULL, the actual cwd is used (i.e. the call
|
---|
171 | * is equivalent to RTPathAbs(pszPath, ...).
|
---|
172 | * @param pszPath The path to resolve.
|
---|
173 | */
|
---|
174 | RTDECL(char *) RTPathAbsExDup(const char *pszBase, const char *pszPath);
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Strips the filename from a path.
|
---|
178 | *
|
---|
179 | * @param pszPath Path which filename should be stripped.
|
---|
180 | * If only filename in the string a '.' will be returned.
|
---|
181 | */
|
---|
182 | RTDECL(void) RTPathStripFilename(char *pszPath);
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Strips the extension from a path.
|
---|
186 | *
|
---|
187 | * @param pszPath Path which extension should be stripped.
|
---|
188 | */
|
---|
189 | RTDECL(void) RTPathStripExt(char *pszPath);
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Strips the trailing slashes of a path name.
|
---|
193 | *
|
---|
194 | * @param pszPath Path to strip.
|
---|
195 | */
|
---|
196 | RTDECL(void) RTPathStripTrailingSlash(char *pszPath);
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Finds the filename in a path.
|
---|
200 | *
|
---|
201 | * @returns Pointer to filename within pszPath.
|
---|
202 | * @returns NULL if no filename (i.e. empty string or ends with a slash).
|
---|
203 | * @param pszPath Path to find filename in.
|
---|
204 | */
|
---|
205 | RTDECL(char *) RTPathFilename(const char *pszPath);
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Finds the extension part of in a path.
|
---|
209 | *
|
---|
210 | * @returns Pointer to extension within pszPath.
|
---|
211 | * @returns NULL if no extension.
|
---|
212 | * @param pszPath Path to find extension in.
|
---|
213 | */
|
---|
214 | RTDECL(char *) RTPathExt(const char *pszPath);
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Checks if a path have an extension.
|
---|
218 | *
|
---|
219 | * @returns true if extension present.
|
---|
220 | * @returns false if no extension.
|
---|
221 | * @param pszPath Path to check.
|
---|
222 | */
|
---|
223 | RTDECL(bool) RTPathHaveExt(const char *pszPath);
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Checks if a path includes more than a filename.
|
---|
227 | *
|
---|
228 | * @returns true if path present.
|
---|
229 | * @returns false if no path.
|
---|
230 | * @param pszPath Path to check.
|
---|
231 | */
|
---|
232 | RTDECL(bool) RTPathHavePath(const char *pszPath);
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Compares two paths.
|
---|
236 | *
|
---|
237 | * The comparison takes platform-dependent details into account,
|
---|
238 | * such as:
|
---|
239 | * <ul>
|
---|
240 | * <li>On DOS-like platforms, both |\| and |/| separator chars are considered
|
---|
241 | * to be equal.
|
---|
242 | * <li>On platforms with case-insensitive file systems, mismatching characters
|
---|
243 | * are uppercased and compared again.
|
---|
244 | * </ul>
|
---|
245 | *
|
---|
246 | * File system details are currently ignored. This means that you won't get
|
---|
247 | * case-insentive compares on unix systems when a path goes into a case-insensitive
|
---|
248 | * filesystem like FAT, HPFS, HFS, NTFS, JFS, or similar. For NT, OS/2 and similar
|
---|
249 | * you'll won't get case-sensitve compares on a case-sensitive file system.
|
---|
250 | *
|
---|
251 | * @param pszPath1 Path to compare (must be an absolute path).
|
---|
252 | * @param pszPath2 Path to compare (must be an absolute path).
|
---|
253 | *
|
---|
254 | * @returns < 0 if the first path less than the second path.
|
---|
255 | * @returns 0 if the first path identical to the second path.
|
---|
256 | * @returns > 0 if the first path greater than the second path.
|
---|
257 | */
|
---|
258 | RTDECL(int) RTPathCompare(const char *pszPath1, const char *pszPath2);
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Checks if a path starts with the given parent path.
|
---|
262 | *
|
---|
263 | * This means that either the path and the parent path matches completely, or that
|
---|
264 | * the path is to some file or directory residing in the tree given by the parent
|
---|
265 | * directory.
|
---|
266 | *
|
---|
267 | * The path comparison takes platform-dependent details into account,
|
---|
268 | * see RTPathCompare() for details.
|
---|
269 | *
|
---|
270 | * @param pszPath Path to check, must be an absolute path.
|
---|
271 | * @param pszParentPath Parent path, must be an absolute path.
|
---|
272 | * No trailing directory slash!
|
---|
273 | *
|
---|
274 | * @returns |true| when \a pszPath starts with \a pszParentPath (or when they
|
---|
275 | * are identical), or |false| otherwise.
|
---|
276 | *
|
---|
277 | * @remark This API doesn't currently handle root directory compares in a manner
|
---|
278 | * consistant with the other APIs. RTPathStartsWith(pszSomePath, "/") will
|
---|
279 | * not work if pszSomePath isn't "/".
|
---|
280 | */
|
---|
281 | RTDECL(bool) RTPathStartsWith(const char *pszPath, const char *pszParentPath);
|
---|
282 |
|
---|
283 |
|
---|
284 | #ifdef IN_RING3
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Gets the program path.
|
---|
288 | *
|
---|
289 | * @returns iprt status code.
|
---|
290 | * @param pszPath Buffer where to store the path.
|
---|
291 | * @param cchPath Buffer size in bytes.
|
---|
292 | */
|
---|
293 | RTDECL(int) RTPathProgram(char *pszPath, unsigned cchPath);
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * Gets the user home directory.
|
---|
297 | *
|
---|
298 | * @returns iprt status code.
|
---|
299 | * @param pszPath Buffer where to store the path.
|
---|
300 | * @param cchPath Buffer size in bytes.
|
---|
301 | */
|
---|
302 | RTDECL(int) RTPathUserHome(char *pszPath, unsigned cchPath);
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Query information about a file system object.
|
---|
306 | *
|
---|
307 | * This API will not resolve symbolic links in the last component (just
|
---|
308 | * like unix lstat()).
|
---|
309 | *
|
---|
310 | * @returns VINF_SUCCESS if the object exists, information returned.
|
---|
311 | * @returns VERR_PATH_NOT_FOUND if any but the last component in the specified
|
---|
312 | * path was not found or was not a directory.
|
---|
313 | * @returns VERR_FILE_NOT_FOUND if the object does not exist (but path to the
|
---|
314 | * parent directory exists).
|
---|
315 | * @returns some other iprt status code.
|
---|
316 | *
|
---|
317 | * @param pszPath Path to the file system object.
|
---|
318 | * @param pObjInfo Object information structure to be filled on successful return.
|
---|
319 | * @param enmAdditionalAttribs
|
---|
320 | * Which set of additional attributes to request.
|
---|
321 | * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
|
---|
322 | */
|
---|
323 | RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs);
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Changes the mode flags of a file system object.
|
---|
327 | *
|
---|
328 | * The API requires at least one of the mode flag sets (Unix/Dos) to
|
---|
329 | * be set. The type is ignored.
|
---|
330 | *
|
---|
331 | * This API will resolve symbolic links in the last component since
|
---|
332 | * mode isn't important for symbolic links.
|
---|
333 | *
|
---|
334 | * @returns iprt status code.
|
---|
335 | * @param pszPath Path to the file system object.
|
---|
336 | * @param fMode The new file mode, see @ref grp_rt_fs for details.
|
---|
337 | */
|
---|
338 | RTR3DECL(int) RTPathSetMode(const char *pszPath, RTFMODE fMode);
|
---|
339 |
|
---|
340 | /**
|
---|
341 | * Gets the mode flags of a file system object.
|
---|
342 | *
|
---|
343 | * @returns iprt status code.
|
---|
344 | * @param pszPath Path to the file system object.
|
---|
345 | * @param pfMode Where to store the file mode, see @ref grp_rt_fs for details.
|
---|
346 | *
|
---|
347 | * @remark This is wrapper around RTPathReal() + RTPathQueryInfo()
|
---|
348 | * and exists to complement RTPathSetMode().
|
---|
349 | */
|
---|
350 | RTR3DECL(int) RTPathGetMode(const char *pszPath, PRTFMODE pfMode);
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * Changes one or more of the timestamps associated of file system object.
|
---|
354 | *
|
---|
355 | * This API will not resolve symbolic links in the last component (just
|
---|
356 | * like unix lutimes()).
|
---|
357 | *
|
---|
358 | * @returns iprt status code.
|
---|
359 | * @param pszPath Path to the file system object.
|
---|
360 | * @param pAccessTime Pointer to the new access time.
|
---|
361 | * @param pModificationTime Pointer to the new modifcation time.
|
---|
362 | * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
|
---|
363 | * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
|
---|
364 | *
|
---|
365 | * @remark The file system might not implement all these time attributes,
|
---|
366 | * the API will ignore the ones which aren't supported.
|
---|
367 | *
|
---|
368 | * @remark The file system might not implement the time resolution
|
---|
369 | * employed by this interface, the time will be chopped to fit.
|
---|
370 | *
|
---|
371 | * @remark The file system may update the change time even if it's
|
---|
372 | * not specified.
|
---|
373 | *
|
---|
374 | * @remark POSIX can only set Access & Modification and will always set both.
|
---|
375 | */
|
---|
376 | RTR3DECL(int) RTPathSetTimes(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
|
---|
377 | PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime);
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Gets one or more of the timestamps associated of file system object.
|
---|
381 | *
|
---|
382 | * @returns iprt status code.
|
---|
383 | * @param pszPath Path to the file system object.
|
---|
384 | * @param pAccessTime Where to store the access time. NULL is ok.
|
---|
385 | * @param pModificationTime Where to store the modifcation time. NULL is ok.
|
---|
386 | * @param pChangeTime Where to store the change time. NULL is ok.
|
---|
387 | * @param pBirthTime Where to store the creation time. NULL is ok.
|
---|
388 | *
|
---|
389 | * @remark This is wrapper around RTPathQueryInfo() and exists to complement RTPathSetTimes().
|
---|
390 | */
|
---|
391 | RTR3DECL(int) RTPathGetTimes(const char *pszPath, PRTTIMESPEC pAccessTime, PRTTIMESPEC pModificationTime,
|
---|
392 | PRTTIMESPEC pChangeTime, PRTTIMESPEC pBirthTime);
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Changes the owner and/or group of a file system object.
|
---|
396 | *
|
---|
397 | * This API will not resolve symbolic links in the last component (just
|
---|
398 | * like unix lchown()).
|
---|
399 | *
|
---|
400 | * @returns iprt status code.
|
---|
401 | * @param pszPath Path to the file system object.
|
---|
402 | * @param uid The new file owner user id. Use -1 (or ~0) to leave this unchanged.
|
---|
403 | * @param gid The new group id. Use -1 (or ~0) to leave this unchanged.
|
---|
404 | */
|
---|
405 | RTR3DECL(int) RTPathSetOwner(const char *pszPath, uint32_t uid, uint32_t gid);
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Gets the owner and/or group of a file system object.
|
---|
409 | *
|
---|
410 | * @returns iprt status code.
|
---|
411 | * @param pszPath Path to the file system object.
|
---|
412 | * @param pUid Where to store the owner user id. NULL is ok.
|
---|
413 | * @param pGid Where to store the group id. NULL is ok.
|
---|
414 | *
|
---|
415 | * @remark This is wrapper around RTPathQueryInfo() and exists to complement RTPathGetOwner().
|
---|
416 | */
|
---|
417 | RTR3DECL(int) RTPathGetOwner(const char *pszPath, uint32_t *pUid, uint32_t *pGid);
|
---|
418 |
|
---|
419 |
|
---|
420 | /** @name RTPathRename, RTDirRename & RTFileRename flags.
|
---|
421 | * @{ */
|
---|
422 | /** This will replace attempt any target which isn't a directory. */
|
---|
423 | #define RTPATHRENAME_FLAGS_REPLACE BIT(0)
|
---|
424 | /** @} */
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Renames a path within a filesystem.
|
---|
428 | *
|
---|
429 | * @returns IPRT status code.
|
---|
430 | * @param pszSrc The source path.
|
---|
431 | * @param pszDst The destination path.
|
---|
432 | * @param fRename Rename flags, RTPATHRENAME_FLAGS_*.
|
---|
433 | */
|
---|
434 | RTR3DECL(int) RTPathRename(const char *pszSrc, const char *pszDst, unsigned fRename);
|
---|
435 |
|
---|
436 | #endif /* IN_RING3 */
|
---|
437 |
|
---|
438 | /** @} */
|
---|
439 |
|
---|
440 | __END_DECLS
|
---|
441 |
|
---|
442 | #endif
|
---|
443 |
|
---|