VirtualBox

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

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

The Giant CDDL Dual-License Header Change.

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