VirtualBox

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

Last change on this file since 24889 was 24889, checked in by vboxsync, 16 years ago

IPRT: Added RTFileQueryFsSizes.

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

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