VirtualBox

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

Last change on this file since 27042 was 26761, checked in by vboxsync, 15 years ago

IPRT: Implemented RTFileOpenBitBucket.

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

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