1 | /* $Id: path-win.cpp 15813 2009-01-05 16:06:55Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Path manipulation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #define LOG_GROUP RTLOGGROUP_PATH
|
---|
36 | #include <Windows.h>
|
---|
37 |
|
---|
38 | #include <iprt/path.h>
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/time.h>
|
---|
42 | #include <iprt/mem.h>
|
---|
43 | #include <iprt/param.h>
|
---|
44 | #include <iprt/log.h>
|
---|
45 | #include <iprt/err.h>
|
---|
46 | #include "internal/path.h"
|
---|
47 | #include "internal/fs.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Get the real (no symlinks, no . or .. components) path, must exist.
|
---|
52 | *
|
---|
53 | * @returns iprt status code.
|
---|
54 | * @param pszPath The path to resolve.
|
---|
55 | * @param pszRealPath Where to store the real path.
|
---|
56 | * @param cchRealPath Size of the buffer.
|
---|
57 | */
|
---|
58 | RTDECL(int) RTPathReal(const char *pszPath, char *pszRealPath, size_t cchRealPath)
|
---|
59 | {
|
---|
60 | /*
|
---|
61 | * Convert to UTF-16, call Win32 APIs, convert back.
|
---|
62 | */
|
---|
63 | PRTUTF16 pwszPath;
|
---|
64 | int rc = RTStrToUtf16(pszPath, &pwszPath);
|
---|
65 | if (!RT_SUCCESS(rc))
|
---|
66 | return (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 | RTUtf16Free(pwszPath);
|
---|
86 |
|
---|
87 | return rc;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
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 | */
|
---|
99 | RTDECL(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 | /*
|
---|
127 | * Remove trailing slash if the path may be pointing to a directory.
|
---|
128 | * (See posix variant.)
|
---|
129 | */
|
---|
130 | if ( cch > 1
|
---|
131 | && RTPATH_IS_SLASH(pszAbsPath[cch - 1])
|
---|
132 | && !RTPATH_IS_VOLSEP(pszAbsPath[cch - 2])
|
---|
133 | && !RTPATH_IS_SLASH(pszAbsPath[cch - 2]))
|
---|
134 | pszAbsPath[cch - 1] = '\0';
|
---|
135 | }
|
---|
136 | }
|
---|
137 | else if (rc <= 0)
|
---|
138 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
139 | else
|
---|
140 | rc = VERR_FILENAME_TOO_LONG;
|
---|
141 |
|
---|
142 | RTUtf16Free(pwszPath);
|
---|
143 | return rc;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Gets the user home directory.
|
---|
149 | *
|
---|
150 | * @returns iprt status code.
|
---|
151 | * @param pszPath Buffer where to store the path.
|
---|
152 | * @param cchPath Buffer size in bytes.
|
---|
153 | */
|
---|
154 | RTDECL(int) RTPathUserHome(char *pszPath, size_t cchPath)
|
---|
155 | {
|
---|
156 | RTUTF16 wszPath[RTPATH_MAX];
|
---|
157 | DWORD dwAttr;
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * There are multiple definitions for what WE think of as user home...
|
---|
161 | */
|
---|
162 | if ( !GetEnvironmentVariableW(L"HOME", &wszPath[0], RTPATH_MAX)
|
---|
163 | || (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
|
---|
164 | || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
|
---|
165 | {
|
---|
166 | if ( !GetEnvironmentVariableW(L"USERPROFILE", &wszPath[0], RTPATH_MAX)
|
---|
167 | || (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
|
---|
168 | || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
|
---|
169 | {
|
---|
170 | /* %HOMEDRIVE%%HOMEPATH% */
|
---|
171 | if (!GetEnvironmentVariableW(L"HOMEDRIVE", &wszPath[0], RTPATH_MAX))
|
---|
172 | return VERR_PATH_NOT_FOUND;
|
---|
173 | size_t const cwc = RTUtf16Len(&wszPath[0]);
|
---|
174 | if ( !GetEnvironmentVariableW(L"HOMEPATH", &wszPath[cwc], RTPATH_MAX - (DWORD)cwc)
|
---|
175 | || (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
|
---|
176 | || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
|
---|
177 | return VERR_PATH_NOT_FOUND;
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * Convert and return.
|
---|
183 | */
|
---|
184 | return RTUtf16ToUtf8Ex(&wszPath[0], RTSTR_MAX, &pszPath, cchPath, NULL);
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
|
---|
189 | {
|
---|
190 | /*
|
---|
191 | * Validate input.
|
---|
192 | */
|
---|
193 | if (!pszPath)
|
---|
194 | {
|
---|
195 | AssertMsgFailed(("Invalid pszPath=%p\n", pszPath));
|
---|
196 | return VERR_INVALID_PARAMETER;
|
---|
197 | }
|
---|
198 | if (!pObjInfo)
|
---|
199 | {
|
---|
200 | AssertMsgFailed(("Invalid pObjInfo=%p\n", pObjInfo));
|
---|
201 | return VERR_INVALID_PARAMETER;
|
---|
202 | }
|
---|
203 | if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
|
---|
204 | || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
|
---|
205 | {
|
---|
206 | AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
|
---|
207 | return VERR_INVALID_PARAMETER;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /*
|
---|
211 | * Query file info.
|
---|
212 | */
|
---|
213 | WIN32_FILE_ATTRIBUTE_DATA Data;
|
---|
214 | #ifndef RT_DONT_CONVERT_FILENAMES
|
---|
215 | PRTUTF16 pwszPath;
|
---|
216 | int rc = RTStrToUtf16(pszPath, &pwszPath);
|
---|
217 | if (RT_FAILURE(rc))
|
---|
218 | return rc;
|
---|
219 | if (!GetFileAttributesExW(pwszPath, GetFileExInfoStandard, &Data))
|
---|
220 | {
|
---|
221 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
222 | RTUtf16Free(pwszPath);
|
---|
223 | return rc;
|
---|
224 | }
|
---|
225 | RTUtf16Free(pwszPath);
|
---|
226 | #else
|
---|
227 | if (!GetFileAttributesExA(pszPath, GetFileExInfoStandard, &Data))
|
---|
228 | return RTErrConvertFromWin32(GetLastError());
|
---|
229 | #endif
|
---|
230 |
|
---|
231 | /*
|
---|
232 | * Setup the returned data.
|
---|
233 | */
|
---|
234 | pObjInfo->cbObject = ((uint64_t)Data.nFileSizeHigh << 32)
|
---|
235 | | (uint64_t)Data.nFileSizeLow;
|
---|
236 | pObjInfo->cbAllocated = pObjInfo->cbObject;
|
---|
237 |
|
---|
238 | Assert(sizeof(uint64_t) == sizeof(Data.ftCreationTime));
|
---|
239 | RTTimeSpecSetNtTime(&pObjInfo->BirthTime, *(uint64_t *)&Data.ftCreationTime);
|
---|
240 | RTTimeSpecSetNtTime(&pObjInfo->AccessTime, *(uint64_t *)&Data.ftLastAccessTime);
|
---|
241 | RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, *(uint64_t *)&Data.ftLastWriteTime);
|
---|
242 | pObjInfo->ChangeTime = pObjInfo->ModificationTime;
|
---|
243 |
|
---|
244 | pObjInfo->Attr.fMode = rtFsModeFromDos((Data.dwFileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT,
|
---|
245 | pszPath, strlen(pszPath));
|
---|
246 |
|
---|
247 | /*
|
---|
248 | * Requested attributes (we cannot provide anything actually).
|
---|
249 | */
|
---|
250 | switch (enmAdditionalAttribs)
|
---|
251 | {
|
---|
252 | case RTFSOBJATTRADD_EASIZE:
|
---|
253 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
|
---|
254 | pObjInfo->Attr.u.EASize.cb = 0;
|
---|
255 | break;
|
---|
256 |
|
---|
257 | case RTFSOBJATTRADD_UNIX:
|
---|
258 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
|
---|
259 | pObjInfo->Attr.u.Unix.uid = ~0U;
|
---|
260 | pObjInfo->Attr.u.Unix.gid = ~0U;
|
---|
261 | pObjInfo->Attr.u.Unix.cHardlinks = 1;
|
---|
262 | pObjInfo->Attr.u.Unix.INodeIdDevice = 0; /** @todo use volume serial number */
|
---|
263 | pObjInfo->Attr.u.Unix.INodeId = 0; /** @todo use fileid (see GetFileInformationByHandle). */
|
---|
264 | pObjInfo->Attr.u.Unix.fFlags = 0;
|
---|
265 | pObjInfo->Attr.u.Unix.GenerationId = 0;
|
---|
266 | pObjInfo->Attr.u.Unix.Device = 0;
|
---|
267 | break;
|
---|
268 |
|
---|
269 | case RTFSOBJATTRADD_NOTHING:
|
---|
270 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
|
---|
271 | break;
|
---|
272 |
|
---|
273 | default:
|
---|
274 | AssertMsgFailed(("Impossible!\n"));
|
---|
275 | return VERR_INTERNAL_ERROR;
|
---|
276 | }
|
---|
277 |
|
---|
278 | return VINF_SUCCESS;
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | RTR3DECL(int) RTPathSetTimes(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
|
---|
283 | PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
|
---|
284 | {
|
---|
285 | /*
|
---|
286 | * Validate input.
|
---|
287 | */
|
---|
288 | AssertMsgReturn(VALID_PTR(pszPath), ("%p\n", pszPath), VERR_INVALID_POINTER);
|
---|
289 | AssertMsgReturn(*pszPath, ("%p\n", pszPath), VERR_INVALID_PARAMETER);
|
---|
290 | AssertMsgReturn(!pAccessTime || VALID_PTR(pAccessTime), ("%p\n", pAccessTime), VERR_INVALID_POINTER);
|
---|
291 | AssertMsgReturn(!pModificationTime || VALID_PTR(pModificationTime), ("%p\n", pModificationTime), VERR_INVALID_POINTER);
|
---|
292 | AssertMsgReturn(!pChangeTime || VALID_PTR(pChangeTime), ("%p\n", pChangeTime), VERR_INVALID_POINTER);
|
---|
293 | AssertMsgReturn(!pBirthTime || VALID_PTR(pBirthTime), ("%p\n", pBirthTime), VERR_INVALID_POINTER);
|
---|
294 |
|
---|
295 | /*
|
---|
296 | * Convert the path.
|
---|
297 | */
|
---|
298 | PRTUTF16 pwszPath;
|
---|
299 | int rc = RTStrToUtf16(pszPath, &pwszPath);
|
---|
300 | if (RT_SUCCESS(rc))
|
---|
301 | {
|
---|
302 | HANDLE hFile = CreateFileW(pwszPath,
|
---|
303 | FILE_WRITE_ATTRIBUTES, /* dwDesiredAccess */
|
---|
304 | FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, /* dwShareMode */
|
---|
305 | NULL, /* security attribs */
|
---|
306 | OPEN_EXISTING, /* dwCreationDisposition */
|
---|
307 | FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_NORMAL,
|
---|
308 | NULL);
|
---|
309 | if (hFile != INVALID_HANDLE_VALUE)
|
---|
310 | {
|
---|
311 | /*
|
---|
312 | * Check if it's a no-op.
|
---|
313 | */
|
---|
314 | if (!pAccessTime && !pModificationTime && !pBirthTime)
|
---|
315 | rc = VINF_SUCCESS; /* NOP */
|
---|
316 | else
|
---|
317 | {
|
---|
318 | /*
|
---|
319 | * Convert the input and call the API.
|
---|
320 | */
|
---|
321 | FILETIME CreationTimeFT;
|
---|
322 | PFILETIME pCreationTimeFT = NULL;
|
---|
323 | if (pBirthTime)
|
---|
324 | pCreationTimeFT = RTTimeSpecGetNtFileTime(pBirthTime, &CreationTimeFT);
|
---|
325 |
|
---|
326 | FILETIME LastAccessTimeFT;
|
---|
327 | PFILETIME pLastAccessTimeFT = NULL;
|
---|
328 | if (pAccessTime)
|
---|
329 | pLastAccessTimeFT = RTTimeSpecGetNtFileTime(pAccessTime, &LastAccessTimeFT);
|
---|
330 |
|
---|
331 | FILETIME LastWriteTimeFT;
|
---|
332 | PFILETIME pLastWriteTimeFT = NULL;
|
---|
333 | if (pModificationTime)
|
---|
334 | pLastWriteTimeFT = RTTimeSpecGetNtFileTime(pModificationTime, &LastWriteTimeFT);
|
---|
335 |
|
---|
336 | if (SetFileTime(hFile, pCreationTimeFT, pLastAccessTimeFT, pLastWriteTimeFT))
|
---|
337 | rc = VINF_SUCCESS;
|
---|
338 | else
|
---|
339 | {
|
---|
340 | DWORD Err = GetLastError();
|
---|
341 | rc = RTErrConvertFromWin32(Err);
|
---|
342 | Log(("RTPathSetTimes('%s', %p, %p, %p, %p): SetFileTime failed with lasterr %d (%Rrc)\n",
|
---|
343 | pszPath, pAccessTime, pModificationTime, pChangeTime, pBirthTime, Err, rc));
|
---|
344 | }
|
---|
345 | }
|
---|
346 | BOOL fRc = CloseHandle(hFile); Assert(fRc); NOREF(fRc);
|
---|
347 | }
|
---|
348 | else
|
---|
349 | {
|
---|
350 | DWORD Err = GetLastError();
|
---|
351 | rc = RTErrConvertFromWin32(Err);
|
---|
352 | Log(("RTPathSetTimes('%s',,,,): failed with %Rrc and lasterr=%u\n", pszPath, rc, Err));
|
---|
353 | }
|
---|
354 |
|
---|
355 | RTUtf16Free(pwszPath);
|
---|
356 | }
|
---|
357 |
|
---|
358 | LogFlow(("RTPathSetTimes(%p:{%s}, %p:{%RDtimespec}, %p:{%RDtimespec}, %p:{%RDtimespec}, %p:{%RDtimespec}): return %Rrc\n",
|
---|
359 | pszPath, pszPath, pAccessTime, pAccessTime, pModificationTime, pModificationTime,
|
---|
360 | pChangeTime, pChangeTime, pBirthTime, pBirthTime));
|
---|
361 | return rc;
|
---|
362 | }
|
---|
363 |
|
---|
364 |
|
---|
365 |
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Internal worker for RTFileRename and RTFileMove.
|
---|
369 | *
|
---|
370 | * @returns iprt status code.
|
---|
371 | * @param pszSrc The source filename.
|
---|
372 | * @param pszDst The destination filename.
|
---|
373 | * @param fFlags The windows MoveFileEx flags.
|
---|
374 | * @param fFileType The filetype. We use the RTFMODE filetypes here. If it's 0,
|
---|
375 | * anything goes. If it's RTFS_TYPE_DIRECTORY we'll check that the
|
---|
376 | * source is a directory. If Its RTFS_TYPE_FILE we'll check that it's
|
---|
377 | * not a directory (we are NOT checking whether it's a file).
|
---|
378 | */
|
---|
379 | int rtPathWin32MoveRename(const char *pszSrc, const char *pszDst, uint32_t fFlags, RTFMODE fFileType)
|
---|
380 | {
|
---|
381 | /*
|
---|
382 | * Convert the strings.
|
---|
383 | */
|
---|
384 | PRTUTF16 pwszSrc;
|
---|
385 | int rc = RTStrToUtf16(pszSrc, &pwszSrc);
|
---|
386 | if (RT_SUCCESS(rc))
|
---|
387 | {
|
---|
388 | PRTUTF16 pwszDst;
|
---|
389 | rc = RTStrToUtf16(pszDst, &pwszDst);
|
---|
390 | if (RT_SUCCESS(rc))
|
---|
391 | {
|
---|
392 | /*
|
---|
393 | * Check object type if requested.
|
---|
394 | * This is open to race conditions.
|
---|
395 | */
|
---|
396 | if (fFileType)
|
---|
397 | {
|
---|
398 | DWORD dwAttr = GetFileAttributesW(pwszSrc);
|
---|
399 | if (dwAttr == INVALID_FILE_ATTRIBUTES)
|
---|
400 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
401 | else if (RTFS_IS_DIRECTORY(fFileType))
|
---|
402 | rc = dwAttr & FILE_ATTRIBUTE_DIRECTORY ? VINF_SUCCESS : VERR_NOT_A_DIRECTORY;
|
---|
403 | else
|
---|
404 | rc = dwAttr & FILE_ATTRIBUTE_DIRECTORY ? VERR_IS_A_DIRECTORY : VINF_SUCCESS;
|
---|
405 | }
|
---|
406 | if (RT_SUCCESS(rc))
|
---|
407 | {
|
---|
408 | if (MoveFileExW(pwszSrc, pwszDst, fFlags))
|
---|
409 | rc = VINF_SUCCESS;
|
---|
410 | else
|
---|
411 | {
|
---|
412 | DWORD Err = GetLastError();
|
---|
413 | rc = RTErrConvertFromWin32(Err);
|
---|
414 | Log(("MoveFileExW('%s', '%s', %#x, %RTfmode): fails with rc=%Rrc & lasterr=%d\n",
|
---|
415 | pszSrc, pszDst, fFlags, fFileType, rc, Err));
|
---|
416 | }
|
---|
417 | }
|
---|
418 | RTUtf16Free(pwszDst);
|
---|
419 | }
|
---|
420 | RTUtf16Free(pwszSrc);
|
---|
421 | }
|
---|
422 | return rc;
|
---|
423 | }
|
---|
424 |
|
---|
425 |
|
---|
426 | RTR3DECL(int) RTPathRename(const char *pszSrc, const char *pszDst, unsigned fRename)
|
---|
427 | {
|
---|
428 | /*
|
---|
429 | * Validate input.
|
---|
430 | */
|
---|
431 | AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
|
---|
432 | AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
|
---|
433 | AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
|
---|
434 | AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
|
---|
435 | AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
|
---|
436 |
|
---|
437 | /*
|
---|
438 | * Call the worker.
|
---|
439 | */
|
---|
440 | int rc = rtPathWin32MoveRename(pszSrc, pszDst, fRename & RTPATHRENAME_FLAGS_REPLACE ? MOVEFILE_REPLACE_EXISTING : 0, 0);
|
---|
441 |
|
---|
442 | LogFlow(("RTPathRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n", pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
|
---|
443 | return rc;
|
---|
444 | }
|
---|
445 |
|
---|
446 |
|
---|
447 | /**
|
---|
448 | * Checks if the path exists.
|
---|
449 | *
|
---|
450 | * Symbolic links will all be attempted resolved.
|
---|
451 | *
|
---|
452 | * @returns true if it exists and false if it doesn't
|
---|
453 | * @param pszPath The path to check.
|
---|
454 | */
|
---|
455 | RTDECL(bool) RTPathExists(const char *pszPath)
|
---|
456 | {
|
---|
457 | /*
|
---|
458 | * Validate input.
|
---|
459 | */
|
---|
460 | AssertPtrReturn(pszPath, false);
|
---|
461 | AssertReturn(*pszPath, false);
|
---|
462 |
|
---|
463 | /*
|
---|
464 | * Try query file info.
|
---|
465 | */
|
---|
466 | #ifndef RT_DONT_CONVERT_FILENAMES
|
---|
467 | PRTUTF16 pwszPath;
|
---|
468 | int rc = RTStrToUtf16(pszPath, &pwszPath);
|
---|
469 | if (RT_SUCCESS(rc))
|
---|
470 | {
|
---|
471 | if (GetFileAttributesW(pwszPath) == INVALID_FILE_ATTRIBUTES)
|
---|
472 | rc = VERR_GENERAL_FAILURE;
|
---|
473 | RTUtf16Free(pwszPath);
|
---|
474 | }
|
---|
475 | #else
|
---|
476 | int rc = VINF_SUCCESS;
|
---|
477 | if (GetFileAttributesExA(pszPath) == INVALID_FILE_ATTRIBUTES)
|
---|
478 | rc = VERR_GENERAL_FAILURE;
|
---|
479 | #endif
|
---|
480 |
|
---|
481 | return RT_SUCCESS(rc);
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | RTDECL(int) RTPathGetCurrent(char *pszPath, size_t cchPath)
|
---|
486 | {
|
---|
487 | int rc;
|
---|
488 |
|
---|
489 | /*
|
---|
490 | * GetCurrentDirectory may in some cases omit the drive letter, according
|
---|
491 | * to MSDN, thus the GetFullPathName call.
|
---|
492 | */
|
---|
493 | #ifndef RT_DONT_CONVERT_FILENAMES
|
---|
494 | RTUTF16 wszCurPath[RTPATH_MAX];
|
---|
495 | if (GetCurrentDirectoryW(RTPATH_MAX, wszCurPath))
|
---|
496 | {
|
---|
497 | RTUTF16 wszFullPath[RTPATH_MAX];
|
---|
498 | if (GetFullPathNameW(wszCurPath, RTPATH_MAX, wszFullPath, NULL))
|
---|
499 | rc = RTUtf16ToUtf8Ex(&wszFullPath[0], RTSTR_MAX, &pszPath, cchPath, NULL);
|
---|
500 | else
|
---|
501 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
502 | }
|
---|
503 | else
|
---|
504 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
505 | #else
|
---|
506 | char szCurPath[RTPATH_MAX];
|
---|
507 | if (GetCurrentDirectory(RTPATH_MAX, szCurPath))
|
---|
508 | {
|
---|
509 | if (GetFullPathName(szCurPath, cchPath, pszPath, NULL))
|
---|
510 | rc = VINF_SUCCESS;
|
---|
511 | else
|
---|
512 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
513 | }
|
---|
514 | else
|
---|
515 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
516 | #endif
|
---|
517 | return rc;
|
---|
518 | }
|
---|
519 |
|
---|
520 |
|
---|
521 | RTDECL(int) RTPathSetCurrent(const char *pszPath)
|
---|
522 | {
|
---|
523 | /*
|
---|
524 | * Validate input.
|
---|
525 | */
|
---|
526 | AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
|
---|
527 | AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
|
---|
528 |
|
---|
529 | /*
|
---|
530 | * This interface is almost identical to the Windows API.
|
---|
531 | */
|
---|
532 | #ifndef RT_DONT_CONVERT_FILENAMES
|
---|
533 | PRTUTF16 pwszPath;
|
---|
534 | int rc = RTStrToUtf16(pszPath, &pwszPath);
|
---|
535 | if (RT_SUCCESS(rc))
|
---|
536 | {
|
---|
537 | /** @todo improove the slash stripping a bit? */
|
---|
538 | size_t cwc = RTUtf16Len(pwszPath);
|
---|
539 | if ( cwc >= 2
|
---|
540 | && ( pwszPath[cwc - 1] == L'/'
|
---|
541 | || pwszPath[cwc - 1] == L'\\')
|
---|
542 | && pwszPath[cwc - 2] != ':')
|
---|
543 | pwszPath[cwc - 1] = L'\0';
|
---|
544 |
|
---|
545 | if (!SetCurrentDirectoryW(pwszPath))
|
---|
546 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
547 |
|
---|
548 | RTUtf16Free(pwszPath);
|
---|
549 | }
|
---|
550 | #else
|
---|
551 | int rc = VINF_SUCCESS;
|
---|
552 | /** @todo improove the slash stripping a bit? */
|
---|
553 | char const *pszEnd = strchr(pszPath, '\0');
|
---|
554 | size_t const cchPath = pszPath - pszEnd;
|
---|
555 | if ( cchPath >= 2
|
---|
556 | && ( pszEnd[-1] == '/'
|
---|
557 | || pszEnd[-1] == '\\')
|
---|
558 | && pszEnd[-2] == ':')
|
---|
559 | {
|
---|
560 | char *pszCopy = (char *)RTMemTmpAlloc(cchPath);
|
---|
561 | if (pszCopy)
|
---|
562 | {
|
---|
563 | memcpy(pszCopy, pszPath, cchPath - 1);
|
---|
564 | pszCopy[cchPath - 1] = '\0';
|
---|
565 | if (!SetCurrentDirectory(pszCopy))
|
---|
566 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
567 | RTMemTmpFree(pszCopy);
|
---|
568 | }
|
---|
569 | else
|
---|
570 | rc = VERR_NO_MEMORY;
|
---|
571 | }
|
---|
572 | else
|
---|
573 | {
|
---|
574 | if (!SetCurrentDirectory(pszPath))
|
---|
575 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
576 | }
|
---|
577 | #endif
|
---|
578 | return rc;
|
---|
579 | }
|
---|
580 |
|
---|