1 | /* $Id: fileio-posix.cpp 16647 2009-02-10 16:56:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File I/O, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #define LOG_GROUP RTLOGGROUP_FILE
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <sys/stat.h>
|
---|
39 | #include <sys/types.h>
|
---|
40 | #include <sys/ioctl.h>
|
---|
41 | #include <sys/fcntl.h>
|
---|
42 | #include <fcntl.h>
|
---|
43 | #ifdef _MSC_VER
|
---|
44 | # include <io.h>
|
---|
45 | # include <stdio.h>
|
---|
46 | #else
|
---|
47 | # include <unistd.h>
|
---|
48 | # include <sys/time.h>
|
---|
49 | #endif
|
---|
50 | #if defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006)
|
---|
51 | # include <io.h>
|
---|
52 | #endif
|
---|
53 | #ifdef RT_OS_L4
|
---|
54 | /* This is currently ifdef'ed out in the relevant L4 header file */
|
---|
55 | /* Same as `utimes', but takes an open file descriptor instead of a name. */
|
---|
56 | extern int futimes(int __fd, __const struct timeval __tvp[2]) __THROW;
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | #ifdef RT_OS_SOLARIS
|
---|
60 | # define futimes(filedes, timeval) futimesat(filedes, NULL, timeval)
|
---|
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 |
|
---|
75 | /*******************************************************************************
|
---|
76 | * Defined Constants And Macros *
|
---|
77 | *******************************************************************************/
|
---|
78 | /** @def RT_DONT_CONVERT_FILENAMES
|
---|
79 | * Define this to pass UTF-8 unconverted to the kernel. */
|
---|
80 | #ifdef __DOXYGEN__
|
---|
81 | #define RT_DONT_CONVERT_FILENAMES 1
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | /** Default file permissions for newly created files. */
|
---|
85 | #if defined(S_IRUSR) && defined(S_IWUSR)
|
---|
86 | # define RT_FILE_PERMISSION (S_IRUSR | S_IWUSR)
|
---|
87 | #else
|
---|
88 | # define RT_FILE_PERMISSION (00600)
|
---|
89 | #endif
|
---|
90 |
|
---|
91 |
|
---|
92 | RTR3DECL(int) RTFileOpen(PRTFILE pFile, const char *pszFilename, unsigned fOpen)
|
---|
93 | {
|
---|
94 | /*
|
---|
95 | * Validate input.
|
---|
96 | */
|
---|
97 | if (!VALID_PTR(pFile))
|
---|
98 | {
|
---|
99 | AssertMsgFailed(("Invalid pFile %p\n", pFile));
|
---|
100 | return VERR_INVALID_PARAMETER;
|
---|
101 | }
|
---|
102 | *pFile = NIL_RTFILE;
|
---|
103 | if (!VALID_PTR(pszFilename))
|
---|
104 | {
|
---|
105 | AssertMsgFailed(("Invalid pszFilename %p\n", pszFilename));
|
---|
106 | return VERR_INVALID_PARAMETER;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * Merge forced open flags and validate them.
|
---|
111 | */
|
---|
112 | int rc = rtFileRecalcAndValidateFlags(&fOpen);
|
---|
113 | if (RT_FAILURE(rc))
|
---|
114 | return rc;
|
---|
115 | #ifndef O_NONBLOCK
|
---|
116 | if (fOpen & RTFILE_O_NON_BLOCK)
|
---|
117 | {
|
---|
118 | AssertMsgFailed(("Invalid parameters! fOpen=%#x\n", fOpen));
|
---|
119 | return VERR_INVALID_PARAMETER;
|
---|
120 | }
|
---|
121 | #endif
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Calculate open mode flags.
|
---|
125 | */
|
---|
126 | int fOpenMode = 0;
|
---|
127 | #ifdef O_BINARY
|
---|
128 | fOpenMode |= O_BINARY; /* (pc) */
|
---|
129 | #endif
|
---|
130 | #ifdef O_LARGEFILE
|
---|
131 | fOpenMode |= O_LARGEFILE; /* (linux) */
|
---|
132 | #endif
|
---|
133 | #ifdef O_NOINHERIT
|
---|
134 | if (!(fOpen & RTFILE_O_INHERIT))
|
---|
135 | fOpenMode |= O_NOINHERIT;
|
---|
136 | #endif
|
---|
137 | #ifdef O_NONBLOCK
|
---|
138 | if (fOpen & RTFILE_O_NON_BLOCK)
|
---|
139 | fOpenMode |= O_NONBLOCK;
|
---|
140 | #endif
|
---|
141 | #ifdef O_SYNC
|
---|
142 | if (fOpen & RTFILE_O_WRITE_THROUGH)
|
---|
143 | fOpenMode |= O_SYNC;
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | /* create/truncate file */
|
---|
147 | switch (fOpen & RTFILE_O_ACTION_MASK)
|
---|
148 | {
|
---|
149 | case RTFILE_O_OPEN: break;
|
---|
150 | case RTFILE_O_OPEN_CREATE: fOpenMode |= O_CREAT; break;
|
---|
151 | case RTFILE_O_CREATE: fOpenMode |= O_CREAT | O_EXCL; break;
|
---|
152 | case RTFILE_O_CREATE_REPLACE: fOpenMode |= O_CREAT | O_TRUNC; break; /** @todo replacing needs fixing, this is *not* a 1:1 mapping! */
|
---|
153 | }
|
---|
154 | if (fOpen & RTFILE_O_TRUNCATE)
|
---|
155 | fOpenMode |= O_TRUNC;
|
---|
156 |
|
---|
157 | switch (fOpen & RTFILE_O_ACCESS_MASK)
|
---|
158 | {
|
---|
159 | case RTFILE_O_READ: fOpenMode |= O_RDONLY; break;
|
---|
160 | case RTFILE_O_WRITE: fOpenMode |= O_WRONLY; break;
|
---|
161 | case RTFILE_O_READWRITE: fOpenMode |= O_RDWR; break;
|
---|
162 | default:
|
---|
163 | AssertMsgFailed(("RTFileOpen received an invalid RW value, fOpen=%#x\n", fOpen));
|
---|
164 | return VERR_INVALID_PARAMETER;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /* File mode. */
|
---|
168 | int fMode = (fOpen & RTFILE_O_CREATE_MODE_MASK)
|
---|
169 | ? (fOpen & RTFILE_O_CREATE_MODE_MASK) >> RTFILE_O_CREATE_MODE_SHIFT
|
---|
170 | : RT_FILE_PERMISSION;
|
---|
171 |
|
---|
172 | /** @todo sharing! */
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * Open/create the file.
|
---|
176 | */
|
---|
177 | #ifdef RT_DONT_CONVERT_FILENAMES
|
---|
178 | int fh = open(pszFilename, fOpenMode, fMode);
|
---|
179 | int iErr = errno;
|
---|
180 | #else
|
---|
181 | char *pszNativeFilename;
|
---|
182 | rc = rtPathToNative(&pszNativeFilename, pszFilename);
|
---|
183 | if (RT_FAILURE(rc))
|
---|
184 | return (rc);
|
---|
185 |
|
---|
186 | int fh = open(pszNativeFilename, fOpenMode, fMode);
|
---|
187 | int iErr = errno;
|
---|
188 | rtPathFreeNative(pszNativeFilename);
|
---|
189 | #endif
|
---|
190 | if (fh >= 0)
|
---|
191 | {
|
---|
192 | /*
|
---|
193 | * Mark the file handle close on exec, unless inherit is specified.
|
---|
194 | */
|
---|
195 | if ( !(fOpen & RTFILE_O_INHERIT)
|
---|
196 | #ifdef O_NOINHERIT
|
---|
197 | || (fOpenMode & O_NOINHERIT) /* careful since it could be a dummy. */
|
---|
198 | #endif
|
---|
199 | || fcntl(fh, F_SETFD, FD_CLOEXEC) >= 0)
|
---|
200 | {
|
---|
201 | *pFile = (RTFILE)fh;
|
---|
202 | Assert((int)*pFile == fh);
|
---|
203 | LogFlow(("RTFileOpen(%p:{%RTfile}, %p:{%s}, %#x): returns %Rrc\n",
|
---|
204 | pFile, *pFile, pszFilename, pszFilename, fOpen, rc));
|
---|
205 | return VINF_SUCCESS;
|
---|
206 | }
|
---|
207 | iErr = errno;
|
---|
208 | close(fh);
|
---|
209 | }
|
---|
210 | return RTErrConvertFromErrno(iErr);
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | RTR3DECL(int) RTFileClose(RTFILE File)
|
---|
215 | {
|
---|
216 | if (close((int)File) == 0)
|
---|
217 | return VINF_SUCCESS;
|
---|
218 | return RTErrConvertFromErrno(errno);
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | RTR3DECL(int) RTFileFromNative(PRTFILE pFile, RTHCINTPTR uNative)
|
---|
223 | {
|
---|
224 | if ( uNative < 0
|
---|
225 | || (RTFILE)uNative != (RTUINTPTR)uNative)
|
---|
226 | {
|
---|
227 | AssertMsgFailed(("%p\n", uNative));
|
---|
228 | *pFile = NIL_RTFILE;
|
---|
229 | return VERR_INVALID_HANDLE;
|
---|
230 | }
|
---|
231 | *pFile = (RTFILE)uNative;
|
---|
232 | return VINF_SUCCESS;
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | RTR3DECL(RTHCINTPTR) RTFileToNative(RTFILE File)
|
---|
237 | {
|
---|
238 | AssertReturn(File != NIL_RTFILE, -1);
|
---|
239 | return (RTHCINTPTR)File;
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | RTR3DECL(int) RTFileDelete(const char *pszFilename)
|
---|
244 | {
|
---|
245 | char *pszNativeFilename;
|
---|
246 | int rc = rtPathToNative(&pszNativeFilename, pszFilename);
|
---|
247 | if (RT_SUCCESS(rc))
|
---|
248 | {
|
---|
249 | if (unlink(pszNativeFilename) != 0)
|
---|
250 | rc = RTErrConvertFromErrno(errno);
|
---|
251 | rtPathFreeNative(pszNativeFilename);
|
---|
252 | }
|
---|
253 | return rc;
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | RTR3DECL(int) RTFileSeek(RTFILE File, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
|
---|
258 | {
|
---|
259 | static const unsigned aSeekRecode[] =
|
---|
260 | {
|
---|
261 | SEEK_SET,
|
---|
262 | SEEK_CUR,
|
---|
263 | SEEK_END,
|
---|
264 | };
|
---|
265 |
|
---|
266 | /*
|
---|
267 | * Validate input.
|
---|
268 | */
|
---|
269 | if (uMethod > RTFILE_SEEK_END)
|
---|
270 | {
|
---|
271 | AssertMsgFailed(("Invalid uMethod=%d\n", uMethod));
|
---|
272 | return VERR_INVALID_PARAMETER;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /* check that within off_t range. */
|
---|
276 | if ( sizeof(off_t) < sizeof(offSeek)
|
---|
277 | && ( (offSeek > 0 && (unsigned)(offSeek >> 32) != 0)
|
---|
278 | || (offSeek < 0 && (unsigned)(-offSeek >> 32) != 0)))
|
---|
279 | {
|
---|
280 | AssertMsgFailed(("64-bit search not supported\n"));
|
---|
281 | return VERR_NOT_SUPPORTED;
|
---|
282 | }
|
---|
283 |
|
---|
284 | off_t offCurrent = lseek((int)File, (off_t)offSeek, aSeekRecode[uMethod]);
|
---|
285 | if (offCurrent != ~0)
|
---|
286 | {
|
---|
287 | if (poffActual)
|
---|
288 | *poffActual = (uint64_t)offCurrent;
|
---|
289 | return VINF_SUCCESS;
|
---|
290 | }
|
---|
291 | return RTErrConvertFromErrno(errno);
|
---|
292 | }
|
---|
293 |
|
---|
294 |
|
---|
295 | RTR3DECL(int) RTFileRead(RTFILE File, void *pvBuf, size_t cbToRead, size_t *pcbRead)
|
---|
296 | {
|
---|
297 | if (cbToRead <= 0)
|
---|
298 | return VINF_SUCCESS;
|
---|
299 |
|
---|
300 | /*
|
---|
301 | * Attempt read.
|
---|
302 | */
|
---|
303 | ssize_t cbRead = read((int)File, pvBuf, cbToRead);
|
---|
304 | if (cbRead >= 0)
|
---|
305 | {
|
---|
306 | if (pcbRead)
|
---|
307 | /* caller can handle partial read. */
|
---|
308 | *pcbRead = cbRead;
|
---|
309 | else
|
---|
310 | {
|
---|
311 | /* Caller expects all to be read. */
|
---|
312 | while ((ssize_t)cbToRead > cbRead)
|
---|
313 | {
|
---|
314 | ssize_t cbReadPart = read((int)File, (char*)pvBuf + cbRead, cbToRead - cbRead);
|
---|
315 | if (cbReadPart <= 0)
|
---|
316 | {
|
---|
317 | if (cbReadPart == 0)
|
---|
318 | return VERR_EOF;
|
---|
319 | return RTErrConvertFromErrno(errno);
|
---|
320 | }
|
---|
321 | cbRead += cbReadPart;
|
---|
322 | }
|
---|
323 | }
|
---|
324 | return VINF_SUCCESS;
|
---|
325 | }
|
---|
326 |
|
---|
327 | return RTErrConvertFromErrno(errno);
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 | RTR3DECL(int) RTFileWrite(RTFILE File, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
|
---|
332 | {
|
---|
333 | if (cbToWrite <= 0)
|
---|
334 | return VINF_SUCCESS;
|
---|
335 |
|
---|
336 | /*
|
---|
337 | * Attempt write.
|
---|
338 | */
|
---|
339 | ssize_t cbWritten = write((int)File, pvBuf, cbToWrite);
|
---|
340 | if (cbWritten >= 0)
|
---|
341 | {
|
---|
342 | if (pcbWritten)
|
---|
343 | /* caller can handle partial write. */
|
---|
344 | *pcbWritten = cbWritten;
|
---|
345 | else
|
---|
346 | {
|
---|
347 | /* Caller expects all to be write. */
|
---|
348 | while ((ssize_t)cbToWrite > cbWritten)
|
---|
349 | {
|
---|
350 | ssize_t cbWrittenPart = write((int)File, (const char *)pvBuf + cbWritten, cbToWrite - cbWritten);
|
---|
351 | if (cbWrittenPart <= 0)
|
---|
352 | return RTErrConvertFromErrno(errno);
|
---|
353 | cbWritten += cbWrittenPart;
|
---|
354 | }
|
---|
355 | }
|
---|
356 | return VINF_SUCCESS;
|
---|
357 | }
|
---|
358 | return RTErrConvertFromErrno(errno);
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 | RTR3DECL(int) RTFileSetSize(RTFILE File, uint64_t cbSize)
|
---|
363 | {
|
---|
364 | /*
|
---|
365 | * Validate offset.
|
---|
366 | */
|
---|
367 | if ( sizeof(off_t) < sizeof(cbSize)
|
---|
368 | && (cbSize >> 32) != 0)
|
---|
369 | {
|
---|
370 | AssertMsgFailed(("64-bit filesize not supported! cbSize=%lld\n", cbSize));
|
---|
371 | return VERR_NOT_SUPPORTED;
|
---|
372 | }
|
---|
373 |
|
---|
374 | #if defined(_MSC_VER) || (defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006))
|
---|
375 | if (chsize((int)File, (off_t)cbSize) == 0)
|
---|
376 | #else
|
---|
377 | /* This relies on a non-standard feature of FreeBSD, Linux, and OS/2
|
---|
378 | * LIBC v0.6 and higher. (SuS doesn't define ftruncate() and size bigger
|
---|
379 | * than the file.)
|
---|
380 | */
|
---|
381 | if (ftruncate((int)File, (off_t)cbSize) == 0)
|
---|
382 | #endif
|
---|
383 | return VINF_SUCCESS;
|
---|
384 | return RTErrConvertFromErrno(errno);
|
---|
385 | }
|
---|
386 |
|
---|
387 |
|
---|
388 | RTR3DECL(int) RTFileGetSize(RTFILE File, uint64_t *pcbSize)
|
---|
389 | {
|
---|
390 | struct stat st;
|
---|
391 | if (!fstat((int)File, &st))
|
---|
392 | {
|
---|
393 | *pcbSize = st.st_size;
|
---|
394 | return VINF_SUCCESS;
|
---|
395 | }
|
---|
396 | return RTErrConvertFromErrno(errno);
|
---|
397 | }
|
---|
398 |
|
---|
399 |
|
---|
400 | /**
|
---|
401 | * Determine the maximum file size.
|
---|
402 | *
|
---|
403 | * @returns IPRT status code.
|
---|
404 | * @param File Handle to the file.
|
---|
405 | * @param pcbMax Where to store the max file size.
|
---|
406 | * @see RTFileGetMaxSize.
|
---|
407 | */
|
---|
408 | RTR3DECL(int) RTFileGetMaxSizeEx(RTFILE File, PRTFOFF pcbMax)
|
---|
409 | {
|
---|
410 | /*
|
---|
411 | * Save the current location
|
---|
412 | */
|
---|
413 | uint64_t offOld;
|
---|
414 | int rc = RTFileSeek(File, 0, RTFILE_SEEK_CURRENT, &offOld);
|
---|
415 | if (RT_FAILURE(rc))
|
---|
416 | return rc;
|
---|
417 |
|
---|
418 | /*
|
---|
419 | * Perform a binary search for the max file size.
|
---|
420 | */
|
---|
421 | uint64_t offLow = 0;
|
---|
422 | uint64_t offHigh = 8 * _1T; /* we don't need bigger files */
|
---|
423 | /** @todo Unfortunately this does not work for certain file system types,
|
---|
424 | * for instance cifs mounts. Even worse, statvfs.f_fsid returns 0 for such
|
---|
425 | * file systems. */
|
---|
426 | //uint64_t offHigh = INT64_MAX;
|
---|
427 | for (;;)
|
---|
428 | {
|
---|
429 | uint64_t cbInterval = (offHigh - offLow) >> 1;
|
---|
430 | if (cbInterval == 0)
|
---|
431 | {
|
---|
432 | if (pcbMax)
|
---|
433 | *pcbMax = offLow;
|
---|
434 | return RTFileSeek(File, offOld, RTFILE_SEEK_BEGIN, NULL);
|
---|
435 | }
|
---|
436 |
|
---|
437 | rc = RTFileSeek(File, offLow + cbInterval, RTFILE_SEEK_BEGIN, NULL);
|
---|
438 | if (RT_FAILURE(rc))
|
---|
439 | offHigh = offLow + cbInterval;
|
---|
440 | else
|
---|
441 | offLow = offLow + cbInterval;
|
---|
442 | }
|
---|
443 | }
|
---|
444 |
|
---|
445 |
|
---|
446 | RTR3DECL(bool) RTFileIsValid(RTFILE File)
|
---|
447 | {
|
---|
448 | if (File != NIL_RTFILE)
|
---|
449 | {
|
---|
450 | int fFlags = fcntl(File, F_GETFD);
|
---|
451 | if (fFlags >= 0)
|
---|
452 | return true;
|
---|
453 | }
|
---|
454 | return false;
|
---|
455 | }
|
---|
456 |
|
---|
457 |
|
---|
458 | RTR3DECL(int) RTFileFlush(RTFILE File)
|
---|
459 | {
|
---|
460 | if (fsync((int)File))
|
---|
461 | return RTErrConvertFromErrno(errno);
|
---|
462 | return VINF_SUCCESS;
|
---|
463 | }
|
---|
464 |
|
---|
465 |
|
---|
466 | RTR3DECL(int) RTFileIoCtl(RTFILE File, int iRequest, void *pvData, unsigned cbData, int *piRet)
|
---|
467 | {
|
---|
468 | int rc = ioctl((int)File, iRequest, pvData);
|
---|
469 | if (piRet)
|
---|
470 | *piRet = rc;
|
---|
471 | return rc >= 0 ? VINF_SUCCESS : RTErrConvertFromErrno(errno);
|
---|
472 | }
|
---|
473 |
|
---|
474 |
|
---|
475 | RTR3DECL(int) RTFileQueryInfo(RTFILE File, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
|
---|
476 | {
|
---|
477 | /*
|
---|
478 | * Validate input.
|
---|
479 | */
|
---|
480 | if (File == NIL_RTFILE)
|
---|
481 | {
|
---|
482 | AssertMsgFailed(("Invalid File=%RTfile\n", File));
|
---|
483 | return VERR_INVALID_PARAMETER;
|
---|
484 | }
|
---|
485 | if (!pObjInfo)
|
---|
486 | {
|
---|
487 | AssertMsgFailed(("Invalid pObjInfo=%p\n", pObjInfo));
|
---|
488 | return VERR_INVALID_PARAMETER;
|
---|
489 | }
|
---|
490 | if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
|
---|
491 | || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
|
---|
492 | {
|
---|
493 | AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
|
---|
494 | return VERR_INVALID_PARAMETER;
|
---|
495 | }
|
---|
496 |
|
---|
497 | /*
|
---|
498 | * Query file info.
|
---|
499 | */
|
---|
500 | struct stat Stat;
|
---|
501 | if (fstat((int)File, &Stat))
|
---|
502 | {
|
---|
503 | int rc = RTErrConvertFromErrno(errno);
|
---|
504 | Log(("RTFileQueryInfo(%RTfile,,%d): returns %Rrc\n", File, enmAdditionalAttribs, rc));
|
---|
505 | return rc;
|
---|
506 | }
|
---|
507 |
|
---|
508 | /*
|
---|
509 | * Setup the returned data.
|
---|
510 | */
|
---|
511 | rtFsConvertStatToObjInfo(pObjInfo, &Stat, NULL, 0);
|
---|
512 |
|
---|
513 | /*
|
---|
514 | * Requested attributes (we cannot provide anything actually).
|
---|
515 | */
|
---|
516 | switch (enmAdditionalAttribs)
|
---|
517 | {
|
---|
518 | case RTFSOBJATTRADD_EASIZE:
|
---|
519 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
|
---|
520 | pObjInfo->Attr.u.EASize.cb = 0;
|
---|
521 | break;
|
---|
522 |
|
---|
523 | case RTFSOBJATTRADD_NOTHING:
|
---|
524 | case RTFSOBJATTRADD_UNIX:
|
---|
525 | /* done */
|
---|
526 | break;
|
---|
527 |
|
---|
528 | default:
|
---|
529 | AssertMsgFailed(("Impossible!\n"));
|
---|
530 | return VERR_INTERNAL_ERROR;
|
---|
531 | }
|
---|
532 |
|
---|
533 | LogFlow(("RTFileQueryInfo(%RTfile,,%d): returns VINF_SUCCESS\n", File, enmAdditionalAttribs));
|
---|
534 | return VINF_SUCCESS;
|
---|
535 | }
|
---|
536 |
|
---|
537 |
|
---|
538 | RTR3DECL(int) RTFileSetTimes(RTFILE File, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
|
---|
539 | PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
|
---|
540 | {
|
---|
541 | /*
|
---|
542 | * We can only set AccessTime and ModificationTime, so if neither
|
---|
543 | * are specified we can return immediately.
|
---|
544 | */
|
---|
545 | if (!pAccessTime && !pModificationTime)
|
---|
546 | return VINF_SUCCESS;
|
---|
547 |
|
---|
548 | /*
|
---|
549 | * Convert the input to timeval, getting the missing one if necessary,
|
---|
550 | * and call the API which does the change.
|
---|
551 | */
|
---|
552 | struct timeval aTimevals[2];
|
---|
553 | if (pAccessTime && pModificationTime)
|
---|
554 | {
|
---|
555 | RTTimeSpecGetTimeval(pAccessTime, &aTimevals[0]);
|
---|
556 | RTTimeSpecGetTimeval(pModificationTime, &aTimevals[1]);
|
---|
557 | }
|
---|
558 | else
|
---|
559 | {
|
---|
560 | RTFSOBJINFO ObjInfo;
|
---|
561 | int rc = RTFileQueryInfo(File, &ObjInfo, RTFSOBJATTRADD_UNIX);
|
---|
562 | if (RT_FAILURE(rc))
|
---|
563 | return rc;
|
---|
564 | RTTimeSpecGetTimeval(pAccessTime ? pAccessTime : &ObjInfo.AccessTime, &aTimevals[0]);
|
---|
565 | RTTimeSpecGetTimeval(pModificationTime ? pModificationTime : &ObjInfo.ModificationTime, &aTimevals[1]);
|
---|
566 | }
|
---|
567 |
|
---|
568 | if (futimes((int)File, aTimevals))
|
---|
569 | {
|
---|
570 | int rc = RTErrConvertFromErrno(errno);
|
---|
571 | Log(("RTFileSetTimes(%RTfile,%p,%p,,): returns %Rrc\n", File, pAccessTime, pModificationTime, rc));
|
---|
572 | return rc;
|
---|
573 | }
|
---|
574 | return VINF_SUCCESS;
|
---|
575 | }
|
---|
576 |
|
---|
577 |
|
---|
578 | RTR3DECL(int) RTFileSetMode(RTFILE File, RTFMODE fMode)
|
---|
579 | {
|
---|
580 | /*
|
---|
581 | * Normalize the mode and call the API.
|
---|
582 | */
|
---|
583 | fMode = rtFsModeNormalize(fMode, NULL, 0);
|
---|
584 | if (!rtFsModeIsValid(fMode))
|
---|
585 | return VERR_INVALID_PARAMETER;
|
---|
586 |
|
---|
587 | if (fchmod((int)File, fMode & RTFS_UNIX_MASK))
|
---|
588 | {
|
---|
589 | int rc = RTErrConvertFromErrno(errno);
|
---|
590 | Log(("RTFileSetMode(%RTfile,%RTfmode): returns %Rrc\n", File, fMode, rc));
|
---|
591 | return rc;
|
---|
592 | }
|
---|
593 | return VINF_SUCCESS;
|
---|
594 | }
|
---|
595 |
|
---|
596 |
|
---|
597 | RTR3DECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename)
|
---|
598 | {
|
---|
599 | /*
|
---|
600 | * Validate input.
|
---|
601 | */
|
---|
602 | AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
|
---|
603 | AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
|
---|
604 | AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
|
---|
605 | AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
|
---|
606 | AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
|
---|
607 |
|
---|
608 | /*
|
---|
609 | * Take common cause with RTPathRename.
|
---|
610 | */
|
---|
611 | int rc = rtPathPosixRename(pszSrc, pszDst, fRename, RTFS_TYPE_FILE);
|
---|
612 |
|
---|
613 | LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
|
---|
614 | pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
|
---|
615 | return rc;
|
---|
616 | }
|
---|
617 |
|
---|
618 |
|
---|