VirtualBox

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

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

iprt: split out RTFileQueryInfo bits from fileio-posix.cpp into fileio2-posix.cpp to try avoid dragging in getpwuid_r when not needed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1/* $Id: fileio2-posix.cpp 34016 2010-11-12 00:20:45Z vboxsync $ */
2/** @file
3 * IPRT - File I/O, POSIX, Part 2.
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_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#include <iprt/file.h>
60#include <iprt/path.h>
61#include <iprt/assert.h>
62#include <iprt/string.h>
63#include <iprt/err.h>
64#include <iprt/log.h>
65#include "internal/file.h"
66#include "internal/fs.h"
67#include "internal/path.h"
68
69
70
71RTR3DECL(int) RTFileQueryInfo(RTFILE File, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
72{
73 /*
74 * Validate input.
75 */
76 if (File == NIL_RTFILE)
77 {
78 AssertMsgFailed(("Invalid File=%RTfile\n", File));
79 return VERR_INVALID_PARAMETER;
80 }
81 if (!pObjInfo)
82 {
83 AssertMsgFailed(("Invalid pObjInfo=%p\n", pObjInfo));
84 return VERR_INVALID_PARAMETER;
85 }
86 if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
87 || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
88 {
89 AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
90 return VERR_INVALID_PARAMETER;
91 }
92
93 /*
94 * Query file info.
95 */
96 struct stat Stat;
97 if (fstat((int)File, &Stat))
98 {
99 int rc = RTErrConvertFromErrno(errno);
100 Log(("RTFileQueryInfo(%RTfile,,%d): returns %Rrc\n", File, enmAdditionalAttribs, rc));
101 return rc;
102 }
103
104 /*
105 * Setup the returned data.
106 */
107 rtFsConvertStatToObjInfo(pObjInfo, &Stat, NULL, 0);
108
109 /*
110 * Requested attributes (we cannot provide anything actually).
111 */
112 switch (enmAdditionalAttribs)
113 {
114 case RTFSOBJATTRADD_NOTHING:
115 case RTFSOBJATTRADD_UNIX:
116 /* done */
117 break;
118
119 case RTFSOBJATTRADD_UNIX_OWNER:
120 rtFsObjInfoAttrSetUnixOwner(pObjInfo, Stat.st_uid);
121 break;
122
123 case RTFSOBJATTRADD_UNIX_GROUP:
124 rtFsObjInfoAttrSetUnixGroup(pObjInfo, Stat.st_gid);
125 break;
126
127 case RTFSOBJATTRADD_EASIZE:
128 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
129 pObjInfo->Attr.u.EASize.cb = 0;
130 break;
131
132 default:
133 AssertMsgFailed(("Impossible!\n"));
134 return VERR_INTERNAL_ERROR;
135 }
136
137 LogFlow(("RTFileQueryInfo(%RTfile,,%d): returns VINF_SUCCESS\n", File, enmAdditionalAttribs));
138 return VINF_SUCCESS;
139}
140
141
142RTR3DECL(int) RTFileSetTimes(RTFILE File, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
143 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
144{
145 /*
146 * We can only set AccessTime and ModificationTime, so if neither
147 * are specified we can return immediately.
148 */
149 if (!pAccessTime && !pModificationTime)
150 return VINF_SUCCESS;
151
152 /*
153 * Convert the input to timeval, getting the missing one if necessary,
154 * and call the API which does the change.
155 */
156 struct timeval aTimevals[2];
157 if (pAccessTime && pModificationTime)
158 {
159 RTTimeSpecGetTimeval(pAccessTime, &aTimevals[0]);
160 RTTimeSpecGetTimeval(pModificationTime, &aTimevals[1]);
161 }
162 else
163 {
164 RTFSOBJINFO ObjInfo;
165 int rc = RTFileQueryInfo(File, &ObjInfo, RTFSOBJATTRADD_UNIX);
166 if (RT_FAILURE(rc))
167 return rc;
168 RTTimeSpecGetTimeval(pAccessTime ? pAccessTime : &ObjInfo.AccessTime, &aTimevals[0]);
169 RTTimeSpecGetTimeval(pModificationTime ? pModificationTime : &ObjInfo.ModificationTime, &aTimevals[1]);
170 }
171
172 if (futimes((int)File, aTimevals))
173 {
174 int rc = RTErrConvertFromErrno(errno);
175 Log(("RTFileSetTimes(%RTfile,%p,%p,,): returns %Rrc\n", File, pAccessTime, pModificationTime, rc));
176 return rc;
177 }
178 return VINF_SUCCESS;
179}
180
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