1 | /* $Id: fileio2-posix.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File I/O, POSIX, Part 2.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 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 |
|
---|
50 | #ifdef RT_OS_SOLARIS
|
---|
51 | # define futimes(filedes, timeval) futimesat(filedes, NULL, timeval)
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | #ifdef RT_OS_HAIKU
|
---|
55 | # define USE_FUTIMENS
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | #include <iprt/file.h>
|
---|
59 | #include <iprt/path.h>
|
---|
60 | #include <iprt/assert.h>
|
---|
61 | #include <iprt/string.h>
|
---|
62 | #include <iprt/err.h>
|
---|
63 | #include <iprt/log.h>
|
---|
64 | #include "internal/file.h"
|
---|
65 | #include "internal/fs.h"
|
---|
66 | #include "internal/path.h"
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | RTR3DECL(int) RTFileQueryInfo(RTFILE hFile, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
|
---|
71 | {
|
---|
72 | /*
|
---|
73 | * Validate input.
|
---|
74 | */
|
---|
75 | AssertReturn(hFile != NIL_RTFILE, VERR_INVALID_PARAMETER);
|
---|
76 | AssertPtrReturn(pObjInfo, VERR_INVALID_PARAMETER);
|
---|
77 | if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
|
---|
78 | || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
|
---|
79 | {
|
---|
80 | AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
|
---|
81 | return VERR_INVALID_PARAMETER;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /*
|
---|
85 | * Query file info.
|
---|
86 | */
|
---|
87 | struct stat Stat;
|
---|
88 | if (fstat(RTFileToNative(hFile), &Stat))
|
---|
89 | {
|
---|
90 | int rc = RTErrConvertFromErrno(errno);
|
---|
91 | Log(("RTFileQueryInfo(%RTfile,,%d): returns %Rrc\n", hFile, enmAdditionalAttribs, rc));
|
---|
92 | return rc;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Setup the returned data.
|
---|
97 | */
|
---|
98 | rtFsConvertStatToObjInfo(pObjInfo, &Stat, NULL, 0);
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Requested attributes (we cannot provide anything actually).
|
---|
102 | */
|
---|
103 | switch (enmAdditionalAttribs)
|
---|
104 | {
|
---|
105 | case RTFSOBJATTRADD_NOTHING:
|
---|
106 | case RTFSOBJATTRADD_UNIX:
|
---|
107 | /* done */
|
---|
108 | break;
|
---|
109 |
|
---|
110 | case RTFSOBJATTRADD_UNIX_OWNER:
|
---|
111 | rtFsObjInfoAttrSetUnixOwner(pObjInfo, Stat.st_uid);
|
---|
112 | break;
|
---|
113 |
|
---|
114 | case RTFSOBJATTRADD_UNIX_GROUP:
|
---|
115 | rtFsObjInfoAttrSetUnixGroup(pObjInfo, Stat.st_gid);
|
---|
116 | break;
|
---|
117 |
|
---|
118 | case RTFSOBJATTRADD_EASIZE:
|
---|
119 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
|
---|
120 | pObjInfo->Attr.u.EASize.cb = 0;
|
---|
121 | break;
|
---|
122 |
|
---|
123 | default:
|
---|
124 | AssertMsgFailed(("Impossible!\n"));
|
---|
125 | return VERR_INTERNAL_ERROR;
|
---|
126 | }
|
---|
127 |
|
---|
128 | LogFlow(("RTFileQueryInfo(%RTfile,,%d): returns VINF_SUCCESS\n", hFile, enmAdditionalAttribs));
|
---|
129 | return VINF_SUCCESS;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | RTR3DECL(int) RTFileSetTimes(RTFILE hFile, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
|
---|
134 | PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
|
---|
135 | {
|
---|
136 | NOREF(pChangeTime); NOREF(pBirthTime);
|
---|
137 |
|
---|
138 | /*
|
---|
139 | * We can only set AccessTime and ModificationTime, so if neither
|
---|
140 | * are specified we can return immediately.
|
---|
141 | */
|
---|
142 | if (!pAccessTime && !pModificationTime)
|
---|
143 | return VINF_SUCCESS;
|
---|
144 |
|
---|
145 | #ifdef USE_FUTIMENS
|
---|
146 | struct timespec aTimespecs[2];
|
---|
147 | if (pAccessTime && pModificationTime)
|
---|
148 | {
|
---|
149 | memcpy(&aTimespecs[0], pAccessTime, sizeof(struct timespec));
|
---|
150 | memcpy(&aTimespecs[1], pModificationTime, sizeof(struct timespec));
|
---|
151 | }
|
---|
152 | else
|
---|
153 | {
|
---|
154 | RTFSOBJINFO ObjInfo;
|
---|
155 | int rc = RTFileQueryInfo(hFile, &ObjInfo, RTFSOBJATTRADD_UNIX);
|
---|
156 | if (RT_FAILURE(rc))
|
---|
157 | return rc;
|
---|
158 | memcpy(&aTimespecs[0], pAccessTime ? pAccessTime : &ObjInfo.AccessTime, sizeof(struct timespec));
|
---|
159 | memcpy(&aTimespecs[1], pModificationTime ? pModificationTime : &ObjInfo.ModificationTime, sizeof(struct timespec));
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (futimens(RTFileToNative(hFile), aTimespecs))
|
---|
163 | {
|
---|
164 | int rc = RTErrConvertFromErrno(errno);
|
---|
165 | Log(("RTFileSetTimes(%RTfile,%p,%p,,): returns %Rrc\n", hFile, pAccessTime, pModificationTime, rc));
|
---|
166 | return rc;
|
---|
167 | }
|
---|
168 | #else
|
---|
169 | /*
|
---|
170 | * Convert the input to timeval, getting the missing one if necessary,
|
---|
171 | * and call the API which does the change.
|
---|
172 | */
|
---|
173 | struct timeval aTimevals[2];
|
---|
174 | if (pAccessTime && pModificationTime)
|
---|
175 | {
|
---|
176 | RTTimeSpecGetTimeval(pAccessTime, &aTimevals[0]);
|
---|
177 | RTTimeSpecGetTimeval(pModificationTime, &aTimevals[1]);
|
---|
178 | }
|
---|
179 | else
|
---|
180 | {
|
---|
181 | RTFSOBJINFO ObjInfo;
|
---|
182 | int rc = RTFileQueryInfo(hFile, &ObjInfo, RTFSOBJATTRADD_UNIX);
|
---|
183 | if (RT_FAILURE(rc))
|
---|
184 | return rc;
|
---|
185 | RTTimeSpecGetTimeval(pAccessTime ? pAccessTime : &ObjInfo.AccessTime, &aTimevals[0]);
|
---|
186 | RTTimeSpecGetTimeval(pModificationTime ? pModificationTime : &ObjInfo.ModificationTime, &aTimevals[1]);
|
---|
187 | }
|
---|
188 |
|
---|
189 | /* XXX this falls back to utimes("/proc/self/fd/...",...) for older kernels/glibcs and this
|
---|
190 | * will not work for hardened builds where this directory is owned by root.root and mode 0500 */
|
---|
191 | if (futimes(RTFileToNative(hFile), aTimevals))
|
---|
192 | {
|
---|
193 | int rc = RTErrConvertFromErrno(errno);
|
---|
194 | Log(("RTFileSetTimes(%RTfile,%p,%p,,): returns %Rrc\n", hFile, pAccessTime, pModificationTime, rc));
|
---|
195 | return rc;
|
---|
196 | }
|
---|
197 | #endif
|
---|
198 | return VINF_SUCCESS;
|
---|
199 | }
|
---|
200 |
|
---|