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