VirtualBox

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

Last change on this file since 8155 was 8155, checked in by vboxsync, 17 years ago

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 16.8 KB
Line 
1/* $Id: path-win.cpp 8155 2008-04-18 15:16:47Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - 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/param.h>
43#include <iprt/log.h>
44#include <iprt/err.h>
45#include "internal/path.h"
46#include "internal/fs.h"
47
48
49/**
50 * Get the real (no symlinks, no . or .. components) path, must exist.
51 *
52 * @returns iprt status code.
53 * @param pszPath The path to resolve.
54 * @param pszRealPath Where to store the real path.
55 * @param cchRealPath Size of the buffer.
56 */
57RTDECL(int) RTPathReal(const char *pszPath, char *pszRealPath, unsigned cchRealPath)
58{
59 /*
60 * Convert to UTF-16, call Win32 APIs, convert back.
61 */
62 PRTUTF16 pwszPath;
63 int rc = RTStrToUtf16(pszPath, &pwszPath);
64 if (!RT_SUCCESS(rc))
65 return (rc);
66
67 LPWSTR lpFile;
68 WCHAR wsz[RTPATH_MAX];
69 rc = GetFullPathNameW((LPCWSTR)pwszPath, ELEMENTS(wsz), &wsz[0], &lpFile);
70 if (rc > 0 && rc < ELEMENTS(wsz))
71 {
72 /* Check that it exists. (Use RTPathAbs() to just resolve the name.) */
73 DWORD dwAttr = GetFileAttributesW(wsz);
74 if (dwAttr != INVALID_FILE_ATTRIBUTES)
75 rc = RTUtf16ToUtf8Ex((PRTUTF16)&wsz[0], RTSTR_MAX, &pszRealPath, cchRealPath, NULL);
76 else
77 rc = RTErrConvertFromWin32(GetLastError());
78 }
79 else if (rc <= 0)
80 rc = RTErrConvertFromWin32(GetLastError());
81 else
82 rc = VERR_FILENAME_TOO_LONG;
83
84 RTUtf16Free(pwszPath);
85
86 return rc;
87}
88
89
90/**
91 * Get the absolute path (no symlinks, no . or .. components), doesn't have to exit.
92 *
93 * @returns iprt status code.
94 * @param pszPath The path to resolve.
95 * @param pszAbsPath Where to store the absolute path.
96 * @param cchAbsPath Size of the buffer.
97 */
98RTDECL(int) RTPathAbs(const char *pszPath, char *pszAbsPath, unsigned cchAbsPath)
99{
100 /*
101 * Convert to UTF-16, call Win32 API, convert back.
102 */
103 LPWSTR pwszPath;
104 int rc = RTStrToUtf16(pszPath, &pwszPath);
105 if (!RT_SUCCESS(rc))
106 return (rc);
107
108 LPWSTR pwszFile; /* Ignored */
109 RTUTF16 wsz[RTPATH_MAX];
110 rc = GetFullPathNameW(pwszPath, RT_ELEMENTS(wsz), &wsz[0], &pwszFile);
111 if (rc > 0 && rc < RT_ELEMENTS(wsz))
112 rc = RTUtf16ToUtf8Ex(&wsz[0], RTSTR_MAX, &pszAbsPath, cchAbsPath, NULL);
113 else if (rc <= 0)
114 rc = RTErrConvertFromWin32(GetLastError());
115 else
116 rc = VERR_FILENAME_TOO_LONG;
117
118 RTUtf16Free(pwszPath);
119
120 return rc;
121}
122
123
124/**
125 * Gets the program path.
126 *
127 * @returns iprt status code.
128 * @param pszPath Buffer where to store the path.
129 * @param cchPath Buffer size in bytes.
130 */
131RTDECL(int) RTPathProgram(char *pszPath, unsigned cchPath)
132{
133 /*
134 * First time only.
135 */
136 if (!g_szrtProgramPath[0])
137 {
138 HMODULE hExe = GetModuleHandle(NULL);
139 if (!GetModuleFileName(hExe, &g_szrtProgramPath[0], sizeof(g_szrtProgramPath)))
140 {
141 AssertMsgFailed(("Couldn't get exe module name. lasterr=%d\n", GetLastError()));
142 return RTErrConvertFromWin32(GetLastError());
143 }
144 RTPathStripFilename(g_szrtProgramPath);
145 }
146
147 /*
148 * Calc the length and check if there is space before copying.
149 */
150 unsigned cch = strlen(g_szrtProgramPath) + 1;
151 if (cch <= cchPath)
152 {
153 memcpy(pszPath, g_szrtProgramPath, cch + 1);
154 return VINF_SUCCESS;
155 }
156
157 AssertMsgFailed(("Buffer too small (%d < %d)\n", cchPath, cch));
158 return VERR_BUFFER_OVERFLOW;
159}
160
161
162/**
163 * Gets the user home directory.
164 *
165 * @returns iprt status code.
166 * @param pszPath Buffer where to store the path.
167 * @param cchPath Buffer size in bytes.
168 */
169RTDECL(int) RTPathUserHome(char *pszPath, unsigned cchPath)
170{
171 RTUTF16 wszPath[RTPATH_MAX];
172 DWORD dwAttr;
173
174 /*
175 * There are multiple definitions for what WE think of as user home...
176 */
177 if ( !GetEnvironmentVariableW(L"HOME", &wszPath[0], RTPATH_MAX)
178 || (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
179 || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
180 {
181 if ( !GetEnvironmentVariableW(L"USERPROFILE", &wszPath[0], RTPATH_MAX)
182 || (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
183 || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
184 {
185 /* %HOMEDRIVE%%HOMEPATH% */
186 if (!GetEnvironmentVariableW(L"HOMEDRIVE", &wszPath[0], RTPATH_MAX))
187 return VERR_PATH_NOT_FOUND;
188 size_t const cwc = RTUtf16Len(&wszPath[0]);
189 if ( !GetEnvironmentVariableW(L"HOMEPATH", &wszPath[cwc], RTPATH_MAX - cwc)
190 || (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES
191 || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
192 return VERR_PATH_NOT_FOUND;
193 }
194 }
195
196 /*
197 * Convert and return.
198 */
199 return RTUtf16ToUtf8Ex(&wszPath[0], RTSTR_MAX, &pszPath, cchPath, NULL);
200}
201
202
203RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
204{
205 /*
206 * Validate input.
207 */
208 if (!pszPath)
209 {
210 AssertMsgFailed(("Invalid pszPath=%p\n", pszPath));
211 return VERR_INVALID_PARAMETER;
212 }
213 if (!pObjInfo)
214 {
215 AssertMsgFailed(("Invalid pObjInfo=%p\n", pObjInfo));
216 return VERR_INVALID_PARAMETER;
217 }
218 if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
219 || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
220 {
221 AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
222 return VERR_INVALID_PARAMETER;
223 }
224
225 /*
226 * Query file info.
227 */
228 WIN32_FILE_ATTRIBUTE_DATA Data;
229#ifndef RT_DONT_CONVERT_FILENAMES
230 PRTUTF16 pwszPath;
231 int rc = RTStrToUtf16(pszPath, &pwszPath);
232 if (RT_FAILURE(rc))
233 return rc;
234 if (!GetFileAttributesExW(pwszPath, GetFileExInfoStandard, &Data))
235 {
236 rc = RTErrConvertFromWin32(GetLastError());
237 RTUtf16Free(pwszPath);
238 return rc;
239 }
240 RTUtf16Free(pwszPath);
241#else
242 if (!GetFileAttributesExA(pszPath, GetFileExInfoStandard, &Data))
243 return RTErrConvertFromWin32(GetLastError());
244#endif
245
246 /*
247 * Setup the returned data.
248 */
249 pObjInfo->cbObject = ((uint64_t)Data.nFileSizeHigh << 32)
250 | (uint64_t)Data.nFileSizeLow;
251 pObjInfo->cbAllocated = pObjInfo->cbObject;
252
253 Assert(sizeof(uint64_t) == sizeof(Data.ftCreationTime));
254 RTTimeSpecSetNtTime(&pObjInfo->BirthTime, *(uint64_t *)&Data.ftCreationTime);
255 RTTimeSpecSetNtTime(&pObjInfo->AccessTime, *(uint64_t *)&Data.ftLastAccessTime);
256 RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, *(uint64_t *)&Data.ftLastWriteTime);
257 pObjInfo->ChangeTime = pObjInfo->ModificationTime;
258
259 pObjInfo->Attr.fMode = rtFsModeFromDos((Data.dwFileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT,
260 pszPath, strlen(pszPath));
261
262 /*
263 * Requested attributes (we cannot provide anything actually).
264 */
265 switch (enmAdditionalAttribs)
266 {
267 case RTFSOBJATTRADD_EASIZE:
268 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
269 pObjInfo->Attr.u.EASize.cb = 0;
270 break;
271
272 case RTFSOBJATTRADD_UNIX:
273 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
274 pObjInfo->Attr.u.Unix.uid = ~0U;
275 pObjInfo->Attr.u.Unix.gid = ~0U;
276 pObjInfo->Attr.u.Unix.cHardlinks = 1;
277 pObjInfo->Attr.u.Unix.INodeIdDevice = 0;
278 pObjInfo->Attr.u.Unix.INodeId = 0;
279 pObjInfo->Attr.u.Unix.fFlags = 0;
280 pObjInfo->Attr.u.Unix.GenerationId = 0;
281 pObjInfo->Attr.u.Unix.Device = 0;
282 break;
283
284 case RTFSOBJATTRADD_NOTHING:
285 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
286 break;
287
288 default:
289 AssertMsgFailed(("Impossible!\n"));
290 return VERR_INTERNAL_ERROR;
291 }
292
293 return VINF_SUCCESS;
294}
295
296
297RTR3DECL(int) RTPathSetTimes(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
298 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
299{
300 /*
301 * Validate input.
302 */
303 AssertMsgReturn(VALID_PTR(pszPath), ("%p\n", pszPath), VERR_INVALID_POINTER);
304 AssertMsgReturn(*pszPath, ("%p\n", pszPath), VERR_INVALID_PARAMETER);
305 AssertMsgReturn(!pAccessTime || VALID_PTR(pAccessTime), ("%p\n", pAccessTime), VERR_INVALID_POINTER);
306 AssertMsgReturn(!pModificationTime || VALID_PTR(pModificationTime), ("%p\n", pModificationTime), VERR_INVALID_POINTER);
307 AssertMsgReturn(!pChangeTime || VALID_PTR(pChangeTime), ("%p\n", pChangeTime), VERR_INVALID_POINTER);
308 AssertMsgReturn(!pBirthTime || VALID_PTR(pBirthTime), ("%p\n", pBirthTime), VERR_INVALID_POINTER);
309
310 /*
311 * Convert the path.
312 */
313 PRTUTF16 pwszPath;
314 int rc = RTStrToUtf16(pszPath, &pwszPath);
315 if (RT_SUCCESS(rc))
316 {
317 HANDLE hFile = CreateFileW(pwszPath,
318 FILE_WRITE_ATTRIBUTES, /* dwDesiredAccess */
319 FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, /* dwShareMode */
320 NULL, /* security attribs */
321 OPEN_EXISTING, /* dwCreationDisposition */
322 FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_NORMAL,
323 NULL);
324 if (hFile != INVALID_HANDLE_VALUE)
325 {
326 /*
327 * Check if it's a no-op.
328 */
329 if (!pAccessTime && !pModificationTime && !pBirthTime)
330 rc = VINF_SUCCESS; /* NOP */
331 else
332 {
333 /*
334 * Convert the input and call the API.
335 */
336 FILETIME CreationTimeFT;
337 PFILETIME pCreationTimeFT = NULL;
338 if (pBirthTime)
339 pCreationTimeFT = RTTimeSpecGetNtFileTime(pBirthTime, &CreationTimeFT);
340
341 FILETIME LastAccessTimeFT;
342 PFILETIME pLastAccessTimeFT = NULL;
343 if (pAccessTime)
344 pLastAccessTimeFT = RTTimeSpecGetNtFileTime(pAccessTime, &LastAccessTimeFT);
345
346 FILETIME LastWriteTimeFT;
347 PFILETIME pLastWriteTimeFT = NULL;
348 if (pModificationTime)
349 pLastWriteTimeFT = RTTimeSpecGetNtFileTime(pModificationTime, &LastWriteTimeFT);
350
351 if (SetFileTime(hFile, pCreationTimeFT, pLastAccessTimeFT, pLastWriteTimeFT))
352 rc = VINF_SUCCESS;
353 else
354 {
355 DWORD Err = GetLastError();
356 rc = RTErrConvertFromWin32(Err);
357 Log(("RTPathSetTimes('%s', %p, %p, %p, %p): SetFileTime failed with lasterr %d (%Vrc)\n",
358 pszPath, pAccessTime, pModificationTime, pChangeTime, pBirthTime, Err, rc));
359 }
360 }
361 BOOL fRc = CloseHandle(hFile); Assert(fRc); NOREF(fRc);
362 }
363 else
364 {
365 DWORD Err = GetLastError();
366 rc = RTErrConvertFromWin32(Err);
367 Log(("RTPathSetTimes('%s',,,,): failed with %Rrc and lasterr=%u\n", pszPath, rc, Err));
368 }
369
370 RTUtf16Free(pwszPath);
371 }
372
373 LogFlow(("RTPathSetTimes(%p:{%s}, %p:{%RDtimespec}, %p:{%RDtimespec}, %p:{%RDtimespec}, %p:{%RDtimespec}): return %Rrc\n",
374 pszPath, pszPath, pAccessTime, pAccessTime, pModificationTime, pModificationTime,
375 pChangeTime, pChangeTime, pBirthTime, pBirthTime));
376 return rc;
377}
378
379
380
381
382/**
383 * Internal worker for RTFileRename and RTFileMove.
384 *
385 * @returns iprt status code.
386 * @param pszSrc The source filename.
387 * @param pszDst The destination filename.
388 * @param fFlags The windows MoveFileEx flags.
389 * @param fFileType The filetype. We use the RTFMODE filetypes here. If it's 0,
390 * anything goes. If it's RTFS_TYPE_DIRECTORY we'll check that the
391 * source is a directory. If Its RTFS_TYPE_FILE we'll check that it's
392 * not a directory (we are NOT checking whether it's a file).
393 */
394int rtPathWin32MoveRename(const char *pszSrc, const char *pszDst, uint32_t fFlags, RTFMODE fFileType)
395{
396 /*
397 * Convert the strings.
398 */
399 PRTUTF16 pwszSrc;
400 int rc = RTStrToUtf16(pszSrc, &pwszSrc);
401 if (RT_SUCCESS(rc))
402 {
403 PRTUTF16 pwszDst;
404 rc = RTStrToUtf16(pszDst, &pwszDst);
405 if (RT_SUCCESS(rc))
406 {
407 /*
408 * Check object type if requested.
409 * This is open to race conditions.
410 */
411 if (fFileType)
412 {
413 DWORD dwAttr = GetFileAttributesW(pwszSrc);
414 if (dwAttr == INVALID_FILE_ATTRIBUTES)
415 rc = RTErrConvertFromWin32(GetLastError());
416 else if (RTFS_IS_DIRECTORY(fFileType))
417 rc = dwAttr & FILE_ATTRIBUTE_DIRECTORY ? VINF_SUCCESS : VERR_NOT_A_DIRECTORY;
418 else
419 rc = dwAttr & FILE_ATTRIBUTE_DIRECTORY ? VERR_IS_A_DIRECTORY : VINF_SUCCESS;
420 }
421 if (RT_SUCCESS(rc))
422 {
423 if (MoveFileExW(pwszSrc, pwszDst, fFlags))
424 rc = VINF_SUCCESS;
425 else
426 {
427 DWORD Err = GetLastError();
428 rc = RTErrConvertFromWin32(Err);
429 Log(("MoveFileExW('%s', '%s', %#x, %RTfmode): fails with rc=%Rrc & lasterr=%d\n",
430 pszSrc, pszDst, fFlags, fFileType, rc, Err));
431 }
432 }
433 RTUtf16Free(pwszDst);
434 }
435 RTUtf16Free(pwszSrc);
436 }
437 return rc;
438}
439
440
441RTR3DECL(int) RTPathRename(const char *pszSrc, const char *pszDst, unsigned fRename)
442{
443 /*
444 * Validate input.
445 */
446 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
447 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
448 AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
449 AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
450 AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
451
452 /*
453 * Call the worker.
454 */
455 int rc = rtPathWin32MoveRename(pszSrc, pszDst, fRename & RTPATHRENAME_FLAGS_REPLACE ? MOVEFILE_REPLACE_EXISTING : 0, 0);
456
457 LogFlow(("RTPathRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n", pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
458 return rc;
459}
460
461
462/**
463 * Checks if the path exists.
464 *
465 * Symbolic links will all be attempted resolved.
466 *
467 * @returns true if it exists and false if it doesn't
468 * @param pszPath The path to check.
469 */
470RTDECL(bool) RTPathExists(const char *pszPath)
471{
472 /*
473 * Validate input.
474 */
475 AssertPtrReturn(pszPath, false);
476 AssertReturn(*pszPath, false);
477
478 /*
479 * Try query file info.
480 */
481#ifndef RT_DONT_CONVERT_FILENAMES
482 PRTUTF16 pwszPath;
483 int rc = RTStrToUtf16(pszPath, &pwszPath);
484 if (RT_SUCCESS(rc))
485 {
486 if (GetFileAttributesW(pwszPath) == INVALID_FILE_ATTRIBUTES)
487 rc = VERR_GENERAL_FAILURE;
488 RTUtf16Free(pwszPath);
489 }
490#else
491 int rc = VINF_SUCCESS;
492 if (GetFileAttributesExA(pszPath) == INVALID_FILE_ATTRIBUTES)
493 rc = VERR_GENERAL_FAILURE;
494#endif
495
496 return RT_SUCCESS(rc);
497}
498
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