VirtualBox

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

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

rebranding: IPRT files again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.3 KB
Line 
1/* $Id: fileio-posix.cpp 8245 2008-04-21 17:24:28Z 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) RTFileDelete(const char *pszFilename)
218{
219 char *pszNativeFilename;
220 int rc = rtPathToNative(&pszNativeFilename, pszFilename);
221 if (RT_SUCCESS(rc))
222 {
223 if (unlink(pszNativeFilename) != 0)
224 rc = RTErrConvertFromErrno(errno);
225 rtPathFreeNative(pszNativeFilename);
226 }
227 return rc;
228}
229
230
231RTR3DECL(int) RTFileSeek(RTFILE File, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
232{
233 static const unsigned aSeekRecode[] =
234 {
235 SEEK_SET,
236 SEEK_CUR,
237 SEEK_END,
238 };
239
240 /*
241 * Validate input.
242 */
243 if (uMethod > RTFILE_SEEK_END)
244 {
245 AssertMsgFailed(("Invalid uMethod=%d\n", uMethod));
246 return VERR_INVALID_PARAMETER;
247 }
248
249 /* check that within off_t range. */
250 if ( sizeof(off_t) < sizeof(offSeek)
251 && ( (offSeek > 0 && (unsigned)(offSeek >> 32) != 0)
252 || (offSeek < 0 && (unsigned)(-offSeek >> 32) != 0)))
253 {
254 AssertMsgFailed(("64-bit search not supported\n"));
255 return VERR_NOT_SUPPORTED;
256 }
257
258 off_t offCurrent = lseek((int)File, (off_t)offSeek, aSeekRecode[uMethod]);
259 if (offCurrent != ~0)
260 {
261 if (poffActual)
262 *poffActual = (uint64_t)offCurrent;
263 return VINF_SUCCESS;
264 }
265 return RTErrConvertFromErrno(errno);
266}
267
268
269RTR3DECL(int) RTFileRead(RTFILE File, void *pvBuf, size_t cbToRead, size_t *pcbRead)
270{
271 if (cbToRead <= 0)
272 return VINF_SUCCESS;
273
274 /*
275 * Attempt read.
276 */
277 ssize_t cbRead = read((int)File, pvBuf, cbToRead);
278 if (cbRead >= 0)
279 {
280 if (pcbRead)
281 /* caller can handle partial read. */
282 *pcbRead = cbRead;
283 else
284 {
285 /* Caller expects all to be read. */
286 while ((ssize_t)cbToRead > cbRead)
287 {
288 ssize_t cbReadPart = read((int)File, (char*)pvBuf + cbRead, cbToRead - cbRead);
289 if (cbReadPart <= 0)
290 {
291 if (cbReadPart == 0)
292 return VERR_EOF;
293 return RTErrConvertFromErrno(errno);
294 }
295 cbRead += cbReadPart;
296 }
297 }
298 return VINF_SUCCESS;
299 }
300
301 return RTErrConvertFromErrno(errno);
302}
303
304
305RTR3DECL(int) RTFileWrite(RTFILE File, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
306{
307 if (cbToWrite <= 0)
308 return VINF_SUCCESS;
309
310 /*
311 * Attempt write.
312 */
313 ssize_t cbWritten = write((int)File, pvBuf, cbToWrite);
314 if (cbWritten >= 0)
315 {
316 if (pcbWritten)
317 /* caller can handle partial write. */
318 *pcbWritten = cbWritten;
319 else
320 {
321 /* Caller expects all to be write. */
322 while ((ssize_t)cbToWrite > cbWritten)
323 {
324 ssize_t cbWrittenPart = write((int)File, (const char *)pvBuf + cbWritten, cbToWrite - cbWritten);
325 if (cbWrittenPart <= 0)
326 return RTErrConvertFromErrno(errno);
327 cbWritten += cbWrittenPart;
328 }
329 }
330 return VINF_SUCCESS;
331 }
332 return RTErrConvertFromErrno(errno);
333}
334
335
336RTR3DECL(int) RTFileSetSize(RTFILE File, uint64_t cbSize)
337{
338 /*
339 * Validate offset.
340 */
341 if ( sizeof(off_t) < sizeof(cbSize)
342 && (cbSize >> 32) != 0)
343 {
344 AssertMsgFailed(("64-bit filesize not supported! cbSize=%lld\n", cbSize));
345 return VERR_NOT_SUPPORTED;
346 }
347
348#if defined(_MSC_VER) || (defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006))
349 if (chsize((int)File, (off_t)cbSize) == 0)
350#else
351 /* This relies on a non-standard feature of FreeBSD, Linux, and OS/2
352 * LIBC v0.6 and higher. (SuS doesn't define ftruncate() and size bigger
353 * than the file.)
354 */
355 if (ftruncate((int)File, (off_t)cbSize) == 0)
356#endif
357 return VINF_SUCCESS;
358 return RTErrConvertFromErrno(errno);
359}
360
361
362RTR3DECL(int) RTFileGetSize(RTFILE File, uint64_t *pcbSize)
363{
364 struct stat st;
365 if (!fstat((int)File, &st))
366 {
367 *pcbSize = st.st_size;
368 return VINF_SUCCESS;
369 }
370 return RTErrConvertFromErrno(errno);
371}
372
373
374RTR3DECL(bool) RTFileIsValid(RTFILE File)
375{
376 if (File != NIL_RTFILE)
377 {
378 int fFlags = fcntl(File, F_GETFD);
379 if (fFlags >= 0)
380 return true;
381 }
382 return false;
383}
384
385
386RTR3DECL(int) RTFileFlush(RTFILE File)
387{
388 if (fsync((int)File))
389 return RTErrConvertFromErrno(errno);
390 return VINF_SUCCESS;
391}
392
393
394RTR3DECL(int) RTFileIoCtl(RTFILE File, int iRequest, void *pvData, unsigned cbData, int *piRet)
395{
396 int rc = ioctl((int)File, iRequest, pvData);
397 if (piRet)
398 *piRet = rc;
399 return rc >= 0 ? VINF_SUCCESS : RTErrConvertFromErrno(errno);
400}
401
402
403RTR3DECL(int) RTFileQueryInfo(RTFILE File, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
404{
405 /*
406 * Validate input.
407 */
408 if (File == NIL_RTFILE)
409 {
410 AssertMsgFailed(("Invalid File=%RTfile\n", File));
411 return VERR_INVALID_PARAMETER;
412 }
413 if (!pObjInfo)
414 {
415 AssertMsgFailed(("Invalid pObjInfo=%p\n", pObjInfo));
416 return VERR_INVALID_PARAMETER;
417 }
418 if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
419 || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
420 {
421 AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
422 return VERR_INVALID_PARAMETER;
423 }
424
425 /*
426 * Query file info.
427 */
428 struct stat Stat;
429 if (fstat((int)File, &Stat))
430 {
431 int rc = RTErrConvertFromErrno(errno);
432 Log(("RTFileQueryInfo(%RTfile,,%d): returns %Rrc\n", File, enmAdditionalAttribs, rc));
433 return rc;
434 }
435
436 /*
437 * Setup the returned data.
438 */
439 rtFsConvertStatToObjInfo(pObjInfo, &Stat, NULL, 0);
440
441 /*
442 * Requested attributes (we cannot provide anything actually).
443 */
444 switch (enmAdditionalAttribs)
445 {
446 case RTFSOBJATTRADD_EASIZE:
447 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
448 pObjInfo->Attr.u.EASize.cb = 0;
449 break;
450
451 case RTFSOBJATTRADD_NOTHING:
452 case RTFSOBJATTRADD_UNIX:
453 /* done */
454 break;
455
456 default:
457 AssertMsgFailed(("Impossible!\n"));
458 return VERR_INTERNAL_ERROR;
459 }
460
461 LogFlow(("RTFileQueryInfo(%RTfile,,%d): returns VINF_SUCCESS\n", File, enmAdditionalAttribs));
462 return VINF_SUCCESS;
463}
464
465
466RTR3DECL(int) RTFileSetTimes(RTFILE File, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
467 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
468{
469 /*
470 * We can only set AccessTime and ModificationTime, so if neither
471 * are specified we can return immediately.
472 */
473 if (!pAccessTime && !pModificationTime)
474 return VINF_SUCCESS;
475
476 /*
477 * Convert the input to timeval, getting the missing one if necessary,
478 * and call the API which does the change.
479 */
480 struct timeval aTimevals[2];
481 if (pAccessTime && pModificationTime)
482 {
483 RTTimeSpecGetTimeval(pAccessTime, &aTimevals[0]);
484 RTTimeSpecGetTimeval(pModificationTime, &aTimevals[1]);
485 }
486 else
487 {
488 RTFSOBJINFO ObjInfo;
489 int rc = RTFileQueryInfo(File, &ObjInfo, RTFSOBJATTRADD_UNIX);
490 if (RT_FAILURE(rc))
491 return rc;
492 RTTimeSpecGetTimeval(pAccessTime ? pAccessTime : &ObjInfo.AccessTime, &aTimevals[0]);
493 RTTimeSpecGetTimeval(pModificationTime ? pModificationTime : &ObjInfo.ModificationTime, &aTimevals[1]);
494 }
495
496 if (futimes((int)File, aTimevals))
497 {
498 int rc = RTErrConvertFromErrno(errno);
499 Log(("RTFileSetTimes(%RTfile,%p,%p,,): returns %Rrc\n", File, pAccessTime, pModificationTime, rc));
500 return rc;
501 }
502 return VINF_SUCCESS;
503}
504
505
506RTR3DECL(int) RTFileSetMode(RTFILE File, RTFMODE fMode)
507{
508 /*
509 * Normalize the mode and call the API.
510 */
511 fMode = rtFsModeNormalize(fMode, NULL, 0);
512 if (!rtFsModeIsValid(fMode))
513 return VERR_INVALID_PARAMETER;
514
515 if (fchmod((int)File, fMode & RTFS_UNIX_MASK))
516 {
517 int rc = RTErrConvertFromErrno(errno);
518 Log(("RTFileSetMode(%RTfile,%RTfmode): returns %Rrc\n", File, fMode));
519 return rc;
520 }
521 return VINF_SUCCESS;
522}
523
524
525RTR3DECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename)
526{
527 /*
528 * Validate input.
529 */
530 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
531 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
532 AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
533 AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
534 AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
535
536 /*
537 * Take common cause with RTPathRename.
538 */
539 int rc = rtPathPosixRename(pszSrc, pszDst, fRename, RTFS_TYPE_FILE);
540
541 LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
542 pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
543 return rc;
544}
545
546
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