VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win32/path-win32.cpp@ 5285

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

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 16.1 KB
Line 
1/* $Id: path-win32.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Path manipulation.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP RTLOGGROUP_PATH
23#include <Windows.h>
24
25#include <iprt/path.h>
26#include <iprt/assert.h>
27#include <iprt/string.h>
28#include <iprt/time.h>
29#include <iprt/param.h>
30#include <iprt/log.h>
31#include <iprt/err.h>
32#include "internal/path.h"
33#include "internal/fs.h"
34
35
36/**
37 * Get the real (no symlinks, no . or .. components) path, must exist.
38 *
39 * @returns iprt status code.
40 * @param pszPath The path to resolve.
41 * @param pszRealPath Where to store the real path.
42 * @param cchRealPath Size of the buffer.
43 */
44RTDECL(int) RTPathReal(const char *pszPath, char *pszRealPath, unsigned cchRealPath)
45{
46 /*
47 * Convert to UCS2, call Win32 APIs, convert back.
48 */
49 PRTUTF16 pwszPath;
50 int rc = RTStrUtf8ToUcs2(&pwszPath, pszPath);
51 if (!RT_SUCCESS(rc))
52 return (rc);
53
54 LPWSTR lpFile;
55 WCHAR wsz[RTPATH_MAX];
56 rc = GetFullPathNameW((LPCWSTR)pwszPath, ELEMENTS(wsz), &wsz[0], &lpFile);
57 if (rc > 0 && rc < ELEMENTS(wsz))
58 {
59 /* Check that it exists. (Use RTPathAbs() to just resolve the name.) */
60 DWORD dwAttr = GetFileAttributesW(wsz);
61 if (dwAttr != INVALID_FILE_ATTRIBUTES)
62 rc = RTStrUcs2ToUtf8Ex(&pszRealPath, cchRealPath, (PRTUTF16)&wsz[0]);
63 else
64 rc = RTErrConvertFromWin32(GetLastError());
65 }
66 else if (rc <= 0)
67 rc = RTErrConvertFromWin32(GetLastError());
68 else
69 rc = VERR_FILENAME_TOO_LONG;
70
71 RTStrUcs2Free(pwszPath);
72
73 return rc;
74}
75
76
77/**
78 * Get the absolute path (no symlinks, no . or .. components), doesn't have to exit.
79 *
80 * @returns iprt status code.
81 * @param pszPath The path to resolve.
82 * @param pszAbsPath Where to store the absolute path.
83 * @param cchAbsPath Size of the buffer.
84 */
85RTDECL(int) RTPathAbs(const char *pszPath, char *pszAbsPath, unsigned cchAbsPath)
86{
87 /*
88 * Convert to UCS2, call Win32 API, convert back.
89 */
90 LPWSTR lpwPath;
91 int rc = RTStrUtf8ToUcs2(&lpwPath, pszPath);
92 if (!RT_SUCCESS(rc))
93 return (rc);
94
95 LPWSTR lpFile;
96 RTUCS2 ucsz[RTPATH_MAX];
97 rc = GetFullPathNameW(lpwPath, ELEMENTS(ucsz), &ucsz[0], &lpFile);
98 if (rc > 0 && rc < ELEMENTS(ucsz))
99 rc = RTStrUcs2ToUtf8Ex(&pszAbsPath, cchAbsPath, &ucsz[0]);
100 else if (rc <= 0)
101 rc = RTErrConvertFromWin32(GetLastError());
102 else
103 rc = VERR_FILENAME_TOO_LONG;
104
105 RTStrUcs2Free(lpwPath);
106
107 return rc;
108}
109
110
111/**
112 * Gets the program path.
113 *
114 * @returns iprt status code.
115 * @param pszPath Buffer where to store the path.
116 * @param cchPath Buffer size in bytes.
117 */
118RTDECL(int) RTPathProgram(char *pszPath, unsigned cchPath)
119{
120 /*
121 * First time only.
122 */
123 if (!g_szrtProgramPath[0])
124 {
125 HMODULE hExe = GetModuleHandle(NULL);
126 if (!GetModuleFileName(hExe, &g_szrtProgramPath[0], sizeof(g_szrtProgramPath)))
127 {
128 AssertMsgFailed(("Couldn't get exe module name. lasterr=%d\n", GetLastError()));
129 return RTErrConvertFromWin32(GetLastError());
130 }
131 RTPathStripFilename(g_szrtProgramPath);
132 }
133
134 /*
135 * Calc the length and check if there is space before copying.
136 */
137 unsigned cch = strlen(g_szrtProgramPath) + 1;
138 if (cch <= cchPath)
139 {
140 memcpy(pszPath, g_szrtProgramPath, cch + 1);
141 return VINF_SUCCESS;
142 }
143
144 AssertMsgFailed(("Buffer too small (%d < %d)\n", cchPath, cch));
145 return VERR_BUFFER_OVERFLOW;
146}
147
148
149/**
150 * Gets the user home directory.
151 *
152 * @returns iprt status code.
153 * @param pszPath Buffer where to store the path.
154 * @param cchPath Buffer size in bytes.
155 */
156RTDECL(int) RTPathUserHome(char *pszPath, unsigned cchPath)
157{
158 RTUCS2 ucszPath[RTPATH_MAX];
159 DWORD dwAttr;
160
161 /*
162 * There are multiple definitions for what WE think of as user home...
163 */
164 if ( !GetEnvironmentVariableW(L"HOME", &ucszPath[0], RTPATH_MAX)
165 || (dwAttr = GetFileAttributesW(&ucszPath[0])) == INVALID_FILE_ATTRIBUTES
166 || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
167 {
168 if ( !GetEnvironmentVariableW(L"USERPROFILE", &ucszPath[0], RTPATH_MAX)
169 || (dwAttr = GetFileAttributesW(&ucszPath[0])) == INVALID_FILE_ATTRIBUTES
170 || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
171 {
172 if (!GetEnvironmentVariableW(L"HOMEDRIVE", &ucszPath[0], RTPATH_MAX))
173 return VERR_PATH_NOT_FOUND;
174 PRTUCS2 pucsz = &ucszPath[RTStrUcs2Len(&ucszPath[0])];
175 if ( !GetEnvironmentVariableW(L"HOMEPATH", &ucszPath[0], RTPATH_MAX)
176 || (dwAttr = GetFileAttributesW(&ucszPath[0])) == INVALID_FILE_ATTRIBUTES
177 || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
178 return VERR_PATH_NOT_FOUND;
179 }
180 }
181
182 /*
183 * Convert and return.
184 */
185 return RTStrUcs2ToUtf8Ex(&pszPath, cchPath, &ucszPath[0]);
186}
187
188
189RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
190{
191 /*
192 * Validate input.
193 */
194 if (!pszPath)
195 {
196 AssertMsgFailed(("Invalid pszPath=%p\n", pszPath));
197 return VERR_INVALID_PARAMETER;
198 }
199 if (!pObjInfo)
200 {
201 AssertMsgFailed(("Invalid pObjInfo=%p\n", pObjInfo));
202 return VERR_INVALID_PARAMETER;
203 }
204 if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
205 || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
206 {
207 AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
208 return VERR_INVALID_PARAMETER;
209 }
210
211 /*
212 * Query file info.
213 */
214 WIN32_FILE_ATTRIBUTE_DATA Data;
215#ifndef RT_DONT_CONVERT_FILENAMES
216 PRTUCS2 puszPath;
217 int rc = RTStrUtf8ToUcs2(&puszPath, pszPath);
218 if (RT_FAILURE(rc))
219 return rc;
220 if (!GetFileAttributesExW(puszPath, GetFileExInfoStandard, &Data))
221 {
222 rc = RTErrConvertFromWin32(GetLastError());
223 RTStrUcs2Free(puszPath);
224 return rc;
225 }
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;
263 pObjInfo->Attr.u.Unix.INodeId = 0;
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
282RTR3DECL(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 (%Vrc)\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 */
379int 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
426RTR3DECL(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 */
455RTDECL(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 PRTUCS2 puszPath;
468 int rc = RTStrUtf8ToUcs2(&puszPath, pszPath);
469 if (RT_SUCCESS(rc))
470 {
471 if (GetFileAttributesW(puszPath) == INVALID_FILE_ATTRIBUTES)
472 rc = VERR_GENERAL_FAILURE;
473 RTStrUcs2Free(puszPath);
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
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