VirtualBox

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

Last change on this file since 71851 was 71851, checked in by vboxsync, 7 years ago

IPRT/nt/RTDirRelDirOpenFiltered: Must strip the filter from the NT path of NtCreateFile will return invalid name, duh!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.3 KB
Line 
1/* $Id: dirrel-r3-nt.cpp 71851 2018-04-12 15:29:50Z 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.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-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/**
190 * Helper for cooking up a path string for rtDirOpenRelativeOrHandle.
191 *
192 * @returns IPRT status code.
193 * @param pszDst The destination buffer.
194 * @param cbDst The size of the destination buffer.
195 * @param pThis The directory this is relative to.
196 * @param pNtPath The NT path with a possibly relative path.
197 * @param fRelative Whether @a pNtPath is relative or not.
198 * @param pszPath The input path.
199 */
200static int rtDirRelJoinPathForDirOpen(char *pszDst, size_t cbDst, PRTDIRINTERNAL pThis,
201 PUNICODE_STRING pNtPath, bool fRelative, const char *pszPath)
202{
203 int rc;
204 if (fRelative)
205 {
206 size_t cchRel = 0;
207 rc = RTUtf16CalcUtf8LenEx(pNtPath->Buffer, pNtPath->Length / sizeof(RTUTF16), &cchRel);
208 AssertRC(rc);
209 if (RT_SUCCESS(rc))
210 {
211 if (pThis->cchPath + cchRel < cbDst)
212 {
213 size_t cchBase = pThis->cchPath;
214 memcpy(pszDst, pThis->pszPath, cchBase);
215 pszDst += cchBase;
216 cbDst -= cchBase;
217 rc = RTUtf16ToUtf8Ex(pNtPath->Buffer, pNtPath->Length / sizeof(RTUTF16), &pszDst, cbDst, NULL);
218 }
219 else
220 rc = VERR_FILENAME_TOO_LONG;
221 }
222 }
223 else
224 {
225 /** @todo would be better to convert pNtName to DOS/WIN path here,
226 * as it is absolute and doesn't need stuff resolved. */
227 rc = RTPathJoin(pszDst, cbDst, pThis->pszPath, pszPath);
228 }
229 return rc;
230}
231
232RTDECL(int) RTDirRelDirOpen(RTDIR hDir, const char *pszDir, RTDIR *phDir)
233{
234 return RTDirRelDirOpenFiltered(hDir, pszDir, RTDIRFILTER_NONE, 0 /*fFlags*/, phDir);
235}
236
237
238RTDECL(int) RTDirRelDirOpenFiltered(RTDIR hDir, const char *pszDirAndFilter, RTDIRFILTER enmFilter,
239 uint32_t fFlags, RTDIR *phDir)
240{
241 PRTDIRINTERNAL pThis = hDir;
242 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
243 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
244
245 /*
246 * Convert and normalize the path.
247 */
248 UNICODE_STRING NtName;
249 HANDLE hRoot = pThis->hDir;
250 int rc = RTNtPathRelativeFromUtf8(&NtName, &hRoot, pszDirAndFilter, RTDIRREL_NT_GET_ASCENT(pThis),
251 pThis->enmInfoClass == FileMaximumInformation);
252 if (RT_SUCCESS(rc))
253 {
254 char szAbsDirAndFilter[RTPATH_MAX];
255 rc = rtDirRelJoinPathForDirOpen(szAbsDirAndFilter, sizeof(szAbsDirAndFilter), pThis,
256 &NtName, hRoot != NULL, pszDirAndFilter);
257 if (RT_SUCCESS(rc))
258 {
259 /* Drop the filter from the NT name. */
260 switch (enmFilter)
261 {
262 case RTDIRFILTER_NONE:
263 break;
264 case RTDIRFILTER_WINNT:
265 case RTDIRFILTER_UNIX:
266 case RTDIRFILTER_UNIX_UPCASED:
267 {
268 size_t cwc = NtName.Length / sizeof(RTUTF16);
269 while ( cwc > 0
270 && NtName.Buffer[cwc - 1] != '\\')
271 cwc--;
272 NtName.Buffer[cwc] = '\0';
273 NtName.Length = (uint16_t)(cwc * sizeof(RTUTF16));
274 break;
275 }
276 default:
277 AssertFailedBreak();
278 }
279
280 rc = rtDirOpenRelativeOrHandle(phDir, szAbsDirAndFilter, enmFilter, fFlags, (uintptr_t)hRoot, &NtName);
281 }
282 RTNtPathFree(&NtName, NULL);
283 }
284 return rc;
285}
286
287
288RTDECL(int) RTDirRelDirCreate(RTDIR hDir, const char *pszRelPath, RTFMODE fMode, uint32_t fCreate, RTDIR *phSubDir)
289{
290 PRTDIRINTERNAL pThis = hDir;
291 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
292 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
293 AssertReturn(!(fCreate & ~RTDIRCREATE_FLAGS_VALID_MASK), VERR_INVALID_FLAGS);
294 fMode = rtFsModeNormalize(fMode, pszRelPath, 0);
295 AssertReturn(rtFsModeIsValidPermissions(fMode), VERR_INVALID_FMODE);
296 AssertPtrNullReturn(phSubDir, VERR_INVALID_POINTER);
297
298 /*
299 * Convert and normalize the path.
300 */
301 UNICODE_STRING NtName;
302 HANDLE hRoot = pThis->hDir;
303 int rc = RTNtPathRelativeFromUtf8(&NtName, &hRoot, pszRelPath, RTDIRREL_NT_GET_ASCENT(pThis),
304 pThis->enmInfoClass == FileMaximumInformation);
305 if (RT_SUCCESS(rc))
306 {
307 HANDLE hNewDir = RTNT_INVALID_HANDLE_VALUE;
308 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
309 OBJECT_ATTRIBUTES ObjAttr;
310 InitializeObjectAttributes(&ObjAttr, &NtName, 0 /*fAttrib*/, hRoot, NULL);
311
312 ULONG fDirAttribs = (fCreate & RTFS_DOS_MASK_NT) >> RTFS_DOS_SHIFT;
313 if (!(fCreate & RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET))
314 fDirAttribs |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
315 if (!fDirAttribs)
316 fDirAttribs = FILE_ATTRIBUTE_NORMAL;
317
318 NTSTATUS rcNt = NtCreateFile(&hNewDir,
319 phSubDir
320 ? FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | FILE_LIST_DIRECTORY | FILE_TRAVERSE | SYNCHRONIZE
321 : SYNCHRONIZE,
322 &ObjAttr,
323 &Ios,
324 NULL /*AllocationSize*/,
325 fDirAttribs,
326 FILE_SHARE_READ | FILE_SHARE_WRITE,
327 FILE_CREATE,
328 FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
329 NULL /*EaBuffer*/,
330 0 /*EaLength*/);
331
332 /* Just in case someone takes offence at FILE_ATTRIBUTE_NOT_CONTENT_INDEXED. */
333 if ( ( rcNt == STATUS_INVALID_PARAMETER
334 || rcNt == STATUS_INVALID_PARAMETER_7)
335 && (fDirAttribs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
336 && (fCreate & RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL) )
337 {
338 fDirAttribs &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
339 if (!fDirAttribs)
340 fDirAttribs = FILE_ATTRIBUTE_NORMAL;
341 rcNt = NtCreateFile(&hNewDir,
342 phSubDir
343 ? FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | FILE_LIST_DIRECTORY | FILE_TRAVERSE | SYNCHRONIZE
344 : SYNCHRONIZE,
345 &ObjAttr,
346 &Ios,
347 NULL /*AllocationSize*/,
348 fDirAttribs,
349 FILE_SHARE_READ | FILE_SHARE_WRITE,
350 FILE_CREATE,
351 FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
352 NULL /*EaBuffer*/,
353 0 /*EaLength*/);
354 }
355
356 if (NT_SUCCESS(rcNt))
357 {
358 if (!phSubDir)
359 {
360 NtClose(hNewDir);
361 rc = VINF_SUCCESS;
362 }
363 else
364 {
365 char szAbsDirAndFilter[RTPATH_MAX];
366 rc = rtDirRelJoinPathForDirOpen(szAbsDirAndFilter, sizeof(szAbsDirAndFilter), pThis,
367 &NtName, hRoot != NULL, pszRelPath);
368 if (RT_SUCCESS(rc))
369 rc = rtDirOpenRelativeOrHandle(phSubDir, pszRelPath, RTDIRFILTER_NONE, 0 /*fFlags*/,
370 (uintptr_t)hNewDir, NULL /*pvNativeRelative*/);
371 if (RT_FAILURE(rc))
372 NtClose(hNewDir);
373 }
374 }
375 else
376 rc = RTErrConvertFromNtStatus(rcNt);
377 RTNtPathFree(&NtName, NULL);
378 }
379 return rc;
380}
381
382
383RTDECL(int) RTDirRelDirRemove(RTDIR hDir, const char *pszRelPath)
384{
385 PRTDIRINTERNAL pThis = hDir;
386 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
387 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
388
389 /*
390 * Convert and normalize the path.
391 */
392 UNICODE_STRING NtName;
393 HANDLE hRoot = pThis->hDir;
394 int rc = RTNtPathRelativeFromUtf8(&NtName, &hRoot, pszRelPath, RTDIRREL_NT_GET_ASCENT(pThis),
395 pThis->enmInfoClass == FileMaximumInformation);
396 if (RT_SUCCESS(rc))
397 {
398 HANDLE hSubDir = RTNT_INVALID_HANDLE_VALUE;
399 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
400 OBJECT_ATTRIBUTES ObjAttr;
401 InitializeObjectAttributes(&ObjAttr, &NtName, 0 /*fAttrib*/, hRoot, NULL);
402
403 NTSTATUS rcNt = NtCreateFile(&hSubDir,
404 DELETE | SYNCHRONIZE,
405 &ObjAttr,
406 &Ios,
407 NULL /*AllocationSize*/,
408 FILE_ATTRIBUTE_NORMAL,
409 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
410 FILE_OPEN,
411 FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT,
412 NULL /*EaBuffer*/,
413 0 /*EaLength*/);
414 if (NT_SUCCESS(rcNt))
415 {
416 FILE_DISPOSITION_INFORMATION DispInfo;
417 DispInfo.DeleteFile = TRUE;
418 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
419 rcNt = NtSetInformationFile(hSubDir, &Ios, &DispInfo, sizeof(DispInfo), FileDispositionInformation);
420
421 NTSTATUS rcNt2 = NtClose(hSubDir);
422 if (!NT_SUCCESS(rcNt2) && NT_SUCCESS(rcNt))
423 rcNt = rcNt2;
424 }
425
426 if (NT_SUCCESS(rcNt))
427 rc = VINF_SUCCESS;
428 else
429 rc = RTErrConvertFromNtStatus(rcNt);
430
431 RTNtPathFree(&NtName, NULL);
432 }
433 return rc;
434}
435
436
437/*
438 *
439 * RTPath stuff.
440 * RTPath stuff.
441 * RTPath stuff.
442 *
443 *
444 */
445
446
447RTDECL(int) RTDirRelPathQueryInfo(RTDIR hDir, const char *pszRelPath, PRTFSOBJINFO pObjInfo,
448 RTFSOBJATTRADD enmAddAttr, uint32_t fFlags)
449{
450 PRTDIRINTERNAL pThis = hDir;
451 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
452 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
453
454 /*
455 * Validate and convert flags.
456 */
457 UNICODE_STRING NtName;
458 HANDLE hRoot = pThis->hDir;
459 int rc = RTNtPathRelativeFromUtf8(&NtName, &hRoot, pszRelPath, RTDIRREL_NT_GET_ASCENT(pThis),
460 pThis->enmInfoClass == FileMaximumInformation);
461 if (RT_SUCCESS(rc))
462 {
463 rc = rtPathNtQueryInfoWorker(hRoot, &NtName, pObjInfo, enmAddAttr, fFlags, pszRelPath);
464 RTNtPathFree(&NtName, NULL);
465 }
466 return rc;
467}
468
469
470/**
471 * Changes the mode flags of a file system object relative to @a hDir.
472 *
473 * The API requires at least one of the mode flag sets (Unix/Dos) to
474 * be set. The type is ignored.
475 *
476 * @returns IPRT status code.
477 * @param hDir The directory @a pszRelPath is relative to.
478 * @param pszRelPath The relative path to the file system object.
479 * @param fMode The new file mode, see @ref grp_rt_fs for details.
480 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
481 *
482 * @sa RTPathSetMode
483 */
484RTDECL(int) RTDirRelPathSetMode(RTDIR hDir, const char *pszRelPath, RTFMODE fMode, uint32_t fFlags)
485{
486 PRTDIRINTERNAL pThis = hDir;
487 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
488 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
489 fMode = rtFsModeNormalize(fMode, pszRelPath, 0);
490 AssertReturn(rtFsModeIsValidPermissions(fMode), VERR_INVALID_FMODE);
491 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_FLAGS);
492
493 /*
494 * Convert and normalize the path.
495 */
496 UNICODE_STRING NtName;
497 HANDLE hRoot = pThis->hDir;
498 int rc = RTNtPathRelativeFromUtf8(&NtName, &hRoot, pszRelPath, RTDIRREL_NT_GET_ASCENT(pThis),
499 pThis->enmInfoClass == FileMaximumInformation);
500 if (RT_SUCCESS(rc))
501 {
502 HANDLE hSubDir = RTNT_INVALID_HANDLE_VALUE;
503 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
504 OBJECT_ATTRIBUTES ObjAttr;
505 InitializeObjectAttributes(&ObjAttr, &NtName, 0 /*fAttrib*/, hRoot, NULL);
506
507 ULONG fOpenOptions = FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT;
508 if (fFlags & RTPATH_F_ON_LINK)
509 fOpenOptions |= FILE_OPEN_REPARSE_POINT;
510 NTSTATUS rcNt = NtCreateFile(&hSubDir,
511 FILE_WRITE_ATTRIBUTES | SYNCHRONIZE,
512 &ObjAttr,
513 &Ios,
514 NULL /*AllocationSize*/,
515 FILE_ATTRIBUTE_NORMAL,
516 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
517 FILE_OPEN,
518 fOpenOptions,
519 NULL /*EaBuffer*/,
520 0 /*EaLength*/);
521 if (NT_SUCCESS(rcNt))
522 {
523 rc = rtNtFileSetModeWorker(hSubDir, fMode);
524
525 rcNt = NtClose(hSubDir);
526 if (!NT_SUCCESS(rcNt) && RT_SUCCESS(rc))
527 rc = RTErrConvertFromNtStatus(rcNt);
528 }
529 else
530 rc = RTErrConvertFromNtStatus(rcNt);
531
532 RTNtPathFree(&NtName, NULL);
533 }
534 return rc;
535}
536
537
538/**
539 * Changes one or more of the timestamps associated of file system object
540 * relative to @a hDir.
541 *
542 * @returns IPRT status code.
543 * @param hDir The directory @a pszRelPath is relative to.
544 * @param pszRelPath The relative path to the file system object.
545 * @param pAccessTime Pointer to the new access time.
546 * @param pModificationTime Pointer to the new modification time.
547 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
548 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
549 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
550 *
551 * @remark The file system might not implement all these time attributes,
552 * the API will ignore the ones which aren't supported.
553 *
554 * @remark The file system might not implement the time resolution
555 * employed by this interface, the time will be chopped to fit.
556 *
557 * @remark The file system may update the change time even if it's
558 * not specified.
559 *
560 * @remark POSIX can only set Access & Modification and will always set both.
561 *
562 * @sa RTPathSetTimesEx
563 */
564RTDECL(int) RTDirRelPathSetTimes(RTDIR hDir, const char *pszRelPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
565 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime, uint32_t fFlags)
566{
567 PRTDIRINTERNAL pThis = hDir;
568 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
569 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
570
571 char szPath[RTPATH_MAX];
572 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
573 if (RT_SUCCESS(rc))
574 {
575RTAssertMsg2("DBG: RTDirRelPathSetTimes(%s)...\n", szPath);
576 rc = RTPathSetTimesEx(szPath, pAccessTime, pModificationTime, pChangeTime, pBirthTime, fFlags);
577 }
578 return rc;
579}
580
581
582/**
583 * Changes the owner and/or group of a file system object relative to @a hDir.
584 *
585 * @returns IPRT status code.
586 * @param hDir The directory @a pszRelPath is relative to.
587 * @param pszRelPath The relative path to the file system object.
588 * @param uid The new file owner user id. Pass NIL_RTUID to leave
589 * this unchanged.
590 * @param gid The new group id. Pass NIL_RTGID to leave this
591 * unchanged.
592 * @param fFlags RTPATH_F_ON_LINK or RTPATH_F_FOLLOW_LINK.
593 *
594 * @sa RTPathSetOwnerEx
595 */
596RTDECL(int) RTDirRelPathSetOwner(RTDIR hDir, const char *pszRelPath, uint32_t uid, uint32_t gid, uint32_t fFlags)
597{
598 PRTDIRINTERNAL pThis = hDir;
599 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
600 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
601
602 char szPath[RTPATH_MAX];
603 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
604 if (RT_SUCCESS(rc))
605 {
606RTAssertMsg2("DBG: RTDirRelPathSetOwner(%s)...\n", szPath);
607#ifndef RT_OS_WINDOWS
608 rc = RTPathSetOwnerEx(szPath, uid, gid, fFlags);
609#else
610 rc = VERR_NOT_IMPLEMENTED;
611 RT_NOREF(uid, gid, fFlags);
612#endif
613 }
614 return rc;
615}
616
617
618/**
619 * Renames a directory relative path within a filesystem.
620 *
621 * This will rename symbolic links. If RTPATHRENAME_FLAGS_REPLACE is used and
622 * pszDst is a symbolic link, it will be replaced and not its target.
623 *
624 * @returns IPRT status code.
625 * @param hDirSrc The directory the source path is relative to.
626 * @param pszSrc The source path, relative to @a hDirSrc.
627 * @param hDirSrc The directory the destination path is relative to.
628 * @param pszDst The destination path, relative to @a hDirDst.
629 * @param fRename Rename flags, RTPATHRENAME_FLAGS_XXX.
630 *
631 * @sa RTPathRename
632 */
633RTDECL(int) RTDirRelPathRename(RTDIR hDirSrc, const char *pszSrc, RTDIR hDirDst, const char *pszDst, unsigned fRename)
634{
635 PRTDIRINTERNAL pThis = hDirSrc;
636 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
637 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
638
639 PRTDIRINTERNAL pThat = hDirDst;
640 if (pThat != pThis)
641 {
642 AssertPtrReturn(pThat, VERR_INVALID_HANDLE);
643 AssertReturn(pThat->u32Magic != RTDIR_MAGIC, VERR_INVALID_HANDLE);
644 }
645
646 char szSrcPath[RTPATH_MAX];
647 int rc = rtDirRelBuildFullPath(pThis, szSrcPath, sizeof(szSrcPath), pszSrc);
648 if (RT_SUCCESS(rc))
649 {
650 char szDstPath[RTPATH_MAX];
651 rc = rtDirRelBuildFullPath(pThis, szDstPath, sizeof(szDstPath), pszDst);
652 if (RT_SUCCESS(rc))
653 {
654RTAssertMsg2("DBG: RTDirRelPathRename(%s,%s)...\n", szSrcPath, szDstPath);
655 rc = RTPathRename(szSrcPath, szDstPath, fRename);
656 }
657 }
658 return rc;
659}
660
661
662/**
663 * Removes the last component of the directory relative path.
664 *
665 * @returns IPRT status code.
666 * @param hDir The directory @a pszRelPath is relative to.
667 * @param pszRelPath The relative path to the file system object.
668 * @param fUnlink Unlink flags, RTPATHUNLINK_FLAGS_XXX.
669 *
670 * @sa RTPathUnlink
671 */
672RTDECL(int) RTDirRelPathUnlink(RTDIR hDir, const char *pszRelPath, uint32_t fUnlink)
673{
674 PRTDIRINTERNAL pThis = hDir;
675 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
676 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
677
678 char szPath[RTPATH_MAX];
679 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszRelPath);
680 if (RT_SUCCESS(rc))
681 {
682RTAssertMsg2("DBG: RTDirRelPathUnlink(%s)...\n", szPath);
683 rc = RTPathUnlink(szPath, fUnlink);
684 }
685 return rc;
686}
687
688
689/*
690 *
691 * RTSymlink stuff.
692 * RTSymlink stuff.
693 * RTSymlink stuff.
694 *
695 *
696 */
697
698
699/**
700 * Creates a symbolic link (@a pszSymlink) relative to @a hDir targeting @a
701 * pszTarget.
702 *
703 * @returns IPRT status code.
704 * @param hDir The directory @a pszSymlink is relative to.
705 * @param pszSymlink The relative path of the symbolic link.
706 * @param pszTarget The path to the symbolic link target. This is
707 * relative to @a pszSymlink or an absolute path.
708 * @param enmType The symbolic link type. For Windows compatability
709 * it is very important to set this correctly. When
710 * RTSYMLINKTYPE_UNKNOWN is used, the API will try
711 * make a guess and may attempt query information
712 * about @a pszTarget in the process.
713 * @param fCreate Create flags, RTSYMLINKCREATE_FLAGS_XXX.
714 *
715 * @sa RTSymlinkCreate
716 */
717RTDECL(int) RTDirRelSymlinkCreate(RTDIR hDir, const char *pszSymlink, const char *pszTarget,
718 RTSYMLINKTYPE enmType, uint32_t fCreate)
719{
720 PRTDIRINTERNAL pThis = hDir;
721 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
722 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
723
724 char szPath[RTPATH_MAX];
725 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszSymlink);
726 if (RT_SUCCESS(rc))
727 {
728RTAssertMsg2("DBG: RTDirRelSymlinkCreate(%s)...\n", szPath);
729 rc = RTSymlinkCreate(szPath, pszTarget, enmType, fCreate);
730 }
731 return rc;
732}
733
734
735/**
736 * Read the symlink target relative to @a hDir.
737 *
738 * @returns IPRT status code.
739 * @retval VERR_NOT_SYMLINK if @a pszSymlink does not specify a symbolic link.
740 * @retval VERR_BUFFER_OVERFLOW if the link is larger than @a cbTarget. The
741 * buffer will contain what all we managed to read, fully terminated
742 * if @a cbTarget > 0.
743 *
744 * @param hDir The directory @a pszSymlink is relative to.
745 * @param pszSymlink The relative path to the symbolic link that should
746 * be read.
747 * @param pszTarget The target buffer.
748 * @param cbTarget The size of the target buffer.
749 * @param fRead Read flags, RTSYMLINKREAD_FLAGS_XXX.
750 *
751 * @sa RTSymlinkRead
752 */
753RTDECL(int) RTDirRelSymlinkRead(RTDIR hDir, const char *pszSymlink, char *pszTarget, size_t cbTarget, uint32_t fRead)
754{
755 PRTDIRINTERNAL pThis = hDir;
756 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
757 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
758
759 char szPath[RTPATH_MAX];
760 int rc = rtDirRelBuildFullPath(pThis, szPath, sizeof(szPath), pszSymlink);
761 if (RT_SUCCESS(rc))
762 {
763RTAssertMsg2("DBG: RTDirRelSymlinkRead(%s)...\n", szPath);
764 rc = RTSymlinkRead(szPath, pszTarget, cbTarget, fRead);
765 }
766 return rc;
767}
768
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