VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/nt/dirrel-r3-nt.cpp@ 69753

最後變更 在這個檔案從69753是 69753,由 vboxsync 提交於 7 年 前

iprt/dir: Morphing PRTDIR into a handle named RTDIR. (Been wanting to correct this for years. Don't know why I makde it a pointer rather than an abstrct handle like everything else.)

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 24.1 KB
 
1/* $Id: dirrel-r3-nt.cpp 69753 2017-11-19 14:27:58Z vboxsync $ */
2/** @file
3 * IPRT - Directory relative base APIs, NT 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.alldomusa.eu.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-r3-nt.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#include "internal/dir.h"
42#include "internal/file.h"
43#include "internal/fs.h"
44#include "internal/path.h"
45
46
47/*********************************************************************************************************************************
48* Defined Constants And Macros *
49*********************************************************************************************************************************/
50/** Getst the RTNTPATHRELATIVEASCENT value for RTNtPathRelativeFromUtf8. */
51#define RTDIRREL_NT_GET_ASCENT(a_pThis) \
52 ( !(pThis->fFlags & RTDIR_F_DENY_ASCENT) ? kRTNtPathRelativeAscent_Allow : kRTNtPathRelativeAscent_Fail )
53
54
55
56/**
57 * Helper that builds a full path for a directory relative path.
58 *
59 * @returns IPRT status code.
60 * @param pThis The directory.
61 * @param pszPathDst The destination buffer.
62 * @param cbPathDst The size of the destination buffer.
63 * @param pszRelPath The relative path.
64 */
65static int rtDirRelBuildFullPath(PRTDIRINTERNAL pThis, char *pszPathDst, size_t cbPathDst, const char *pszRelPath)
66{
67 AssertMsgReturn(!RTPathStartsWithRoot(pszRelPath), ("pszRelPath='%s'\n", pszRelPath), VERR_PATH_IS_NOT_RELATIVE);
68
69 /*
70 * Let's hope we can avoid checking for ascension.
71 *
72 * Note! We don't take symbolic links into account here. That can be
73 * done later if desired.
74 */
75 if ( !(pThis->fFlags & RTDIR_F_DENY_ASCENT)
76 || strstr(pszRelPath, "..") == NULL)
77 {
78 size_t const cchRelPath = strlen(pszRelPath);
79 size_t const cchDirPath = pThis->cchPath;
80 if (cchDirPath + cchRelPath < cbPathDst)
81 {
82 memcpy(pszPathDst, pThis->pszPath, cchDirPath);
83 memcpy(&pszPathDst[cchDirPath], pszRelPath, cchRelPath);
84 pszPathDst[cchDirPath + cchRelPath] = '\0';
85 return VINF_SUCCESS;
86 }
87 return VERR_FILENAME_TOO_LONG;
88 }
89
90 /*
91 * Calc the absolute path using the directory as a base, then check if the result
92 * still starts with the full directory path.
93 *
94 * This ASSUMES that pThis->pszPath is an absolute path.
95 */
96 int rc = RTPathAbsEx(pThis->pszPath, pszRelPath, pszPathDst, cbPathDst);
97 if (RT_SUCCESS(rc))
98 {
99 if (RTPathStartsWith(pszPathDst, pThis->pszPath))
100 return VINF_SUCCESS;
101 return VERR_PATH_NOT_FOUND;
102 }
103 return rc;
104}
105
106
107/*
108 *
109 *
110 * RTFile stuff.
111 * RTFile stuff.
112 * RTFile stuff.
113 *
114 *
115 */
116
117
118RTDECL(int) RTDirRelFileOpen(RTDIR hDir, const char *pszRelFilename, uint64_t fOpen, PRTFILE phFile)
119{
120 PRTDIRINTERNAL pThis = hDir;
121 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
122 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
123
124 /*
125 * Validate and convert flags.
126 */
127 uint32_t fDesiredAccess;
128 uint32_t fObjAttribs;
129 uint32_t fFileAttribs;
130 uint32_t fShareAccess;
131 uint32_t fCreateDisposition;
132 uint32_t fCreateOptions;
133 int rc = rtFileNtValidateAndConvertFlags(fOpen, &fDesiredAccess, &fObjAttribs, &fFileAttribs,
134 &fShareAccess, &fCreateDisposition, &fCreateOptions);
135 if (RT_SUCCESS(rc))
136 {
137 /*
138 * Convert and normalize the path.
139 */
140 UNICODE_STRING NtName;
141 HANDLE hRoot = pThis->hDir;
142 rc = RTNtPathRelativeFromUtf8(&NtName, &hRoot, pszRelFilename, RTDIRREL_NT_GET_ASCENT(pThis),
143 pThis->enmInfoClass == FileMaximumInformation);
144 if (RT_SUCCESS(rc))
145 {
146 HANDLE hFile = RTNT_INVALID_HANDLE_VALUE;
147 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
148 OBJECT_ATTRIBUTES ObjAttr;
149 InitializeObjectAttributes(&ObjAttr, &NtName, fObjAttribs, hRoot, NULL /*pSecDesc*/);
150
151 NTSTATUS rcNt = NtCreateFile(&hFile,
152 fDesiredAccess,
153 &ObjAttr,
154 &Ios,
155 NULL /* AllocationSize*/,
156 fFileAttribs,
157 fShareAccess,
158 fCreateDisposition,
159 fCreateOptions,
160 NULL /*EaBuffer*/,
161 0 /*EaLength*/);
162 if (NT_SUCCESS(rcNt))
163 {
164 rc = RTFileFromNative(phFile, (uintptr_t)hFile);
165 if (RT_FAILURE(rc))
166 NtClose(hFile);
167 }
168 else
169 rc = RTErrConvertFromNtStatus(rcNt);
170 RTNtPathFree(&NtName, NULL);
171 }
172 }
173 return rc;
174}
175
176
177
178/*
179 *
180 *
181 * RTDir stuff.
182 * RTDir stuff.
183 * RTDir stuff.
184 *
185 *
186 */
187
188
189
190RTDECL(int) RTDirRelDirOpen(RTDIR hDir, const char *pszDir, RTDIR *phDir)
191{
192 return RTDirRelDirOpenFiltered(hDir, pszDir, RTDIRFILTER_NONE, 0 /*fFlags*/, phDir);
193}
194
195
196RTDECL(int) RTDirRelDirOpenFiltered(RTDIR hDir, const char *pszDirAndFilter, RTDIRFILTER enmFilter,
197 uint32_t fFlags, RTDIR *phDir)
198{
199 PRTDIRINTERNAL pThis = hDir;
200 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
201 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
202
203 /*
204 * Convert and normalize the path.
205 */
206 UNICODE_STRING NtName;
207 HANDLE hRoot = pThis->hDir;
208 int rc = RTNtPathRelativeFromUtf8(&NtName, &hRoot, pszDirAndFilter, RTDIRREL_NT_GET_ASCENT(pThis),
209 pThis->enmInfoClass == FileMaximumInformation);
210 if (RT_SUCCESS(rc))
211 {
212 rc = rtDirOpenRelativeOrHandle(phDir, pszDirAndFilter, enmFilter, fFlags, (uintptr_t)hRoot, &NtName);
213 RTNtPathFree(&NtName, NULL);
214 }
215 return rc;
216}
217
218
219RTDECL(int) RTDirRelDirCreate(RTDIR hDir, const char *pszRelPath, RTFMODE fMode, uint32_t fCreate, RTDIR *phSubDir)
220{
221 PRTDIRINTERNAL pThis = hDir;
222 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
223 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
224 AssertReturn(!(fCreate & ~RTDIRCREATE_FLAGS_VALID_MASK), VERR_INVALID_FLAGS);
225 fMode = rtFsModeNormalize(fMode, pszRelPath, 0);
226 AssertReturn(rtFsModeIsValidPermissions(fMode), VERR_INVALID_FMODE);
227 AssertPtrNullReturn(phSubDir, VERR_INVALID_POINTER);
228
229 /*
230 * Convert and normalize the path.
231 */
232 UNICODE_STRING NtName;
233 HANDLE hRoot = pThis->hDir;
234 int rc = RTNtPathRelativeFromUtf8(&NtName, &hRoot, pszRelPath, RTDIRREL_NT_GET_ASCENT(pThis),
235 pThis->enmInfoClass == FileMaximumInformation);
236 if (RT_SUCCESS(rc))
237 {
238 HANDLE hNewDir = RTNT_INVALID_HANDLE_VALUE;
239 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
240 OBJECT_ATTRIBUTES ObjAttr;
241 InitializeObjectAttributes(&ObjAttr, &NtName, 0 /*fAttrib*/, hRoot, NULL);
242
243 ULONG fDirAttribs = (fCreate & RTFS_DOS_MASK_NT) >> RTFS_DOS_SHIFT;
244 if (!(fCreate & RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET))
245 fDirAttribs |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
246 if (!fDirAttribs)
247 fDirAttribs = FILE_ATTRIBUTE_NORMAL;
248
249 NTSTATUS rcNt = NtCreateFile(&hNewDir,
250 phSubDir
251 ? FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | FILE_LIST_DIRECTORY | FILE_TRAVERSE | SYNCHRONIZE
252 : SYNCHRONIZE,
253 &ObjAttr,
254 &Ios,
255 NULL /*AllocationSize*/,
256 fDirAttribs,
257 FILE_SHARE_READ | FILE_SHARE_WRITE,
258 FILE_CREATE,
259 FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
260 NULL /*EaBuffer*/,
261 0 /*EaLength*/);
262
263 /* Just in case someone takes offence at FILE_ATTRIBUTE_NOT_CONTENT_INDEXED. */
264 if ( ( rcNt == STATUS_INVALID_PARAMETER
265 || rcNt == STATUS_INVALID_PARAMETER_7)
266 && (fDirAttribs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
267 && (fCreate & RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL) )
268 {
269 fDirAttribs &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
270 if (!fDirAttribs)
271 fDirAttribs = FILE_ATTRIBUTE_NORMAL;
272 rcNt = NtCreateFile(&hNewDir,
273 phSubDir
274 ? FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | FILE_LIST_DIRECTORY | FILE_TRAVERSE | SYNCHRONIZE
275 : SYNCHRONIZE,
276 &ObjAttr,
277 &Ios,
278 NULL /*AllocationSize*/,
279 fDirAttribs,
280 FILE_SHARE_READ | FILE_SHARE_WRITE,
281 FILE_CREATE,
282 FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
283 NULL /*EaBuffer*/,
284 0 /*EaLength*/);
285 }
286
287 if (NT_SUCCESS(rcNt))
288 {
289 if (!phSubDir)
290 {
291 NtClose(hNewDir);
292 rc = VINF_SUCCESS;
293 }
294 else
295 {
296 rc = rtDirOpenRelativeOrHandle(phSubDir, pszRelPath, RTDIRFILTER_NONE, 0 /*fFlags*/,
297 (uintptr_t)hNewDir, NULL /*pvNativeRelative*/);
298 if (RT_FAILURE(rc))
299 NtClose(hNewDir);
300 }
301 }
302 else
303 rc = RTErrConvertFromNtStatus(rcNt);
304 RTNtPathFree(&NtName, NULL);
305 }
306 return rc;
307}
308
309
310RTDECL(int) RTDirRelDirRemove(RTDIR hDir, const char *pszRelPath)
311{
312 PRTDIRINTERNAL pThis = hDir;
313 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
314 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
315
316 /*
317 * Convert and normalize the path.
318 */
319 UNICODE_STRING NtName;
320 HANDLE hRoot = pThis->hDir;
321 int rc = RTNtPathRelativeFromUtf8(&NtName, &hRoot, pszRelPath, RTDIRREL_NT_GET_ASCENT(pThis),
322 pThis->enmInfoClass == FileMaximumInformation);
323 if (RT_SUCCESS(rc))
324 {
325 HANDLE hSubDir = RTNT_INVALID_HANDLE_VALUE;
326 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
327 OBJECT_ATTRIBUTES ObjAttr;
328 InitializeObjectAttributes(&ObjAttr, &NtName, 0 /*fAttrib*/, hRoot, NULL);
329
330 NTSTATUS rcNt = NtCreateFile(&hSubDir,
331 DELETE | SYNCHRONIZE,
332 &ObjAttr,
333 &Ios,
334 NULL /*AllocationSize*/,
335 FILE_ATTRIBUTE_NORMAL,
336 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
337 FILE_OPEN,
338 FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT,
339 NULL /*EaBuffer*/,
340 0 /*EaLength*/);
341 if (NT_SUCCESS(rcNt))
342 {
343 FILE_DISPOSITION_INFORMATION DispInfo;
344 DispInfo.DeleteFile = TRUE;
345 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
346 rcNt = NtSetInformationFile(hSubDir, &Ios, &DispInfo, sizeof(DispInfo), FileDispositionInformation);
347
348 NTSTATUS rcNt2 = NtClose(hSubDir);
349 if (!NT_SUCCESS(rcNt2) && NT_SUCCESS(rcNt))
350 rcNt = rcNt2;
351 }
352
353 if (NT_SUCCESS(rcNt))
354 rc = VINF_SUCCESS;
355 else
356 rc = RTErrConvertFromNtStatus(rcNt);
357
358 RTNtPathFree(&NtName, NULL);
359 }
360 return rc;
361}
362
363
364/*
365 *
366 * RTPath stuff.
367 * RTPath stuff.
368 * RTPath stuff.
369 *
370 *
371 */
372
373
374RTDECL(int) RTDirRelPathQueryInfo(RTDIR hDir, const char *pszRelPath, PRTFSOBJINFO pObjInfo,
375 RTFSOBJATTRADD enmAddAttr, uint32_t fFlags)
376{
377 PRTDIRINTERNAL pThis = hDir;
378 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
379 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
380
381 /*
382 * Validate and convert flags.
383 */
384 UNICODE_STRING NtName;
385 HANDLE hRoot = pThis->hDir;
386 int rc = RTNtPathRelativeFromUtf8(&NtName, &hRoot, pszRelPath, RTDIRREL_NT_GET_ASCENT(pThis),
387 pThis->enmInfoClass == FileMaximumInformation);
388 if (RT_SUCCESS(rc))
389 {
390 rc = rtPathNtQueryInfoWorker(hRoot, &NtName, pObjInfo, enmAddAttr, fFlags, pszRelPath);
391 RTNtPathFree(&NtName, NULL);
392 }
393 return rc;
394}
395
396
397/**
398 * Changes the mode flags of a file system object relative to @a hDir.
399 *
400 * The API requires at least one of the mode flag sets (Unix/Dos) to
401 * be set. The type is ignored.
402 *
403 * @returns IPRT status code.
404 * @param hDir The directory @a pszRelPath is relative to.
405 * @param pszRelPath The relative path to the file system object.
406 * @param fMode The new file mode, see @ref grp_rt_fs for details.
407 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
408 *
409 * @sa RTPathSetMode
410 */
411RTDECL(int) RTDirRelPathSetMode(RTDIR hDir, const char *pszRelPath, RTFMODE fMode, uint32_t fFlags)
412{
413 PRTDIRINTERNAL pThis = hDir;
414 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
415 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
416 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_FLAGS);
417
418 char szPath[RTPATH_MAX];
419 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
420 if (RT_SUCCESS(rc))
421 {
422RTAssertMsg2("DBG: RTDirRelPathSetMode(%s)...\n", szPath);
423#ifndef RT_OS_WINDOWS
424 rc = RTPathSetMode(szPath, fMode); /** @todo fFlags is currently ignored. */
425#else
426 rc = VERR_NOT_IMPLEMENTED; /** @todo implement RTPathSetMode on windows. */
427 RT_NOREF(fMode);
428#endif
429 }
430 return rc;
431}
432
433
434/**
435 * Changes one or more of the timestamps associated of file system object
436 * relative to @a hDir.
437 *
438 * @returns IPRT status code.
439 * @param hDir The directory @a pszRelPath is relative to.
440 * @param pszRelPath The relative path to the file system object.
441 * @param pAccessTime Pointer to the new access time.
442 * @param pModificationTime Pointer to the new modification time.
443 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
444 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
445 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
446 *
447 * @remark The file system might not implement all these time attributes,
448 * the API will ignore the ones which aren't supported.
449 *
450 * @remark The file system might not implement the time resolution
451 * employed by this interface, the time will be chopped to fit.
452 *
453 * @remark The file system may update the change time even if it's
454 * not specified.
455 *
456 * @remark POSIX can only set Access & Modification and will always set both.
457 *
458 * @sa RTPathSetTimesEx
459 */
460RTDECL(int) RTDirRelPathSetTimes(RTDIR hDir, const char *pszRelPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
461 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime, uint32_t fFlags)
462{
463 PRTDIRINTERNAL 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 {
471RTAssertMsg2("DBG: RTDirRelPathSetTimes(%s)...\n", szPath);
472 rc = RTPathSetTimesEx(szPath, pAccessTime, pModificationTime, pChangeTime, pBirthTime, fFlags);
473 }
474 return rc;
475}
476
477
478/**
479 * Changes the owner and/or group of a file system object relative to @a hDir.
480 *
481 * @returns IPRT status code.
482 * @param hDir The directory @a pszRelPath is relative to.
483 * @param pszRelPath The relative path to the file system object.
484 * @param uid The new file owner user id. Pass NIL_RTUID to leave
485 * this unchanged.
486 * @param gid The new group id. Pass NIL_RTGID to leave this
487 * unchanged.
488 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
489 *
490 * @sa RTPathSetOwnerEx
491 */
492RTDECL(int) RTDirRelPathSetOwner(RTDIR hDir, const char *pszRelPath, uint32_t uid, uint32_t gid, uint32_t fFlags)
493{
494 PRTDIRINTERNAL pThis = hDir;
495 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
496 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
497
498 char szPath[RTPATH_MAX];
499 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
500 if (RT_SUCCESS(rc))
501 {
502RTAssertMsg2("DBG: RTDirRelPathSetOwner(%s)...\n", szPath);
503#ifndef RT_OS_WINDOWS
504 rc = RTPathSetOwnerEx(szPath, uid, gid, fFlags);
505#else
506 rc = VERR_NOT_IMPLEMENTED;
507 RT_NOREF(uid, gid, fFlags);
508#endif
509 }
510 return rc;
511}
512
513
514/**
515 * Renames a directory relative path within a filesystem.
516 *
517 * This will rename symbolic links. If RTPATHRENAME_FLAGS_REPLACE is used and
518 * pszDst is a symbolic link, it will be replaced and not its target.
519 *
520 * @returns IPRT status code.
521 * @param hDirSrc The directory the source path is relative to.
522 * @param pszSrc The source path, relative to @a hDirSrc.
523 * @param hDirSrc The directory the destination path is relative to.
524 * @param pszDst The destination path, relative to @a hDirDst.
525 * @param fRename Rename flags, RTPATHRENAME_FLAGS_XXX.
526 *
527 * @sa RTPathRename
528 */
529RTDECL(int) RTDirRelPathRename(RTDIR hDirSrc, const char *pszSrc, RTDIR hDirDst, const char *pszDst, unsigned fRename)
530{
531 PRTDIRINTERNAL pThis = hDirSrc;
532 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
533 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
534
535 PRTDIRINTERNAL pThat = hDirDst;
536 if (pThat != pThis)
537 {
538 AssertPtrReturn(pThat, VERR_INVALID_HANDLE);
539 AssertReturn(pThat->u32Magic != RTDIR_MAGIC, VERR_INVALID_HANDLE);
540 }
541
542 char szSrcPath[RTPATH_MAX];
543 int rc = rtDirRelBuildFullPath(pThis, szSrcPath, sizeof(szSrcPath), pszSrc);
544 if (RT_SUCCESS(rc))
545 {
546 char szDstPath[RTPATH_MAX];
547 rc = rtDirRelBuildFullPath(pThis, szDstPath, sizeof(szDstPath), pszDst);
548 if (RT_SUCCESS(rc))
549 {
550RTAssertMsg2("DBG: RTDirRelPathRename(%s,%s)...\n", szSrcPath, szDstPath);
551 rc = RTPathRename(szSrcPath, szDstPath, fRename);
552 }
553 }
554 return rc;
555}
556
557
558/**
559 * Removes the last component of the directory relative path.
560 *
561 * @returns IPRT status code.
562 * @param hDir The directory @a pszRelPath is relative to.
563 * @param pszRelPath The relative path to the file system object.
564 * @param fUnlink Unlink flags, RTPATHUNLINK_FLAGS_XXX.
565 *
566 * @sa RTPathUnlink
567 */
568RTDECL(int) RTDirRelPathUnlink(RTDIR hDir, const char *pszRelPath, uint32_t fUnlink)
569{
570 PRTDIRINTERNAL pThis = hDir;
571 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
572 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
573
574 char szPath[RTPATH_MAX];
575 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
576 if (RT_SUCCESS(rc))
577 {
578RTAssertMsg2("DBG: RTDirRelPathUnlink(%s)...\n", szPath);
579 rc = RTPathUnlink(szPath, fUnlink);
580 }
581 return rc;
582}
583
584
585/*
586 *
587 * RTSymlink stuff.
588 * RTSymlink stuff.
589 * RTSymlink stuff.
590 *
591 *
592 */
593
594
595/**
596 * Creates a symbolic link (@a pszSymlink) relative to @a hDir targeting @a
597 * pszTarget.
598 *
599 * @returns IPRT status code.
600 * @param hDir The directory @a pszSymlink is relative to.
601 * @param pszSymlink The relative path of the symbolic link.
602 * @param pszTarget The path to the symbolic link target. This is
603 * relative to @a pszSymlink or an absolute path.
604 * @param enmType The symbolic link type. For Windows compatability
605 * it is very important to set this correctly. When
606 * RTSYMLINKTYPE_UNKNOWN is used, the API will try
607 * make a guess and may attempt query information
608 * about @a pszTarget in the process.
609 * @param fCreate Create flags, RTSYMLINKCREATE_FLAGS_XXX.
610 *
611 * @sa RTSymlinkCreate
612 */
613RTDECL(int) RTDirRelSymlinkCreate(RTDIR hDir, const char *pszSymlink, const char *pszTarget,
614 RTSYMLINKTYPE enmType, uint32_t fCreate)
615{
616 PRTDIRINTERNAL pThis = hDir;
617 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
618 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
619
620 char szPath[RTPATH_MAX];
621 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszSymlink);
622 if (RT_SUCCESS(rc))
623 {
624RTAssertMsg2("DBG: RTDirRelSymlinkCreate(%s)...\n", szPath);
625 rc = RTSymlinkCreate(szPath, pszTarget, enmType, fCreate);
626 }
627 return rc;
628}
629
630
631/**
632 * Read the symlink target relative to @a hDir.
633 *
634 * @returns IPRT status code.
635 * @retval VERR_NOT_SYMLINK if @a pszSymlink does not specify a symbolic link.
636 * @retval VERR_BUFFER_OVERFLOW if the link is larger than @a cbTarget. The
637 * buffer will contain what all we managed to read, fully terminated
638 * if @a cbTarget > 0.
639 *
640 * @param hDir The directory @a pszSymlink is relative to.
641 * @param pszSymlink The relative path to the symbolic link that should
642 * be read.
643 * @param pszTarget The target buffer.
644 * @param cbTarget The size of the target buffer.
645 * @param fRead Read flags, RTSYMLINKREAD_FLAGS_XXX.
646 *
647 * @sa RTSymlinkRead
648 */
649RTDECL(int) RTDirRelSymlinkRead(RTDIR hDir, const char *pszSymlink, char *pszTarget, size_t cbTarget, uint32_t fRead)
650{
651 PRTDIRINTERNAL pThis = hDir;
652 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
653 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
654
655 char szPath[RTPATH_MAX];
656 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszSymlink);
657 if (RT_SUCCESS(rc))
658 {
659RTAssertMsg2("DBG: RTDirRelSymlinkRead(%s)...\n", szPath);
660 rc = RTSymlinkRead(szPath, pszTarget, cbTarget, fRead);
661 }
662 return rc;
663}
664
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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