VirtualBox

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

Last change on this file since 25659 was 25642, checked in by vboxsync, 15 years ago

DOXYGEN -> DOXYGEN_RUNNING.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 21.2 KB
Line 
1/* $Id: fileio-posix.cpp 25642 2010-01-05 08:19:10Z 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) RTFileClose(RTFILE File)
338{
339 if (close((int)File) == 0)
340 return VINF_SUCCESS;
341 return RTErrConvertFromErrno(errno);
342}
343
344
345RTR3DECL(int) RTFileFromNative(PRTFILE pFile, RTHCINTPTR uNative)
346{
347 if ( uNative < 0
348 || (RTFILE)uNative != (RTUINTPTR)uNative)
349 {
350 AssertMsgFailed(("%p\n", uNative));
351 *pFile = NIL_RTFILE;
352 return VERR_INVALID_HANDLE;
353 }
354 *pFile = (RTFILE)uNative;
355 return VINF_SUCCESS;
356}
357
358
359RTR3DECL(RTHCINTPTR) RTFileToNative(RTFILE File)
360{
361 AssertReturn(File != NIL_RTFILE, -1);
362 return (RTHCINTPTR)File;
363}
364
365
366RTR3DECL(int) RTFileDelete(const char *pszFilename)
367{
368 char *pszNativeFilename;
369 int rc = rtPathToNative(&pszNativeFilename, pszFilename);
370 if (RT_SUCCESS(rc))
371 {
372 if (unlink(pszNativeFilename) != 0)
373 rc = RTErrConvertFromErrno(errno);
374 rtPathFreeNative(pszNativeFilename);
375 }
376 return rc;
377}
378
379
380RTR3DECL(int) RTFileSeek(RTFILE File, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
381{
382 static const unsigned aSeekRecode[] =
383 {
384 SEEK_SET,
385 SEEK_CUR,
386 SEEK_END,
387 };
388
389 /*
390 * Validate input.
391 */
392 if (uMethod > RTFILE_SEEK_END)
393 {
394 AssertMsgFailed(("Invalid uMethod=%d\n", uMethod));
395 return VERR_INVALID_PARAMETER;
396 }
397
398 /* check that within off_t range. */
399 if ( sizeof(off_t) < sizeof(offSeek)
400 && ( (offSeek > 0 && (unsigned)(offSeek >> 32) != 0)
401 || (offSeek < 0 && (unsigned)(-offSeek >> 32) != 0)))
402 {
403 AssertMsgFailed(("64-bit search not supported\n"));
404 return VERR_NOT_SUPPORTED;
405 }
406
407 off_t offCurrent = lseek((int)File, (off_t)offSeek, aSeekRecode[uMethod]);
408 if (offCurrent != ~0)
409 {
410 if (poffActual)
411 *poffActual = (uint64_t)offCurrent;
412 return VINF_SUCCESS;
413 }
414 return RTErrConvertFromErrno(errno);
415}
416
417
418RTR3DECL(int) RTFileRead(RTFILE File, void *pvBuf, size_t cbToRead, size_t *pcbRead)
419{
420 if (cbToRead <= 0)
421 return VINF_SUCCESS;
422
423 /*
424 * Attempt read.
425 */
426 ssize_t cbRead = read((int)File, pvBuf, cbToRead);
427 if (cbRead >= 0)
428 {
429 if (pcbRead)
430 /* caller can handle partial read. */
431 *pcbRead = cbRead;
432 else
433 {
434 /* Caller expects all to be read. */
435 while ((ssize_t)cbToRead > cbRead)
436 {
437 ssize_t cbReadPart = read((int)File, (char*)pvBuf + cbRead, cbToRead - cbRead);
438 if (cbReadPart <= 0)
439 {
440 if (cbReadPart == 0)
441 return VERR_EOF;
442 return RTErrConvertFromErrno(errno);
443 }
444 cbRead += cbReadPart;
445 }
446 }
447 return VINF_SUCCESS;
448 }
449
450 return RTErrConvertFromErrno(errno);
451}
452
453
454RTR3DECL(int) RTFileWrite(RTFILE File, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
455{
456 if (cbToWrite <= 0)
457 return VINF_SUCCESS;
458
459 /*
460 * Attempt write.
461 */
462 ssize_t cbWritten = write((int)File, pvBuf, cbToWrite);
463 if (cbWritten >= 0)
464 {
465 if (pcbWritten)
466 /* caller can handle partial write. */
467 *pcbWritten = cbWritten;
468 else
469 {
470 /* Caller expects all to be write. */
471 while ((ssize_t)cbToWrite > cbWritten)
472 {
473 ssize_t cbWrittenPart = write((int)File, (const char *)pvBuf + cbWritten, cbToWrite - cbWritten);
474 if (cbWrittenPart <= 0)
475 return RTErrConvertFromErrno(errno);
476 cbWritten += cbWrittenPart;
477 }
478 }
479 return VINF_SUCCESS;
480 }
481 return RTErrConvertFromErrno(errno);
482}
483
484
485RTR3DECL(int) RTFileSetSize(RTFILE File, uint64_t cbSize)
486{
487 /*
488 * Validate offset.
489 */
490 if ( sizeof(off_t) < sizeof(cbSize)
491 && (cbSize >> 32) != 0)
492 {
493 AssertMsgFailed(("64-bit filesize not supported! cbSize=%lld\n", cbSize));
494 return VERR_NOT_SUPPORTED;
495 }
496
497#if defined(_MSC_VER) || (defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006))
498 if (chsize((int)File, (off_t)cbSize) == 0)
499#else
500 /* This relies on a non-standard feature of FreeBSD, Linux, and OS/2
501 * LIBC v0.6 and higher. (SuS doesn't define ftruncate() and size bigger
502 * than the file.)
503 */
504 if (ftruncate((int)File, (off_t)cbSize) == 0)
505#endif
506 return VINF_SUCCESS;
507 return RTErrConvertFromErrno(errno);
508}
509
510
511RTR3DECL(int) RTFileGetSize(RTFILE File, uint64_t *pcbSize)
512{
513 struct stat st;
514 if (!fstat((int)File, &st))
515 {
516 *pcbSize = st.st_size;
517 return VINF_SUCCESS;
518 }
519 return RTErrConvertFromErrno(errno);
520}
521
522
523/**
524 * Determine the maximum file size.
525 *
526 * @returns IPRT status code.
527 * @param File Handle to the file.
528 * @param pcbMax Where to store the max file size.
529 * @see RTFileGetMaxSize.
530 */
531RTR3DECL(int) RTFileGetMaxSizeEx(RTFILE File, PRTFOFF pcbMax)
532{
533 /*
534 * Save the current location
535 */
536 uint64_t offOld;
537 int rc = RTFileSeek(File, 0, RTFILE_SEEK_CURRENT, &offOld);
538 if (RT_FAILURE(rc))
539 return rc;
540
541 /*
542 * Perform a binary search for the max file size.
543 */
544 uint64_t offLow = 0;
545 uint64_t offHigh = 8 * _1T; /* we don't need bigger files */
546 /** @todo Unfortunately this does not work for certain file system types,
547 * for instance cifs mounts. Even worse, statvfs.f_fsid returns 0 for such
548 * file systems. */
549 //uint64_t offHigh = INT64_MAX;
550 for (;;)
551 {
552 uint64_t cbInterval = (offHigh - offLow) >> 1;
553 if (cbInterval == 0)
554 {
555 if (pcbMax)
556 *pcbMax = offLow;
557 return RTFileSeek(File, offOld, RTFILE_SEEK_BEGIN, NULL);
558 }
559
560 rc = RTFileSeek(File, offLow + cbInterval, RTFILE_SEEK_BEGIN, NULL);
561 if (RT_FAILURE(rc))
562 offHigh = offLow + cbInterval;
563 else
564 offLow = offLow + cbInterval;
565 }
566}
567
568
569RTR3DECL(bool) RTFileIsValid(RTFILE File)
570{
571 if (File != NIL_RTFILE)
572 {
573 int fFlags = fcntl(File, F_GETFD);
574 if (fFlags >= 0)
575 return true;
576 }
577 return false;
578}
579
580
581RTR3DECL(int) RTFileFlush(RTFILE File)
582{
583 if (fsync((int)File))
584 return RTErrConvertFromErrno(errno);
585 return VINF_SUCCESS;
586}
587
588
589RTR3DECL(int) RTFileIoCtl(RTFILE File, int iRequest, void *pvData, unsigned cbData, int *piRet)
590{
591 int rc = ioctl((int)File, iRequest, pvData);
592 if (piRet)
593 *piRet = rc;
594 return rc >= 0 ? VINF_SUCCESS : RTErrConvertFromErrno(errno);
595}
596
597
598RTR3DECL(int) RTFileQueryInfo(RTFILE File, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
599{
600 /*
601 * Validate input.
602 */
603 if (File == NIL_RTFILE)
604 {
605 AssertMsgFailed(("Invalid File=%RTfile\n", File));
606 return VERR_INVALID_PARAMETER;
607 }
608 if (!pObjInfo)
609 {
610 AssertMsgFailed(("Invalid pObjInfo=%p\n", pObjInfo));
611 return VERR_INVALID_PARAMETER;
612 }
613 if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
614 || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
615 {
616 AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
617 return VERR_INVALID_PARAMETER;
618 }
619
620 /*
621 * Query file info.
622 */
623 struct stat Stat;
624 if (fstat((int)File, &Stat))
625 {
626 int rc = RTErrConvertFromErrno(errno);
627 Log(("RTFileQueryInfo(%RTfile,,%d): returns %Rrc\n", File, enmAdditionalAttribs, rc));
628 return rc;
629 }
630
631 /*
632 * Setup the returned data.
633 */
634 rtFsConvertStatToObjInfo(pObjInfo, &Stat, NULL, 0);
635
636 /*
637 * Requested attributes (we cannot provide anything actually).
638 */
639 switch (enmAdditionalAttribs)
640 {
641 case RTFSOBJATTRADD_EASIZE:
642 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
643 pObjInfo->Attr.u.EASize.cb = 0;
644 break;
645
646 case RTFSOBJATTRADD_NOTHING:
647 case RTFSOBJATTRADD_UNIX:
648 /* done */
649 break;
650
651 default:
652 AssertMsgFailed(("Impossible!\n"));
653 return VERR_INTERNAL_ERROR;
654 }
655
656 LogFlow(("RTFileQueryInfo(%RTfile,,%d): returns VINF_SUCCESS\n", File, enmAdditionalAttribs));
657 return VINF_SUCCESS;
658}
659
660
661RTR3DECL(int) RTFileSetTimes(RTFILE File, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
662 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
663{
664 /*
665 * We can only set AccessTime and ModificationTime, so if neither
666 * are specified we can return immediately.
667 */
668 if (!pAccessTime && !pModificationTime)
669 return VINF_SUCCESS;
670
671 /*
672 * Convert the input to timeval, getting the missing one if necessary,
673 * and call the API which does the change.
674 */
675 struct timeval aTimevals[2];
676 if (pAccessTime && pModificationTime)
677 {
678 RTTimeSpecGetTimeval(pAccessTime, &aTimevals[0]);
679 RTTimeSpecGetTimeval(pModificationTime, &aTimevals[1]);
680 }
681 else
682 {
683 RTFSOBJINFO ObjInfo;
684 int rc = RTFileQueryInfo(File, &ObjInfo, RTFSOBJATTRADD_UNIX);
685 if (RT_FAILURE(rc))
686 return rc;
687 RTTimeSpecGetTimeval(pAccessTime ? pAccessTime : &ObjInfo.AccessTime, &aTimevals[0]);
688 RTTimeSpecGetTimeval(pModificationTime ? pModificationTime : &ObjInfo.ModificationTime, &aTimevals[1]);
689 }
690
691 if (futimes((int)File, aTimevals))
692 {
693 int rc = RTErrConvertFromErrno(errno);
694 Log(("RTFileSetTimes(%RTfile,%p,%p,,): returns %Rrc\n", File, pAccessTime, pModificationTime, rc));
695 return rc;
696 }
697 return VINF_SUCCESS;
698}
699
700
701RTR3DECL(int) RTFileSetMode(RTFILE File, RTFMODE fMode)
702{
703 /*
704 * Normalize the mode and call the API.
705 */
706 fMode = rtFsModeNormalize(fMode, NULL, 0);
707 if (!rtFsModeIsValid(fMode))
708 return VERR_INVALID_PARAMETER;
709
710 if (fchmod((int)File, fMode & RTFS_UNIX_MASK))
711 {
712 int rc = RTErrConvertFromErrno(errno);
713 Log(("RTFileSetMode(%RTfile,%RTfmode): returns %Rrc\n", File, fMode, rc));
714 return rc;
715 }
716 return VINF_SUCCESS;
717}
718
719
720RTR3DECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename)
721{
722 /*
723 * Validate input.
724 */
725 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
726 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
727 AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
728 AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
729 AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
730
731 /*
732 * Take common cause with RTPathRename.
733 */
734 int rc = rtPathPosixRename(pszSrc, pszDst, fRename, RTFS_TYPE_FILE);
735
736 LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
737 pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
738 return rc;
739}
740
741
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