VirtualBox

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

Last change on this file since 28863 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette