VirtualBox

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

Last change on this file since 81106 was 78184, checked in by vboxsync, 6 years ago

IPRT/RTPathGetCurrent/win: Upper case the drive letter so it is consistent with RTPathAbsEx. bugref:9172

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