1 | /* $Id: fileio2-posix.cpp 39083 2011-10-22 00:28:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File I/O, POSIX, Part 2.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2011 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. */
|
---|
52 | extern 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 |
|
---|
71 | RTR3DECL(int) RTFileQueryInfo(RTFILE hFile, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
|
---|
72 | {
|
---|
73 | /*
|
---|
74 | * Validate input.
|
---|
75 | */
|
---|
76 | AssertReturn(hFile != NIL_RTFILE, VERR_INVALID_PARAMETER);
|
---|
77 | AssertPtrReturn(pObjInfo, VERR_INVALID_PARAMETER);
|
---|
78 | if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
|
---|
79 | || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
|
---|
80 | {
|
---|
81 | AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
|
---|
82 | return VERR_INVALID_PARAMETER;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * Query file info.
|
---|
87 | */
|
---|
88 | struct stat Stat;
|
---|
89 | if (fstat(RTFileToNative(hFile), &Stat))
|
---|
90 | {
|
---|
91 | int rc = RTErrConvertFromErrno(errno);
|
---|
92 | Log(("RTFileQueryInfo(%RTfile,,%d): returns %Rrc\n", hFile, enmAdditionalAttribs, rc));
|
---|
93 | return rc;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Setup the returned data.
|
---|
98 | */
|
---|
99 | rtFsConvertStatToObjInfo(pObjInfo, &Stat, NULL, 0);
|
---|
100 |
|
---|
101 | /*
|
---|
102 | * Requested attributes (we cannot provide anything actually).
|
---|
103 | */
|
---|
104 | switch (enmAdditionalAttribs)
|
---|
105 | {
|
---|
106 | case RTFSOBJATTRADD_NOTHING:
|
---|
107 | case RTFSOBJATTRADD_UNIX:
|
---|
108 | /* done */
|
---|
109 | break;
|
---|
110 |
|
---|
111 | case RTFSOBJATTRADD_UNIX_OWNER:
|
---|
112 | rtFsObjInfoAttrSetUnixOwner(pObjInfo, Stat.st_uid);
|
---|
113 | break;
|
---|
114 |
|
---|
115 | case RTFSOBJATTRADD_UNIX_GROUP:
|
---|
116 | rtFsObjInfoAttrSetUnixGroup(pObjInfo, Stat.st_gid);
|
---|
117 | break;
|
---|
118 |
|
---|
119 | case RTFSOBJATTRADD_EASIZE:
|
---|
120 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
|
---|
121 | pObjInfo->Attr.u.EASize.cb = 0;
|
---|
122 | break;
|
---|
123 |
|
---|
124 | default:
|
---|
125 | AssertMsgFailed(("Impossible!\n"));
|
---|
126 | return VERR_INTERNAL_ERROR;
|
---|
127 | }
|
---|
128 |
|
---|
129 | LogFlow(("RTFileQueryInfo(%RTfile,,%d): returns VINF_SUCCESS\n", hFile, enmAdditionalAttribs));
|
---|
130 | return VINF_SUCCESS;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | RTR3DECL(int) RTFileSetTimes(RTFILE hFile, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
|
---|
135 | PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
|
---|
136 | {
|
---|
137 | NOREF(pChangeTime); NOREF(pBirthTime);
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * We can only set AccessTime and ModificationTime, so if neither
|
---|
141 | * are specified we can return immediately.
|
---|
142 | */
|
---|
143 | if (!pAccessTime && !pModificationTime)
|
---|
144 | return VINF_SUCCESS;
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * Convert the input to timeval, getting the missing one if necessary,
|
---|
148 | * and call the API which does the change.
|
---|
149 | */
|
---|
150 | struct timeval aTimevals[2];
|
---|
151 | if (pAccessTime && pModificationTime)
|
---|
152 | {
|
---|
153 | RTTimeSpecGetTimeval(pAccessTime, &aTimevals[0]);
|
---|
154 | RTTimeSpecGetTimeval(pModificationTime, &aTimevals[1]);
|
---|
155 | }
|
---|
156 | else
|
---|
157 | {
|
---|
158 | RTFSOBJINFO ObjInfo;
|
---|
159 | int rc = RTFileQueryInfo(hFile, &ObjInfo, RTFSOBJATTRADD_UNIX);
|
---|
160 | if (RT_FAILURE(rc))
|
---|
161 | return rc;
|
---|
162 | RTTimeSpecGetTimeval(pAccessTime ? pAccessTime : &ObjInfo.AccessTime, &aTimevals[0]);
|
---|
163 | RTTimeSpecGetTimeval(pModificationTime ? pModificationTime : &ObjInfo.ModificationTime, &aTimevals[1]);
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (futimes(RTFileToNative(hFile), aTimevals))
|
---|
167 | {
|
---|
168 | int rc = RTErrConvertFromErrno(errno);
|
---|
169 | Log(("RTFileSetTimes(%RTfile,%p,%p,,): returns %Rrc\n", hFile, pAccessTime, pModificationTime, rc));
|
---|
170 | return rc;
|
---|
171 | }
|
---|
172 | return VINF_SUCCESS;
|
---|
173 | }
|
---|
174 |
|
---|