VirtualBox

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

Last change on this file since 90330 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.8 KB
Line 
1/* $Id: dirrel-r3-generic.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - Directory relative base APIs, generic implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
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 (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(PRTDIRINTERNAL 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, RTPATH_STR_F_STYLE_HOST, 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(RTDIR hDir, const char *pszRelFilename, uint64_t fOpen, PRTFILE phFile)
124{
125 PRTDIRINTERNAL 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(RTDIR hDir, const char *pszDir, RTDIR *phDir)
161{
162 PRTDIRINTERNAL 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(RTDIR hDir, const char *pszDirAndFilter, RTDIRFILTER enmFilter,
191 uint32_t fFlags, RTDIR *phDir)
192{
193 PRTDIRINTERNAL 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 * @param phSubDir Where to return the handle of the created directory.
214 * Optional.
215 *
216 * @sa RTDirCreate
217 */
218RTDECL(int) RTDirRelDirCreate(RTDIR hDir, const char *pszRelPath, RTFMODE fMode, uint32_t fCreate, RTDIR *phSubDir)
219{
220 PRTDIRINTERNAL pThis = hDir;
221 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
222 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
223
224 char szPath[RTPATH_MAX];
225 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
226 if (RT_SUCCESS(rc))
227 {
228 rc = RTDirCreate(szPath, fMode, fCreate);
229 if (RT_SUCCESS(rc) && phSubDir)
230 rc = RTDirOpen(phSubDir, szPath);
231 }
232 return rc;
233}
234
235
236/**
237 * Removes a directory relative to @a hDir if empty.
238 *
239 * @returns IPRT status code.
240 * @param hDir The directory @a pszRelPath is relative to.
241 * @param pszRelPath The relative path to the directory to remove.
242 *
243 * @sa RTDirRemove
244 */
245RTDECL(int) RTDirRelDirRemove(RTDIR hDir, const char *pszRelPath)
246{
247 PRTDIRINTERNAL pThis = hDir;
248 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
249 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
250
251 char szPath[RTPATH_MAX];
252 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
253 if (RT_SUCCESS(rc))
254 rc = RTDirRemove(szPath);
255 return rc;
256}
257
258
259/*
260 *
261 * RTPath stuff.
262 * RTPath stuff.
263 * RTPath stuff.
264 *
265 *
266 */
267
268
269/**
270 * Query information about a file system object relative to @a hDir.
271 *
272 * @returns IPRT status code.
273 * @retval VINF_SUCCESS if the object exists, information returned.
274 * @retval VERR_PATH_NOT_FOUND if any but the last component in the specified
275 * path was not found or was not a directory.
276 * @retval VERR_FILE_NOT_FOUND if the object does not exist (but path to the
277 * parent directory exists).
278 *
279 * @param hDir The directory @a pszRelPath is relative to.
280 * @param pszRelPath The relative path to the file system object.
281 * @param pObjInfo Object information structure to be filled on successful
282 * return.
283 * @param enmAddAttr Which set of additional attributes to request.
284 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
285 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
286 *
287 * @sa RTPathQueryInfoEx
288 */
289RTDECL(int) RTDirRelPathQueryInfo(RTDIR hDir, const char *pszRelPath, PRTFSOBJINFO pObjInfo,
290 RTFSOBJATTRADD enmAddAttr, uint32_t fFlags)
291{
292 PRTDIRINTERNAL pThis = hDir;
293 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
294 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
295
296 char szPath[RTPATH_MAX];
297 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
298 if (RT_SUCCESS(rc))
299 rc = RTPathQueryInfoEx(szPath, pObjInfo, enmAddAttr, fFlags);
300 return rc;
301}
302
303
304/**
305 * Changes the mode flags of a file system object relative to @a hDir.
306 *
307 * The API requires at least one of the mode flag sets (Unix/Dos) to
308 * be set. The type is ignored.
309 *
310 * @returns IPRT status code.
311 * @param hDir The directory @a pszRelPath is relative to.
312 * @param pszRelPath The relative path to the file system object.
313 * @param fMode The new file mode, see @ref grp_rt_fs for details.
314 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
315 *
316 * @sa RTPathSetMode
317 */
318RTDECL(int) RTDirRelPathSetMode(RTDIR hDir, const char *pszRelPath, RTFMODE fMode, uint32_t fFlags)
319{
320 PRTDIRINTERNAL pThis = hDir;
321 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
322 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
323 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_FLAGS);
324
325 char szPath[RTPATH_MAX];
326 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
327 if (RT_SUCCESS(rc))
328 {
329#ifndef RT_OS_WINDOWS
330 rc = RTPathSetMode(szPath, fMode); /** @todo fFlags is currently ignored. */
331#else
332 rc = VERR_NOT_IMPLEMENTED; /** @todo implement RTPathSetMode on windows. */
333 RT_NOREF(fMode);
334#endif
335 }
336 return rc;
337}
338
339
340/**
341 * Changes one or more of the timestamps associated of file system object
342 * relative to @a hDir.
343 *
344 * @returns IPRT status code.
345 * @param hDir The directory @a pszRelPath is relative to.
346 * @param pszRelPath The relative path to the file system object.
347 * @param pAccessTime Pointer to the new access time.
348 * @param pModificationTime Pointer to the new modification time.
349 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
350 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
351 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
352 *
353 * @remark The file system might not implement all these time attributes,
354 * the API will ignore the ones which aren't supported.
355 *
356 * @remark The file system might not implement the time resolution
357 * employed by this interface, the time will be chopped to fit.
358 *
359 * @remark The file system may update the change time even if it's
360 * not specified.
361 *
362 * @remark POSIX can only set Access & Modification and will always set both.
363 *
364 * @sa RTPathSetTimesEx
365 */
366RTDECL(int) RTDirRelPathSetTimes(RTDIR hDir, const char *pszRelPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
367 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime, uint32_t fFlags)
368{
369 PRTDIRINTERNAL pThis = hDir;
370 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
371 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
372
373 char szPath[RTPATH_MAX];
374 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
375 if (RT_SUCCESS(rc))
376 rc = RTPathSetTimesEx(szPath, pAccessTime, pModificationTime, pChangeTime, pBirthTime, fFlags);
377 return rc;
378}
379
380
381/**
382 * Changes the owner and/or group of a file system object relative to @a hDir.
383 *
384 * @returns IPRT status code.
385 * @param hDir The directory @a pszRelPath is relative to.
386 * @param pszRelPath The relative path to the file system object.
387 * @param uid The new file owner user id. Pass NIL_RTUID to leave
388 * this unchanged.
389 * @param gid The new group id. Pass NIL_RTGID to leave this
390 * unchanged.
391 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
392 *
393 * @sa RTPathSetOwnerEx
394 */
395RTDECL(int) RTDirRelPathSetOwner(RTDIR hDir, const char *pszRelPath, uint32_t uid, uint32_t gid, uint32_t fFlags)
396{
397 PRTDIRINTERNAL pThis = hDir;
398 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
399 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
400
401 char szPath[RTPATH_MAX];
402 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
403 if (RT_SUCCESS(rc))
404 {
405#ifndef RT_OS_WINDOWS
406 rc = RTPathSetOwnerEx(szPath, uid, gid, fFlags);
407#else
408 rc = VERR_NOT_IMPLEMENTED;
409 RT_NOREF(uid, gid, fFlags);
410#endif
411 }
412 return rc;
413}
414
415
416/**
417 * Renames a directory relative path within a filesystem.
418 *
419 * This will rename symbolic links. If RTPATHRENAME_FLAGS_REPLACE is used and
420 * pszDst is a symbolic link, it will be replaced and not its target.
421 *
422 * @returns IPRT status code.
423 * @param hDirSrc The directory the source path is relative to.
424 * @param pszSrc The source path, relative to @a hDirSrc.
425 * @param hDirDst The directory the destination path is relative to.
426 * @param pszDst The destination path, relative to @a hDirDst.
427 * @param fRename Rename flags, RTPATHRENAME_FLAGS_XXX.
428 *
429 * @sa RTPathRename
430 */
431RTDECL(int) RTDirRelPathRename(RTDIR hDirSrc, const char *pszSrc, RTDIR hDirDst, const char *pszDst, unsigned fRename)
432{
433 PRTDIRINTERNAL pThis = hDirSrc;
434 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
435 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
436
437 PRTDIRINTERNAL pThat = hDirDst;
438 if (pThat != pThis)
439 {
440 AssertPtrReturn(pThat, VERR_INVALID_HANDLE);
441 AssertReturn(pThat->u32Magic != RTDIR_MAGIC, VERR_INVALID_HANDLE);
442 }
443
444 char szSrcPath[RTPATH_MAX];
445 int rc = rtDirRelBuildFullPath(pThis, szSrcPath, sizeof(szSrcPath), pszSrc);
446 if (RT_SUCCESS(rc))
447 {
448 char szDstPath[RTPATH_MAX];
449 rc = rtDirRelBuildFullPath(pThis, szDstPath, sizeof(szDstPath), pszDst);
450 if (RT_SUCCESS(rc))
451 rc = RTPathRename(szSrcPath, szDstPath, fRename);
452 }
453 return rc;
454}
455
456
457/**
458 * Removes the last component of the directory relative path.
459 *
460 * @returns IPRT status code.
461 * @param hDir The directory @a pszRelPath is relative to.
462 * @param pszRelPath The relative path to the file system object.
463 * @param fUnlink Unlink flags, RTPATHUNLINK_FLAGS_XXX.
464 *
465 * @sa RTPathUnlink
466 */
467RTDECL(int) RTDirRelPathUnlink(RTDIR hDir, const char *pszRelPath, uint32_t fUnlink)
468{
469 PRTDIRINTERNAL pThis = hDir;
470 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
471 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
472
473 char szPath[RTPATH_MAX];
474 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
475 if (RT_SUCCESS(rc))
476 rc = RTPathUnlink(szPath, fUnlink);
477 return rc;
478}
479
480
481/*
482 *
483 * RTSymlink stuff.
484 * RTSymlink stuff.
485 * RTSymlink stuff.
486 *
487 *
488 */
489
490
491/**
492 * Creates a symbolic link (@a pszSymlink) relative to @a hDir targeting @a
493 * pszTarget.
494 *
495 * @returns IPRT status code.
496 * @param hDir The directory @a pszSymlink is relative to.
497 * @param pszSymlink The relative path of the symbolic link.
498 * @param pszTarget The path to the symbolic link target. This is
499 * relative to @a pszSymlink or an absolute path.
500 * @param enmType The symbolic link type. For Windows compatability
501 * it is very important to set this correctly. When
502 * RTSYMLINKTYPE_UNKNOWN is used, the API will try
503 * make a guess and may attempt query information
504 * about @a pszTarget in the process.
505 * @param fCreate Create flags, RTSYMLINKCREATE_FLAGS_XXX.
506 *
507 * @sa RTSymlinkCreate
508 */
509RTDECL(int) RTDirRelSymlinkCreate(RTDIR hDir, const char *pszSymlink, const char *pszTarget,
510 RTSYMLINKTYPE enmType, uint32_t fCreate)
511{
512 PRTDIRINTERNAL pThis = hDir;
513 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
514 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
515
516 char szPath[RTPATH_MAX];
517 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszSymlink);
518 if (RT_SUCCESS(rc))
519 rc = RTSymlinkCreate(szPath, pszTarget, enmType, fCreate);
520 return rc;
521}
522
523
524/**
525 * Read the symlink target relative to @a hDir.
526 *
527 * @returns IPRT status code.
528 * @retval VERR_NOT_SYMLINK if @a pszSymlink does not specify a symbolic link.
529 * @retval VERR_BUFFER_OVERFLOW if the link is larger than @a cbTarget. The
530 * buffer will contain what all we managed to read, fully terminated
531 * if @a cbTarget > 0.
532 *
533 * @param hDir The directory @a pszSymlink is relative to.
534 * @param pszSymlink The relative path to the symbolic link that should
535 * be read.
536 * @param pszTarget The target buffer.
537 * @param cbTarget The size of the target buffer.
538 * @param fRead Read flags, RTSYMLINKREAD_FLAGS_XXX.
539 *
540 * @sa RTSymlinkRead
541 */
542RTDECL(int) RTDirRelSymlinkRead(RTDIR hDir, const char *pszSymlink, char *pszTarget, size_t cbTarget, uint32_t fRead)
543{
544 PRTDIRINTERNAL pThis = hDir;
545 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
546 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
547
548 char szPath[RTPATH_MAX];
549 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszSymlink);
550 if (RT_SUCCESS(rc))
551 rc = RTSymlinkRead(szPath, pszTarget, cbTarget, fRead);
552 return rc;
553}
554
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