VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/path-win.cpp@ 103795

Last change on this file since 103795 was 99758, checked in by vboxsync, 19 months ago

IPRT: Make doxygen 1.9.6 happy. Mostly removing duplicate docs (iprt is documented in the header files). bugref:10442

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 25.9 KB
Line 
1/* $Id: path-win.cpp 99758 2023-05-11 21:37:59Z vboxsync $ */
2/** @file
3 * IPRT - Path manipulation.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_PATH
42#include <iprt/win/windows.h>
43#include <iprt/win/shlobj.h>
44
45#include <iprt/path.h>
46#include <iprt/assert.h>
47#include <iprt/ctype.h>
48#include <iprt/err.h>
49#include <iprt/ldr.h>
50#include <iprt/log.h>
51#include <iprt/mem.h>
52#include <iprt/param.h>
53#include <iprt/string.h>
54#include <iprt/time.h>
55#include <iprt/utf16.h>
56#include "internal/path.h"
57#include "internal/fs.h"
58
59/* Needed for lazy loading SHGetFolderPathW in RTPathUserDocuments(). */
60typedef HRESULT WINAPI FNSHGETFOLDERPATHW(HWND, int, HANDLE, DWORD, LPWSTR);
61typedef FNSHGETFOLDERPATHW *PFNSHGETFOLDERPATHW;
62
63
64RTDECL(int) RTPathReal(const char *pszPath, char *pszRealPath, size_t cchRealPath)
65{
66 /*
67 * Convert to UTF-16, call Win32 APIs, convert back.
68 */
69 PRTUTF16 pwszPath;
70 int rc = RTPathWinFromUtf8(&pwszPath, pszPath, 0 /*fFlags*/);
71 if (RT_SUCCESS(rc))
72 {
73 LPWSTR lpFile;
74 WCHAR wsz[RTPATH_MAX];
75 rc = GetFullPathNameW((LPCWSTR)pwszPath, RT_ELEMENTS(wsz), &wsz[0], &lpFile);
76 if (rc > 0 && rc < RT_ELEMENTS(wsz))
77 {
78 /* Check that it exists. (Use RTPathAbs() to just resolve the name.) */
79 DWORD dwAttr = GetFileAttributesW(wsz);
80 if (dwAttr != INVALID_FILE_ATTRIBUTES)
81 rc = RTUtf16ToUtf8Ex((PRTUTF16)&wsz[0], RTSTR_MAX, &pszRealPath, cchRealPath, NULL);
82 else
83 rc = RTErrConvertFromWin32(GetLastError());
84 }
85 else if (rc <= 0)
86 rc = RTErrConvertFromWin32(GetLastError());
87 else
88 rc = VERR_FILENAME_TOO_LONG;
89
90 RTPathWinFree(pwszPath);
91 }
92 return rc;
93}
94
95#if 0
96RTDECL(int) RTPathAbs(const char *pszPath, char *pszAbsPath, size_t cchAbsPath)
97{
98 /*
99 * Validation.
100 */
101 AssertPtr(pszAbsPath);
102 AssertPtr(pszPath);
103 if (RT_UNLIKELY(!*pszPath))
104 return VERR_INVALID_PARAMETER;
105
106 /*
107 * Convert to UTF-16, call Win32 API, convert back.
108 */
109 LPWSTR pwszPath;
110 int rc = RTStrToUtf16(pszPath, &pwszPath);
111 if (!RT_SUCCESS(rc))
112 return (rc);
113
114 LPWSTR pwszFile; /* Ignored */
115 RTUTF16 wsz[RTPATH_MAX];
116 rc = GetFullPathNameW(pwszPath, RT_ELEMENTS(wsz), &wsz[0], &pwszFile);
117 if (rc > 0 && rc < RT_ELEMENTS(wsz))
118 {
119 size_t cch;
120 rc = RTUtf16ToUtf8Ex(&wsz[0], RTSTR_MAX, &pszAbsPath, cchAbsPath, &cch);
121 if (RT_SUCCESS(rc))
122 {
123# if 1 /** @todo This code is completely bonkers. */
124 /*
125 * Remove trailing slash if the path may be pointing to a directory.
126 * (See posix variant.)
127 */
128 if ( cch > 1
129 && RTPATH_IS_SLASH(pszAbsPath[cch - 1])
130 && !RTPATH_IS_VOLSEP(pszAbsPath[cch - 2])
131 && !RTPATH_IS_SLASH(pszAbsPath[cch - 2]))
132 pszAbsPath[cch - 1] = '\0';
133# endif
134 }
135 }
136 else if (rc <= 0)
137 rc = RTErrConvertFromWin32(GetLastError());
138 else
139 rc = VERR_FILENAME_TOO_LONG;
140
141 RTUtf16Free(pwszPath);
142 return rc;
143}
144#endif
145
146
147RTDECL(int) RTPathUserHome(char *pszPath, size_t cchPath)
148{
149 /*
150 * Validate input
151 */
152 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
153 AssertReturn(cchPath, VERR_INVALID_PARAMETER);
154
155 RTUTF16 wszPath[RTPATH_MAX];
156 bool fValidFolderPath = false;
157
158 /*
159 * Try with Windows XP+ functionality first.
160 */
161 RTLDRMOD hShell32;
162 int rc = RTLdrLoadSystem("Shell32.dll", true /*fNoUnload*/, &hShell32);
163 if (RT_SUCCESS(rc))
164 {
165 PFNSHGETFOLDERPATHW pfnSHGetFolderPathW;
166 rc = RTLdrGetSymbol(hShell32, "SHGetFolderPathW", (void**)&pfnSHGetFolderPathW);
167 if (RT_SUCCESS(rc))
168 {
169 HRESULT hrc = pfnSHGetFolderPathW(0, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, wszPath);
170 fValidFolderPath = (hrc == S_OK);
171 }
172 RTLdrClose(hShell32);
173 }
174
175 DWORD dwAttr;
176 if ( !fValidFolderPath
177 || (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
178 || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
179 {
180 /*
181 * Fall back to Windows specific environment variables. HOME is not used.
182 */
183 if ( !GetEnvironmentVariableW(L"USERPROFILE", &wszPath[0], RTPATH_MAX)
184 || (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
185 || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
186 {
187 /* %HOMEDRIVE%%HOMEPATH% */
188 if (!GetEnvironmentVariableW(L"HOMEDRIVE", &wszPath[0], RTPATH_MAX))
189 return VERR_PATH_NOT_FOUND;
190 size_t const cwc = RTUtf16Len(&wszPath[0]);
191 if ( !GetEnvironmentVariableW(L"HOMEPATH", &wszPath[cwc], RTPATH_MAX - (DWORD)cwc)
192 || (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
193 || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
194 return VERR_PATH_NOT_FOUND;
195 }
196 }
197
198 /*
199 * Convert and return.
200 */
201 return RTUtf16ToUtf8Ex(&wszPath[0], RTSTR_MAX, &pszPath, cchPath, NULL);
202}
203
204
205RTDECL(int) RTPathUserDocuments(char *pszPath, size_t cchPath)
206{
207 /*
208 * Validate input
209 */
210 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
211 AssertReturn(cchPath, VERR_INVALID_PARAMETER);
212
213 RTLDRMOD hShell32;
214 int rc = RTLdrLoadSystem("Shell32.dll", true /*fNoUnload*/, &hShell32);
215 if (RT_SUCCESS(rc))
216 {
217 PFNSHGETFOLDERPATHW pfnSHGetFolderPathW;
218 rc = RTLdrGetSymbol(hShell32, "SHGetFolderPathW", (void**)&pfnSHGetFolderPathW);
219 if (RT_SUCCESS(rc))
220 {
221 RTUTF16 wszPath[RTPATH_MAX];
222 HRESULT hrc = pfnSHGetFolderPathW(0, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, wszPath);
223 if ( hrc == S_OK /* Found */
224 || hrc == S_FALSE) /* Found, but doesn't exist */
225 {
226 /*
227 * Convert and return.
228 */
229 RTLdrClose(hShell32);
230 return RTUtf16ToUtf8Ex(&wszPath[0], RTSTR_MAX, &pszPath, cchPath, NULL);
231 }
232 }
233 RTLdrClose(hShell32);
234 }
235 return VERR_PATH_NOT_FOUND;
236}
237
238
239#if 0 /* use nt version of this */
240
241RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
242{
243 return RTPathQueryInfoEx(pszPath, pObjInfo, enmAdditionalAttribs, RTPATH_F_ON_LINK);
244}
245#endif
246#if 0
247
248
249RTR3DECL(int) RTPathQueryInfoEx(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
250{
251 /*
252 * Validate input.
253 */
254 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
255 AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
256 AssertPtrReturn(pObjInfo, VERR_INVALID_POINTER);
257 AssertMsgReturn( enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING
258 && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
259 ("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs),
260 VERR_INVALID_PARAMETER);
261 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
262
263 /*
264 * Query file info.
265 */
266 uint32_t uReparseTag = RTFSMODE_SYMLINK_REPARSE_TAG;
267 WIN32_FILE_ATTRIBUTE_DATA Data;
268 PRTUTF16 pwszPath;
269 int rc = RTStrToUtf16(pszPath, &pwszPath);
270 if (RT_FAILURE(rc))
271 return rc;
272 if (!GetFileAttributesExW(pwszPath, GetFileExInfoStandard, &Data))
273 {
274 /* Fallback to FindFileFirst in case of sharing violation. */
275 if (GetLastError() == ERROR_SHARING_VIOLATION)
276 {
277 WIN32_FIND_DATAW FindData;
278 HANDLE hDir = FindFirstFileW(pwszPath, &FindData);
279 if (hDir == INVALID_HANDLE_VALUE)
280 {
281 rc = RTErrConvertFromWin32(GetLastError());
282 RTUtf16Free(pwszPath);
283 return rc;
284 }
285 FindClose(hDir);
286
287 Data.dwFileAttributes = FindData.dwFileAttributes;
288 Data.ftCreationTime = FindData.ftCreationTime;
289 Data.ftLastAccessTime = FindData.ftLastAccessTime;
290 Data.ftLastWriteTime = FindData.ftLastWriteTime;
291 Data.nFileSizeHigh = FindData.nFileSizeHigh;
292 Data.nFileSizeLow = FindData.nFileSizeLow;
293 uReparseTag = FindData.dwReserved0;
294 }
295 else
296 {
297 rc = RTErrConvertFromWin32(GetLastError());
298 RTUtf16Free(pwszPath);
299 return rc;
300 }
301 }
302
303 /*
304 * Getting the information for the link target is a bit annoying and
305 * subject to the same access violation mess as above.. :/
306 */
307 /** @todo we're too lazy wrt to error paths here... */
308 if ( (Data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
309 && ((fFlags & RTPATH_F_FOLLOW_LINK) || uReparseTag != RTFSMODE_SYMLINK_REPARSE_TAG))
310 {
311 HANDLE hFinal = CreateFileW(pwszPath,
312 GENERIC_READ,
313 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
314 NULL,
315 OPEN_EXISTING,
316 FILE_FLAG_BACKUP_SEMANTICS,
317 NULL);
318 if (hFinal != INVALID_HANDLE_VALUE)
319 {
320 BY_HANDLE_FILE_INFORMATION FileData;
321 if (GetFileInformationByHandle(hFinal, &FileData))
322 {
323 Data.dwFileAttributes = FileData.dwFileAttributes;
324 Data.ftCreationTime = FileData.ftCreationTime;
325 Data.ftLastAccessTime = FileData.ftLastAccessTime;
326 Data.ftLastWriteTime = FileData.ftLastWriteTime;
327 Data.nFileSizeHigh = FileData.nFileSizeHigh;
328 Data.nFileSizeLow = FileData.nFileSizeLow;
329 uReparseTag = 0;
330 }
331 CloseHandle(hFinal);
332 }
333 else if (GetLastError() != ERROR_SHARING_VIOLATION)
334 {
335 rc = RTErrConvertFromWin32(GetLastError());
336 RTUtf16Free(pwszPath);
337 return rc;
338 }
339 }
340
341 RTUtf16Free(pwszPath);
342
343 /*
344 * Setup the returned data.
345 */
346 pObjInfo->cbObject = ((uint64_t)Data.nFileSizeHigh << 32)
347 | (uint64_t)Data.nFileSizeLow;
348 pObjInfo->cbAllocated = pObjInfo->cbObject;
349
350 Assert(sizeof(uint64_t) == sizeof(Data.ftCreationTime));
351 RTTimeSpecSetNtTime(&pObjInfo->BirthTime, *(uint64_t *)&Data.ftCreationTime);
352 RTTimeSpecSetNtTime(&pObjInfo->AccessTime, *(uint64_t *)&Data.ftLastAccessTime);
353 RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, *(uint64_t *)&Data.ftLastWriteTime);
354 pObjInfo->ChangeTime = pObjInfo->ModificationTime;
355
356 pObjInfo->Attr.fMode = rtFsModeFromDos((Data.dwFileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT,
357 pszPath, strlen(pszPath), uReparseTag);
358
359 /*
360 * Requested attributes (we cannot provide anything actually).
361 */
362 switch (enmAdditionalAttribs)
363 {
364 case RTFSOBJATTRADD_NOTHING:
365 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
366 break;
367
368 case RTFSOBJATTRADD_UNIX:
369 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
370 pObjInfo->Attr.u.Unix.uid = ~0U;
371 pObjInfo->Attr.u.Unix.gid = ~0U;
372 pObjInfo->Attr.u.Unix.cHardlinks = 1;
373 pObjInfo->Attr.u.Unix.INodeIdDevice = 0; /** @todo use volume serial number */
374 pObjInfo->Attr.u.Unix.INodeId = 0; /** @todo use fileid (see GetFileInformationByHandle). */
375 pObjInfo->Attr.u.Unix.fFlags = 0;
376 pObjInfo->Attr.u.Unix.GenerationId = 0;
377 pObjInfo->Attr.u.Unix.Device = 0;
378 break;
379
380 case RTFSOBJATTRADD_UNIX_OWNER:
381 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX_OWNER;
382 pObjInfo->Attr.u.UnixOwner.uid = ~0U;
383 pObjInfo->Attr.u.UnixOwner.szName[0] = '\0'; /** @todo return something sensible here. */
384 break;
385
386 case RTFSOBJATTRADD_UNIX_GROUP:
387 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX_GROUP;
388 pObjInfo->Attr.u.UnixGroup.gid = ~0U;
389 pObjInfo->Attr.u.UnixGroup.szName[0] = '\0';
390 break;
391
392 case RTFSOBJATTRADD_EASIZE:
393 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
394 pObjInfo->Attr.u.EASize.cb = 0;
395 break;
396
397 default:
398 AssertMsgFailed(("Impossible!\n"));
399 return VERR_INTERNAL_ERROR;
400 }
401
402 return VINF_SUCCESS;
403}
404
405#endif /* using NT version*/
406
407
408RTR3DECL(int) RTPathSetTimes(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
409 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
410{
411 return RTPathSetTimesEx(pszPath, pAccessTime, pModificationTime, pChangeTime, pBirthTime, RTPATH_F_ON_LINK);
412}
413
414
415RTR3DECL(int) RTPathSetTimesEx(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
416 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime, uint32_t fFlags)
417{
418 /*
419 * Validate input.
420 */
421 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
422 AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
423 AssertPtrNullReturn(pAccessTime, VERR_INVALID_POINTER);
424 AssertPtrNullReturn(pModificationTime, VERR_INVALID_POINTER);
425 AssertPtrNullReturn(pChangeTime, VERR_INVALID_POINTER);
426 AssertPtrNullReturn(pBirthTime, VERR_INVALID_POINTER);
427 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
428
429 /*
430 * Convert the path.
431 */
432 PRTUTF16 pwszPath;
433 int rc = RTPathWinFromUtf8(&pwszPath, pszPath, 0 /*fFlags*/);
434 if (RT_SUCCESS(rc))
435 {
436 HANDLE hFile;
437 if (fFlags & RTPATH_F_FOLLOW_LINK)
438 hFile = CreateFileW(pwszPath,
439 FILE_WRITE_ATTRIBUTES, /* dwDesiredAccess */
440 FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, /* dwShareMode */
441 NULL, /* security attribs */
442 OPEN_EXISTING, /* dwCreationDisposition */
443 FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_NORMAL,
444 NULL);
445 else
446 {
447/** @todo Symlink: Test RTPathSetTimesEx on Windows. (The code is disabled
448 * because it's not tested yet.) */
449#if 0 //def FILE_FLAG_OPEN_REPARSE_POINT
450 hFile = CreateFileW(pwszPath,
451 FILE_WRITE_ATTRIBUTES, /* dwDesiredAccess */
452 FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, /* dwShareMode */
453 NULL, /* security attribs */
454 OPEN_EXISTING, /* dwCreationDisposition */
455 FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT,
456 NULL);
457
458 if (hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER)
459#endif
460 hFile = CreateFileW(pwszPath,
461 FILE_WRITE_ATTRIBUTES, /* dwDesiredAccess */
462 FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, /* dwShareMode */
463 NULL, /* security attribs */
464 OPEN_EXISTING, /* dwCreationDisposition */
465 FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_NORMAL,
466 NULL);
467 }
468 if (hFile != INVALID_HANDLE_VALUE)
469 {
470 /*
471 * Check if it's a no-op.
472 */
473 if (!pAccessTime && !pModificationTime && !pBirthTime)
474 rc = VINF_SUCCESS; /* NOP */
475 else
476 {
477 /*
478 * Convert the input and call the API.
479 */
480 FILETIME CreationTimeFT;
481 PFILETIME pCreationTimeFT = NULL;
482 if (pBirthTime)
483 pCreationTimeFT = RTTimeSpecGetNtFileTime(pBirthTime, &CreationTimeFT);
484
485 FILETIME LastAccessTimeFT;
486 PFILETIME pLastAccessTimeFT = NULL;
487 if (pAccessTime)
488 pLastAccessTimeFT = RTTimeSpecGetNtFileTime(pAccessTime, &LastAccessTimeFT);
489
490 FILETIME LastWriteTimeFT;
491 PFILETIME pLastWriteTimeFT = NULL;
492 if (pModificationTime)
493 pLastWriteTimeFT = RTTimeSpecGetNtFileTime(pModificationTime, &LastWriteTimeFT);
494
495 if (SetFileTime(hFile, pCreationTimeFT, pLastAccessTimeFT, pLastWriteTimeFT))
496 rc = VINF_SUCCESS;
497 else
498 {
499 DWORD Err = GetLastError();
500 rc = RTErrConvertFromWin32(Err);
501 Log(("RTPathSetTimes('%s', %p, %p, %p, %p): SetFileTime failed with lasterr %d (%Rrc)\n",
502 pszPath, pAccessTime, pModificationTime, pChangeTime, pBirthTime, Err, rc));
503 }
504 }
505 BOOL fRc = CloseHandle(hFile); Assert(fRc); NOREF(fRc);
506 }
507 else
508 {
509 DWORD Err = GetLastError();
510 rc = RTErrConvertFromWin32(Err);
511 Log(("RTPathSetTimes('%s',,,,): failed with %Rrc and lasterr=%u\n", pszPath, rc, Err));
512 }
513
514 RTPathWinFree(pwszPath);
515 }
516
517 LogFlow(("RTPathSetTimes(%p:{%s}, %p:{%RDtimespec}, %p:{%RDtimespec}, %p:{%RDtimespec}, %p:{%RDtimespec}): return %Rrc\n",
518 pszPath, pszPath, pAccessTime, pAccessTime, pModificationTime, pModificationTime,
519 pChangeTime, pChangeTime, pBirthTime, pBirthTime));
520 return rc;
521}
522
523
524
525
526/**
527 * Internal worker for RTFileRename and RTFileMove.
528 *
529 * @returns iprt status code.
530 * @param pszSrc The source filename.
531 * @param pszDst The destination filename.
532 * @param fFlags The windows MoveFileEx flags.
533 * @param fFileType The filetype. We use the RTFMODE filetypes here. If it's 0,
534 * anything goes. If it's RTFS_TYPE_DIRECTORY we'll check that the
535 * source is a directory. If Its RTFS_TYPE_FILE we'll check that it's
536 * not a directory (we are NOT checking whether it's a file).
537 */
538DECLHIDDEN(int) rtPathWin32MoveRename(const char *pszSrc, const char *pszDst, uint32_t fFlags, RTFMODE fFileType)
539{
540 /*
541 * Convert the strings.
542 */
543 PRTUTF16 pwszSrc;
544 int rc = RTPathWinFromUtf8(&pwszSrc, pszSrc, 0 /*fFlags*/);
545 if (RT_SUCCESS(rc))
546 {
547 PRTUTF16 pwszDst;
548 rc = RTPathWinFromUtf8(&pwszDst, pszDst, 0 /*fFlags*/);
549 if (RT_SUCCESS(rc))
550 {
551 /*
552 * Check object type if requested.
553 * This is open to race conditions.
554 */
555 if (fFileType)
556 {
557 DWORD dwAttr = GetFileAttributesW(pwszSrc);
558 if (dwAttr == INVALID_FILE_ATTRIBUTES)
559 rc = RTErrConvertFromWin32(GetLastError());
560 else if (RTFS_IS_DIRECTORY(fFileType))
561 rc = dwAttr & FILE_ATTRIBUTE_DIRECTORY ? VINF_SUCCESS : VERR_NOT_A_DIRECTORY;
562 else
563 rc = dwAttr & FILE_ATTRIBUTE_DIRECTORY ? VERR_IS_A_DIRECTORY : VINF_SUCCESS;
564 }
565 if (RT_SUCCESS(rc))
566 {
567 if (MoveFileExW(pwszSrc, pwszDst, fFlags))
568 rc = VINF_SUCCESS;
569 else
570 {
571 DWORD Err = GetLastError();
572 rc = RTErrConvertFromWin32(Err);
573 Log(("MoveFileExW('%s', '%s', %#x, %RTfmode): fails with rc=%Rrc & lasterr=%d\n",
574 pszSrc, pszDst, fFlags, fFileType, rc, Err));
575 }
576 }
577 RTPathWinFree(pwszDst);
578 }
579 RTPathWinFree(pwszSrc);
580 }
581 return rc;
582}
583
584
585RTR3DECL(int) RTPathRename(const char *pszSrc, const char *pszDst, unsigned fRename)
586{
587 /*
588 * Validate input.
589 */
590 AssertPtrReturn(pszSrc, VERR_INVALID_POINTER);
591 AssertPtrReturn(pszDst, VERR_INVALID_POINTER);
592 AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
593 AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
594 AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
595
596 /*
597 * Call the worker.
598 */
599 int rc = rtPathWin32MoveRename(pszSrc, pszDst, fRename & RTPATHRENAME_FLAGS_REPLACE ? MOVEFILE_REPLACE_EXISTING : 0, 0);
600
601 LogFlow(("RTPathRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n", pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
602 return rc;
603}
604
605
606RTR3DECL(int) RTPathUnlink(const char *pszPath, uint32_t fUnlink)
607{
608 RT_NOREF_PV(pszPath); RT_NOREF_PV(fUnlink);
609 return VERR_NOT_IMPLEMENTED;
610}
611
612
613RTDECL(bool) RTPathExists(const char *pszPath)
614{
615 return RTPathExistsEx(pszPath, RTPATH_F_FOLLOW_LINK);
616}
617
618
619RTDECL(bool) RTPathExistsEx(const char *pszPath, uint32_t fFlags)
620{
621 /*
622 * Validate input.
623 */
624 AssertPtrReturn(pszPath, false);
625 AssertReturn(*pszPath, false);
626 Assert(RTPATH_F_IS_VALID(fFlags, 0));
627
628 /*
629 * Try query file info.
630 */
631 DWORD dwAttr;
632 PRTUTF16 pwszPath;
633 int rc = RTPathWinFromUtf8(&pwszPath, pszPath, 0 /*fFlags*/);
634 if (RT_SUCCESS(rc))
635 {
636 dwAttr = GetFileAttributesW(pwszPath);
637 RTPathWinFree(pwszPath);
638 }
639 else
640 dwAttr = INVALID_FILE_ATTRIBUTES;
641 if (dwAttr == INVALID_FILE_ATTRIBUTES)
642 return false;
643
644#ifdef FILE_ATTRIBUTE_REPARSE_POINT
645 if ( (fFlags & RTPATH_F_FOLLOW_LINK)
646 && (dwAttr & FILE_ATTRIBUTE_REPARSE_POINT))
647 {
648 AssertFailed();
649 /** @todo Symlinks: RTPathExists+RTPathExistsEx is misbehaving on symbolic
650 * links on Windows. */
651 }
652#endif
653
654 return true;
655}
656
657
658RTDECL(int) RTPathGetCurrent(char *pszPath, size_t cchPath)
659{
660 int rc;
661
662 if (cchPath > 0)
663 {
664 /*
665 * GetCurrentDirectory may in some cases omit the drive letter, according
666 * to MSDN, thus the GetFullPathName call.
667 */
668 RTUTF16 wszCurPath[RTPATH_MAX];
669 if (GetCurrentDirectoryW(RTPATH_MAX, wszCurPath))
670 {
671 RTUTF16 wszFullPath[RTPATH_MAX];
672 if (GetFullPathNameW(wszCurPath, RTPATH_MAX, wszFullPath, NULL))
673 {
674 if ( wszFullPath[1] == ':'
675 && RT_C_IS_LOWER(wszFullPath[0]))
676 wszFullPath[0] = RT_C_TO_UPPER(wszFullPath[0]);
677
678 rc = RTUtf16ToUtf8Ex(&wszFullPath[0], RTSTR_MAX, &pszPath, cchPath, NULL);
679 }
680 else
681 rc = RTErrConvertFromWin32(GetLastError());
682 }
683 else
684 rc = RTErrConvertFromWin32(GetLastError());
685 }
686 else
687 rc = VERR_BUFFER_OVERFLOW;
688 return rc;
689}
690
691
692RTDECL(int) RTPathSetCurrent(const char *pszPath)
693{
694 /*
695 * Validate input.
696 */
697 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
698 AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
699
700 /*
701 * This interface is almost identical to the Windows API.
702 */
703 PRTUTF16 pwszPath;
704 int rc = RTPathWinFromUtf8(&pwszPath, pszPath, 0 /*fFlags*/);
705 if (RT_SUCCESS(rc))
706 {
707 /** @todo improve the slash stripping a bit? */
708 size_t cwc = RTUtf16Len(pwszPath);
709 if ( cwc >= 2
710 && ( pwszPath[cwc - 1] == L'/'
711 || pwszPath[cwc - 1] == L'\\')
712 && pwszPath[cwc - 2] != ':')
713 pwszPath[cwc - 1] = L'\0';
714
715 if (!SetCurrentDirectoryW(pwszPath))
716 rc = RTErrConvertFromWin32(GetLastError());
717
718 RTPathWinFree(pwszPath);
719 }
720 return rc;
721}
722
723
724RTDECL(int) RTPathGetCurrentOnDrive(char chDrive, char *pszPath, size_t cbPath)
725{
726 int rc;
727 if (cbPath > 0)
728 {
729 WCHAR wszInput[4];
730 wszInput[0] = chDrive;
731 wszInput[1] = ':';
732 wszInput[2] = '\0';
733 RTUTF16 wszFullPath[RTPATH_MAX];
734 if (GetFullPathNameW(wszInput, RTPATH_MAX, wszFullPath, NULL))
735 rc = RTUtf16ToUtf8Ex(&wszFullPath[0], RTSTR_MAX, &pszPath, cbPath, NULL);
736 else
737 rc = RTErrConvertFromWin32(GetLastError());
738 }
739 else
740 rc = VERR_BUFFER_OVERFLOW;
741 return rc;
742}
743
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