VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/fileio2-posix.cpp@ 43437

Last change on this file since 43437 was 43363, checked in by vboxsync, 13 years ago

Haiku Additions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1/* $Id: fileio2-posix.cpp 43363 2012-09-20 09:56:07Z vboxsync $ */
2/** @file
3 * IPRT - File I/O, POSIX, Part 2.
4 */
5
6/*
7 * Copyright (C) 2006-2012 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_FILE
32
33#include <errno.h>
34#include <sys/stat.h>
35#include <sys/types.h>
36#ifdef _MSC_VER
37# include <io.h>
38# include <stdio.h>
39#else
40# include <unistd.h>
41# include <sys/time.h>
42#endif
43#ifdef RT_OS_LINUX
44# include <sys/file.h>
45#endif
46#if defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006)
47# include <io.h>
48#endif
49#ifdef RT_OS_L4
50/* This is currently ifdef'ed out in the relevant L4 header file */
51/* Same as `utimes', but takes an open file descriptor instead of a name. */
52extern int futimes(int __fd, __const struct timeval __tvp[2]) __THROW;
53#endif
54
55#ifdef RT_OS_SOLARIS
56# define futimes(filedes, timeval) futimesat(filedes, NULL, timeval)
57#endif
58
59#ifdef RT_OS_HAIKU
60# define USE_FUTIMENS
61#endif
62
63#include <iprt/file.h>
64#include <iprt/path.h>
65#include <iprt/assert.h>
66#include <iprt/string.h>
67#include <iprt/err.h>
68#include <iprt/log.h>
69#include "internal/file.h"
70#include "internal/fs.h"
71#include "internal/path.h"
72
73
74
75RTR3DECL(int) RTFileQueryInfo(RTFILE hFile, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
76{
77 /*
78 * Validate input.
79 */
80 AssertReturn(hFile != NIL_RTFILE, VERR_INVALID_PARAMETER);
81 AssertPtrReturn(pObjInfo, VERR_INVALID_PARAMETER);
82 if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
83 || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
84 {
85 AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
86 return VERR_INVALID_PARAMETER;
87 }
88
89 /*
90 * Query file info.
91 */
92 struct stat Stat;
93 if (fstat(RTFileToNative(hFile), &Stat))
94 {
95 int rc = RTErrConvertFromErrno(errno);
96 Log(("RTFileQueryInfo(%RTfile,,%d): returns %Rrc\n", hFile, enmAdditionalAttribs, rc));
97 return rc;
98 }
99
100 /*
101 * Setup the returned data.
102 */
103 rtFsConvertStatToObjInfo(pObjInfo, &Stat, NULL, 0);
104
105 /*
106 * Requested attributes (we cannot provide anything actually).
107 */
108 switch (enmAdditionalAttribs)
109 {
110 case RTFSOBJATTRADD_NOTHING:
111 case RTFSOBJATTRADD_UNIX:
112 /* done */
113 break;
114
115 case RTFSOBJATTRADD_UNIX_OWNER:
116 rtFsObjInfoAttrSetUnixOwner(pObjInfo, Stat.st_uid);
117 break;
118
119 case RTFSOBJATTRADD_UNIX_GROUP:
120 rtFsObjInfoAttrSetUnixGroup(pObjInfo, Stat.st_gid);
121 break;
122
123 case RTFSOBJATTRADD_EASIZE:
124 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
125 pObjInfo->Attr.u.EASize.cb = 0;
126 break;
127
128 default:
129 AssertMsgFailed(("Impossible!\n"));
130 return VERR_INTERNAL_ERROR;
131 }
132
133 LogFlow(("RTFileQueryInfo(%RTfile,,%d): returns VINF_SUCCESS\n", hFile, enmAdditionalAttribs));
134 return VINF_SUCCESS;
135}
136
137
138RTR3DECL(int) RTFileSetTimes(RTFILE hFile, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
139 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
140{
141 NOREF(pChangeTime); NOREF(pBirthTime);
142
143 /*
144 * We can only set AccessTime and ModificationTime, so if neither
145 * are specified we can return immediately.
146 */
147 if (!pAccessTime && !pModificationTime)
148 return VINF_SUCCESS;
149
150#ifdef USE_FUTIMENS
151 struct timespec aTimespecs[2];
152 if (pAccessTime && pModificationTime)
153 {
154 memcpy(&aTimespecs[0], pAccessTime, sizeof(struct timespec));
155 memcpy(&aTimespecs[1], pModificationTime, sizeof(struct timespec));
156 }
157 else
158 {
159 RTFSOBJINFO ObjInfo;
160 int rc = RTFileQueryInfo(hFile, &ObjInfo, RTFSOBJATTRADD_UNIX);
161 if (RT_FAILURE(rc))
162 return rc;
163 memcpy(&aTimespecs[0], pAccessTime ? pAccessTime : &ObjInfo.AccessTime, sizeof(struct timespec));
164 memcpy(&aTimespecs[1], pModificationTime ? pModificationTime : &ObjInfo.ModificationTime, sizeof(struct timespec));
165 }
166
167 if (futimens(RTFileToNative(hFile), aTimespecs))
168 {
169 int rc = RTErrConvertFromErrno(errno);
170 Log(("RTFileSetTimes(%RTfile,%p,%p,,): returns %Rrc\n", hFile, pAccessTime, pModificationTime, rc));
171 return rc;
172 }
173#else
174 /*
175 * Convert the input to timeval, getting the missing one if necessary,
176 * and call the API which does the change.
177 */
178 struct timeval aTimevals[2];
179 if (pAccessTime && pModificationTime)
180 {
181 RTTimeSpecGetTimeval(pAccessTime, &aTimevals[0]);
182 RTTimeSpecGetTimeval(pModificationTime, &aTimevals[1]);
183 }
184 else
185 {
186 RTFSOBJINFO ObjInfo;
187 int rc = RTFileQueryInfo(hFile, &ObjInfo, RTFSOBJATTRADD_UNIX);
188 if (RT_FAILURE(rc))
189 return rc;
190 RTTimeSpecGetTimeval(pAccessTime ? pAccessTime : &ObjInfo.AccessTime, &aTimevals[0]);
191 RTTimeSpecGetTimeval(pModificationTime ? pModificationTime : &ObjInfo.ModificationTime, &aTimevals[1]);
192 }
193
194 /* XXX this falls back to utimes("/proc/self/fd/...",...) for older kernels/glibcs and this
195 * will not work for hardened builds where this directory is owned by root.root and mode 0500 */
196 if (futimes(RTFileToNative(hFile), aTimevals))
197 {
198 int rc = RTErrConvertFromErrno(errno);
199 Log(("RTFileSetTimes(%RTfile,%p,%p,,): returns %Rrc\n", hFile, pAccessTime, pModificationTime, rc));
200 return rc;
201 }
202#endif
203 return VINF_SUCCESS;
204}
205
Note: See TracBrowser for help on using the repository browser.

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