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