VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/generic/dirrel-r3-generic.cpp@ 69705

Last change on this file since 69705 was 69705, checked in by vboxsync, 8 years ago

IPRT: VFS and NT path handling fixes.

  • Rewrote RTDirQueryInfo for NT. When RTDirOpen* now opens directories, it will request read-attribute access in additions to listing.
  • Major adjustment of the VFS path parser. It now accepts both slashes and will deal differently with '..' in operations on directories.
  • Implemented native RTDirRelPathQueryInfo for NT.
  • NT directory object (NT namespace objects, not file system dirs) fixes for NT specific RTDirRel APIs.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.4 KB
Line 
1/* $Id: dirrel-r3-generic.cpp 69705 2017-11-15 16:42:59Z vboxsync $ */
2/** @file
3 * IPRT - Directory relative base APIs, generic implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_DIR
32#include <iprt/dir.h>
33#include "internal/iprt.h"
34
35#include <iprt/assert.h>
36#include <iprt/file.h>
37#include <iprt/err.h>
38#include <iprt/path.h>
39#include <iprt/string.h>
40#include <iprt/symlink.h>
41#define RTDIR_AGNOSTIC
42#include "internal/dir.h"
43
44
45
46/**
47 * Helper that builds a full path for a directory relative path.
48 *
49 * @returns IPRT status code.
50 * @param pThis The directory.
51 * @param pszPathDst The destination buffer.
52 * @param cbPathDst The size of the destination buffer.
53 * @param pszRelPath The relative path.
54 */
55static int rtDirRelBuildFullPath(PRTDIR pThis, char *pszPathDst, size_t cbPathDst, const char *pszRelPath)
56{
57 AssertMsgReturn(!RTPathStartsWithRoot(pszRelPath), ("pszRelPath='%s'\n", pszRelPath), VERR_PATH_IS_NOT_RELATIVE);
58
59 /*
60 * Let's hope we can avoid checking for ascension.
61 *
62 * Note! We don't take symbolic links into account here. That can be
63 * done later if desired.
64 */
65 if ( !(pThis->fFlags & RTDIR_F_DENY_ASCENT)
66 || strstr(pszRelPath, "..") == NULL)
67 {
68 size_t const cchRelPath = strlen(pszRelPath);
69 size_t const cchDirPath = pThis->cchPath;
70 if (cchDirPath + cchRelPath < cbPathDst)
71 {
72 memcpy(pszPathDst, pThis->pszPath, cchDirPath);
73 memcpy(&pszPathDst[cchDirPath], pszRelPath, cchRelPath);
74 pszPathDst[cchDirPath + cchRelPath] = '\0';
75 return VINF_SUCCESS;
76 }
77 return VERR_FILENAME_TOO_LONG;
78 }
79
80 /*
81 * Calc the absolute path using the directory as a base, then check if the result
82 * still starts with the full directory path.
83 *
84 * This ASSUMES that pThis->pszPath is an absolute path.
85 */
86 int rc = RTPathAbsEx(pThis->pszPath, pszRelPath, pszPathDst, cbPathDst);
87 if (RT_SUCCESS(rc))
88 {
89 if (RTPathStartsWith(pszPathDst, pThis->pszPath))
90 return VINF_SUCCESS;
91 return VERR_PATH_NOT_FOUND;
92 }
93 return rc;
94}
95
96
97/*
98 *
99 *
100 * RTFile stuff.
101 * RTFile stuff.
102 * RTFile stuff.
103 *
104 *
105 */
106
107
108
109
110/**
111 * Open a file relative to @a hDir.
112 *
113 * @returns IPRT status code.
114 * @param hDir The directory to open relative to.
115 * @param pszRelFilename The relative path to the file.
116 * @param fOpen Open flags, i.e a combination of the RTFILE_O_XXX
117 * defines. The ACCESS, ACTION and DENY flags are
118 * mandatory!
119 * @param phFile Where to store the handle to the opened file.
120 *
121 * @sa RTFileOpen
122 */
123RTDECL(int) RTDirRelFileOpen(PRTDIR hDir, const char *pszRelFilename, uint64_t fOpen, PRTFILE phFile)
124{
125 PRTDIR pThis = hDir;
126 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
127 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
128
129 char szPath[RTPATH_MAX];
130 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelFilename);
131 if (RT_SUCCESS(rc))
132 rc = RTFileOpen(phFile, szPath, fOpen);
133 return rc;
134}
135
136
137
138/*
139 *
140 *
141 * RTDir stuff.
142 * RTDir stuff.
143 * RTDir stuff.
144 *
145 *
146 */
147
148
149
150/**
151 * Opens a directory relative to @a hDir.
152 *
153 * @returns IPRT status code.
154 * @param hDir The directory to open relative to.
155 * @param pszDir The relative path to the directory to open.
156 * @param phDir Where to store the directory handle.
157 *
158 * @sa RTDirOpen
159 */
160RTDECL(int) RTDirRelDirOpen(PRTDIR hDir, const char *pszDir, PRTDIR *phDir)
161{
162 PRTDIR pThis = hDir;
163 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
164 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
165
166 char szPath[RTPATH_MAX];
167 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszDir);
168 if (RT_SUCCESS(rc))
169 rc = RTDirOpen(phDir, szPath);
170 return rc;
171
172}
173
174
175/**
176 * Opens a directory relative to @a hDir, with flags and optional filtering.
177 *
178 * @returns IPRT status code.
179 * @param hDir The directory to open relative to.
180 * @param pszDirAndFilter The relative path to the directory to search, this
181 * must include wildcards.
182 * @param enmFilter The kind of filter to apply. Setting this to
183 * RTDIRFILTER_NONE makes this function behave like
184 * RTDirOpen.
185 * @param fFlags Open flags, RTDIR_F_XXX.
186 * @param phDir Where to store the directory handle.
187 *
188 * @sa RTDirOpenFiltered
189 */
190RTDECL(int) RTDirRelDirOpenFiltered(PRTDIR hDir, const char *pszDirAndFilter, RTDIRFILTER enmFilter,
191 uint32_t fFlags, PRTDIR *phDir)
192{
193 PRTDIR pThis = hDir;
194 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
195 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
196
197 char szPath[RTPATH_MAX];
198 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszDirAndFilter);
199 if (RT_SUCCESS(rc))
200 rc = RTDirOpenFiltered(phDir, szPath, enmFilter, fFlags);
201 return rc;
202}
203
204
205/**
206 * Creates a directory relative to @a hDir.
207 *
208 * @returns IPRT status code.
209 * @param hDir The directory @a pszRelPath is relative to.
210 * @param pszRelPath The relative path to the directory to create.
211 * @param fMode The mode of the new directory.
212 * @param fCreate Create flags, RTDIRCREATE_FLAGS_XXX.
213 *
214 * @sa RTDirCreate
215 */
216RTDECL(int) RTDirRelDirCreate(PRTDIR hDir, const char *pszRelPath, RTFMODE fMode, uint32_t fCreate)
217{
218 PRTDIR pThis = hDir;
219 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
220 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
221
222 char szPath[RTPATH_MAX];
223 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
224 if (RT_SUCCESS(rc))
225 rc = RTDirCreate(szPath, fMode, fCreate);
226 return rc;
227}
228
229
230/**
231 * Removes a directory relative to @a hDir if empty.
232 *
233 * @returns IPRT status code.
234 * @param hDir The directory @a pszRelPath is relative to.
235 * @param pszRelPath The relative path to the directory to remove.
236 *
237 * @sa RTDirRemove
238 */
239RTDECL(int) RTDirRelDirRemove(PRTDIR hDir, const char *pszRelPath)
240{
241 PRTDIR pThis = hDir;
242 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
243 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
244
245 char szPath[RTPATH_MAX];
246 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
247 if (RT_SUCCESS(rc))
248 rc = RTDirRemove(szPath);
249 return rc;
250}
251
252
253/*
254 *
255 * RTPath stuff.
256 * RTPath stuff.
257 * RTPath stuff.
258 *
259 *
260 */
261
262
263/**
264 * Query information about a file system object relative to @a hDir.
265 *
266 * @returns IPRT status code.
267 * @retval VINF_SUCCESS if the object exists, information returned.
268 * @retval VERR_PATH_NOT_FOUND if any but the last component in the specified
269 * path was not found or was not a directory.
270 * @retval VERR_FILE_NOT_FOUND if the object does not exist (but path to the
271 * parent directory exists).
272 *
273 * @param hDir The directory @a pszRelPath is relative to.
274 * @param pszRelPath The relative path to the file system object.
275 * @param pObjInfo Object information structure to be filled on successful
276 * return.
277 * @param enmAddAttr Which set of additional attributes to request.
278 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
279 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
280 *
281 * @sa RTPathQueryInfoEx
282 */
283RTDECL(int) RTDirRelPathQueryInfo(PRTDIR hDir, const char *pszRelPath, PRTFSOBJINFO pObjInfo,
284 RTFSOBJATTRADD enmAddAttr, uint32_t fFlags)
285{
286 PRTDIR pThis = hDir;
287 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
288 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
289
290 char szPath[RTPATH_MAX];
291 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
292 if (RT_SUCCESS(rc))
293 rc = RTPathQueryInfoEx(szPath, pObjInfo, enmAddAttr, fFlags);
294 return rc;
295}
296
297
298/**
299 * Changes the mode flags of a file system object relative to @a hDir.
300 *
301 * The API requires at least one of the mode flag sets (Unix/Dos) to
302 * be set. The type is ignored.
303 *
304 * @returns IPRT status code.
305 * @param hDir The directory @a pszRelPath is relative to.
306 * @param pszRelPath The relative path to the file system object.
307 * @param fMode The new file mode, see @ref grp_rt_fs for details.
308 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
309 *
310 * @sa RTPathSetMode
311 */
312RTDECL(int) RTDirRelPathSetMode(PRTDIR hDir, const char *pszRelPath, RTFMODE fMode, uint32_t fFlags)
313{
314 PRTDIR pThis = hDir;
315 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
316 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
317 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_FLAGS);
318
319 char szPath[RTPATH_MAX];
320 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
321 if (RT_SUCCESS(rc))
322 {
323#ifndef RT_OS_WINDOWS
324 rc = RTPathSetMode(szPath, fMode); /** @todo fFlags is currently ignored. */
325#else
326 rc = VERR_NOT_IMPLEMENTED; /** @todo implement RTPathSetMode on windows. */
327 RT_NOREF(fMode);
328#endif
329 }
330 return rc;
331}
332
333
334/**
335 * Changes one or more of the timestamps associated of file system object
336 * relative to @a hDir.
337 *
338 * @returns IPRT status code.
339 * @param hDir The directory @a pszRelPath is relative to.
340 * @param pszRelPath The relative path to the file system object.
341 * @param pAccessTime Pointer to the new access time.
342 * @param pModificationTime Pointer to the new modification time.
343 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
344 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
345 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
346 *
347 * @remark The file system might not implement all these time attributes,
348 * the API will ignore the ones which aren't supported.
349 *
350 * @remark The file system might not implement the time resolution
351 * employed by this interface, the time will be chopped to fit.
352 *
353 * @remark The file system may update the change time even if it's
354 * not specified.
355 *
356 * @remark POSIX can only set Access & Modification and will always set both.
357 *
358 * @sa RTPathSetTimesEx
359 */
360RTDECL(int) RTDirRelPathSetTimes(PRTDIR hDir, const char *pszRelPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
361 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime, uint32_t fFlags)
362{
363 PRTDIR pThis = hDir;
364 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
365 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
366
367 char szPath[RTPATH_MAX];
368 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
369 if (RT_SUCCESS(rc))
370 rc = RTPathSetTimesEx(szPath, pAccessTime, pModificationTime, pChangeTime, pBirthTime, fFlags);
371 return rc;
372}
373
374
375/**
376 * Changes the owner and/or group of a file system object relative to @a hDir.
377 *
378 * @returns IPRT status code.
379 * @param hDir The directory @a pszRelPath is relative to.
380 * @param pszRelPath The relative path to the file system object.
381 * @param uid The new file owner user id. Pass NIL_RTUID to leave
382 * this unchanged.
383 * @param gid The new group id. Pass NIL_RTGID to leave this
384 * unchanged.
385 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
386 *
387 * @sa RTPathSetOwnerEx
388 */
389RTDECL(int) RTDirRelPathSetOwner(PRTDIR hDir, const char *pszRelPath, uint32_t uid, uint32_t gid, uint32_t fFlags)
390{
391 PRTDIR pThis = hDir;
392 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
393 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
394
395 char szPath[RTPATH_MAX];
396 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
397 if (RT_SUCCESS(rc))
398 {
399#ifndef RT_OS_WINDOWS
400 rc = RTPathSetOwnerEx(szPath, uid, gid, fFlags);
401#else
402 rc = VERR_NOT_IMPLEMENTED;
403 RT_NOREF(uid, gid, fFlags);
404#endif
405 }
406 return rc;
407}
408
409
410/**
411 * Renames a directory relative path within a filesystem.
412 *
413 * This will rename symbolic links. If RTPATHRENAME_FLAGS_REPLACE is used and
414 * pszDst is a symbolic link, it will be replaced and not its target.
415 *
416 * @returns IPRT status code.
417 * @param hDirSrc The directory the source path is relative to.
418 * @param pszSrc The source path, relative to @a hDirSrc.
419 * @param hDirDst The directory the destination path is relative to.
420 * @param pszDst The destination path, relative to @a hDirDst.
421 * @param fRename Rename flags, RTPATHRENAME_FLAGS_XXX.
422 *
423 * @sa RTPathRename
424 */
425RTDECL(int) RTDirRelPathRename(PRTDIR hDirSrc, const char *pszSrc, PRTDIR hDirDst, const char *pszDst, unsigned fRename)
426{
427 PRTDIR pThis = hDirSrc;
428 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
429 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
430
431 PRTDIR pThat = hDirDst;
432 if (pThat != pThis)
433 {
434 AssertPtrReturn(pThat, VERR_INVALID_HANDLE);
435 AssertReturn(pThat->u32Magic != RTDIR_MAGIC, VERR_INVALID_HANDLE);
436 }
437
438 char szSrcPath[RTPATH_MAX];
439 int rc = rtDirRelBuildFullPath(pThis, szSrcPath, sizeof(szSrcPath), pszSrc);
440 if (RT_SUCCESS(rc))
441 {
442 char szDstPath[RTPATH_MAX];
443 rc = rtDirRelBuildFullPath(pThis, szDstPath, sizeof(szDstPath), pszDst);
444 if (RT_SUCCESS(rc))
445 rc = RTPathRename(szSrcPath, szDstPath, fRename);
446 }
447 return rc;
448}
449
450
451/**
452 * Removes the last component of the directory relative path.
453 *
454 * @returns IPRT status code.
455 * @param hDir The directory @a pszRelPath is relative to.
456 * @param pszRelPath The relative path to the file system object.
457 * @param fUnlink Unlink flags, RTPATHUNLINK_FLAGS_XXX.
458 *
459 * @sa RTPathUnlink
460 */
461RTDECL(int) RTDirRelPathUnlink(PRTDIR hDir, const char *pszRelPath, uint32_t fUnlink)
462{
463 PRTDIR pThis = hDir;
464 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
465 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
466
467 char szPath[RTPATH_MAX];
468 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
469 if (RT_SUCCESS(rc))
470 rc = RTPathUnlink(szPath, fUnlink);
471 return rc;
472}
473
474
475/*
476 *
477 * RTSymlink stuff.
478 * RTSymlink stuff.
479 * RTSymlink stuff.
480 *
481 *
482 */
483
484
485/**
486 * Creates a symbolic link (@a pszSymlink) relative to @a hDir targeting @a
487 * pszTarget.
488 *
489 * @returns IPRT status code.
490 * @param hDir The directory @a pszSymlink is relative to.
491 * @param pszSymlink The relative path of the symbolic link.
492 * @param pszTarget The path to the symbolic link target. This is
493 * relative to @a pszSymlink or an absolute path.
494 * @param enmType The symbolic link type. For Windows compatability
495 * it is very important to set this correctly. When
496 * RTSYMLINKTYPE_UNKNOWN is used, the API will try
497 * make a guess and may attempt query information
498 * about @a pszTarget in the process.
499 * @param fCreate Create flags, RTSYMLINKCREATE_FLAGS_XXX.
500 *
501 * @sa RTSymlinkCreate
502 */
503RTDECL(int) RTDirRelSymlinkCreate(PRTDIR hDir, const char *pszSymlink, const char *pszTarget,
504 RTSYMLINKTYPE enmType, uint32_t fCreate)
505{
506 PRTDIR pThis = hDir;
507 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
508 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
509
510 char szPath[RTPATH_MAX];
511 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszSymlink);
512 if (RT_SUCCESS(rc))
513 rc = RTSymlinkCreate(szPath, pszTarget, enmType, fCreate);
514 return rc;
515}
516
517
518/**
519 * Read the symlink target relative to @a hDir.
520 *
521 * @returns IPRT status code.
522 * @retval VERR_NOT_SYMLINK if @a pszSymlink does not specify a symbolic link.
523 * @retval VERR_BUFFER_OVERFLOW if the link is larger than @a cbTarget. The
524 * buffer will contain what all we managed to read, fully terminated
525 * if @a cbTarget > 0.
526 *
527 * @param hDir The directory @a pszSymlink is relative to.
528 * @param pszSymlink The relative path to the symbolic link that should
529 * be read.
530 * @param pszTarget The target buffer.
531 * @param cbTarget The size of the target buffer.
532 * @param fRead Read flags, RTSYMLINKREAD_FLAGS_XXX.
533 *
534 * @sa RTSymlinkRead
535 */
536RTDECL(int) RTDirRelSymlinkRead(PRTDIR hDir, const char *pszSymlink, char *pszTarget, size_t cbTarget, uint32_t fRead)
537{
538 PRTDIR pThis = hDir;
539 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
540 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
541
542 char szPath[RTPATH_MAX];
543 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszSymlink);
544 if (RT_SUCCESS(rc))
545 rc = RTSymlinkRead(szPath, pszTarget, cbTarget, fRead);
546 return rc;
547}
548
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette