1 | /* $Id: path-win.cpp 36611 2011-04-07 10:35:29Z 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.215389.xyz. 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 | RTDECL(int) RTPathUserDocuments(char *pszPath, size_t cchPath)
|
---|
184 | {
|
---|
185 | /*
|
---|
186 | * Validate input
|
---|
187 | */
|
---|
188 | AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
|
---|
189 | AssertReturn(cchPath, VERR_INVALID_PARAMETER);
|
---|
190 |
|
---|
191 | RTUTF16 wszPath[RTPATH_MAX];
|
---|
192 | HRESULT rc = SHGetFolderPath(0, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, wszPath);
|
---|
193 | if ( rc == S_OK /* Found */
|
---|
194 | || rc == S_FALSE) /* Found, but doesn't exists */
|
---|
195 | /*
|
---|
196 | * Convert and return.
|
---|
197 | */
|
---|
198 | return RTUtf16ToUtf8Ex(&wszPath[0], RTSTR_MAX, &pszPath, cchPath, NULL);
|
---|
199 |
|
---|
200 | return VERR_PATH_NOT_FOUND;
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
|
---|
205 | {
|
---|
206 | return RTPathQueryInfoEx(pszPath, pObjInfo, enmAdditionalAttribs, RTPATH_F_ON_LINK);
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | RTR3DECL(int) RTPathQueryInfoEx(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
|
---|
211 | {
|
---|
212 | /*
|
---|
213 | * Validate input.
|
---|
214 | */
|
---|
215 | AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
|
---|
216 | AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
|
---|
217 | AssertPtrReturn(pObjInfo, VERR_INVALID_POINTER);
|
---|
218 | AssertMsgReturn( enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING
|
---|
219 | && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
|
---|
220 | ("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs),
|
---|
221 | VERR_INVALID_PARAMETER);
|
---|
222 | AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
|
---|
223 |
|
---|
224 | /*
|
---|
225 | * Query file info.
|
---|
226 | */
|
---|
227 | WIN32_FILE_ATTRIBUTE_DATA Data;
|
---|
228 | PRTUTF16 pwszPath;
|
---|
229 | int rc = RTStrToUtf16(pszPath, &pwszPath);
|
---|
230 | if (RT_FAILURE(rc))
|
---|
231 | return rc;
|
---|
232 | if (!GetFileAttributesExW(pwszPath, GetFileExInfoStandard, &Data))
|
---|
233 | {
|
---|
234 | /* Fallback to FindFileFirst in case of sharing violation. */
|
---|
235 | if (GetLastError() == ERROR_SHARING_VIOLATION)
|
---|
236 | {
|
---|
237 | WIN32_FIND_DATAW FindData;
|
---|
238 | HANDLE hDir = FindFirstFileW(pwszPath, &FindData);
|
---|
239 | if (hDir == INVALID_HANDLE_VALUE)
|
---|
240 | {
|
---|
241 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
242 | RTUtf16Free(pwszPath);
|
---|
243 | return rc;
|
---|
244 | }
|
---|
245 | FindClose(hDir);
|
---|
246 |
|
---|
247 | Data.dwFileAttributes = FindData.dwFileAttributes;
|
---|
248 | Data.ftCreationTime = FindData.ftCreationTime;
|
---|
249 | Data.ftLastAccessTime = FindData.ftLastAccessTime;
|
---|
250 | Data.ftLastWriteTime = FindData.ftLastWriteTime;
|
---|
251 | Data.nFileSizeHigh = FindData.nFileSizeHigh;
|
---|
252 | Data.nFileSizeLow = FindData.nFileSizeLow;
|
---|
253 | }
|
---|
254 | else
|
---|
255 | {
|
---|
256 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
257 | RTUtf16Free(pwszPath);
|
---|
258 | return rc;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * Getting the information for the link target is a bit annoying and
|
---|
264 | * subject to the same access violation mess as above.. :/
|
---|
265 | */
|
---|
266 | /** @todo we're too lazy wrt to error paths here... */
|
---|
267 | if ( (fFlags & RTPATH_F_FOLLOW_LINK)
|
---|
268 | && (Data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
|
---|
269 | {
|
---|
270 | HANDLE hFinal = CreateFileW(pwszPath,
|
---|
271 | GENERIC_READ,
|
---|
272 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
---|
273 | NULL,
|
---|
274 | OPEN_EXISTING,
|
---|
275 | FILE_FLAG_BACKUP_SEMANTICS,
|
---|
276 | NULL);
|
---|
277 | if (hFinal != INVALID_HANDLE_VALUE)
|
---|
278 | {
|
---|
279 | BY_HANDLE_FILE_INFORMATION FileData;
|
---|
280 | if (GetFileInformationByHandle(hFinal, &FileData))
|
---|
281 | {
|
---|
282 | Data.dwFileAttributes = FileData.dwFileAttributes;
|
---|
283 | Data.ftCreationTime = FileData.ftCreationTime;
|
---|
284 | Data.ftLastAccessTime = FileData.ftLastAccessTime;
|
---|
285 | Data.ftLastWriteTime = FileData.ftLastWriteTime;
|
---|
286 | Data.nFileSizeHigh = FileData.nFileSizeHigh;
|
---|
287 | Data.nFileSizeLow = FileData.nFileSizeLow;
|
---|
288 | }
|
---|
289 | CloseHandle(hFinal);
|
---|
290 | }
|
---|
291 | else if (GetLastError() != ERROR_SHARING_VIOLATION)
|
---|
292 | {
|
---|
293 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
294 | RTUtf16Free(pwszPath);
|
---|
295 | return rc;
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | RTUtf16Free(pwszPath);
|
---|
300 |
|
---|
301 | /*
|
---|
302 | * Setup the returned data.
|
---|
303 | */
|
---|
304 | pObjInfo->cbObject = ((uint64_t)Data.nFileSizeHigh << 32)
|
---|
305 | | (uint64_t)Data.nFileSizeLow;
|
---|
306 | pObjInfo->cbAllocated = pObjInfo->cbObject;
|
---|
307 |
|
---|
308 | Assert(sizeof(uint64_t) == sizeof(Data.ftCreationTime));
|
---|
309 | RTTimeSpecSetNtTime(&pObjInfo->BirthTime, *(uint64_t *)&Data.ftCreationTime);
|
---|
310 | RTTimeSpecSetNtTime(&pObjInfo->AccessTime, *(uint64_t *)&Data.ftLastAccessTime);
|
---|
311 | RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, *(uint64_t *)&Data.ftLastWriteTime);
|
---|
312 | pObjInfo->ChangeTime = pObjInfo->ModificationTime;
|
---|
313 |
|
---|
314 | pObjInfo->Attr.fMode = rtFsModeFromDos((Data.dwFileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT,
|
---|
315 | pszPath, strlen(pszPath));
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * Requested attributes (we cannot provide anything actually).
|
---|
319 | */
|
---|
320 | switch (enmAdditionalAttribs)
|
---|
321 | {
|
---|
322 | case RTFSOBJATTRADD_NOTHING:
|
---|
323 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
|
---|
324 | break;
|
---|
325 |
|
---|
326 | case RTFSOBJATTRADD_UNIX:
|
---|
327 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
|
---|
328 | pObjInfo->Attr.u.Unix.uid = ~0U;
|
---|
329 | pObjInfo->Attr.u.Unix.gid = ~0U;
|
---|
330 | pObjInfo->Attr.u.Unix.cHardlinks = 1;
|
---|
331 | pObjInfo->Attr.u.Unix.INodeIdDevice = 0; /** @todo use volume serial number */
|
---|
332 | pObjInfo->Attr.u.Unix.INodeId = 0; /** @todo use fileid (see GetFileInformationByHandle). */
|
---|
333 | pObjInfo->Attr.u.Unix.fFlags = 0;
|
---|
334 | pObjInfo->Attr.u.Unix.GenerationId = 0;
|
---|
335 | pObjInfo->Attr.u.Unix.Device = 0;
|
---|
336 | break;
|
---|
337 |
|
---|
338 | case RTFSOBJATTRADD_UNIX_OWNER:
|
---|
339 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX_OWNER;
|
---|
340 | pObjInfo->Attr.u.UnixOwner.uid = ~0U;
|
---|
341 | pObjInfo->Attr.u.UnixOwner.szName[0] = '\0'; /** @todo return something sensible here. */
|
---|
342 | break;
|
---|
343 |
|
---|
344 | case RTFSOBJATTRADD_UNIX_GROUP:
|
---|
345 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX_GROUP;
|
---|
346 | pObjInfo->Attr.u.UnixGroup.gid = ~0U;
|
---|
347 | pObjInfo->Attr.u.UnixGroup.szName[0] = '\0';
|
---|
348 | break;
|
---|
349 |
|
---|
350 | case RTFSOBJATTRADD_EASIZE:
|
---|
351 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
|
---|
352 | pObjInfo->Attr.u.EASize.cb = 0;
|
---|
353 | break;
|
---|
354 |
|
---|
355 | default:
|
---|
356 | AssertMsgFailed(("Impossible!\n"));
|
---|
357 | return VERR_INTERNAL_ERROR;
|
---|
358 | }
|
---|
359 |
|
---|
360 | return VINF_SUCCESS;
|
---|
361 | }
|
---|
362 |
|
---|
363 |
|
---|
364 | RTR3DECL(int) RTPathSetTimes(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
|
---|
365 | PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
|
---|
366 | {
|
---|
367 | return RTPathSetTimesEx(pszPath, pAccessTime, pModificationTime, pChangeTime, pBirthTime, RTPATH_F_ON_LINK);
|
---|
368 | }
|
---|
369 |
|
---|
370 |
|
---|
371 | RTR3DECL(int) RTPathSetTimesEx(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
|
---|
372 | PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime, uint32_t fFlags)
|
---|
373 | {
|
---|
374 | /*
|
---|
375 | * Validate input.
|
---|
376 | */
|
---|
377 | AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
|
---|
378 | AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
|
---|
379 | AssertPtrNullReturn(pAccessTime, VERR_INVALID_POINTER);
|
---|
380 | AssertPtrNullReturn(pModificationTime, VERR_INVALID_POINTER);
|
---|
381 | AssertPtrNullReturn(pChangeTime, VERR_INVALID_POINTER);
|
---|
382 | AssertPtrNullReturn(pBirthTime, VERR_INVALID_POINTER);
|
---|
383 | AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
|
---|
384 |
|
---|
385 | /*
|
---|
386 | * Convert the path.
|
---|
387 | */
|
---|
388 | PRTUTF16 pwszPath;
|
---|
389 | int rc = RTStrToUtf16(pszPath, &pwszPath);
|
---|
390 | if (RT_SUCCESS(rc))
|
---|
391 | {
|
---|
392 | HANDLE hFile;
|
---|
393 | if (fFlags & RTPATH_F_FOLLOW_LINK)
|
---|
394 | hFile = CreateFileW(pwszPath,
|
---|
395 | FILE_WRITE_ATTRIBUTES, /* dwDesiredAccess */
|
---|
396 | FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, /* dwShareMode */
|
---|
397 | NULL, /* security attribs */
|
---|
398 | OPEN_EXISTING, /* dwCreationDisposition */
|
---|
399 | FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_NORMAL,
|
---|
400 | NULL);
|
---|
401 | else
|
---|
402 | {
|
---|
403 | /** @todo Symlink: Test RTPathSetTimesEx on Windows. (The code is disabled
|
---|
404 | * because it's not tested yet.) */
|
---|
405 | #if 0 //def FILE_FLAG_OPEN_REPARSE_POINT
|
---|
406 | hFile = CreateFileW(pwszPath,
|
---|
407 | FILE_WRITE_ATTRIBUTES, /* dwDesiredAccess */
|
---|
408 | FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, /* dwShareMode */
|
---|
409 | NULL, /* security attribs */
|
---|
410 | OPEN_EXISTING, /* dwCreationDisposition */
|
---|
411 | FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT,
|
---|
412 | NULL);
|
---|
413 |
|
---|
414 | if (hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER)
|
---|
415 | #endif
|
---|
416 | hFile = CreateFileW(pwszPath,
|
---|
417 | FILE_WRITE_ATTRIBUTES, /* dwDesiredAccess */
|
---|
418 | FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, /* dwShareMode */
|
---|
419 | NULL, /* security attribs */
|
---|
420 | OPEN_EXISTING, /* dwCreationDisposition */
|
---|
421 | FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_NORMAL,
|
---|
422 | NULL);
|
---|
423 | }
|
---|
424 | if (hFile != INVALID_HANDLE_VALUE)
|
---|
425 | {
|
---|
426 | /*
|
---|
427 | * Check if it's a no-op.
|
---|
428 | */
|
---|
429 | if (!pAccessTime && !pModificationTime && !pBirthTime)
|
---|
430 | rc = VINF_SUCCESS; /* NOP */
|
---|
431 | else
|
---|
432 | {
|
---|
433 | /*
|
---|
434 | * Convert the input and call the API.
|
---|
435 | */
|
---|
436 | FILETIME CreationTimeFT;
|
---|
437 | PFILETIME pCreationTimeFT = NULL;
|
---|
438 | if (pBirthTime)
|
---|
439 | pCreationTimeFT = RTTimeSpecGetNtFileTime(pBirthTime, &CreationTimeFT);
|
---|
440 |
|
---|
441 | FILETIME LastAccessTimeFT;
|
---|
442 | PFILETIME pLastAccessTimeFT = NULL;
|
---|
443 | if (pAccessTime)
|
---|
444 | pLastAccessTimeFT = RTTimeSpecGetNtFileTime(pAccessTime, &LastAccessTimeFT);
|
---|
445 |
|
---|
446 | FILETIME LastWriteTimeFT;
|
---|
447 | PFILETIME pLastWriteTimeFT = NULL;
|
---|
448 | if (pModificationTime)
|
---|
449 | pLastWriteTimeFT = RTTimeSpecGetNtFileTime(pModificationTime, &LastWriteTimeFT);
|
---|
450 |
|
---|
451 | if (SetFileTime(hFile, pCreationTimeFT, pLastAccessTimeFT, pLastWriteTimeFT))
|
---|
452 | rc = VINF_SUCCESS;
|
---|
453 | else
|
---|
454 | {
|
---|
455 | DWORD Err = GetLastError();
|
---|
456 | rc = RTErrConvertFromWin32(Err);
|
---|
457 | Log(("RTPathSetTimes('%s', %p, %p, %p, %p): SetFileTime failed with lasterr %d (%Rrc)\n",
|
---|
458 | pszPath, pAccessTime, pModificationTime, pChangeTime, pBirthTime, Err, rc));
|
---|
459 | }
|
---|
460 | }
|
---|
461 | BOOL fRc = CloseHandle(hFile); Assert(fRc); NOREF(fRc);
|
---|
462 | }
|
---|
463 | else
|
---|
464 | {
|
---|
465 | DWORD Err = GetLastError();
|
---|
466 | rc = RTErrConvertFromWin32(Err);
|
---|
467 | Log(("RTPathSetTimes('%s',,,,): failed with %Rrc and lasterr=%u\n", pszPath, rc, Err));
|
---|
468 | }
|
---|
469 |
|
---|
470 | RTUtf16Free(pwszPath);
|
---|
471 | }
|
---|
472 |
|
---|
473 | LogFlow(("RTPathSetTimes(%p:{%s}, %p:{%RDtimespec}, %p:{%RDtimespec}, %p:{%RDtimespec}, %p:{%RDtimespec}): return %Rrc\n",
|
---|
474 | pszPath, pszPath, pAccessTime, pAccessTime, pModificationTime, pModificationTime,
|
---|
475 | pChangeTime, pChangeTime, pBirthTime, pBirthTime));
|
---|
476 | return rc;
|
---|
477 | }
|
---|
478 |
|
---|
479 |
|
---|
480 |
|
---|
481 |
|
---|
482 | /**
|
---|
483 | * Internal worker for RTFileRename and RTFileMove.
|
---|
484 | *
|
---|
485 | * @returns iprt status code.
|
---|
486 | * @param pszSrc The source filename.
|
---|
487 | * @param pszDst The destination filename.
|
---|
488 | * @param fFlags The windows MoveFileEx flags.
|
---|
489 | * @param fFileType The filetype. We use the RTFMODE filetypes here. If it's 0,
|
---|
490 | * anything goes. If it's RTFS_TYPE_DIRECTORY we'll check that the
|
---|
491 | * source is a directory. If Its RTFS_TYPE_FILE we'll check that it's
|
---|
492 | * not a directory (we are NOT checking whether it's a file).
|
---|
493 | */
|
---|
494 | DECLHIDDEN(int) rtPathWin32MoveRename(const char *pszSrc, const char *pszDst, uint32_t fFlags, RTFMODE fFileType)
|
---|
495 | {
|
---|
496 | /*
|
---|
497 | * Convert the strings.
|
---|
498 | */
|
---|
499 | PRTUTF16 pwszSrc;
|
---|
500 | int rc = RTStrToUtf16(pszSrc, &pwszSrc);
|
---|
501 | if (RT_SUCCESS(rc))
|
---|
502 | {
|
---|
503 | PRTUTF16 pwszDst;
|
---|
504 | rc = RTStrToUtf16(pszDst, &pwszDst);
|
---|
505 | if (RT_SUCCESS(rc))
|
---|
506 | {
|
---|
507 | /*
|
---|
508 | * Check object type if requested.
|
---|
509 | * This is open to race conditions.
|
---|
510 | */
|
---|
511 | if (fFileType)
|
---|
512 | {
|
---|
513 | DWORD dwAttr = GetFileAttributesW(pwszSrc);
|
---|
514 | if (dwAttr == INVALID_FILE_ATTRIBUTES)
|
---|
515 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
516 | else if (RTFS_IS_DIRECTORY(fFileType))
|
---|
517 | rc = dwAttr & FILE_ATTRIBUTE_DIRECTORY ? VINF_SUCCESS : VERR_NOT_A_DIRECTORY;
|
---|
518 | else
|
---|
519 | rc = dwAttr & FILE_ATTRIBUTE_DIRECTORY ? VERR_IS_A_DIRECTORY : VINF_SUCCESS;
|
---|
520 | }
|
---|
521 | if (RT_SUCCESS(rc))
|
---|
522 | {
|
---|
523 | if (MoveFileExW(pwszSrc, pwszDst, fFlags))
|
---|
524 | rc = VINF_SUCCESS;
|
---|
525 | else
|
---|
526 | {
|
---|
527 | DWORD Err = GetLastError();
|
---|
528 | rc = RTErrConvertFromWin32(Err);
|
---|
529 | Log(("MoveFileExW('%s', '%s', %#x, %RTfmode): fails with rc=%Rrc & lasterr=%d\n",
|
---|
530 | pszSrc, pszDst, fFlags, fFileType, rc, Err));
|
---|
531 | }
|
---|
532 | }
|
---|
533 | RTUtf16Free(pwszDst);
|
---|
534 | }
|
---|
535 | RTUtf16Free(pwszSrc);
|
---|
536 | }
|
---|
537 | return rc;
|
---|
538 | }
|
---|
539 |
|
---|
540 |
|
---|
541 | RTR3DECL(int) RTPathRename(const char *pszSrc, const char *pszDst, unsigned fRename)
|
---|
542 | {
|
---|
543 | /*
|
---|
544 | * Validate input.
|
---|
545 | */
|
---|
546 | AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
|
---|
547 | AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
|
---|
548 | AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
|
---|
549 | AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
|
---|
550 | AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
|
---|
551 |
|
---|
552 | /*
|
---|
553 | * Call the worker.
|
---|
554 | */
|
---|
555 | int rc = rtPathWin32MoveRename(pszSrc, pszDst, fRename & RTPATHRENAME_FLAGS_REPLACE ? MOVEFILE_REPLACE_EXISTING : 0, 0);
|
---|
556 |
|
---|
557 | LogFlow(("RTPathRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n", pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
|
---|
558 | return rc;
|
---|
559 | }
|
---|
560 |
|
---|
561 |
|
---|
562 | RTDECL(bool) RTPathExists(const char *pszPath)
|
---|
563 | {
|
---|
564 | return RTPathExistsEx(pszPath, RTPATH_F_FOLLOW_LINK);
|
---|
565 | }
|
---|
566 |
|
---|
567 |
|
---|
568 | RTDECL(bool) RTPathExistsEx(const char *pszPath, uint32_t fFlags)
|
---|
569 | {
|
---|
570 | /*
|
---|
571 | * Validate input.
|
---|
572 | */
|
---|
573 | AssertPtrReturn(pszPath, false);
|
---|
574 | AssertReturn(*pszPath, false);
|
---|
575 | Assert(RTPATH_F_IS_VALID(fFlags, 0));
|
---|
576 |
|
---|
577 | /*
|
---|
578 | * Try query file info.
|
---|
579 | */
|
---|
580 | DWORD dwAttr;
|
---|
581 | PRTUTF16 pwszPath;
|
---|
582 | int rc = RTStrToUtf16(pszPath, &pwszPath);
|
---|
583 | if (RT_SUCCESS(rc))
|
---|
584 | {
|
---|
585 | dwAttr = GetFileAttributesW(pwszPath);
|
---|
586 | RTUtf16Free(pwszPath);
|
---|
587 | }
|
---|
588 | else
|
---|
589 | dwAttr = INVALID_FILE_ATTRIBUTES;
|
---|
590 | if (dwAttr == INVALID_FILE_ATTRIBUTES)
|
---|
591 | return false;
|
---|
592 |
|
---|
593 | #ifdef FILE_ATTRIBUTE_REPARSE_POINT
|
---|
594 | if ( (fFlags & RTPATH_F_FOLLOW_LINK)
|
---|
595 | && (dwAttr & FILE_ATTRIBUTE_REPARSE_POINT))
|
---|
596 | {
|
---|
597 | AssertFailed();
|
---|
598 | /** @todo Symlinks: RTPathExists+RTPathExistsEx is misbehaving on symbolic
|
---|
599 | * links on Windows. */
|
---|
600 | }
|
---|
601 | #endif
|
---|
602 |
|
---|
603 | return true;
|
---|
604 | }
|
---|
605 |
|
---|
606 |
|
---|
607 | RTDECL(int) RTPathGetCurrent(char *pszPath, size_t cchPath)
|
---|
608 | {
|
---|
609 | int rc;
|
---|
610 |
|
---|
611 | /*
|
---|
612 | * GetCurrentDirectory may in some cases omit the drive letter, according
|
---|
613 | * to MSDN, thus the GetFullPathName call.
|
---|
614 | */
|
---|
615 | RTUTF16 wszCurPath[RTPATH_MAX];
|
---|
616 | if (GetCurrentDirectoryW(RTPATH_MAX, wszCurPath))
|
---|
617 | {
|
---|
618 | RTUTF16 wszFullPath[RTPATH_MAX];
|
---|
619 | if (GetFullPathNameW(wszCurPath, RTPATH_MAX, wszFullPath, NULL))
|
---|
620 | rc = RTUtf16ToUtf8Ex(&wszFullPath[0], RTSTR_MAX, &pszPath, cchPath, NULL);
|
---|
621 | else
|
---|
622 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
623 | }
|
---|
624 | else
|
---|
625 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
626 | return rc;
|
---|
627 | }
|
---|
628 |
|
---|
629 |
|
---|
630 | RTDECL(int) RTPathSetCurrent(const char *pszPath)
|
---|
631 | {
|
---|
632 | /*
|
---|
633 | * Validate input.
|
---|
634 | */
|
---|
635 | AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
|
---|
636 | AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
|
---|
637 |
|
---|
638 | /*
|
---|
639 | * This interface is almost identical to the Windows API.
|
---|
640 | */
|
---|
641 | PRTUTF16 pwszPath;
|
---|
642 | int rc = RTStrToUtf16(pszPath, &pwszPath);
|
---|
643 | if (RT_SUCCESS(rc))
|
---|
644 | {
|
---|
645 | /** @todo improve the slash stripping a bit? */
|
---|
646 | size_t cwc = RTUtf16Len(pwszPath);
|
---|
647 | if ( cwc >= 2
|
---|
648 | && ( pwszPath[cwc - 1] == L'/'
|
---|
649 | || pwszPath[cwc - 1] == L'\\')
|
---|
650 | && pwszPath[cwc - 2] != ':')
|
---|
651 | pwszPath[cwc - 1] = L'\0';
|
---|
652 |
|
---|
653 | if (!SetCurrentDirectoryW(pwszPath))
|
---|
654 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
655 |
|
---|
656 | RTUtf16Free(pwszPath);
|
---|
657 | }
|
---|
658 | return rc;
|
---|
659 | }
|
---|
660 |
|
---|