1 | /* $Id: fileio2-posix.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File I/O, POSIX, Part 2.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #define LOG_GROUP RTLOGGROUP_FILE
|
---|
42 |
|
---|
43 | #include <errno.h>
|
---|
44 | #include <sys/stat.h>
|
---|
45 | #include <sys/types.h>
|
---|
46 | #ifdef _MSC_VER
|
---|
47 | # include <io.h>
|
---|
48 | # include <stdio.h>
|
---|
49 | #else
|
---|
50 | # include <unistd.h>
|
---|
51 | # include <sys/time.h>
|
---|
52 | #endif
|
---|
53 | #ifdef RT_OS_LINUX
|
---|
54 | # include <sys/file.h>
|
---|
55 | #endif
|
---|
56 | #if defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006)
|
---|
57 | # include <io.h>
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | #ifdef RT_OS_SOLARIS
|
---|
61 | # define futimes(filedes, timeval) futimesat(filedes, NULL, timeval)
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | #ifdef RT_OS_HAIKU
|
---|
65 | # define USE_FUTIMENS
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | #include <iprt/file.h>
|
---|
69 | #include <iprt/path.h>
|
---|
70 | #include <iprt/assert.h>
|
---|
71 | #include <iprt/string.h>
|
---|
72 | #include <iprt/errcore.h>
|
---|
73 | #include <iprt/log.h>
|
---|
74 | #include "internal/file.h"
|
---|
75 | #include "internal/fs.h"
|
---|
76 | #include "internal/path.h"
|
---|
77 |
|
---|
78 |
|
---|
79 |
|
---|
80 | RTR3DECL(int) RTFileQueryInfo(RTFILE hFile, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
|
---|
81 | {
|
---|
82 | /*
|
---|
83 | * Validate input.
|
---|
84 | */
|
---|
85 | AssertReturn(hFile != NIL_RTFILE, VERR_INVALID_PARAMETER);
|
---|
86 | AssertPtrReturn(pObjInfo, VERR_INVALID_PARAMETER);
|
---|
87 | if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
|
---|
88 | || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
|
---|
89 | {
|
---|
90 | AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
|
---|
91 | return VERR_INVALID_PARAMETER;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * Query file info.
|
---|
96 | */
|
---|
97 | struct stat Stat;
|
---|
98 | if (fstat(RTFileToNative(hFile), &Stat))
|
---|
99 | {
|
---|
100 | int rc = RTErrConvertFromErrno(errno);
|
---|
101 | Log(("RTFileQueryInfo(%RTfile,,%d): returns %Rrc\n", hFile, enmAdditionalAttribs, rc));
|
---|
102 | return rc;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * Setup the returned data.
|
---|
107 | */
|
---|
108 | rtFsConvertStatToObjInfo(pObjInfo, &Stat, NULL, 0);
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Requested attributes (we cannot provide anything actually).
|
---|
112 | */
|
---|
113 | switch (enmAdditionalAttribs)
|
---|
114 | {
|
---|
115 | case RTFSOBJATTRADD_NOTHING:
|
---|
116 | case RTFSOBJATTRADD_UNIX:
|
---|
117 | /* done */
|
---|
118 | break;
|
---|
119 |
|
---|
120 | case RTFSOBJATTRADD_UNIX_OWNER:
|
---|
121 | rtFsObjInfoAttrSetUnixOwner(pObjInfo, Stat.st_uid);
|
---|
122 | break;
|
---|
123 |
|
---|
124 | case RTFSOBJATTRADD_UNIX_GROUP:
|
---|
125 | rtFsObjInfoAttrSetUnixGroup(pObjInfo, Stat.st_gid);
|
---|
126 | break;
|
---|
127 |
|
---|
128 | case RTFSOBJATTRADD_EASIZE:
|
---|
129 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
|
---|
130 | pObjInfo->Attr.u.EASize.cb = 0;
|
---|
131 | break;
|
---|
132 |
|
---|
133 | default:
|
---|
134 | AssertMsgFailed(("Impossible!\n"));
|
---|
135 | return VERR_INTERNAL_ERROR;
|
---|
136 | }
|
---|
137 |
|
---|
138 | LogFlow(("RTFileQueryInfo(%RTfile,,%d): returns VINF_SUCCESS\n", hFile, enmAdditionalAttribs));
|
---|
139 | return VINF_SUCCESS;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | RTR3DECL(int) RTFileSetTimes(RTFILE hFile, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
|
---|
144 | PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
|
---|
145 | {
|
---|
146 | NOREF(pChangeTime); NOREF(pBirthTime);
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * We can only set AccessTime and ModificationTime, so if neither
|
---|
150 | * are specified we can return immediately.
|
---|
151 | */
|
---|
152 | if (!pAccessTime && !pModificationTime)
|
---|
153 | return VINF_SUCCESS;
|
---|
154 |
|
---|
155 | #ifdef USE_FUTIMENS
|
---|
156 | struct timespec aTimespecs[2];
|
---|
157 | if (pAccessTime && pModificationTime)
|
---|
158 | {
|
---|
159 | memcpy(&aTimespecs[0], pAccessTime, sizeof(struct timespec));
|
---|
160 | memcpy(&aTimespecs[1], pModificationTime, sizeof(struct timespec));
|
---|
161 | }
|
---|
162 | else
|
---|
163 | {
|
---|
164 | RTFSOBJINFO ObjInfo;
|
---|
165 | int rc = RTFileQueryInfo(hFile, &ObjInfo, RTFSOBJATTRADD_UNIX);
|
---|
166 | if (RT_FAILURE(rc))
|
---|
167 | return rc;
|
---|
168 | memcpy(&aTimespecs[0], pAccessTime ? pAccessTime : &ObjInfo.AccessTime, sizeof(struct timespec));
|
---|
169 | memcpy(&aTimespecs[1], pModificationTime ? pModificationTime : &ObjInfo.ModificationTime, sizeof(struct timespec));
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (futimens(RTFileToNative(hFile), aTimespecs))
|
---|
173 | {
|
---|
174 | int rc = RTErrConvertFromErrno(errno);
|
---|
175 | Log(("RTFileSetTimes(%RTfile,%p,%p,,): returns %Rrc\n", hFile, pAccessTime, pModificationTime, rc));
|
---|
176 | return rc;
|
---|
177 | }
|
---|
178 | #else
|
---|
179 | /*
|
---|
180 | * Convert the input to timeval, getting the missing one if necessary,
|
---|
181 | * and call the API which does the change.
|
---|
182 | */
|
---|
183 | struct timeval aTimevals[2];
|
---|
184 | if (pAccessTime && pModificationTime)
|
---|
185 | {
|
---|
186 | RTTimeSpecGetTimeval(pAccessTime, &aTimevals[0]);
|
---|
187 | RTTimeSpecGetTimeval(pModificationTime, &aTimevals[1]);
|
---|
188 | }
|
---|
189 | else
|
---|
190 | {
|
---|
191 | RTFSOBJINFO ObjInfo;
|
---|
192 | int rc = RTFileQueryInfo(hFile, &ObjInfo, RTFSOBJATTRADD_UNIX);
|
---|
193 | if (RT_FAILURE(rc))
|
---|
194 | return rc;
|
---|
195 | RTTimeSpecGetTimeval(pAccessTime ? pAccessTime : &ObjInfo.AccessTime, &aTimevals[0]);
|
---|
196 | RTTimeSpecGetTimeval(pModificationTime ? pModificationTime : &ObjInfo.ModificationTime, &aTimevals[1]);
|
---|
197 | }
|
---|
198 |
|
---|
199 | /* XXX this falls back to utimes("/proc/self/fd/...",...) for older kernels/glibcs and this
|
---|
200 | * will not work for hardened builds where this directory is owned by root.root and mode 0500 */
|
---|
201 | if (futimes(RTFileToNative(hFile), aTimevals))
|
---|
202 | {
|
---|
203 | int rc = RTErrConvertFromErrno(errno);
|
---|
204 | Log(("RTFileSetTimes(%RTfile,%p,%p,,): returns %Rrc\n", hFile, pAccessTime, pModificationTime, rc));
|
---|
205 | return rc;
|
---|
206 | }
|
---|
207 | #endif
|
---|
208 | return VINF_SUCCESS;
|
---|
209 | }
|
---|
210 |
|
---|