VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/fileio-posix.cpp@ 43363

Last change on this file since 43363 was 43363, checked in by vboxsync, 12 years ago

Haiku Additions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 21.1 KB
Line 
1/* $Id: fileio-posix.cpp 43363 2012-09-20 09:56:07Z vboxsync $ */
2/** @file
3 * IPRT - File I/O, POSIX, Part 1.
4 */
5
6/*
7 * Copyright (C) 2006-2012 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#include <sys/ioctl.h>
37#include <fcntl.h>
38#ifdef _MSC_VER
39# include <io.h>
40# include <stdio.h>
41#else
42# include <unistd.h>
43# include <sys/time.h>
44#endif
45#ifdef RT_OS_LINUX
46# include <sys/file.h>
47#endif
48#if defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006)
49# include <io.h>
50#endif
51#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
52# include <sys/disk.h>
53#endif
54#ifdef RT_OS_SOLARIS
55# include <stropts.h>
56# include <sys/dkio.h>
57# include <sys/vtoc.h>
58#endif /* RT_OS_SOLARIS */
59
60#include <iprt/file.h>
61#include <iprt/path.h>
62#include <iprt/assert.h>
63#include <iprt/string.h>
64#include <iprt/err.h>
65#include <iprt/log.h>
66#include "internal/file.h"
67#include "internal/fs.h"
68#include "internal/path.h"
69
70
71
72/*******************************************************************************
73* Defined Constants And Macros *
74*******************************************************************************/
75/** Default file permissions for newly created files. */
76#if defined(S_IRUSR) && defined(S_IWUSR)
77# define RT_FILE_PERMISSION (S_IRUSR | S_IWUSR)
78#else
79# define RT_FILE_PERMISSION (00600)
80#endif
81
82
83RTDECL(bool) RTFileExists(const char *pszPath)
84{
85 bool fRc = false;
86 char const *pszNativePath;
87 int rc = rtPathToNative(&pszNativePath, pszPath, NULL);
88 if (RT_SUCCESS(rc))
89 {
90 struct stat s;
91 fRc = !stat(pszNativePath, &s)
92 && S_ISREG(s.st_mode);
93
94 rtPathFreeNative(pszNativePath, pszPath);
95 }
96
97 LogFlow(("RTFileExists(%p={%s}): returns %RTbool\n", pszPath, pszPath, fRc));
98 return fRc;
99}
100
101
102RTR3DECL(int) RTFileOpen(PRTFILE pFile, const char *pszFilename, uint64_t fOpen)
103{
104 /*
105 * Validate input.
106 */
107 AssertPtrReturn(pFile, VERR_INVALID_POINTER);
108 *pFile = NIL_RTFILE;
109 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
110
111 /*
112 * Merge forced open flags and validate them.
113 */
114 int rc = rtFileRecalcAndValidateFlags(&fOpen);
115 if (RT_FAILURE(rc))
116 return rc;
117#ifndef O_NONBLOCK
118 if (fOpen & RTFILE_O_NON_BLOCK)
119 {
120 AssertMsgFailed(("Invalid parameters! fOpen=%#llx\n", fOpen));
121 return VERR_INVALID_PARAMETER;
122 }
123#endif
124
125 /*
126 * Calculate open mode flags.
127 */
128 int fOpenMode = 0;
129#ifdef O_BINARY
130 fOpenMode |= O_BINARY; /* (pc) */
131#endif
132#ifdef O_LARGEFILE
133 fOpenMode |= O_LARGEFILE; /* (linux, solaris) */
134#endif
135#ifdef O_NOINHERIT
136 if (!(fOpen & RTFILE_O_INHERIT))
137 fOpenMode |= O_NOINHERIT;
138#endif
139#ifdef O_CLOEXEC
140 static int s_fHave_O_CLOEXEC = 0; /* {-1,0,1}; since Linux 2.6.23 */
141 if (!(fOpen & RTFILE_O_INHERIT) && s_fHave_O_CLOEXEC >= 0)
142 fOpenMode |= O_CLOEXEC;
143#endif
144#ifdef O_NONBLOCK
145 if (fOpen & RTFILE_O_NON_BLOCK)
146 fOpenMode |= O_NONBLOCK;
147#endif
148#ifdef O_SYNC
149 if (fOpen & RTFILE_O_WRITE_THROUGH)
150 fOpenMode |= O_SYNC;
151#endif
152#if defined(O_DIRECT) && defined(RT_OS_LINUX)
153 /* O_DIRECT is mandatory to get async I/O working on Linux. */
154 if (fOpen & RTFILE_O_ASYNC_IO)
155 fOpenMode |= O_DIRECT;
156#endif
157#if defined(O_DIRECT) && (defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD))
158 /* Disable the kernel cache. */
159 if (fOpen & RTFILE_O_NO_CACHE)
160 fOpenMode |= O_DIRECT;
161#endif
162
163 /* create/truncate file */
164 switch (fOpen & RTFILE_O_ACTION_MASK)
165 {
166 case RTFILE_O_OPEN: break;
167 case RTFILE_O_OPEN_CREATE: fOpenMode |= O_CREAT; break;
168 case RTFILE_O_CREATE: fOpenMode |= O_CREAT | O_EXCL; break;
169 case RTFILE_O_CREATE_REPLACE: fOpenMode |= O_CREAT | O_TRUNC; break; /** @todo replacing needs fixing, this is *not* a 1:1 mapping! */
170 }
171 if (fOpen & RTFILE_O_TRUNCATE)
172 fOpenMode |= O_TRUNC;
173
174 switch (fOpen & RTFILE_O_ACCESS_MASK)
175 {
176 case RTFILE_O_READ:
177 fOpenMode |= O_RDONLY; /* RTFILE_O_APPEND is ignored. */
178 break;
179 case RTFILE_O_WRITE:
180 fOpenMode |= fOpen & RTFILE_O_APPEND ? O_APPEND | O_WRONLY : O_WRONLY;
181 break;
182 case RTFILE_O_READWRITE:
183 fOpenMode |= fOpen & RTFILE_O_APPEND ? O_APPEND | O_RDWR : O_RDWR;
184 break;
185 default:
186 AssertMsgFailed(("RTFileOpen received an invalid RW value, fOpen=%#llx\n", fOpen));
187 return VERR_INVALID_PARAMETER;
188 }
189
190 /* File mode. */
191 int fMode = (fOpen & RTFILE_O_CREATE_MODE_MASK)
192 ? (fOpen & RTFILE_O_CREATE_MODE_MASK) >> RTFILE_O_CREATE_MODE_SHIFT
193 : RT_FILE_PERMISSION;
194
195 /** @todo sharing! */
196
197 /*
198 * Open/create the file.
199 */
200 char const *pszNativeFilename;
201 rc = rtPathToNative(&pszNativeFilename, pszFilename, NULL);
202 if (RT_FAILURE(rc))
203 return (rc);
204
205 int fh = open(pszNativeFilename, fOpenMode, fMode);
206 int iErr = errno;
207
208#ifdef O_CLOEXEC
209 if ( (fOpenMode & O_CLOEXEC)
210 && s_fHave_O_CLOEXEC == 0)
211 {
212 if (fh < 0 && iErr == EINVAL)
213 {
214 s_fHave_O_CLOEXEC = -1;
215 fh = open(pszNativeFilename, fOpenMode, fMode);
216 iErr = errno;
217 }
218 else if (fh >= 0)
219 s_fHave_O_CLOEXEC = fcntl(fh, F_GETFD, 0) > 0 ? 1 : -1;
220 }
221#endif
222
223 rtPathFreeNative(pszNativeFilename, pszFilename);
224 if (fh >= 0)
225 {
226 iErr = 0;
227
228 /*
229 * Mark the file handle close on exec, unless inherit is specified.
230 */
231 if ( !(fOpen & RTFILE_O_INHERIT)
232#ifdef O_NOINHERIT
233 && !(fOpenMode & O_NOINHERIT) /* Take care since it might be a zero value dummy. */
234#endif
235#ifdef O_CLOEXEC
236 && s_fHave_O_CLOEXEC <= 0
237#endif
238 )
239 iErr = fcntl(fh, F_SETFD, FD_CLOEXEC) >= 0 ? 0 : errno;
240
241 /*
242 * Switch direct I/O on now if requested and required.
243 */
244#if defined(RT_OS_DARWIN) \
245 || (defined(RT_OS_SOLARIS) && !defined(IN_GUEST))
246 if (iErr == 0 && (fOpen & RTFILE_O_NO_CACHE))
247 {
248# if defined(RT_OS_DARWIN)
249 iErr = fcntl(fh, F_NOCACHE, 1) >= 0 ? 0 : errno;
250# else
251 iErr = directio(fh, DIRECTIO_ON) >= 0 ? 0 : errno;
252# endif
253 }
254#endif
255
256 /*
257 * Implement / emulate file sharing.
258 *
259 * We need another mode which allows skipping this stuff completely
260 * and do things the UNIX way. So for the present this is just a debug
261 * aid that can be enabled by developers too lazy to test on Windows.
262 */
263#if 0 && defined(RT_OS_LINUX)
264 if (iErr == 0)
265 {
266 /* This approach doesn't work because only knfsd checks for these
267 buggers. :-( */
268 int iLockOp;
269 switch (fOpen & RTFILE_O_DENY_MASK)
270 {
271 default:
272 AssertFailed();
273 case RTFILE_O_DENY_NONE:
274 case RTFILE_O_DENY_NOT_DELETE:
275 iLockOp = LOCK_MAND | LOCK_READ | LOCK_WRITE;
276 break;
277 case RTFILE_O_DENY_READ:
278 case RTFILE_O_DENY_READ | RTFILE_O_DENY_NOT_DELETE:
279 iLockOp = LOCK_MAND | LOCK_WRITE;
280 break;
281 case RTFILE_O_DENY_WRITE:
282 case RTFILE_O_DENY_WRITE | RTFILE_O_DENY_NOT_DELETE:
283 iLockOp = LOCK_MAND | LOCK_READ;
284 break;
285 case RTFILE_O_DENY_WRITE | RTFILE_O_DENY_READ:
286 case RTFILE_O_DENY_WRITE | RTFILE_O_DENY_READ | RTFILE_O_DENY_NOT_DELETE:
287 iLockOp = LOCK_MAND;
288 break;
289 }
290 iErr = flock(fh, iLockOp | LOCK_NB);
291 if (iErr != 0)
292 iErr = errno == EAGAIN ? ETXTBSY : 0;
293 }
294#endif /* 0 && RT_OS_LINUX */
295#if defined(DEBUG_bird) && !defined(RT_OS_SOLARIS)
296 if (iErr == 0)
297 {
298 /* This emulation is incomplete but useful. */
299 switch (fOpen & RTFILE_O_DENY_MASK)
300 {
301 default:
302 AssertFailed();
303 case RTFILE_O_DENY_NONE:
304 case RTFILE_O_DENY_NOT_DELETE:
305 case RTFILE_O_DENY_READ:
306 case RTFILE_O_DENY_READ | RTFILE_O_DENY_NOT_DELETE:
307 break;
308 case RTFILE_O_DENY_WRITE:
309 case RTFILE_O_DENY_WRITE | RTFILE_O_DENY_NOT_DELETE:
310 case RTFILE_O_DENY_WRITE | RTFILE_O_DENY_READ:
311 case RTFILE_O_DENY_WRITE | RTFILE_O_DENY_READ | RTFILE_O_DENY_NOT_DELETE:
312 if (fOpen & RTFILE_O_WRITE)
313 {
314 iErr = flock(fh, LOCK_EX | LOCK_NB);
315 if (iErr != 0)
316 iErr = errno == EAGAIN ? ETXTBSY : 0;
317 }
318 break;
319 }
320 }
321#endif
322#ifdef RT_OS_SOLARIS
323 /** @todo Use fshare_t and associates, it's a perfect match. see sys/fcntl.h */
324#endif
325
326 /*
327 * We're done.
328 */
329 if (iErr == 0)
330 {
331 *pFile = (RTFILE)(uintptr_t)fh;
332 Assert((intptr_t)*pFile == fh);
333 LogFlow(("RTFileOpen(%p:{%RTfile}, %p:{%s}, %#llx): returns %Rrc\n",
334 pFile, *pFile, pszFilename, pszFilename, fOpen, rc));
335 return VINF_SUCCESS;
336 }
337
338 close(fh);
339 }
340 return RTErrConvertFromErrno(iErr);
341}
342
343
344RTR3DECL(int) RTFileOpenBitBucket(PRTFILE phFile, uint64_t fAccess)
345{
346 AssertReturn( fAccess == RTFILE_O_READ
347 || fAccess == RTFILE_O_WRITE
348 || fAccess == RTFILE_O_READWRITE,
349 VERR_INVALID_PARAMETER);
350 return RTFileOpen(phFile, "/dev/null", fAccess | RTFILE_O_DENY_NONE | RTFILE_O_OPEN);
351}
352
353
354RTR3DECL(int) RTFileClose(RTFILE hFile)
355{
356 if (hFile == NIL_RTFILE)
357 return VINF_SUCCESS;
358 if (close(RTFileToNative(hFile)) == 0)
359 return VINF_SUCCESS;
360 return RTErrConvertFromErrno(errno);
361}
362
363
364RTR3DECL(int) RTFileFromNative(PRTFILE pFile, RTHCINTPTR uNative)
365{
366 AssertCompile(sizeof(uNative) == sizeof(*pFile));
367 if (uNative < 0)
368 {
369 AssertMsgFailed(("%p\n", uNative));
370 *pFile = NIL_RTFILE;
371 return VERR_INVALID_HANDLE;
372 }
373 *pFile = (RTFILE)uNative;
374 return VINF_SUCCESS;
375}
376
377
378RTR3DECL(RTHCINTPTR) RTFileToNative(RTFILE hFile)
379{
380 AssertReturn(hFile != NIL_RTFILE, -1);
381 return (intptr_t)hFile;
382}
383
384
385RTFILE rtFileGetStandard(RTHANDLESTD enmStdHandle)
386{
387 int fd;
388 switch (enmStdHandle)
389 {
390 case RTHANDLESTD_INPUT: fd = 0; break;
391 case RTHANDLESTD_OUTPUT: fd = 1; break;
392 case RTHANDLESTD_ERROR: fd = 2; break;
393 break;
394 default:
395 AssertFailedReturn(NIL_RTFILE);
396 }
397
398 struct stat st;
399 int rc = fstat(fd, &st);
400 if (rc == -1)
401 return NIL_RTFILE;
402 return (RTFILE)(intptr_t)fd;
403}
404
405
406RTR3DECL(int) RTFileDelete(const char *pszFilename)
407{
408 char const *pszNativeFilename;
409 int rc = rtPathToNative(&pszNativeFilename, pszFilename, NULL);
410 if (RT_SUCCESS(rc))
411 {
412 if (unlink(pszNativeFilename) != 0)
413 rc = RTErrConvertFromErrno(errno);
414 rtPathFreeNative(pszNativeFilename, pszFilename);
415 }
416 return rc;
417}
418
419
420RTR3DECL(int) RTFileSeek(RTFILE hFile, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
421{
422 static const unsigned aSeekRecode[] =
423 {
424 SEEK_SET,
425 SEEK_CUR,
426 SEEK_END,
427 };
428
429 /*
430 * Validate input.
431 */
432 if (uMethod > RTFILE_SEEK_END)
433 {
434 AssertMsgFailed(("Invalid uMethod=%d\n", uMethod));
435 return VERR_INVALID_PARAMETER;
436 }
437
438 /* check that within off_t range. */
439 if ( sizeof(off_t) < sizeof(offSeek)
440 && ( (offSeek > 0 && (unsigned)(offSeek >> 32) != 0)
441 || (offSeek < 0 && (unsigned)(-offSeek >> 32) != 0)))
442 {
443 AssertMsgFailed(("64-bit search not supported\n"));
444 return VERR_NOT_SUPPORTED;
445 }
446
447 off_t offCurrent = lseek(RTFileToNative(hFile), (off_t)offSeek, aSeekRecode[uMethod]);
448 if (offCurrent != ~0)
449 {
450 if (poffActual)
451 *poffActual = (uint64_t)offCurrent;
452 return VINF_SUCCESS;
453 }
454 return RTErrConvertFromErrno(errno);
455}
456
457
458RTR3DECL(int) RTFileRead(RTFILE hFile, void *pvBuf, size_t cbToRead, size_t *pcbRead)
459{
460 if (cbToRead <= 0)
461 return VINF_SUCCESS;
462
463 /*
464 * Attempt read.
465 */
466 ssize_t cbRead = read(RTFileToNative(hFile), pvBuf, cbToRead);
467 if (cbRead >= 0)
468 {
469 if (pcbRead)
470 /* caller can handle partial read. */
471 *pcbRead = cbRead;
472 else
473 {
474 /* Caller expects all to be read. */
475 while ((ssize_t)cbToRead > cbRead)
476 {
477 ssize_t cbReadPart = read(RTFileToNative(hFile), (char*)pvBuf + cbRead, cbToRead - cbRead);
478 if (cbReadPart <= 0)
479 {
480 if (cbReadPart == 0)
481 return VERR_EOF;
482 return RTErrConvertFromErrno(errno);
483 }
484 cbRead += cbReadPart;
485 }
486 }
487 return VINF_SUCCESS;
488 }
489
490 return RTErrConvertFromErrno(errno);
491}
492
493
494RTR3DECL(int) RTFileWrite(RTFILE hFile, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
495{
496 if (cbToWrite <= 0)
497 return VINF_SUCCESS;
498
499 /*
500 * Attempt write.
501 */
502 ssize_t cbWritten = write(RTFileToNative(hFile), pvBuf, cbToWrite);
503 if (cbWritten >= 0)
504 {
505 if (pcbWritten)
506 /* caller can handle partial write. */
507 *pcbWritten = cbWritten;
508 else
509 {
510 /* Caller expects all to be write. */
511 while ((ssize_t)cbToWrite > cbWritten)
512 {
513 ssize_t cbWrittenPart = write(RTFileToNative(hFile), (const char *)pvBuf + cbWritten, cbToWrite - cbWritten);
514 if (cbWrittenPart <= 0)
515 return RTErrConvertFromErrno(errno);
516 cbWritten += cbWrittenPart;
517 }
518 }
519 return VINF_SUCCESS;
520 }
521 return RTErrConvertFromErrno(errno);
522}
523
524
525RTR3DECL(int) RTFileSetSize(RTFILE hFile, uint64_t cbSize)
526{
527 /*
528 * Validate offset.
529 */
530 if ( sizeof(off_t) < sizeof(cbSize)
531 && (cbSize >> 32) != 0)
532 {
533 AssertMsgFailed(("64-bit filesize not supported! cbSize=%lld\n", cbSize));
534 return VERR_NOT_SUPPORTED;
535 }
536
537#if defined(_MSC_VER) || (defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006))
538 if (chsize(RTFileToNative(hFile), (off_t)cbSize) == 0)
539#else
540 /* This relies on a non-standard feature of FreeBSD, Linux, and OS/2
541 * LIBC v0.6 and higher. (SuS doesn't define ftruncate() and size bigger
542 * than the file.)
543 */
544 if (ftruncate(RTFileToNative(hFile), (off_t)cbSize) == 0)
545#endif
546 return VINF_SUCCESS;
547 return RTErrConvertFromErrno(errno);
548}
549
550
551RTR3DECL(int) RTFileGetSize(RTFILE hFile, uint64_t *pcbSize)
552{
553 /*
554 * Ask fstat() first.
555 */
556 struct stat st;
557 if (!fstat(RTFileToNative(hFile), &st))
558 {
559 *pcbSize = st.st_size;
560 if ( st.st_size != 0
561#if defined(RT_OS_SOLARIS)
562 || (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
563#elif defined(RT_OS_FREEBSD)
564 || !S_ISCHR(st.st_mode)
565#else
566 || !S_ISBLK(st.st_mode)
567#endif
568 )
569 return VINF_SUCCESS;
570
571 /*
572 * It could be a block device. Try determin the size by I/O control
573 * query or seek.
574 */
575#ifdef RT_OS_DARWIN
576 uint64_t cBlocks;
577 if (!ioctl(RTFileToNative(hFile), DKIOCGETBLOCKCOUNT, &cBlocks))
578 {
579 uint32_t cbBlock;
580 if (!ioctl(RTFileToNative(hFile), DKIOCGETBLOCKSIZE, &cbBlock))
581 {
582 *pcbSize = cBlocks * cbBlock;
583 return VINF_SUCCESS;
584 }
585 }
586 /* must be a block device, fail on failure. */
587
588#elif defined(RT_OS_SOLARIS)
589 struct dk_minfo MediaInfo;
590 if (!ioctl(RTFileToNative(hFile), DKIOCGMEDIAINFO, &MediaInfo))
591 {
592 *pcbSize = MediaInfo.dki_capacity * MediaInfo.dki_lbsize;
593 return VINF_SUCCESS;
594 }
595 /* might not be a block device. */
596 if (errno == EINVAL || errno == ENOTTY)
597 return VINF_SUCCESS;
598
599#elif defined(RT_OS_FREEBSD)
600 off_t cbMedia = 0;
601 if (!ioctl(RTFileToNative(hFile), DIOCGMEDIASIZE, &cbMedia))
602 {
603 *pcbSize = cbMedia;
604 return VINF_SUCCESS;
605 }
606 /* might not be a block device. */
607 if (errno == EINVAL || errno == ENOTTY)
608 return VINF_SUCCESS;
609
610#else
611 /* PORTME! Avoid this path when possible. */
612 uint64_t offSaved;
613 int rc = RTFileSeek(hFile, 0, RTFILE_SEEK_CURRENT, &offSaved);
614 if (RT_SUCCESS(rc))
615 {
616 rc = RTFileSeek(hFile, 0, RTFILE_SEEK_END, pcbSize);
617 int rc2 = RTFileSeek(hFile, offSaved, RTFILE_SEEK_BEGIN, NULL);
618 if (RT_SUCCESS(rc))
619 return rc2;
620 }
621#endif
622 }
623 return RTErrConvertFromErrno(errno);
624}
625
626
627RTR3DECL(int) RTFileGetMaxSizeEx(RTFILE hFile, PRTFOFF pcbMax)
628{
629 /*
630 * Save the current location
631 */
632 uint64_t offOld;
633 int rc = RTFileSeek(hFile, 0, RTFILE_SEEK_CURRENT, &offOld);
634 if (RT_FAILURE(rc))
635 return rc;
636
637 /*
638 * Perform a binary search for the max file size.
639 */
640 uint64_t offLow = 0;
641 uint64_t offHigh = 8 * _1T; /* we don't need bigger files */
642 /** @todo Unfortunately this does not work for certain file system types,
643 * for instance cifs mounts. Even worse, statvfs.f_fsid returns 0 for such
644 * file systems. */
645 //uint64_t offHigh = INT64_MAX;
646 for (;;)
647 {
648 uint64_t cbInterval = (offHigh - offLow) >> 1;
649 if (cbInterval == 0)
650 {
651 if (pcbMax)
652 *pcbMax = offLow;
653 return RTFileSeek(hFile, offOld, RTFILE_SEEK_BEGIN, NULL);
654 }
655
656 rc = RTFileSeek(hFile, offLow + cbInterval, RTFILE_SEEK_BEGIN, NULL);
657 if (RT_FAILURE(rc))
658 offHigh = offLow + cbInterval;
659 else
660 offLow = offLow + cbInterval;
661 }
662}
663
664
665RTR3DECL(bool) RTFileIsValid(RTFILE hFile)
666{
667 if (hFile != NIL_RTFILE)
668 {
669 int fFlags = fcntl(RTFileToNative(hFile), F_GETFD);
670 if (fFlags >= 0)
671 return true;
672 }
673 return false;
674}
675
676
677RTR3DECL(int) RTFileFlush(RTFILE hFile)
678{
679 if (fsync(RTFileToNative(hFile)))
680 return RTErrConvertFromErrno(errno);
681 return VINF_SUCCESS;
682}
683
684
685RTR3DECL(int) RTFileIoCtl(RTFILE hFile, unsigned long ulRequest, void *pvData, unsigned cbData, int *piRet)
686{
687 NOREF(cbData);
688 int rc = ioctl(RTFileToNative(hFile), ulRequest, pvData);
689 if (piRet)
690 *piRet = rc;
691 return rc >= 0 ? VINF_SUCCESS : RTErrConvertFromErrno(errno);
692}
693
694
695RTR3DECL(int) RTFileSetMode(RTFILE hFile, RTFMODE fMode)
696{
697 /*
698 * Normalize the mode and call the API.
699 */
700 fMode = rtFsModeNormalize(fMode, NULL, 0);
701 if (!rtFsModeIsValid(fMode))
702 return VERR_INVALID_PARAMETER;
703
704 if (fchmod(RTFileToNative(hFile), fMode & RTFS_UNIX_MASK))
705 {
706 int rc = RTErrConvertFromErrno(errno);
707 Log(("RTFileSetMode(%RTfile,%RTfmode): returns %Rrc\n", hFile, fMode, rc));
708 return rc;
709 }
710 return VINF_SUCCESS;
711}
712
713
714RTR3DECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename)
715{
716 /*
717 * Validate input.
718 */
719 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
720 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
721 AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
722 AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
723 AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
724
725 /*
726 * Take common cause with RTPathRename.
727 */
728 int rc = rtPathPosixRename(pszSrc, pszDst, fRename, RTFS_TYPE_FILE);
729
730 LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
731 pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
732 return rc;
733}
734
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette