VirtualBox

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

Last change on this file since 77577 was 77234, checked in by vboxsync, 6 years ago

IPRT/fileio-posix.cpp: Return VERR_TRY_AGAIN when write() returns zero and we don't have pchWritten, don't try to convert errno as it shouldn't be set on 0 returns.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 21.9 KB
Line 
1/* $Id: fileio-posix.cpp 77234 2019-02-09 18:28:44Z vboxsync $ */
2/** @file
3 * IPRT - File I/O, POSIX, Part 1.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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) || defined(RT_OS_NETBSD))
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 default:
394 AssertFailedReturn(NIL_RTFILE);
395 }
396
397 struct stat st;
398 int rc = fstat(fd, &st);
399 if (rc == -1)
400 return NIL_RTFILE;
401 return (RTFILE)(intptr_t)fd;
402}
403
404
405RTR3DECL(int) RTFileDelete(const char *pszFilename)
406{
407 char const *pszNativeFilename;
408 int rc = rtPathToNative(&pszNativeFilename, pszFilename, NULL);
409 if (RT_SUCCESS(rc))
410 {
411 if (unlink(pszNativeFilename) != 0)
412 rc = RTErrConvertFromErrno(errno);
413 rtPathFreeNative(pszNativeFilename, pszFilename);
414 }
415 return rc;
416}
417
418
419RTR3DECL(int) RTFileSeek(RTFILE hFile, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
420{
421 static const unsigned aSeekRecode[] =
422 {
423 SEEK_SET,
424 SEEK_CUR,
425 SEEK_END,
426 };
427
428 /*
429 * Validate input.
430 */
431 if (uMethod > RTFILE_SEEK_END)
432 {
433 AssertMsgFailed(("Invalid uMethod=%d\n", uMethod));
434 return VERR_INVALID_PARAMETER;
435 }
436
437 /* check that within off_t range. */
438 if ( sizeof(off_t) < sizeof(offSeek)
439 && ( (offSeek > 0 && (unsigned)(offSeek >> 32) != 0)
440 || (offSeek < 0 && (unsigned)(-offSeek >> 32) != 0)))
441 {
442 AssertMsgFailed(("64-bit search not supported\n"));
443 return VERR_NOT_SUPPORTED;
444 }
445
446 off_t offCurrent = lseek(RTFileToNative(hFile), (off_t)offSeek, aSeekRecode[uMethod]);
447 if (offCurrent != ~0)
448 {
449 if (poffActual)
450 *poffActual = (uint64_t)offCurrent;
451 return VINF_SUCCESS;
452 }
453 return RTErrConvertFromErrno(errno);
454}
455
456
457RTR3DECL(int) RTFileRead(RTFILE hFile, void *pvBuf, size_t cbToRead, size_t *pcbRead)
458{
459 if (cbToRead <= 0)
460 {
461 if (pcbRead)
462 *pcbRead = 0;
463 return VINF_SUCCESS;
464 }
465
466 /*
467 * Attempt read.
468 */
469 ssize_t cbRead = read(RTFileToNative(hFile), pvBuf, cbToRead);
470 if (cbRead >= 0)
471 {
472 if (pcbRead)
473 /* caller can handle partial read. */
474 *pcbRead = cbRead;
475 else
476 {
477 /* Caller expects all to be read. */
478 while ((ssize_t)cbToRead > cbRead)
479 {
480 ssize_t cbReadPart = read(RTFileToNative(hFile), (char*)pvBuf + cbRead, cbToRead - cbRead);
481 if (cbReadPart <= 0)
482 {
483 if (cbReadPart == 0)
484 return VERR_EOF;
485 return RTErrConvertFromErrno(errno);
486 }
487 cbRead += cbReadPart;
488 }
489 }
490 return VINF_SUCCESS;
491 }
492
493 return RTErrConvertFromErrno(errno);
494}
495
496
497RTR3DECL(int) RTFileWrite(RTFILE hFile, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
498{
499 if (cbToWrite <= 0)
500 return VINF_SUCCESS;
501
502 /*
503 * Attempt write.
504 */
505 ssize_t cbWritten = write(RTFileToNative(hFile), pvBuf, cbToWrite);
506 if (cbWritten >= 0)
507 {
508 if (pcbWritten)
509 /* caller can handle partial write. */
510 *pcbWritten = cbWritten;
511 else
512 {
513 /* Caller expects all to be write. */
514 while ((ssize_t)cbToWrite > cbWritten)
515 {
516 ssize_t cbWrittenPart = write(RTFileToNative(hFile), (const char *)pvBuf + cbWritten, cbToWrite - cbWritten);
517 if (cbWrittenPart <= 0)
518 return cbWrittenPart < 0 ? RTErrConvertFromErrno(errno) : VERR_TRY_AGAIN;
519 cbWritten += cbWrittenPart;
520 }
521 }
522 return VINF_SUCCESS;
523 }
524 return RTErrConvertFromErrno(errno);
525}
526
527
528RTR3DECL(int) RTFileSetSize(RTFILE hFile, uint64_t cbSize)
529{
530 /*
531 * Validate offset.
532 */
533 if ( sizeof(off_t) < sizeof(cbSize)
534 && (cbSize >> 32) != 0)
535 {
536 AssertMsgFailed(("64-bit filesize not supported! cbSize=%lld\n", cbSize));
537 return VERR_NOT_SUPPORTED;
538 }
539
540#if defined(_MSC_VER) || (defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006))
541 if (chsize(RTFileToNative(hFile), (off_t)cbSize) == 0)
542#else
543 /* This relies on a non-standard feature of FreeBSD, Linux, and OS/2
544 * LIBC v0.6 and higher. (SuS doesn't define ftruncate() and size bigger
545 * than the file.)
546 */
547 if (ftruncate(RTFileToNative(hFile), (off_t)cbSize) == 0)
548#endif
549 return VINF_SUCCESS;
550 return RTErrConvertFromErrno(errno);
551}
552
553
554RTR3DECL(int) RTFileGetSize(RTFILE hFile, uint64_t *pcbSize)
555{
556 /*
557 * Ask fstat() first.
558 */
559 struct stat st;
560 if (!fstat(RTFileToNative(hFile), &st))
561 {
562 *pcbSize = st.st_size;
563 if ( st.st_size != 0
564#if defined(RT_OS_SOLARIS)
565 || (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
566#elif defined(RT_OS_FREEBSD) || defined(RT_OS_NETBSD)
567 || !S_ISCHR(st.st_mode)
568#else
569 || !S_ISBLK(st.st_mode)
570#endif
571 )
572 return VINF_SUCCESS;
573
574 /*
575 * It could be a block device. Try determin the size by I/O control
576 * query or seek.
577 */
578#ifdef RT_OS_DARWIN
579 uint64_t cBlocks;
580 if (!ioctl(RTFileToNative(hFile), DKIOCGETBLOCKCOUNT, &cBlocks))
581 {
582 uint32_t cbBlock;
583 if (!ioctl(RTFileToNative(hFile), DKIOCGETBLOCKSIZE, &cbBlock))
584 {
585 *pcbSize = cBlocks * cbBlock;
586 return VINF_SUCCESS;
587 }
588 }
589 /* must be a block device, fail on failure. */
590
591#elif defined(RT_OS_SOLARIS)
592 struct dk_minfo MediaInfo;
593 if (!ioctl(RTFileToNative(hFile), DKIOCGMEDIAINFO, &MediaInfo))
594 {
595 *pcbSize = MediaInfo.dki_capacity * MediaInfo.dki_lbsize;
596 return VINF_SUCCESS;
597 }
598 /* might not be a block device. */
599 if (errno == EINVAL || errno == ENOTTY)
600 return VINF_SUCCESS;
601
602#elif defined(RT_OS_FREEBSD)
603 off_t cbMedia = 0;
604 if (!ioctl(RTFileToNative(hFile), DIOCGMEDIASIZE, &cbMedia))
605 {
606 *pcbSize = cbMedia;
607 return VINF_SUCCESS;
608 }
609 /* might not be a block device. */
610 if (errno == EINVAL || errno == ENOTTY)
611 return VINF_SUCCESS;
612
613#else
614 /* PORTME! Avoid this path when possible. */
615 uint64_t offSaved;
616 int rc = RTFileSeek(hFile, 0, RTFILE_SEEK_CURRENT, &offSaved);
617 if (RT_SUCCESS(rc))
618 {
619 rc = RTFileSeek(hFile, 0, RTFILE_SEEK_END, pcbSize);
620 int rc2 = RTFileSeek(hFile, offSaved, RTFILE_SEEK_BEGIN, NULL);
621 if (RT_SUCCESS(rc))
622 return rc2;
623 }
624#endif
625 }
626 return RTErrConvertFromErrno(errno);
627}
628
629
630RTR3DECL(int) RTFileGetMaxSizeEx(RTFILE hFile, PRTFOFF pcbMax)
631{
632 /*
633 * Save the current location
634 */
635 uint64_t offOld;
636 int rc = RTFileSeek(hFile, 0, RTFILE_SEEK_CURRENT, &offOld);
637 if (RT_FAILURE(rc))
638 return rc;
639
640 /*
641 * Perform a binary search for the max file size.
642 */
643 uint64_t offLow = 0;
644 uint64_t offHigh = 8 * _1T; /* we don't need bigger files */
645 /** @todo Unfortunately this does not work for certain file system types,
646 * for instance cifs mounts. Even worse, statvfs.f_fsid returns 0 for such
647 * file systems. */
648 //uint64_t offHigh = INT64_MAX;
649 for (;;)
650 {
651 uint64_t cbInterval = (offHigh - offLow) >> 1;
652 if (cbInterval == 0)
653 {
654 if (pcbMax)
655 *pcbMax = offLow;
656 return RTFileSeek(hFile, offOld, RTFILE_SEEK_BEGIN, NULL);
657 }
658
659 rc = RTFileSeek(hFile, offLow + cbInterval, RTFILE_SEEK_BEGIN, NULL);
660 if (RT_FAILURE(rc))
661 offHigh = offLow + cbInterval;
662 else
663 offLow = offLow + cbInterval;
664 }
665}
666
667
668RTR3DECL(bool) RTFileIsValid(RTFILE hFile)
669{
670 if (hFile != NIL_RTFILE)
671 {
672 int fFlags = fcntl(RTFileToNative(hFile), F_GETFD);
673 if (fFlags >= 0)
674 return true;
675 }
676 return false;
677}
678
679
680RTR3DECL(int) RTFileFlush(RTFILE hFile)
681{
682 if (fsync(RTFileToNative(hFile)))
683 return RTErrConvertFromErrno(errno);
684 return VINF_SUCCESS;
685}
686
687
688RTR3DECL(int) RTFileIoCtl(RTFILE hFile, unsigned long ulRequest, void *pvData, unsigned cbData, int *piRet)
689{
690 NOREF(cbData);
691 int rc = ioctl(RTFileToNative(hFile), ulRequest, pvData);
692 if (piRet)
693 *piRet = rc;
694 return rc >= 0 ? VINF_SUCCESS : RTErrConvertFromErrno(errno);
695}
696
697
698RTR3DECL(int) RTFileSetMode(RTFILE hFile, RTFMODE fMode)
699{
700 /*
701 * Normalize the mode and call the API.
702 */
703 fMode = rtFsModeNormalize(fMode, NULL, 0);
704 if (!rtFsModeIsValid(fMode))
705 return VERR_INVALID_PARAMETER;
706
707 if (fchmod(RTFileToNative(hFile), fMode & RTFS_UNIX_MASK))
708 {
709 int rc = RTErrConvertFromErrno(errno);
710 Log(("RTFileSetMode(%RTfile,%RTfmode): returns %Rrc\n", hFile, fMode, rc));
711 return rc;
712 }
713 return VINF_SUCCESS;
714}
715
716
717RTDECL(int) RTFileSetOwner(RTFILE hFile, uint32_t uid, uint32_t gid)
718{
719 uid_t uidNative = uid != NIL_RTUID ? (uid_t)uid : (uid_t)-1;
720 AssertReturn(uid == uidNative, VERR_INVALID_PARAMETER);
721 gid_t gidNative = gid != NIL_RTGID ? (gid_t)gid : (gid_t)-1;
722 AssertReturn(gid == gidNative, VERR_INVALID_PARAMETER);
723
724 if (fchown(RTFileToNative(hFile), uidNative, gidNative))
725 return RTErrConvertFromErrno(errno);
726 return VINF_SUCCESS;
727}
728
729
730RTR3DECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename)
731{
732 /*
733 * Validate input.
734 */
735 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
736 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
737 AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
738 AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
739 AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
740
741 /*
742 * Take common cause with RTPathRename.
743 */
744 int rc = rtPathPosixRename(pszSrc, pszDst, fRename, RTFS_TYPE_FILE);
745
746 LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
747 pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
748 return rc;
749}
750
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