VirtualBox

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

Last change on this file since 28800 was 28800, checked in by vboxsync, 14 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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

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