VirtualBox

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

Last change on this file since 76553 was 76553, checked in by vboxsync, 5 years ago

scm --update-copyright-year

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

© 2023 Oracle
ContactPrivacy policyTerms of Use