VirtualBox

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

Last change on this file since 8561 was 8561, checked in by vboxsync, 17 years ago

To/FromNative - posix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.7 KB
Line 
1/* $Id: fileio-posix.cpp 8561 2008-05-05 10:13:47Z 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#if defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006)
51# include <io.h>
52#endif
53#ifdef RT_OS_L4
54/* This is currently ifdef'ed out in the relevant L4 header file */
55/* Same as `utimes', but takes an open file descriptor instead of a name. */
56extern int futimes(int __fd, __const struct timeval __tvp[2]) __THROW;
57#endif
58
59#ifdef RT_OS_SOLARIS
60# define futimes(filedes, timeval) futimesat(filedes, NULL, timeval)
61#endif
62
63#include <iprt/file.h>
64#include <iprt/path.h>
65#include <iprt/assert.h>
66#include <iprt/string.h>
67#include <iprt/err.h>
68#include <iprt/log.h>
69#include "internal/file.h"
70#include "internal/fs.h"
71#include "internal/path.h"
72
73
74
75/*******************************************************************************
76* Defined Constants And Macros *
77*******************************************************************************/
78/** @def RT_DONT_CONVERT_FILENAMES
79 * Define this to pass UTF-8 unconverted to the kernel. */
80#ifdef __DOXYGEN__
81#define RT_DONT_CONVERT_FILENAMES 1
82#endif
83
84/** Default file permissions for newly created files. */
85#if defined(S_IRUSR) && defined(S_IWUSR)
86# define RT_FILE_PERMISSION (S_IRUSR | S_IWUSR)
87#else
88# define RT_FILE_PERMISSION (00600)
89#endif
90
91
92RTR3DECL(int) RTFileOpen(PRTFILE pFile, const char *pszFilename, unsigned fOpen)
93{
94 /*
95 * Validate input.
96 */
97 if (!VALID_PTR(pFile))
98 {
99 AssertMsgFailed(("Invalid pFile %p\n", pFile));
100 return VERR_INVALID_PARAMETER;
101 }
102 *pFile = NIL_RTFILE;
103 if (!VALID_PTR(pszFilename))
104 {
105 AssertMsgFailed(("Invalid pszFilename %p\n", pszFilename));
106 return VERR_INVALID_PARAMETER;
107 }
108
109 /*
110 * Merge forced open flags and validate them.
111 */
112 int rc = rtFileRecalcAndValidateFlags(&fOpen);
113 if (RT_FAILURE(rc))
114 return rc;
115#ifndef O_NONBLOCK
116 if (fOpen & RTFILE_O_NON_BLOCK)
117 {
118 AssertMsgFailed(("Invalid parameters! fOpen=%#x\n", fOpen));
119 return VERR_INVALID_PARAMETER;
120 }
121#endif
122
123 /*
124 * Calculate open mode flags.
125 */
126 int fOpenMode = 0;
127#ifdef O_BINARY
128 fOpenMode |= O_BINARY; /* (pc) */
129#endif
130#ifdef O_LARGEFILE
131 fOpenMode |= O_LARGEFILE; /* (linux) */
132#endif
133#ifdef O_NOINHERIT
134 if (!(fOpen & RTFILE_O_INHERIT))
135 fOpenMode |= O_NOINHERIT;
136#endif
137#ifdef O_NONBLOCK
138 if (fOpen & RTFILE_O_NON_BLOCK)
139 fOpenMode |= O_NONBLOCK;
140#endif
141#ifdef O_SYNC
142 if (fOpen & RTFILE_O_WRITE_THROUGH)
143 fOpenMode |= O_SYNC;
144#endif
145
146 /* create/truncate file */
147 switch (fOpen & RTFILE_O_ACTION_MASK)
148 {
149 case RTFILE_O_OPEN: break;
150 case RTFILE_O_OPEN_CREATE: fOpenMode |= O_CREAT; break;
151 case RTFILE_O_CREATE: fOpenMode |= O_CREAT | O_EXCL; break;
152 case RTFILE_O_CREATE_REPLACE: fOpenMode |= O_CREAT | O_TRUNC; break; /** @todo replacing needs fixing, this is *not* a 1:1 mapping! */
153 }
154 if (fOpen & RTFILE_O_TRUNCATE)
155 fOpenMode |= O_TRUNC;
156
157 switch (fOpen & RTFILE_O_ACCESS_MASK)
158 {
159 case RTFILE_O_READ: fOpenMode |= O_RDONLY; break;
160 case RTFILE_O_WRITE: fOpenMode |= O_WRONLY; break;
161 case RTFILE_O_READWRITE: fOpenMode |= O_RDWR; break;
162 default:
163 AssertMsgFailed(("RTFileOpen received an invalid RW value, fOpen=%#x\n", fOpen));
164 return VERR_INVALID_PARAMETER;
165 }
166
167 /** @todo sharing! */
168
169 /*
170 * Open/create the file.
171 */
172#ifdef RT_DONT_CONVERT_FILENAMES
173 int fh = open(pszFilename, fOpenMode, RT_FILE_PERMISSION);
174 int iErr = errno;
175#else
176 char *pszNativeFilename;
177 rc = rtPathToNative(&pszNativeFilename, pszFilename);
178 if (RT_FAILURE(rc))
179 return (rc);
180
181 int fh = open(pszNativeFilename, fOpenMode, RT_FILE_PERMISSION);
182 int iErr = errno;
183 rtPathFreeNative(pszNativeFilename);
184#endif
185 if (fh >= 0)
186 {
187 /*
188 * Mark the file handle close on exec, unless inherit is specified.
189 */
190 if ( !(fOpen & RTFILE_O_INHERIT)
191#ifdef O_NOINHERIT
192 || (fOpenMode & O_NOINHERIT) /* careful since it could be a dummy. */
193#endif
194 || fcntl(fh, F_SETFD, FD_CLOEXEC) >= 0)
195 {
196 *pFile = (RTFILE)fh;
197 Assert((int)*pFile == fh);
198 LogFlow(("RTFileOpen(%p:{%RTfile}, %p:{%s}, %#x): returns %Rrc\n",
199 pFile, *pFile, pszFilename, pszFilename, fOpen, rc));
200 return VINF_SUCCESS;
201 }
202 iErr = errno;
203 close(fh);
204 }
205 return RTErrConvertFromErrno(iErr);
206}
207
208
209RTR3DECL(int) RTFileClose(RTFILE File)
210{
211 if (close((int)File) == 0)
212 return VINF_SUCCESS;
213 return RTErrConvertFromErrno(errno);
214}
215
216
217RTR3DECL(int) RTFileFromNative(PRTFILE pFile, RTHCINTPTR uNative)
218{
219 if ( uNative < 0
220 || (RTFILE)uNative != (RTUINTPTR)uNative)
221 {
222 AssertMsgFailed(("%p\n", uNative));
223 *pFile = NIL_RTFILE;
224 return VERR_INVALID_HANDLE;
225 }
226 *pFile = (RTFILE)uNative;
227 return VINF_SUCCESS;
228}
229
230
231RTR3DECL(RTHCINTPTR) RTFileToNative(RTFILE File)
232{
233 AssertReturn(File != NIL_RTFILE, -1);
234 return (RTHCINTPTR)File;
235}
236
237
238RTR3DECL(int) RTFileDelete(const char *pszFilename)
239{
240 char *pszNativeFilename;
241 int rc = rtPathToNative(&pszNativeFilename, pszFilename);
242 if (RT_SUCCESS(rc))
243 {
244 if (unlink(pszNativeFilename) != 0)
245 rc = RTErrConvertFromErrno(errno);
246 rtPathFreeNative(pszNativeFilename);
247 }
248 return rc;
249}
250
251
252RTR3DECL(int) RTFileSeek(RTFILE File, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
253{
254 static const unsigned aSeekRecode[] =
255 {
256 SEEK_SET,
257 SEEK_CUR,
258 SEEK_END,
259 };
260
261 /*
262 * Validate input.
263 */
264 if (uMethod > RTFILE_SEEK_END)
265 {
266 AssertMsgFailed(("Invalid uMethod=%d\n", uMethod));
267 return VERR_INVALID_PARAMETER;
268 }
269
270 /* check that within off_t range. */
271 if ( sizeof(off_t) < sizeof(offSeek)
272 && ( (offSeek > 0 && (unsigned)(offSeek >> 32) != 0)
273 || (offSeek < 0 && (unsigned)(-offSeek >> 32) != 0)))
274 {
275 AssertMsgFailed(("64-bit search not supported\n"));
276 return VERR_NOT_SUPPORTED;
277 }
278
279 off_t offCurrent = lseek((int)File, (off_t)offSeek, aSeekRecode[uMethod]);
280 if (offCurrent != ~0)
281 {
282 if (poffActual)
283 *poffActual = (uint64_t)offCurrent;
284 return VINF_SUCCESS;
285 }
286 return RTErrConvertFromErrno(errno);
287}
288
289
290RTR3DECL(int) RTFileRead(RTFILE File, void *pvBuf, size_t cbToRead, size_t *pcbRead)
291{
292 if (cbToRead <= 0)
293 return VINF_SUCCESS;
294
295 /*
296 * Attempt read.
297 */
298 ssize_t cbRead = read((int)File, pvBuf, cbToRead);
299 if (cbRead >= 0)
300 {
301 if (pcbRead)
302 /* caller can handle partial read. */
303 *pcbRead = cbRead;
304 else
305 {
306 /* Caller expects all to be read. */
307 while ((ssize_t)cbToRead > cbRead)
308 {
309 ssize_t cbReadPart = read((int)File, (char*)pvBuf + cbRead, cbToRead - cbRead);
310 if (cbReadPart <= 0)
311 {
312 if (cbReadPart == 0)
313 return VERR_EOF;
314 return RTErrConvertFromErrno(errno);
315 }
316 cbRead += cbReadPart;
317 }
318 }
319 return VINF_SUCCESS;
320 }
321
322 return RTErrConvertFromErrno(errno);
323}
324
325
326RTR3DECL(int) RTFileWrite(RTFILE File, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
327{
328 if (cbToWrite <= 0)
329 return VINF_SUCCESS;
330
331 /*
332 * Attempt write.
333 */
334 ssize_t cbWritten = write((int)File, pvBuf, cbToWrite);
335 if (cbWritten >= 0)
336 {
337 if (pcbWritten)
338 /* caller can handle partial write. */
339 *pcbWritten = cbWritten;
340 else
341 {
342 /* Caller expects all to be write. */
343 while ((ssize_t)cbToWrite > cbWritten)
344 {
345 ssize_t cbWrittenPart = write((int)File, (const char *)pvBuf + cbWritten, cbToWrite - cbWritten);
346 if (cbWrittenPart <= 0)
347 return RTErrConvertFromErrno(errno);
348 cbWritten += cbWrittenPart;
349 }
350 }
351 return VINF_SUCCESS;
352 }
353 return RTErrConvertFromErrno(errno);
354}
355
356
357RTR3DECL(int) RTFileSetSize(RTFILE File, uint64_t cbSize)
358{
359 /*
360 * Validate offset.
361 */
362 if ( sizeof(off_t) < sizeof(cbSize)
363 && (cbSize >> 32) != 0)
364 {
365 AssertMsgFailed(("64-bit filesize not supported! cbSize=%lld\n", cbSize));
366 return VERR_NOT_SUPPORTED;
367 }
368
369#if defined(_MSC_VER) || (defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006))
370 if (chsize((int)File, (off_t)cbSize) == 0)
371#else
372 /* This relies on a non-standard feature of FreeBSD, Linux, and OS/2
373 * LIBC v0.6 and higher. (SuS doesn't define ftruncate() and size bigger
374 * than the file.)
375 */
376 if (ftruncate((int)File, (off_t)cbSize) == 0)
377#endif
378 return VINF_SUCCESS;
379 return RTErrConvertFromErrno(errno);
380}
381
382
383RTR3DECL(int) RTFileGetSize(RTFILE File, uint64_t *pcbSize)
384{
385 struct stat st;
386 if (!fstat((int)File, &st))
387 {
388 *pcbSize = st.st_size;
389 return VINF_SUCCESS;
390 }
391 return RTErrConvertFromErrno(errno);
392}
393
394
395RTR3DECL(bool) RTFileIsValid(RTFILE File)
396{
397 if (File != NIL_RTFILE)
398 {
399 int fFlags = fcntl(File, F_GETFD);
400 if (fFlags >= 0)
401 return true;
402 }
403 return false;
404}
405
406
407RTR3DECL(int) RTFileFlush(RTFILE File)
408{
409 if (fsync((int)File))
410 return RTErrConvertFromErrno(errno);
411 return VINF_SUCCESS;
412}
413
414
415RTR3DECL(int) RTFileIoCtl(RTFILE File, int iRequest, void *pvData, unsigned cbData, int *piRet)
416{
417 int rc = ioctl((int)File, iRequest, pvData);
418 if (piRet)
419 *piRet = rc;
420 return rc >= 0 ? VINF_SUCCESS : RTErrConvertFromErrno(errno);
421}
422
423
424RTR3DECL(int) RTFileQueryInfo(RTFILE File, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
425{
426 /*
427 * Validate input.
428 */
429 if (File == NIL_RTFILE)
430 {
431 AssertMsgFailed(("Invalid File=%RTfile\n", File));
432 return VERR_INVALID_PARAMETER;
433 }
434 if (!pObjInfo)
435 {
436 AssertMsgFailed(("Invalid pObjInfo=%p\n", pObjInfo));
437 return VERR_INVALID_PARAMETER;
438 }
439 if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
440 || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
441 {
442 AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
443 return VERR_INVALID_PARAMETER;
444 }
445
446 /*
447 * Query file info.
448 */
449 struct stat Stat;
450 if (fstat((int)File, &Stat))
451 {
452 int rc = RTErrConvertFromErrno(errno);
453 Log(("RTFileQueryInfo(%RTfile,,%d): returns %Rrc\n", File, enmAdditionalAttribs, rc));
454 return rc;
455 }
456
457 /*
458 * Setup the returned data.
459 */
460 rtFsConvertStatToObjInfo(pObjInfo, &Stat, NULL, 0);
461
462 /*
463 * Requested attributes (we cannot provide anything actually).
464 */
465 switch (enmAdditionalAttribs)
466 {
467 case RTFSOBJATTRADD_EASIZE:
468 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
469 pObjInfo->Attr.u.EASize.cb = 0;
470 break;
471
472 case RTFSOBJATTRADD_NOTHING:
473 case RTFSOBJATTRADD_UNIX:
474 /* done */
475 break;
476
477 default:
478 AssertMsgFailed(("Impossible!\n"));
479 return VERR_INTERNAL_ERROR;
480 }
481
482 LogFlow(("RTFileQueryInfo(%RTfile,,%d): returns VINF_SUCCESS\n", File, enmAdditionalAttribs));
483 return VINF_SUCCESS;
484}
485
486
487RTR3DECL(int) RTFileSetTimes(RTFILE File, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
488 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
489{
490 /*
491 * We can only set AccessTime and ModificationTime, so if neither
492 * are specified we can return immediately.
493 */
494 if (!pAccessTime && !pModificationTime)
495 return VINF_SUCCESS;
496
497 /*
498 * Convert the input to timeval, getting the missing one if necessary,
499 * and call the API which does the change.
500 */
501 struct timeval aTimevals[2];
502 if (pAccessTime && pModificationTime)
503 {
504 RTTimeSpecGetTimeval(pAccessTime, &aTimevals[0]);
505 RTTimeSpecGetTimeval(pModificationTime, &aTimevals[1]);
506 }
507 else
508 {
509 RTFSOBJINFO ObjInfo;
510 int rc = RTFileQueryInfo(File, &ObjInfo, RTFSOBJATTRADD_UNIX);
511 if (RT_FAILURE(rc))
512 return rc;
513 RTTimeSpecGetTimeval(pAccessTime ? pAccessTime : &ObjInfo.AccessTime, &aTimevals[0]);
514 RTTimeSpecGetTimeval(pModificationTime ? pModificationTime : &ObjInfo.ModificationTime, &aTimevals[1]);
515 }
516
517 if (futimes((int)File, aTimevals))
518 {
519 int rc = RTErrConvertFromErrno(errno);
520 Log(("RTFileSetTimes(%RTfile,%p,%p,,): returns %Rrc\n", File, pAccessTime, pModificationTime, rc));
521 return rc;
522 }
523 return VINF_SUCCESS;
524}
525
526
527RTR3DECL(int) RTFileSetMode(RTFILE File, RTFMODE fMode)
528{
529 /*
530 * Normalize the mode and call the API.
531 */
532 fMode = rtFsModeNormalize(fMode, NULL, 0);
533 if (!rtFsModeIsValid(fMode))
534 return VERR_INVALID_PARAMETER;
535
536 if (fchmod((int)File, fMode & RTFS_UNIX_MASK))
537 {
538 int rc = RTErrConvertFromErrno(errno);
539 Log(("RTFileSetMode(%RTfile,%RTfmode): returns %Rrc\n", File, fMode));
540 return rc;
541 }
542 return VINF_SUCCESS;
543}
544
545
546RTR3DECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename)
547{
548 /*
549 * Validate input.
550 */
551 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
552 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
553 AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
554 AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
555 AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
556
557 /*
558 * Take common cause with RTPathRename.
559 */
560 int rc = rtPathPosixRename(pszSrc, pszDst, fRename, RTFS_TYPE_FILE);
561
562 LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
563 pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
564 return rc;
565}
566
567
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