1 | /* $Id: fileio-posix.cpp 34016 2010-11-12 00:20:45Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File I/O, POSIX, Part 1.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 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 |
|
---|
53 | #include <iprt/file.h>
|
---|
54 | #include <iprt/path.h>
|
---|
55 | #include <iprt/assert.h>
|
---|
56 | #include <iprt/string.h>
|
---|
57 | #include <iprt/err.h>
|
---|
58 | #include <iprt/log.h>
|
---|
59 | #include "internal/file.h"
|
---|
60 | #include "internal/fs.h"
|
---|
61 | #include "internal/path.h"
|
---|
62 |
|
---|
63 |
|
---|
64 |
|
---|
65 | /*******************************************************************************
|
---|
66 | * Defined Constants And Macros *
|
---|
67 | *******************************************************************************/
|
---|
68 | /** Default file permissions for newly created files. */
|
---|
69 | #if defined(S_IRUSR) && defined(S_IWUSR)
|
---|
70 | # define RT_FILE_PERMISSION (S_IRUSR | S_IWUSR)
|
---|
71 | #else
|
---|
72 | # define RT_FILE_PERMISSION (00600)
|
---|
73 | #endif
|
---|
74 |
|
---|
75 |
|
---|
76 | RTDECL(bool) RTFileExists(const char *pszPath)
|
---|
77 | {
|
---|
78 | bool fRc = false;
|
---|
79 | char const *pszNativePath;
|
---|
80 | int rc = rtPathToNative(&pszNativePath, pszPath, NULL);
|
---|
81 | if (RT_SUCCESS(rc))
|
---|
82 | {
|
---|
83 | struct stat s;
|
---|
84 | fRc = !stat(pszNativePath, &s)
|
---|
85 | && S_ISREG(s.st_mode);
|
---|
86 |
|
---|
87 | rtPathFreeNative(pszNativePath, pszPath);
|
---|
88 | }
|
---|
89 |
|
---|
90 | LogFlow(("RTFileExists(%p={%s}): returns %RTbool\n", pszPath, pszPath, fRc));
|
---|
91 | return fRc;
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | RTR3DECL(int) RTFileOpen(PRTFILE pFile, const char *pszFilename, uint32_t fOpen)
|
---|
96 | {
|
---|
97 | /*
|
---|
98 | * Validate input.
|
---|
99 | */
|
---|
100 | AssertPtrReturn(pFile, VERR_INVALID_POINTER);
|
---|
101 | *pFile = NIL_RTFILE;
|
---|
102 | AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Merge forced open flags and validate them.
|
---|
106 | */
|
---|
107 | int rc = rtFileRecalcAndValidateFlags(&fOpen);
|
---|
108 | if (RT_FAILURE(rc))
|
---|
109 | return rc;
|
---|
110 | #ifndef O_NONBLOCK
|
---|
111 | if (fOpen & RTFILE_O_NON_BLOCK)
|
---|
112 | {
|
---|
113 | AssertMsgFailed(("Invalid parameters! fOpen=%#x\n", fOpen));
|
---|
114 | return VERR_INVALID_PARAMETER;
|
---|
115 | }
|
---|
116 | #endif
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Calculate open mode flags.
|
---|
120 | */
|
---|
121 | int fOpenMode = 0;
|
---|
122 | #ifdef O_BINARY
|
---|
123 | fOpenMode |= O_BINARY; /* (pc) */
|
---|
124 | #endif
|
---|
125 | #ifdef O_LARGEFILE
|
---|
126 | fOpenMode |= O_LARGEFILE; /* (linux, solaris) */
|
---|
127 | #endif
|
---|
128 | #ifdef O_NOINHERIT
|
---|
129 | if (!(fOpen & RTFILE_O_INHERIT))
|
---|
130 | fOpenMode |= O_NOINHERIT;
|
---|
131 | #endif
|
---|
132 | #ifdef O_CLOEXEC
|
---|
133 | static int s_fHave_O_CLOEXEC = 0; /* {-1,0,1}; since Linux 2.6.23 */
|
---|
134 | if (!(fOpen & RTFILE_O_INHERIT) && s_fHave_O_CLOEXEC >= 0)
|
---|
135 | fOpenMode |= O_CLOEXEC;
|
---|
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 | #if defined(O_DIRECT) && defined(RT_OS_LINUX)
|
---|
146 | /* O_DIRECT is mandatory to get async I/O working on Linux. */
|
---|
147 | if (fOpen & RTFILE_O_ASYNC_IO)
|
---|
148 | fOpenMode |= O_DIRECT;
|
---|
149 | #endif
|
---|
150 | #if defined(O_DIRECT) && (defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD))
|
---|
151 | /* Disable the kernel cache. */
|
---|
152 | if (fOpen & RTFILE_O_NO_CACHE)
|
---|
153 | fOpenMode |= O_DIRECT;
|
---|
154 | #endif
|
---|
155 |
|
---|
156 | /* create/truncate file */
|
---|
157 | switch (fOpen & RTFILE_O_ACTION_MASK)
|
---|
158 | {
|
---|
159 | case RTFILE_O_OPEN: break;
|
---|
160 | case RTFILE_O_OPEN_CREATE: fOpenMode |= O_CREAT; break;
|
---|
161 | case RTFILE_O_CREATE: fOpenMode |= O_CREAT | O_EXCL; break;
|
---|
162 | case RTFILE_O_CREATE_REPLACE: fOpenMode |= O_CREAT | O_TRUNC; break; /** @todo replacing needs fixing, this is *not* a 1:1 mapping! */
|
---|
163 | }
|
---|
164 | if (fOpen & RTFILE_O_TRUNCATE)
|
---|
165 | fOpenMode |= O_TRUNC;
|
---|
166 |
|
---|
167 | switch (fOpen & RTFILE_O_ACCESS_MASK)
|
---|
168 | {
|
---|
169 | case RTFILE_O_READ:
|
---|
170 | fOpenMode |= O_RDONLY; /* RTFILE_O_APPEND is ignored. */
|
---|
171 | break;
|
---|
172 | case RTFILE_O_WRITE:
|
---|
173 | fOpenMode |= fOpen & RTFILE_O_APPEND ? O_APPEND | O_WRONLY : O_WRONLY;
|
---|
174 | break;
|
---|
175 | case RTFILE_O_READWRITE:
|
---|
176 | fOpenMode |= fOpen & RTFILE_O_APPEND ? O_APPEND | O_RDWR : O_RDWR;
|
---|
177 | break;
|
---|
178 | default:
|
---|
179 | AssertMsgFailed(("RTFileOpen received an invalid RW value, fOpen=%#x\n", fOpen));
|
---|
180 | return VERR_INVALID_PARAMETER;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /* File mode. */
|
---|
184 | int fMode = (fOpen & RTFILE_O_CREATE_MODE_MASK)
|
---|
185 | ? (fOpen & RTFILE_O_CREATE_MODE_MASK) >> RTFILE_O_CREATE_MODE_SHIFT
|
---|
186 | : RT_FILE_PERMISSION;
|
---|
187 |
|
---|
188 | /** @todo sharing! */
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * Open/create the file.
|
---|
192 | */
|
---|
193 | char const *pszNativeFilename;
|
---|
194 | rc = rtPathToNative(&pszNativeFilename, pszFilename, NULL);
|
---|
195 | if (RT_FAILURE(rc))
|
---|
196 | return (rc);
|
---|
197 |
|
---|
198 | int fh = open(pszNativeFilename, fOpenMode, fMode);
|
---|
199 | int iErr = errno;
|
---|
200 |
|
---|
201 | #ifdef O_CLOEXEC
|
---|
202 | if ( (fOpenMode & O_CLOEXEC)
|
---|
203 | && s_fHave_O_CLOEXEC == 0)
|
---|
204 | {
|
---|
205 | if (fh < 0 && iErr == EINVAL)
|
---|
206 | {
|
---|
207 | s_fHave_O_CLOEXEC = -1;
|
---|
208 | fh = open(pszNativeFilename, fOpenMode, fMode);
|
---|
209 | iErr = errno;
|
---|
210 | }
|
---|
211 | else if (fh >= 0)
|
---|
212 | s_fHave_O_CLOEXEC = fcntl(fh, F_GETFD, 0) > 0 ? 1 : -1;
|
---|
213 | }
|
---|
214 | #endif
|
---|
215 |
|
---|
216 | rtPathFreeNative(pszNativeFilename, pszFilename);
|
---|
217 | if (fh >= 0)
|
---|
218 | {
|
---|
219 | iErr = 0;
|
---|
220 |
|
---|
221 | /*
|
---|
222 | * Mark the file handle close on exec, unless inherit is specified.
|
---|
223 | */
|
---|
224 | if ( !(fOpen & RTFILE_O_INHERIT)
|
---|
225 | #ifdef O_NOINHERIT
|
---|
226 | && !(fOpenMode & O_NOINHERIT) /* Take care since it might be a zero value dummy. */
|
---|
227 | #endif
|
---|
228 | #ifdef O_CLOEXEC
|
---|
229 | && s_fHave_O_CLOEXEC <= 0
|
---|
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 | #if defined(DEBUG_bird) && !defined(RT_OS_SOLARIS)
|
---|
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 |
|
---|
337 | RTR3DECL(int) RTFileOpenBitBucket(PRTFILE phFile, uint32_t fAccess)
|
---|
338 | {
|
---|
339 | AssertReturn( fAccess == RTFILE_O_READ
|
---|
340 | || fAccess == RTFILE_O_WRITE
|
---|
341 | || fAccess == RTFILE_O_READWRITE,
|
---|
342 | VERR_INVALID_PARAMETER);
|
---|
343 | return RTFileOpen(phFile, "/dev/null", fAccess | RTFILE_O_DENY_NONE | RTFILE_O_OPEN);
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 | RTR3DECL(int) RTFileClose(RTFILE File)
|
---|
348 | {
|
---|
349 | if (close((int)File) == 0)
|
---|
350 | return VINF_SUCCESS;
|
---|
351 | return RTErrConvertFromErrno(errno);
|
---|
352 | }
|
---|
353 |
|
---|
354 |
|
---|
355 | RTR3DECL(int) RTFileFromNative(PRTFILE pFile, RTHCINTPTR uNative)
|
---|
356 | {
|
---|
357 | if ( uNative < 0
|
---|
358 | || (RTFILE)uNative != (RTUINTPTR)uNative)
|
---|
359 | {
|
---|
360 | AssertMsgFailed(("%p\n", uNative));
|
---|
361 | *pFile = NIL_RTFILE;
|
---|
362 | return VERR_INVALID_HANDLE;
|
---|
363 | }
|
---|
364 | *pFile = (RTFILE)uNative;
|
---|
365 | return VINF_SUCCESS;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | RTR3DECL(RTHCINTPTR) RTFileToNative(RTFILE File)
|
---|
370 | {
|
---|
371 | AssertReturn(File != NIL_RTFILE, -1);
|
---|
372 | return (RTHCINTPTR)File;
|
---|
373 | }
|
---|
374 |
|
---|
375 |
|
---|
376 | RTR3DECL(int) RTFileDelete(const char *pszFilename)
|
---|
377 | {
|
---|
378 | char const *pszNativeFilename;
|
---|
379 | int rc = rtPathToNative(&pszNativeFilename, pszFilename, NULL);
|
---|
380 | if (RT_SUCCESS(rc))
|
---|
381 | {
|
---|
382 | if (unlink(pszNativeFilename) != 0)
|
---|
383 | rc = RTErrConvertFromErrno(errno);
|
---|
384 | rtPathFreeNative(pszNativeFilename, pszFilename);
|
---|
385 | }
|
---|
386 | return rc;
|
---|
387 | }
|
---|
388 |
|
---|
389 |
|
---|
390 | RTR3DECL(int) RTFileSeek(RTFILE File, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
|
---|
391 | {
|
---|
392 | static const unsigned aSeekRecode[] =
|
---|
393 | {
|
---|
394 | SEEK_SET,
|
---|
395 | SEEK_CUR,
|
---|
396 | SEEK_END,
|
---|
397 | };
|
---|
398 |
|
---|
399 | /*
|
---|
400 | * Validate input.
|
---|
401 | */
|
---|
402 | if (uMethod > RTFILE_SEEK_END)
|
---|
403 | {
|
---|
404 | AssertMsgFailed(("Invalid uMethod=%d\n", uMethod));
|
---|
405 | return VERR_INVALID_PARAMETER;
|
---|
406 | }
|
---|
407 |
|
---|
408 | /* check that within off_t range. */
|
---|
409 | if ( sizeof(off_t) < sizeof(offSeek)
|
---|
410 | && ( (offSeek > 0 && (unsigned)(offSeek >> 32) != 0)
|
---|
411 | || (offSeek < 0 && (unsigned)(-offSeek >> 32) != 0)))
|
---|
412 | {
|
---|
413 | AssertMsgFailed(("64-bit search not supported\n"));
|
---|
414 | return VERR_NOT_SUPPORTED;
|
---|
415 | }
|
---|
416 |
|
---|
417 | off_t offCurrent = lseek((int)File, (off_t)offSeek, aSeekRecode[uMethod]);
|
---|
418 | if (offCurrent != ~0)
|
---|
419 | {
|
---|
420 | if (poffActual)
|
---|
421 | *poffActual = (uint64_t)offCurrent;
|
---|
422 | return VINF_SUCCESS;
|
---|
423 | }
|
---|
424 | return RTErrConvertFromErrno(errno);
|
---|
425 | }
|
---|
426 |
|
---|
427 |
|
---|
428 | RTR3DECL(int) RTFileRead(RTFILE File, void *pvBuf, size_t cbToRead, size_t *pcbRead)
|
---|
429 | {
|
---|
430 | if (cbToRead <= 0)
|
---|
431 | return VINF_SUCCESS;
|
---|
432 |
|
---|
433 | /*
|
---|
434 | * Attempt read.
|
---|
435 | */
|
---|
436 | ssize_t cbRead = read((int)File, pvBuf, cbToRead);
|
---|
437 | if (cbRead >= 0)
|
---|
438 | {
|
---|
439 | if (pcbRead)
|
---|
440 | /* caller can handle partial read. */
|
---|
441 | *pcbRead = cbRead;
|
---|
442 | else
|
---|
443 | {
|
---|
444 | /* Caller expects all to be read. */
|
---|
445 | while ((ssize_t)cbToRead > cbRead)
|
---|
446 | {
|
---|
447 | ssize_t cbReadPart = read((int)File, (char*)pvBuf + cbRead, cbToRead - cbRead);
|
---|
448 | if (cbReadPart <= 0)
|
---|
449 | {
|
---|
450 | if (cbReadPart == 0)
|
---|
451 | return VERR_EOF;
|
---|
452 | return RTErrConvertFromErrno(errno);
|
---|
453 | }
|
---|
454 | cbRead += cbReadPart;
|
---|
455 | }
|
---|
456 | }
|
---|
457 | return VINF_SUCCESS;
|
---|
458 | }
|
---|
459 |
|
---|
460 | return RTErrConvertFromErrno(errno);
|
---|
461 | }
|
---|
462 |
|
---|
463 |
|
---|
464 | RTR3DECL(int) RTFileWrite(RTFILE File, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
|
---|
465 | {
|
---|
466 | if (cbToWrite <= 0)
|
---|
467 | return VINF_SUCCESS;
|
---|
468 |
|
---|
469 | /*
|
---|
470 | * Attempt write.
|
---|
471 | */
|
---|
472 | ssize_t cbWritten = write((int)File, pvBuf, cbToWrite);
|
---|
473 | if (cbWritten >= 0)
|
---|
474 | {
|
---|
475 | if (pcbWritten)
|
---|
476 | /* caller can handle partial write. */
|
---|
477 | *pcbWritten = cbWritten;
|
---|
478 | else
|
---|
479 | {
|
---|
480 | /* Caller expects all to be write. */
|
---|
481 | while ((ssize_t)cbToWrite > cbWritten)
|
---|
482 | {
|
---|
483 | ssize_t cbWrittenPart = write((int)File, (const char *)pvBuf + cbWritten, cbToWrite - cbWritten);
|
---|
484 | if (cbWrittenPart <= 0)
|
---|
485 | return RTErrConvertFromErrno(errno);
|
---|
486 | cbWritten += cbWrittenPart;
|
---|
487 | }
|
---|
488 | }
|
---|
489 | return VINF_SUCCESS;
|
---|
490 | }
|
---|
491 | return RTErrConvertFromErrno(errno);
|
---|
492 | }
|
---|
493 |
|
---|
494 |
|
---|
495 | RTR3DECL(int) RTFileSetSize(RTFILE File, uint64_t cbSize)
|
---|
496 | {
|
---|
497 | /*
|
---|
498 | * Validate offset.
|
---|
499 | */
|
---|
500 | if ( sizeof(off_t) < sizeof(cbSize)
|
---|
501 | && (cbSize >> 32) != 0)
|
---|
502 | {
|
---|
503 | AssertMsgFailed(("64-bit filesize not supported! cbSize=%lld\n", cbSize));
|
---|
504 | return VERR_NOT_SUPPORTED;
|
---|
505 | }
|
---|
506 |
|
---|
507 | #if defined(_MSC_VER) || (defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006))
|
---|
508 | if (chsize((int)File, (off_t)cbSize) == 0)
|
---|
509 | #else
|
---|
510 | /* This relies on a non-standard feature of FreeBSD, Linux, and OS/2
|
---|
511 | * LIBC v0.6 and higher. (SuS doesn't define ftruncate() and size bigger
|
---|
512 | * than the file.)
|
---|
513 | */
|
---|
514 | if (ftruncate((int)File, (off_t)cbSize) == 0)
|
---|
515 | #endif
|
---|
516 | return VINF_SUCCESS;
|
---|
517 | return RTErrConvertFromErrno(errno);
|
---|
518 | }
|
---|
519 |
|
---|
520 |
|
---|
521 | RTR3DECL(int) RTFileGetSize(RTFILE File, uint64_t *pcbSize)
|
---|
522 | {
|
---|
523 | struct stat st;
|
---|
524 | if (!fstat((int)File, &st))
|
---|
525 | {
|
---|
526 | *pcbSize = st.st_size;
|
---|
527 | return VINF_SUCCESS;
|
---|
528 | }
|
---|
529 | return RTErrConvertFromErrno(errno);
|
---|
530 | }
|
---|
531 |
|
---|
532 |
|
---|
533 | /**
|
---|
534 | * Determine the maximum file size.
|
---|
535 | *
|
---|
536 | * @returns IPRT status code.
|
---|
537 | * @param File Handle to the file.
|
---|
538 | * @param pcbMax Where to store the max file size.
|
---|
539 | * @see RTFileGetMaxSize.
|
---|
540 | */
|
---|
541 | RTR3DECL(int) RTFileGetMaxSizeEx(RTFILE File, PRTFOFF pcbMax)
|
---|
542 | {
|
---|
543 | /*
|
---|
544 | * Save the current location
|
---|
545 | */
|
---|
546 | uint64_t offOld;
|
---|
547 | int rc = RTFileSeek(File, 0, RTFILE_SEEK_CURRENT, &offOld);
|
---|
548 | if (RT_FAILURE(rc))
|
---|
549 | return rc;
|
---|
550 |
|
---|
551 | /*
|
---|
552 | * Perform a binary search for the max file size.
|
---|
553 | */
|
---|
554 | uint64_t offLow = 0;
|
---|
555 | uint64_t offHigh = 8 * _1T; /* we don't need bigger files */
|
---|
556 | /** @todo Unfortunately this does not work for certain file system types,
|
---|
557 | * for instance cifs mounts. Even worse, statvfs.f_fsid returns 0 for such
|
---|
558 | * file systems. */
|
---|
559 | //uint64_t offHigh = INT64_MAX;
|
---|
560 | for (;;)
|
---|
561 | {
|
---|
562 | uint64_t cbInterval = (offHigh - offLow) >> 1;
|
---|
563 | if (cbInterval == 0)
|
---|
564 | {
|
---|
565 | if (pcbMax)
|
---|
566 | *pcbMax = offLow;
|
---|
567 | return RTFileSeek(File, offOld, RTFILE_SEEK_BEGIN, NULL);
|
---|
568 | }
|
---|
569 |
|
---|
570 | rc = RTFileSeek(File, offLow + cbInterval, RTFILE_SEEK_BEGIN, NULL);
|
---|
571 | if (RT_FAILURE(rc))
|
---|
572 | offHigh = offLow + cbInterval;
|
---|
573 | else
|
---|
574 | offLow = offLow + cbInterval;
|
---|
575 | }
|
---|
576 | }
|
---|
577 |
|
---|
578 |
|
---|
579 | RTR3DECL(bool) RTFileIsValid(RTFILE File)
|
---|
580 | {
|
---|
581 | if (File != NIL_RTFILE)
|
---|
582 | {
|
---|
583 | int fFlags = fcntl(File, F_GETFD);
|
---|
584 | if (fFlags >= 0)
|
---|
585 | return true;
|
---|
586 | }
|
---|
587 | return false;
|
---|
588 | }
|
---|
589 |
|
---|
590 |
|
---|
591 | RTR3DECL(int) RTFileFlush(RTFILE File)
|
---|
592 | {
|
---|
593 | if (fsync((int)File))
|
---|
594 | return RTErrConvertFromErrno(errno);
|
---|
595 | return VINF_SUCCESS;
|
---|
596 | }
|
---|
597 |
|
---|
598 |
|
---|
599 | RTR3DECL(int) RTFileIoCtl(RTFILE File, unsigned long ulRequest, void *pvData, unsigned cbData, int *piRet)
|
---|
600 | {
|
---|
601 | int rc = ioctl((int)File, ulRequest, pvData);
|
---|
602 | if (piRet)
|
---|
603 | *piRet = rc;
|
---|
604 | return rc >= 0 ? VINF_SUCCESS : RTErrConvertFromErrno(errno);
|
---|
605 | }
|
---|
606 |
|
---|
607 |
|
---|
608 | RTR3DECL(int) RTFileSetMode(RTFILE File, RTFMODE fMode)
|
---|
609 | {
|
---|
610 | /*
|
---|
611 | * Normalize the mode and call the API.
|
---|
612 | */
|
---|
613 | fMode = rtFsModeNormalize(fMode, NULL, 0);
|
---|
614 | if (!rtFsModeIsValid(fMode))
|
---|
615 | return VERR_INVALID_PARAMETER;
|
---|
616 |
|
---|
617 | if (fchmod((int)File, fMode & RTFS_UNIX_MASK))
|
---|
618 | {
|
---|
619 | int rc = RTErrConvertFromErrno(errno);
|
---|
620 | Log(("RTFileSetMode(%RTfile,%RTfmode): returns %Rrc\n", File, fMode, rc));
|
---|
621 | return rc;
|
---|
622 | }
|
---|
623 | return VINF_SUCCESS;
|
---|
624 | }
|
---|
625 |
|
---|
626 |
|
---|
627 | RTR3DECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename)
|
---|
628 | {
|
---|
629 | /*
|
---|
630 | * Validate input.
|
---|
631 | */
|
---|
632 | AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
|
---|
633 | AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
|
---|
634 | AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
|
---|
635 | AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
|
---|
636 | AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
|
---|
637 |
|
---|
638 | /*
|
---|
639 | * Take common cause with RTPathRename.
|
---|
640 | */
|
---|
641 | int rc = rtPathPosixRename(pszSrc, pszDst, fRename, RTFS_TYPE_FILE);
|
---|
642 |
|
---|
643 | LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
|
---|
644 | pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
|
---|
645 | return rc;
|
---|
646 | }
|
---|
647 |
|
---|