VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/path2-posix.cpp@ 34015

Last change on this file since 34015 was 34015, checked in by vboxsync, 14 years ago

iprt: try split out some of the RTPathQueryInfo* dependent bits into separate files in the posix world.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.6 KB
Line 
1/* $Id: path2-posix.cpp 34015 2010-11-12 00:15:05Z vboxsync $ */
2/** @file
3 * IPRT - Path Manipulation, POSIX, Part 2 - RTPathQueryInfo.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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 <stdlib.h>
33#include <limits.h>
34#include <errno.h>
35#include <unistd.h>
36#include <sys/stat.h>
37#include <sys/time.h>
38#include <stdio.h>
39#include <sys/types.h>
40
41#include <iprt/path.h>
42#include <iprt/env.h>
43#include <iprt/assert.h>
44#include <iprt/string.h>
45#include <iprt/err.h>
46#include <iprt/log.h>
47#include "internal/path.h"
48#include "internal/process.h"
49#include "internal/fs.h"
50
51#ifdef RT_OS_L4
52# include <l4/vboxserver/vboxserver.h>
53#endif
54
55
56RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
57{
58 return RTPathQueryInfoEx(pszPath, pObjInfo, enmAdditionalAttribs, RTPATH_F_ON_LINK);
59}
60
61
62RTR3DECL(int) RTPathQueryInfoEx(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
63{
64 /*
65 * Validate input.
66 */
67 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
68 AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
69 AssertPtrReturn(pObjInfo, VERR_INVALID_POINTER);
70 AssertMsgReturn( enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING
71 && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
72 ("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs),
73 VERR_INVALID_PARAMETER);
74 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
75
76 /*
77 * Convert the filename.
78 */
79 char const *pszNativePath;
80 int rc = rtPathToNative(&pszNativePath, pszPath, NULL);
81 if (RT_SUCCESS(rc))
82 {
83 struct stat Stat;
84 if (fFlags & RTPATH_F_FOLLOW_LINK)
85 rc = stat(pszNativePath, &Stat);
86 else
87 rc = lstat(pszNativePath, &Stat); /** @todo how doesn't have lstat again? */
88 if (!rc)
89 {
90 rtFsConvertStatToObjInfo(pObjInfo, &Stat, pszPath, 0);
91 switch (enmAdditionalAttribs)
92 {
93 case RTFSOBJATTRADD_NOTHING:
94 case RTFSOBJATTRADD_UNIX:
95 Assert(pObjInfo->Attr.enmAdditional == RTFSOBJATTRADD_UNIX);
96 break;
97
98 case RTFSOBJATTRADD_UNIX_OWNER:
99 rtFsObjInfoAttrSetUnixOwner(pObjInfo, Stat.st_uid);
100 break;
101
102 case RTFSOBJATTRADD_UNIX_GROUP:
103 rtFsObjInfoAttrSetUnixGroup(pObjInfo, Stat.st_gid);
104 break;
105
106 case RTFSOBJATTRADD_EASIZE:
107 /** @todo Use SGI extended attribute interface to query EA info. */
108 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
109 pObjInfo->Attr.u.EASize.cb = 0;
110 break;
111
112 default:
113 AssertMsgFailed(("Impossible!\n"));
114 return VERR_INTERNAL_ERROR;
115 }
116 }
117 else
118 rc = RTErrConvertFromErrno(errno);
119 rtPathFreeNative(pszNativePath, pszPath);
120 }
121
122 LogFlow(("RTPathQueryInfoEx(%p:{%s}, pObjInfo=%p, %d): returns %Rrc\n",
123 pszPath, pszPath, pObjInfo, enmAdditionalAttribs, rc));
124 return rc;
125}
126
127
128RTR3DECL(int) RTPathSetTimes(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
129 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
130{
131 return RTPathSetTimesEx(pszPath, pAccessTime, pModificationTime, pChangeTime, pBirthTime, RTPATH_F_ON_LINK);
132}
133
134
135RTR3DECL(int) RTPathSetTimesEx(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
136 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime, uint32_t fFlags)
137{
138 /*
139 * Validate input.
140 */
141 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
142 AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
143 AssertPtrNullReturn(pAccessTime, VERR_INVALID_POINTER);
144 AssertPtrNullReturn(pModificationTime, VERR_INVALID_POINTER);
145 AssertPtrNullReturn(pChangeTime, VERR_INVALID_POINTER);
146 AssertPtrNullReturn(pBirthTime, VERR_INVALID_POINTER);
147 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
148
149 /*
150 * Convert the paths.
151 */
152 char const *pszNativePath;
153 int rc = rtPathToNative(&pszNativePath, pszPath, NULL);
154 if (RT_SUCCESS(rc))
155 {
156 RTFSOBJINFO ObjInfo;
157
158 /*
159 * If it's a no-op, we'll only verify the existance of the file.
160 */
161 if (!pAccessTime && !pModificationTime)
162 rc = RTPathQueryInfoEx(pszPath, &ObjInfo, RTFSOBJATTRADD_NOTHING, fFlags);
163 else
164 {
165 /*
166 * Convert the input to timeval, getting the missing one if necessary,
167 * and call the API which does the change.
168 */
169 struct timeval aTimevals[2];
170 if (pAccessTime && pModificationTime)
171 {
172 RTTimeSpecGetTimeval(pAccessTime, &aTimevals[0]);
173 RTTimeSpecGetTimeval(pModificationTime, &aTimevals[1]);
174 }
175 else
176 {
177 rc = RTPathQueryInfoEx(pszPath, &ObjInfo, RTFSOBJATTRADD_UNIX, fFlags);
178 if (RT_SUCCESS(rc))
179 {
180 RTTimeSpecGetTimeval(pAccessTime ? pAccessTime : &ObjInfo.AccessTime, &aTimevals[0]);
181 RTTimeSpecGetTimeval(pModificationTime ? pModificationTime : &ObjInfo.ModificationTime, &aTimevals[1]);
182 }
183 else
184 Log(("RTPathSetTimes('%s',%p,%p,,): RTPathQueryInfo failed with %Rrc\n",
185 pszPath, pAccessTime, pModificationTime, rc));
186 }
187 if (RT_SUCCESS(rc))
188 {
189 if (fFlags & RTPATH_F_FOLLOW_LINK)
190 {
191 if (utimes(pszNativePath, aTimevals))
192 rc = RTErrConvertFromErrno(errno);
193 }
194#if (defined(RT_OS_DARWIN) && MAC_OS_X_VERSION_MIN_REQUIRED >= 1050) \
195 || defined(RT_OS_FREEBSD) \
196 || defined(RT_OS_LINUX) \
197 || defined(RT_OS_OS2) /** @todo who really has lutimes? */
198 else
199 {
200 if (lutimes(pszNativePath, aTimevals))
201 rc = RTErrConvertFromErrno(errno);
202 }
203#else
204 else
205 {
206 if (pAccessTime && pModificationTime)
207 rc = RTPathQueryInfoEx(pszPath, &ObjInfo, RTFSOBJATTRADD_UNIX, fFlags);
208 if (RT_SUCCESS(rc) && RTFS_IS_SYMLINK(ObjInfo.Attr.fMode))
209 rc = VERR_NS_SYMLINK_SET_TIME;
210 else if (RT_SUCCESS(rc))
211 {
212 if (utimes(pszNativePath, aTimevals))
213 rc = RTErrConvertFromErrno(errno);
214 }
215 }
216#endif
217 if (RT_FAILURE(rc))
218 Log(("RTPathSetTimes('%s',%p,%p,,): failed with %Rrc and errno=%d\n",
219 pszPath, pAccessTime, pModificationTime, rc, errno));
220 }
221 }
222 rtPathFreeNative(pszNativePath, pszPath);
223 }
224
225 LogFlow(("RTPathSetTimes(%p:{%s}, %p:{%RDtimespec}, %p:{%RDtimespec}, %p:{%RDtimespec}, %p:{%RDtimespec}): return %Rrc\n",
226 pszPath, pszPath, pAccessTime, pAccessTime, pModificationTime, pModificationTime,
227 pChangeTime, pChangeTime, pBirthTime, pBirthTime, rc));
228 return rc;
229}
230
231
232RTR3DECL(int) RTPathSetOwner(const char *pszPath, uint32_t uid, uint32_t gid)
233{
234 return RTPathSetOwnerEx(pszPath, uid, gid, RTPATH_F_ON_LINK);
235}
236
237
238RTR3DECL(int) RTPathSetOwnerEx(const char *pszPath, uint32_t uid, uint32_t gid, uint32_t fFlags)
239{
240 /*
241 * Validate input.
242 */
243 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
244 AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
245 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
246 uid_t uidNative = uid != UINT32_MAX ? (uid_t)uid : (uid_t)-1;
247 AssertReturn(uid == uidNative, VERR_INVALID_PARAMETER);
248 gid_t gidNative = gid != UINT32_MAX ? (gid_t)gid : (uid_t)-1;
249 AssertReturn(gid == gidNative, VERR_INVALID_PARAMETER);
250
251 /*
252 * Convert the path.
253 */
254 char const *pszNativePath;
255 int rc = rtPathToNative(&pszNativePath, pszPath, NULL);
256 if (RT_SUCCESS(rc))
257 {
258 if (fFlags & RTPATH_F_FOLLOW_LINK)
259 {
260 if (chown(pszNativePath, uidNative, gidNative))
261 rc = RTErrConvertFromErrno(errno);
262 }
263#if 1
264 else
265 {
266 if (lchown(pszNativePath, uidNative, gidNative))
267 rc = RTErrConvertFromErrno(errno);
268 }
269#else
270 else
271 {
272 RTFSOBJINFO ObjInfo;
273 rc = RTPathQueryInfoEx(pszPath, &ObjInfo, RTFSOBJATTRADD_UNIX, fFlags);
274 if (RT_SUCCESS(rc) && RTFS_IS_SYMLINK(ObjInfo.Attr.fMode))
275 rc = VERR_NS_SYMLINK_CHANGE_OWNER;
276 else if (RT_SUCCESS(rc))
277 {
278 if (lchown(pszNativePath, uidNative, gidNative))
279 rc = RTErrConvertFromErrno(errno);
280 }
281 }
282#endif
283 if (RT_FAILURE(rc))
284 Log(("RTPathSetOwnerEx('%s',%d,%d): failed with %Rrc and errno=%d\n",
285 pszPath, uid, gid, rc, errno));
286
287 rtPathFreeNative(pszNativePath, pszPath);
288 }
289
290 LogFlow(("RTPathSetOwnerEx(%p:{%s}, uid, gid): return %Rrc\n",
291 pszPath, pszPath, uid, gid, rc));
292 return rc;
293}
294
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