1 | /* $Id: fileio-posix.cpp 21499 2009-07-10 20:34:48Z 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 | #if defined(O_DIRECT) && defined(RT_OS_LINUX)
|
---|
146 | /* O_DIRECT is mandatory to get async I/O working on Linux. */
|
---|
147 | if (fOpen & RTFILE_O_ASYNC_IO)
|
---|
148 | fOpenMode |= O_DIRECT;
|
---|
149 | #endif
|
---|
150 | #if defined(O_DIRECT) && (defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD))
|
---|
151 | /* Disable the kernel cache. */
|
---|
152 | if (fOpen & RTFILE_O_NO_CACHE)
|
---|
153 | fOpenMode |= O_DIRECT;
|
---|
154 | #endif
|
---|
155 |
|
---|
156 | /* create/truncate file */
|
---|
157 | switch (fOpen & RTFILE_O_ACTION_MASK)
|
---|
158 | {
|
---|
159 | case RTFILE_O_OPEN: break;
|
---|
160 | case RTFILE_O_OPEN_CREATE: fOpenMode |= O_CREAT; break;
|
---|
161 | case RTFILE_O_CREATE: fOpenMode |= O_CREAT | O_EXCL; break;
|
---|
162 | case RTFILE_O_CREATE_REPLACE: fOpenMode |= O_CREAT | O_TRUNC; break; /** @todo replacing needs fixing, this is *not* a 1:1 mapping! */
|
---|
163 | }
|
---|
164 | if (fOpen & RTFILE_O_TRUNCATE)
|
---|
165 | fOpenMode |= O_TRUNC;
|
---|
166 |
|
---|
167 | switch (fOpen & RTFILE_O_ACCESS_MASK)
|
---|
168 | {
|
---|
169 | case RTFILE_O_READ: fOpenMode |= O_RDONLY; break;
|
---|
170 | case RTFILE_O_WRITE: fOpenMode |= O_WRONLY; break;
|
---|
171 | case RTFILE_O_READWRITE: fOpenMode |= O_RDWR; break;
|
---|
172 | default:
|
---|
173 | AssertMsgFailed(("RTFileOpen received an invalid RW value, fOpen=%#x\n", fOpen));
|
---|
174 | return VERR_INVALID_PARAMETER;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /* File mode. */
|
---|
178 | int fMode = (fOpen & RTFILE_O_CREATE_MODE_MASK)
|
---|
179 | ? (fOpen & RTFILE_O_CREATE_MODE_MASK) >> RTFILE_O_CREATE_MODE_SHIFT
|
---|
180 | : RT_FILE_PERMISSION;
|
---|
181 |
|
---|
182 | /** @todo sharing! */
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * Open/create the file.
|
---|
186 | */
|
---|
187 | #ifdef RT_DONT_CONVERT_FILENAMES
|
---|
188 | int fh = open(pszFilename, fOpenMode, fMode);
|
---|
189 | int iErr = errno;
|
---|
190 | #else
|
---|
191 | char *pszNativeFilename;
|
---|
192 | rc = rtPathToNative(&pszNativeFilename, pszFilename);
|
---|
193 | if (RT_FAILURE(rc))
|
---|
194 | return (rc);
|
---|
195 |
|
---|
196 | int fh = open(pszNativeFilename, fOpenMode, fMode);
|
---|
197 | int iErr = errno;
|
---|
198 | rtPathFreeNative(pszNativeFilename);
|
---|
199 | #endif
|
---|
200 | if (fh >= 0)
|
---|
201 | {
|
---|
202 | /*
|
---|
203 | * Mark the file handle close on exec, unless inherit is specified.
|
---|
204 | */
|
---|
205 | if ( !(fOpen & RTFILE_O_INHERIT)
|
---|
206 | #ifdef O_NOINHERIT
|
---|
207 | || (fOpenMode & O_NOINHERIT) /* careful since it could be a dummy. */
|
---|
208 | #endif
|
---|
209 | || fcntl(fh, F_SETFD, FD_CLOEXEC) >= 0)
|
---|
210 | {
|
---|
211 | #if defined(RT_OS_SOLARIS) || defined(RT_OS_DARWIN)
|
---|
212 | iErr = 0;
|
---|
213 | /* Switch direct I/O on now if requested */
|
---|
214 | if (fOpen & RTFILE_O_NO_CACHE)
|
---|
215 | # if defined(RT_OS_SOLARIS) && !defined(IN_GUEST)
|
---|
216 | iErr = directio(fh, DIRECTIO_ON);
|
---|
217 | # elif defined(RT_OS_DARWIN)
|
---|
218 | iErr = fcntl(fh, F_NOCACHE);
|
---|
219 | # endif
|
---|
220 | if (iErr < 0)
|
---|
221 | iErr = errno;
|
---|
222 | else
|
---|
223 | #endif
|
---|
224 | {
|
---|
225 | *pFile = (RTFILE)fh;
|
---|
226 | Assert((int)*pFile == fh);
|
---|
227 | LogFlow(("RTFileOpen(%p:{%RTfile}, %p:{%s}, %#x): returns %Rrc\n",
|
---|
228 | pFile, *pFile, pszFilename, pszFilename, fOpen, rc));
|
---|
229 | return VINF_SUCCESS;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | iErr = errno;
|
---|
233 | close(fh);
|
---|
234 | }
|
---|
235 | return RTErrConvertFromErrno(iErr);
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | RTR3DECL(int) RTFileClose(RTFILE File)
|
---|
240 | {
|
---|
241 | if (close((int)File) == 0)
|
---|
242 | return VINF_SUCCESS;
|
---|
243 | return RTErrConvertFromErrno(errno);
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | RTR3DECL(int) RTFileFromNative(PRTFILE pFile, RTHCINTPTR uNative)
|
---|
248 | {
|
---|
249 | if ( uNative < 0
|
---|
250 | || (RTFILE)uNative != (RTUINTPTR)uNative)
|
---|
251 | {
|
---|
252 | AssertMsgFailed(("%p\n", uNative));
|
---|
253 | *pFile = NIL_RTFILE;
|
---|
254 | return VERR_INVALID_HANDLE;
|
---|
255 | }
|
---|
256 | *pFile = (RTFILE)uNative;
|
---|
257 | return VINF_SUCCESS;
|
---|
258 | }
|
---|
259 |
|
---|
260 |
|
---|
261 | RTR3DECL(RTHCINTPTR) RTFileToNative(RTFILE File)
|
---|
262 | {
|
---|
263 | AssertReturn(File != NIL_RTFILE, -1);
|
---|
264 | return (RTHCINTPTR)File;
|
---|
265 | }
|
---|
266 |
|
---|
267 |
|
---|
268 | RTR3DECL(int) RTFileDelete(const char *pszFilename)
|
---|
269 | {
|
---|
270 | char *pszNativeFilename;
|
---|
271 | int rc = rtPathToNative(&pszNativeFilename, pszFilename);
|
---|
272 | if (RT_SUCCESS(rc))
|
---|
273 | {
|
---|
274 | if (unlink(pszNativeFilename) != 0)
|
---|
275 | rc = RTErrConvertFromErrno(errno);
|
---|
276 | rtPathFreeNative(pszNativeFilename);
|
---|
277 | }
|
---|
278 | return rc;
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | RTR3DECL(int) RTFileSeek(RTFILE File, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
|
---|
283 | {
|
---|
284 | static const unsigned aSeekRecode[] =
|
---|
285 | {
|
---|
286 | SEEK_SET,
|
---|
287 | SEEK_CUR,
|
---|
288 | SEEK_END,
|
---|
289 | };
|
---|
290 |
|
---|
291 | /*
|
---|
292 | * Validate input.
|
---|
293 | */
|
---|
294 | if (uMethod > RTFILE_SEEK_END)
|
---|
295 | {
|
---|
296 | AssertMsgFailed(("Invalid uMethod=%d\n", uMethod));
|
---|
297 | return VERR_INVALID_PARAMETER;
|
---|
298 | }
|
---|
299 |
|
---|
300 | /* check that within off_t range. */
|
---|
301 | if ( sizeof(off_t) < sizeof(offSeek)
|
---|
302 | && ( (offSeek > 0 && (unsigned)(offSeek >> 32) != 0)
|
---|
303 | || (offSeek < 0 && (unsigned)(-offSeek >> 32) != 0)))
|
---|
304 | {
|
---|
305 | AssertMsgFailed(("64-bit search not supported\n"));
|
---|
306 | return VERR_NOT_SUPPORTED;
|
---|
307 | }
|
---|
308 |
|
---|
309 | off_t offCurrent = lseek((int)File, (off_t)offSeek, aSeekRecode[uMethod]);
|
---|
310 | if (offCurrent != ~0)
|
---|
311 | {
|
---|
312 | if (poffActual)
|
---|
313 | *poffActual = (uint64_t)offCurrent;
|
---|
314 | return VINF_SUCCESS;
|
---|
315 | }
|
---|
316 | return RTErrConvertFromErrno(errno);
|
---|
317 | }
|
---|
318 |
|
---|
319 |
|
---|
320 | RTR3DECL(int) RTFileRead(RTFILE File, void *pvBuf, size_t cbToRead, size_t *pcbRead)
|
---|
321 | {
|
---|
322 | if (cbToRead <= 0)
|
---|
323 | return VINF_SUCCESS;
|
---|
324 |
|
---|
325 | /*
|
---|
326 | * Attempt read.
|
---|
327 | */
|
---|
328 | ssize_t cbRead = read((int)File, pvBuf, cbToRead);
|
---|
329 | if (cbRead >= 0)
|
---|
330 | {
|
---|
331 | if (pcbRead)
|
---|
332 | /* caller can handle partial read. */
|
---|
333 | *pcbRead = cbRead;
|
---|
334 | else
|
---|
335 | {
|
---|
336 | /* Caller expects all to be read. */
|
---|
337 | while ((ssize_t)cbToRead > cbRead)
|
---|
338 | {
|
---|
339 | ssize_t cbReadPart = read((int)File, (char*)pvBuf + cbRead, cbToRead - cbRead);
|
---|
340 | if (cbReadPart <= 0)
|
---|
341 | {
|
---|
342 | if (cbReadPart == 0)
|
---|
343 | return VERR_EOF;
|
---|
344 | return RTErrConvertFromErrno(errno);
|
---|
345 | }
|
---|
346 | cbRead += cbReadPart;
|
---|
347 | }
|
---|
348 | }
|
---|
349 | return VINF_SUCCESS;
|
---|
350 | }
|
---|
351 |
|
---|
352 | return RTErrConvertFromErrno(errno);
|
---|
353 | }
|
---|
354 |
|
---|
355 |
|
---|
356 | RTR3DECL(int) RTFileWrite(RTFILE File, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
|
---|
357 | {
|
---|
358 | if (cbToWrite <= 0)
|
---|
359 | return VINF_SUCCESS;
|
---|
360 |
|
---|
361 | /*
|
---|
362 | * Attempt write.
|
---|
363 | */
|
---|
364 | ssize_t cbWritten = write((int)File, pvBuf, cbToWrite);
|
---|
365 | if (cbWritten >= 0)
|
---|
366 | {
|
---|
367 | if (pcbWritten)
|
---|
368 | /* caller can handle partial write. */
|
---|
369 | *pcbWritten = cbWritten;
|
---|
370 | else
|
---|
371 | {
|
---|
372 | /* Caller expects all to be write. */
|
---|
373 | while ((ssize_t)cbToWrite > cbWritten)
|
---|
374 | {
|
---|
375 | ssize_t cbWrittenPart = write((int)File, (const char *)pvBuf + cbWritten, cbToWrite - cbWritten);
|
---|
376 | if (cbWrittenPart <= 0)
|
---|
377 | return RTErrConvertFromErrno(errno);
|
---|
378 | cbWritten += cbWrittenPart;
|
---|
379 | }
|
---|
380 | }
|
---|
381 | return VINF_SUCCESS;
|
---|
382 | }
|
---|
383 | return RTErrConvertFromErrno(errno);
|
---|
384 | }
|
---|
385 |
|
---|
386 |
|
---|
387 | RTR3DECL(int) RTFileSetSize(RTFILE File, uint64_t cbSize)
|
---|
388 | {
|
---|
389 | /*
|
---|
390 | * Validate offset.
|
---|
391 | */
|
---|
392 | if ( sizeof(off_t) < sizeof(cbSize)
|
---|
393 | && (cbSize >> 32) != 0)
|
---|
394 | {
|
---|
395 | AssertMsgFailed(("64-bit filesize not supported! cbSize=%lld\n", cbSize));
|
---|
396 | return VERR_NOT_SUPPORTED;
|
---|
397 | }
|
---|
398 |
|
---|
399 | #if defined(_MSC_VER) || (defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006))
|
---|
400 | if (chsize((int)File, (off_t)cbSize) == 0)
|
---|
401 | #else
|
---|
402 | /* This relies on a non-standard feature of FreeBSD, Linux, and OS/2
|
---|
403 | * LIBC v0.6 and higher. (SuS doesn't define ftruncate() and size bigger
|
---|
404 | * than the file.)
|
---|
405 | */
|
---|
406 | if (ftruncate((int)File, (off_t)cbSize) == 0)
|
---|
407 | #endif
|
---|
408 | return VINF_SUCCESS;
|
---|
409 | return RTErrConvertFromErrno(errno);
|
---|
410 | }
|
---|
411 |
|
---|
412 |
|
---|
413 | RTR3DECL(int) RTFileGetSize(RTFILE File, uint64_t *pcbSize)
|
---|
414 | {
|
---|
415 | struct stat st;
|
---|
416 | if (!fstat((int)File, &st))
|
---|
417 | {
|
---|
418 | *pcbSize = st.st_size;
|
---|
419 | return VINF_SUCCESS;
|
---|
420 | }
|
---|
421 | return RTErrConvertFromErrno(errno);
|
---|
422 | }
|
---|
423 |
|
---|
424 |
|
---|
425 | /**
|
---|
426 | * Determine the maximum file size.
|
---|
427 | *
|
---|
428 | * @returns IPRT status code.
|
---|
429 | * @param File Handle to the file.
|
---|
430 | * @param pcbMax Where to store the max file size.
|
---|
431 | * @see RTFileGetMaxSize.
|
---|
432 | */
|
---|
433 | RTR3DECL(int) RTFileGetMaxSizeEx(RTFILE File, PRTFOFF pcbMax)
|
---|
434 | {
|
---|
435 | /*
|
---|
436 | * Save the current location
|
---|
437 | */
|
---|
438 | uint64_t offOld;
|
---|
439 | int rc = RTFileSeek(File, 0, RTFILE_SEEK_CURRENT, &offOld);
|
---|
440 | if (RT_FAILURE(rc))
|
---|
441 | return rc;
|
---|
442 |
|
---|
443 | /*
|
---|
444 | * Perform a binary search for the max file size.
|
---|
445 | */
|
---|
446 | uint64_t offLow = 0;
|
---|
447 | uint64_t offHigh = 8 * _1T; /* we don't need bigger files */
|
---|
448 | /** @todo Unfortunately this does not work for certain file system types,
|
---|
449 | * for instance cifs mounts. Even worse, statvfs.f_fsid returns 0 for such
|
---|
450 | * file systems. */
|
---|
451 | //uint64_t offHigh = INT64_MAX;
|
---|
452 | for (;;)
|
---|
453 | {
|
---|
454 | uint64_t cbInterval = (offHigh - offLow) >> 1;
|
---|
455 | if (cbInterval == 0)
|
---|
456 | {
|
---|
457 | if (pcbMax)
|
---|
458 | *pcbMax = offLow;
|
---|
459 | return RTFileSeek(File, offOld, RTFILE_SEEK_BEGIN, NULL);
|
---|
460 | }
|
---|
461 |
|
---|
462 | rc = RTFileSeek(File, offLow + cbInterval, RTFILE_SEEK_BEGIN, NULL);
|
---|
463 | if (RT_FAILURE(rc))
|
---|
464 | offHigh = offLow + cbInterval;
|
---|
465 | else
|
---|
466 | offLow = offLow + cbInterval;
|
---|
467 | }
|
---|
468 | }
|
---|
469 |
|
---|
470 |
|
---|
471 | RTR3DECL(bool) RTFileIsValid(RTFILE File)
|
---|
472 | {
|
---|
473 | if (File != NIL_RTFILE)
|
---|
474 | {
|
---|
475 | int fFlags = fcntl(File, F_GETFD);
|
---|
476 | if (fFlags >= 0)
|
---|
477 | return true;
|
---|
478 | }
|
---|
479 | return false;
|
---|
480 | }
|
---|
481 |
|
---|
482 |
|
---|
483 | RTR3DECL(int) RTFileFlush(RTFILE File)
|
---|
484 | {
|
---|
485 | if (fsync((int)File))
|
---|
486 | return RTErrConvertFromErrno(errno);
|
---|
487 | return VINF_SUCCESS;
|
---|
488 | }
|
---|
489 |
|
---|
490 |
|
---|
491 | RTR3DECL(int) RTFileIoCtl(RTFILE File, int iRequest, void *pvData, unsigned cbData, int *piRet)
|
---|
492 | {
|
---|
493 | int rc = ioctl((int)File, iRequest, pvData);
|
---|
494 | if (piRet)
|
---|
495 | *piRet = rc;
|
---|
496 | return rc >= 0 ? VINF_SUCCESS : RTErrConvertFromErrno(errno);
|
---|
497 | }
|
---|
498 |
|
---|
499 |
|
---|
500 | RTR3DECL(int) RTFileQueryInfo(RTFILE File, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
|
---|
501 | {
|
---|
502 | /*
|
---|
503 | * Validate input.
|
---|
504 | */
|
---|
505 | if (File == NIL_RTFILE)
|
---|
506 | {
|
---|
507 | AssertMsgFailed(("Invalid File=%RTfile\n", File));
|
---|
508 | return VERR_INVALID_PARAMETER;
|
---|
509 | }
|
---|
510 | if (!pObjInfo)
|
---|
511 | {
|
---|
512 | AssertMsgFailed(("Invalid pObjInfo=%p\n", pObjInfo));
|
---|
513 | return VERR_INVALID_PARAMETER;
|
---|
514 | }
|
---|
515 | if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
|
---|
516 | || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
|
---|
517 | {
|
---|
518 | AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
|
---|
519 | return VERR_INVALID_PARAMETER;
|
---|
520 | }
|
---|
521 |
|
---|
522 | /*
|
---|
523 | * Query file info.
|
---|
524 | */
|
---|
525 | struct stat Stat;
|
---|
526 | if (fstat((int)File, &Stat))
|
---|
527 | {
|
---|
528 | int rc = RTErrConvertFromErrno(errno);
|
---|
529 | Log(("RTFileQueryInfo(%RTfile,,%d): returns %Rrc\n", File, enmAdditionalAttribs, rc));
|
---|
530 | return rc;
|
---|
531 | }
|
---|
532 |
|
---|
533 | /*
|
---|
534 | * Setup the returned data.
|
---|
535 | */
|
---|
536 | rtFsConvertStatToObjInfo(pObjInfo, &Stat, NULL, 0);
|
---|
537 |
|
---|
538 | /*
|
---|
539 | * Requested attributes (we cannot provide anything actually).
|
---|
540 | */
|
---|
541 | switch (enmAdditionalAttribs)
|
---|
542 | {
|
---|
543 | case RTFSOBJATTRADD_EASIZE:
|
---|
544 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
|
---|
545 | pObjInfo->Attr.u.EASize.cb = 0;
|
---|
546 | break;
|
---|
547 |
|
---|
548 | case RTFSOBJATTRADD_NOTHING:
|
---|
549 | case RTFSOBJATTRADD_UNIX:
|
---|
550 | /* done */
|
---|
551 | break;
|
---|
552 |
|
---|
553 | default:
|
---|
554 | AssertMsgFailed(("Impossible!\n"));
|
---|
555 | return VERR_INTERNAL_ERROR;
|
---|
556 | }
|
---|
557 |
|
---|
558 | LogFlow(("RTFileQueryInfo(%RTfile,,%d): returns VINF_SUCCESS\n", File, enmAdditionalAttribs));
|
---|
559 | return VINF_SUCCESS;
|
---|
560 | }
|
---|
561 |
|
---|
562 |
|
---|
563 | RTR3DECL(int) RTFileSetTimes(RTFILE File, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
|
---|
564 | PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
|
---|
565 | {
|
---|
566 | /*
|
---|
567 | * We can only set AccessTime and ModificationTime, so if neither
|
---|
568 | * are specified we can return immediately.
|
---|
569 | */
|
---|
570 | if (!pAccessTime && !pModificationTime)
|
---|
571 | return VINF_SUCCESS;
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * Convert the input to timeval, getting the missing one if necessary,
|
---|
575 | * and call the API which does the change.
|
---|
576 | */
|
---|
577 | struct timeval aTimevals[2];
|
---|
578 | if (pAccessTime && pModificationTime)
|
---|
579 | {
|
---|
580 | RTTimeSpecGetTimeval(pAccessTime, &aTimevals[0]);
|
---|
581 | RTTimeSpecGetTimeval(pModificationTime, &aTimevals[1]);
|
---|
582 | }
|
---|
583 | else
|
---|
584 | {
|
---|
585 | RTFSOBJINFO ObjInfo;
|
---|
586 | int rc = RTFileQueryInfo(File, &ObjInfo, RTFSOBJATTRADD_UNIX);
|
---|
587 | if (RT_FAILURE(rc))
|
---|
588 | return rc;
|
---|
589 | RTTimeSpecGetTimeval(pAccessTime ? pAccessTime : &ObjInfo.AccessTime, &aTimevals[0]);
|
---|
590 | RTTimeSpecGetTimeval(pModificationTime ? pModificationTime : &ObjInfo.ModificationTime, &aTimevals[1]);
|
---|
591 | }
|
---|
592 |
|
---|
593 | if (futimes((int)File, aTimevals))
|
---|
594 | {
|
---|
595 | int rc = RTErrConvertFromErrno(errno);
|
---|
596 | Log(("RTFileSetTimes(%RTfile,%p,%p,,): returns %Rrc\n", File, pAccessTime, pModificationTime, rc));
|
---|
597 | return rc;
|
---|
598 | }
|
---|
599 | return VINF_SUCCESS;
|
---|
600 | }
|
---|
601 |
|
---|
602 |
|
---|
603 | RTR3DECL(int) RTFileSetMode(RTFILE File, RTFMODE fMode)
|
---|
604 | {
|
---|
605 | /*
|
---|
606 | * Normalize the mode and call the API.
|
---|
607 | */
|
---|
608 | fMode = rtFsModeNormalize(fMode, NULL, 0);
|
---|
609 | if (!rtFsModeIsValid(fMode))
|
---|
610 | return VERR_INVALID_PARAMETER;
|
---|
611 |
|
---|
612 | if (fchmod((int)File, fMode & RTFS_UNIX_MASK))
|
---|
613 | {
|
---|
614 | int rc = RTErrConvertFromErrno(errno);
|
---|
615 | Log(("RTFileSetMode(%RTfile,%RTfmode): returns %Rrc\n", File, fMode, rc));
|
---|
616 | return rc;
|
---|
617 | }
|
---|
618 | return VINF_SUCCESS;
|
---|
619 | }
|
---|
620 |
|
---|
621 |
|
---|
622 | RTR3DECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename)
|
---|
623 | {
|
---|
624 | /*
|
---|
625 | * Validate input.
|
---|
626 | */
|
---|
627 | AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
|
---|
628 | AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
|
---|
629 | AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
|
---|
630 | AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
|
---|
631 | AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
|
---|
632 |
|
---|
633 | /*
|
---|
634 | * Take common cause with RTPathRename.
|
---|
635 | */
|
---|
636 | int rc = rtPathPosixRename(pszSrc, pszDst, fRename, RTFS_TYPE_FILE);
|
---|
637 |
|
---|
638 | LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
|
---|
639 | pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
|
---|
640 | return rc;
|
---|
641 | }
|
---|
642 |
|
---|
643 |
|
---|