VirtualBox

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

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

IPRT: Implemented long filename support for windows (except for LoadLibrary). bugref:9248

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