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