VirtualBox

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

Last change on this file since 28 was 1, checked in by vboxsync, 55 years ago

import

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