VirtualBox

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

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

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