VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/path/RTPathAbsEx.cpp@ 21673

最後變更 在這個檔案從21673是 21673,由 vboxsync 提交於 16 年 前

IPRT: Split up r3/path.cpp.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 5.2 KB
 
1/* $Id: RTPathAbsEx.cpp 21673 2009-07-17 12:10:10Z vboxsync $ */
2/** @file
3 * IPRT - RTPathAbsEx
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.alldomusa.eu.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#include "internal/iprt.h"
36#include <iprt/path.h>
37#include <iprt/err.h>
38#include <iprt/param.h>
39#include <iprt/string.h>
40#include "internal/fs.h"
41
42
43
44/**
45 * Returns the length of the volume name specifier of the given path.
46 * If no such specifier zero is returned.
47 */
48size_t rtPathVolumeSpecLen(const char *pszPath)
49{
50#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
51 if (pszPath && *pszPath)
52 {
53 /* UTC path. */
54 /** @todo r=bird: it's UNC and we have to check that the next char isn't a
55 * slash, then skip both the server and the share name. */
56 if ( (pszPath[0] == '\\' || pszPath[0] == '/')
57 && (pszPath[1] == '\\' || pszPath[1] == '/'))
58 return strcspn(pszPath + 2, "\\/") + 2;
59
60 /* Drive letter. */
61 if ( pszPath[1] == ':'
62 && toupper(pszPath[0]) >= 'A' && toupper(pszPath[0]) <= 'Z')
63 return 2;
64 }
65 return 0;
66
67#else
68 /* This isn't quite right when looking at the above stuff, but it works assuming that '//' does not mean UNC. */
69 /// @todo (dmik) well, it's better to consider there's no volume name
70 // at all on *nix systems
71 return 0;
72// return pszPath && pszPath[0] == '/';
73#endif
74}
75
76
77/**
78 * Get the absolute path (no symlinks, no . or .. components), assuming the
79 * given base path as the current directory. The resulting path doesn't have
80 * to exist.
81 *
82 * @returns iprt status code.
83 * @param pszBase The base path to act like a current directory.
84 * When NULL, the actual cwd is used (i.e. the call
85 * is equivalent to RTPathAbs(pszPath, ...).
86 * @param pszPath The path to resolve.
87 * @param pszAbsPath Where to store the absolute path.
88 * @param cchAbsPath Size of the buffer.
89 */
90RTDECL(int) RTPathAbsEx(const char *pszBase, const char *pszPath, char *pszAbsPath, size_t cchAbsPath)
91{
92 if (pszBase && pszPath && !rtPathVolumeSpecLen(pszPath))
93 {
94#if defined(RT_OS_WINDOWS)
95 /* The format for very long paths is not supported. */
96 if ( (pszBase[0] == '/' || pszBase[0] == '\\')
97 && (pszBase[1] == '/' || pszBase[1] == '\\')
98 && pszBase[2] == '?'
99 && (pszBase[3] == '/' || pszBase[3] == '\\'))
100 return VERR_INVALID_NAME;
101#endif
102
103 /** @todo there are a couple of things which isn't 100% correct, although the
104 * current code will have to work for now - I don't have time to fix it right now.
105 *
106 * 1) On Windows & OS/2 we confuse '/' with an abspath spec and will
107 * not necessarily resolve it on the right drive.
108 * 2) A trailing slash in the base might cause UNC names to be created.
109 * 3) The lengths total doesn't have to be less than max length
110 * if the pszPath starts with a slash.
111 */
112 size_t cchBase = strlen(pszBase);
113 size_t cchPath = strlen(pszPath);
114 if (cchBase + cchPath >= RTPATH_MAX)
115 return VERR_FILENAME_TOO_LONG;
116
117 bool fRootSpec = pszPath[0] == '/'
118#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
119 || pszPath[0] == '\\'
120#endif
121 ;
122 size_t cchVolSpec = rtPathVolumeSpecLen(pszBase);
123 char szPath[RTPATH_MAX];
124 if (fRootSpec)
125 {
126 /* join the disk name from base and the path */
127 memcpy(szPath, pszBase, cchVolSpec);
128 strcpy(&szPath[cchVolSpec], pszPath);
129 }
130 else
131 {
132 /* join the base path and the path */
133 strcpy(szPath, pszBase);
134 szPath[cchBase] = RTPATH_DELIMITER;
135 strcpy(&szPath[cchBase + 1], pszPath);
136 }
137 return RTPathAbs(szPath, pszAbsPath, cchAbsPath);
138 }
139
140 /* Fallback to the non *Ex version */
141 return RTPathAbs(pszPath, pszAbsPath, cchAbsPath);
142}
143
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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