VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/vfs/vfsstdfile.cpp@ 106579

Last change on this file since 106579 was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.2 KB
Line 
1/* $Id: vfsstdfile.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - Virtual File System, Standard File Implementation.
4 */
5
6/*
7 * Copyright (C) 2010-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/vfs.h>
42#include <iprt/vfslowlevel.h>
43
44#include <iprt/assert.h>
45#include <iprt/err.h>
46#include <iprt/file.h>
47#include <iprt/poll.h>
48#include <iprt/string.h>
49#include <iprt/thread.h>
50
51
52/*********************************************************************************************************************************
53* Structures and Typedefs *
54*********************************************************************************************************************************/
55/**
56 * Private data of a standard file.
57 */
58typedef struct RTVFSSTDFILE
59{
60 /** The file handle. */
61 RTFILE hFile;
62 /** Whether to leave the handle open when the VFS handle is closed. */
63 bool fLeaveOpen;
64} RTVFSSTDFILE;
65/** Pointer to the private data of a standard file. */
66typedef RTVFSSTDFILE *PRTVFSSTDFILE;
67
68
69/**
70 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
71 */
72static DECLCALLBACK(int) rtVfsStdFile_Close(void *pvThis)
73{
74 PRTVFSSTDFILE pThis = (PRTVFSSTDFILE)pvThis;
75
76 int rc;
77 if (!pThis->fLeaveOpen)
78 rc = RTFileClose(pThis->hFile);
79 else
80 rc = VINF_SUCCESS;
81 pThis->hFile = NIL_RTFILE;
82
83 return rc;
84}
85
86
87/**
88 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
89 */
90static DECLCALLBACK(int) rtVfsStdFile_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
91{
92 PRTVFSSTDFILE pThis = (PRTVFSSTDFILE)pvThis;
93 return RTFileQueryInfo(pThis->hFile, pObjInfo, enmAddAttr);
94}
95
96
97/**
98 * RTFileRead and RTFileReadAt does not return VINF_EOF or VINF_TRY_AGAIN, this
99 * function tries to fix this as best as it can.
100 *
101 * This fixing can be subject to races if some other thread or process is
102 * modifying the file size between the read and our size query here.
103 *
104 * @returns VINF_SUCCESS, VINF_EOF or VINF_TRY_AGAIN.
105 * @param pThis The instance data.
106 * @param off The offset parameter.
107 * @param cbToRead The number of bytes attempted read .
108 * @param cbActuallyRead The number of bytes actually read.
109 */
110DECLINLINE(int) rtVfsStdFile_ReadFixRC(PRTVFSSTDFILE pThis, RTFOFF off, size_t cbToRead, size_t cbActuallyRead)
111{
112 /* If the read returned less bytes than requested, it means the end of the
113 file has been reached. */
114 if (cbToRead > cbActuallyRead)
115 return VINF_EOF;
116
117 /* The other case here is the very special zero byte read at the end of the
118 file, where we're supposed to indicate EOF. */
119 if (cbToRead > 0)
120 return VINF_SUCCESS;
121
122 uint64_t cbFile;
123 int rc = RTFileQuerySize(pThis->hFile, &cbFile);
124 if (RT_FAILURE(rc))
125 return rc;
126
127 uint64_t off2;
128 if (off >= 0)
129 off2 = off;
130 else
131 {
132 rc = RTFileSeek(pThis->hFile, 0, RTFILE_SEEK_CURRENT, &off2);
133 if (RT_FAILURE(rc))
134 return rc;
135 }
136
137 return off2 >= cbFile ? VINF_EOF : VINF_SUCCESS;
138}
139
140
141/**
142 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
143 */
144static DECLCALLBACK(int) rtVfsStdFile_Read(void *pvThis, RTFOFF off, PRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
145{
146 PRTVFSSTDFILE pThis = (PRTVFSSTDFILE)pvThis;
147 int rc;
148
149 NOREF(fBlocking);
150 if (pSgBuf->cSegs == 1)
151 {
152 size_t cbToRead = 0;
153 void * const pvDst = RTSgBufGetCurrentSegment(pSgBuf, ~(size_t)0, &cbToRead);
154
155 if (off < 0)
156 rc = RTFileRead(pThis->hFile, pvDst, cbToRead, pcbRead);
157 else
158 {
159 rc = RTFileReadAt(pThis->hFile, off, pvDst, cbToRead, pcbRead);
160 if (RT_SUCCESS(rc)) /* RTFileReadAt() doesn't increment the file-position indicator on some platforms */
161 rc = RTFileSeek(pThis->hFile, off + (pcbRead ? *pcbRead : cbToRead), RTFILE_SEEK_BEGIN, NULL);
162 }
163 if (rc == VINF_SUCCESS && pcbRead)
164 rc = rtVfsStdFile_ReadFixRC(pThis, off, cbToRead, *pcbRead);
165 if (RT_SUCCESS(rc))
166 RTSgBufAdvance(pSgBuf, pcbRead ? *pcbRead : cbToRead);
167 }
168 else
169 {
170 size_t cbSeg = 0;
171 size_t cbRead = 0;
172 rc = VINF_SUCCESS;
173
174 while (!RTSgBufIsAtEnd(pSgBuf))
175 {
176 void * const pvSeg = RTSgBufGetCurrentSegment(pSgBuf, ~(size_t)0, &cbSeg);
177
178 size_t cbReadSeg = cbSeg;
179 if (off < 0)
180 rc = RTFileRead( pThis->hFile, pvSeg, cbSeg, pcbRead ? &cbReadSeg : NULL);
181 else
182 {
183 rc = RTFileReadAt(pThis->hFile, off, pvSeg, cbSeg, pcbRead ? &cbReadSeg : NULL);
184 if (RT_SUCCESS(rc)) /* RTFileReadAt() doesn't increment the file-position indicator on some platforms */
185 rc = RTFileSeek(pThis->hFile, off + cbReadSeg, RTFILE_SEEK_BEGIN, NULL);
186 }
187 if (RT_FAILURE(rc))
188 break;
189
190 if (off >= 0)
191 off += cbReadSeg;
192 cbRead += cbReadSeg;
193 RTSgBufAdvance(pSgBuf, cbReadSeg);
194
195 if ((pcbRead && cbReadSeg != cbSeg) || rc != VINF_SUCCESS)
196 break;
197 }
198
199 if (pcbRead)
200 {
201 *pcbRead = cbRead;
202 if (rc == VINF_SUCCESS)
203 rc = rtVfsStdFile_ReadFixRC(pThis, off, cbSeg, cbRead);
204 }
205 }
206
207 return rc;
208}
209
210
211/**
212 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
213 */
214static DECLCALLBACK(int) rtVfsStdFile_Write(void *pvThis, RTFOFF off, PRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
215{
216 PRTVFSSTDFILE pThis = (PRTVFSSTDFILE)pvThis;
217 int rc;
218
219 NOREF(fBlocking);
220 if (pSgBuf->cSegs == 1)
221 {
222 size_t cbToWrite = 0;
223 void const * const pvSrc = RTSgBufGetCurrentSegment(pSgBuf, ~(size_t)0, &cbToWrite);
224
225 if (off < 0)
226 rc = RTFileWrite(pThis->hFile, pvSrc, cbToWrite, pcbWritten);
227 else
228 {
229 rc = RTFileWriteAt(pThis->hFile, off, pvSrc, cbToWrite, pcbWritten);
230 if (RT_SUCCESS(rc)) /* RTFileWriteAt() doesn't increment the file-position indicator on some platforms */
231 rc = RTFileSeek(pThis->hFile, off + (pcbWritten ? *pcbWritten : cbToWrite), RTFILE_SEEK_BEGIN, NULL);
232 }
233 if (RT_SUCCESS(rc))
234 RTSgBufAdvance(pSgBuf, pcbWritten ? *pcbWritten : cbToWrite);
235 }
236 else
237 {
238 size_t cbWritten = 0;
239 size_t cbWrittenSeg;
240 size_t *pcbWrittenSeg = pcbWritten ? &cbWrittenSeg : NULL;
241 rc = VINF_SUCCESS;
242
243 while (!RTSgBufIsAtEnd(pSgBuf))
244 {
245 size_t cbSeg = 0;
246 void const * const pvSeg = RTSgBufGetCurrentSegment(pSgBuf, ~(size_t)0, &cbSeg);
247
248 cbWrittenSeg = 0;
249 if (off < 0)
250 rc = RTFileWrite(pThis->hFile, pvSeg, cbSeg, pcbWrittenSeg);
251 else
252 {
253 rc = RTFileWriteAt(pThis->hFile, off, pvSeg, cbSeg, pcbWrittenSeg);
254 if (RT_SUCCESS(rc))
255 {
256 off += pcbWrittenSeg ? *pcbWrittenSeg : cbSeg;
257 /* RTFileWriteAt() doesn't increment the file-position indicator on some platforms */
258 rc = RTFileSeek(pThis->hFile, off, RTFILE_SEEK_BEGIN, NULL);
259 }
260 }
261 if (RT_FAILURE(rc))
262 break;
263 if (pcbWritten)
264 {
265 RTSgBufAdvance(pSgBuf, cbWrittenSeg);
266 cbWritten += cbWrittenSeg;
267 if (cbWrittenSeg != cbSeg)
268 break;
269 }
270 else
271 RTSgBufAdvance(pSgBuf, cbSeg);
272 }
273
274 if (pcbWritten)
275 *pcbWritten = cbWritten;
276 }
277
278 return rc;
279}
280
281
282/**
283 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
284 */
285static DECLCALLBACK(int) rtVfsStdFile_Flush(void *pvThis)
286{
287 PRTVFSSTDFILE pThis = (PRTVFSSTDFILE)pvThis;
288 int rc = RTFileFlush(pThis->hFile);
289#ifdef RT_OS_WINDOWS
290 /* Workaround for console handles. */ /** @todo push this further down? */
291 if ( rc == VERR_INVALID_HANDLE
292 && RTFileIsValid(pThis->hFile))
293 rc = VINF_NOT_SUPPORTED; /* not flushable */
294#endif
295 return rc;
296}
297
298
299/**
300 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
301 */
302static DECLCALLBACK(int) rtVfsStdFile_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
303 uint32_t *pfRetEvents)
304{
305 NOREF(pvThis);
306 int rc;
307 if (fEvents != RTPOLL_EVT_ERROR)
308 {
309 *pfRetEvents = fEvents & ~RTPOLL_EVT_ERROR;
310 rc = VINF_SUCCESS;
311 }
312 else if (fIntr)
313 rc = RTThreadSleep(cMillies);
314 else
315 {
316 uint64_t uMsStart = RTTimeMilliTS();
317 do
318 rc = RTThreadSleep(cMillies);
319 while ( rc == VERR_INTERRUPTED
320 && RTTimeMilliTS() - uMsStart < cMillies);
321 if (rc == VERR_INTERRUPTED)
322 rc = VERR_TIMEOUT;
323 }
324 return rc;
325}
326
327
328/**
329 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
330 */
331static DECLCALLBACK(int) rtVfsStdFile_Tell(void *pvThis, PRTFOFF poffActual)
332{
333 PRTVFSSTDFILE pThis = (PRTVFSSTDFILE)pvThis;
334 uint64_t offActual;
335 int rc = RTFileSeek(pThis->hFile, 0, RTFILE_SEEK_CURRENT, &offActual);
336 if (RT_SUCCESS(rc))
337 *poffActual = (RTFOFF)offActual;
338 return rc;
339}
340
341
342/**
343 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnSkip}
344 */
345static DECLCALLBACK(int) rtVfsStdFile_Skip(void *pvThis, RTFOFF cb)
346{
347 PRTVFSSTDFILE pThis = (PRTVFSSTDFILE)pvThis;
348 uint64_t offIgnore;
349 return RTFileSeek(pThis->hFile, cb, RTFILE_SEEK_CURRENT, &offIgnore);
350}
351
352
353/**
354 * @interface_method_impl{RTVFSOBJSETOPS,pfnMode}
355 */
356static DECLCALLBACK(int) rtVfsStdFile_SetMode(void *pvThis, RTFMODE fMode, RTFMODE fMask)
357{
358 PRTVFSSTDFILE pThis = (PRTVFSSTDFILE)pvThis;
359 if (fMask != ~RTFS_TYPE_MASK)
360 {
361#if 0
362 RTFMODE fCurMode;
363 int rc = RTFileGetMode(pThis->hFile, &fCurMode);
364 if (RT_FAILURE(rc))
365 return rc;
366 fMode |= ~fMask & fCurMode;
367#else
368 RTFSOBJINFO ObjInfo;
369 int rc = RTFileQueryInfo(pThis->hFile, &ObjInfo, RTFSOBJATTRADD_NOTHING);
370 if (RT_FAILURE(rc))
371 return rc;
372 fMode |= ~fMask & ObjInfo.Attr.fMode;
373#endif
374 }
375 return RTFileSetMode(pThis->hFile, fMode);
376}
377
378
379/**
380 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetTimes}
381 */
382static DECLCALLBACK(int) rtVfsStdFile_SetTimes(void *pvThis, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
383 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
384{
385 PRTVFSSTDFILE pThis = (PRTVFSSTDFILE)pvThis;
386 return RTFileSetTimes(pThis->hFile, pAccessTime, pModificationTime, pChangeTime, pBirthTime);
387}
388
389
390/**
391 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetOwner}
392 */
393static DECLCALLBACK(int) rtVfsStdFile_SetOwner(void *pvThis, RTUID uid, RTGID gid)
394{
395#if 0
396 PRTVFSSTDFILE pThis = (PRTVFSSTDFILE)pvThis;
397 return RTFileSetOwner(pThis->hFile, uid, gid);
398#else
399 NOREF(pvThis); NOREF(uid); NOREF(gid);
400 return VERR_NOT_IMPLEMENTED;
401#endif
402}
403
404
405/**
406 * @interface_method_impl{RTVFSFILEOPS,pfnSeek}
407 */
408static DECLCALLBACK(int) rtVfsStdFile_Seek(void *pvThis, RTFOFF offSeek, unsigned uMethod, PRTFOFF poffActual)
409{
410 PRTVFSSTDFILE pThis = (PRTVFSSTDFILE)pvThis;
411 uint64_t offActual = 0;
412 int rc = RTFileSeek(pThis->hFile, offSeek, uMethod, &offActual);
413 if (RT_SUCCESS(rc))
414 *poffActual = offActual;
415 return rc;
416}
417
418
419/**
420 * @interface_method_impl{RTVFSFILEOPS,pfnQuerySize}
421 */
422static DECLCALLBACK(int) rtVfsStdFile_QuerySize(void *pvThis, uint64_t *pcbFile)
423{
424 PRTVFSSTDFILE pThis = (PRTVFSSTDFILE)pvThis;
425 return RTFileQuerySize(pThis->hFile, pcbFile);
426}
427
428
429/**
430 * @interface_method_impl{RTVFSFILEOPS,pfnSetSize}
431 */
432static DECLCALLBACK(int) rtVfsStdFile_SetSize(void *pvThis, uint64_t cbFile, uint32_t fFlags)
433{
434 PRTVFSSTDFILE pThis = (PRTVFSSTDFILE)pvThis;
435 switch (fFlags & RTVFSFILE_SIZE_F_ACTION_MASK)
436 {
437 case RTVFSFILE_SIZE_F_NORMAL:
438 return RTFileSetSize(pThis->hFile, cbFile);
439 case RTVFSFILE_SIZE_F_GROW:
440 return RTFileSetAllocationSize(pThis->hFile, cbFile, RTFILE_ALLOC_SIZE_F_DEFAULT);
441 case RTVFSFILE_SIZE_F_GROW_KEEP_SIZE:
442 return RTFileSetAllocationSize(pThis->hFile, cbFile, RTFILE_ALLOC_SIZE_F_KEEP_SIZE);
443 default:
444 return VERR_NOT_SUPPORTED;
445 }
446}
447
448
449/**
450 * @interface_method_impl{RTVFSFILEOPS,pfnQueryMaxSize}
451 */
452static DECLCALLBACK(int) rtVfsStdFile_QueryMaxSize(void *pvThis, uint64_t *pcbMax)
453{
454 PRTVFSSTDFILE pThis = (PRTVFSSTDFILE)pvThis;
455 RTFOFF cbMax = 0;
456 int rc = RTFileQueryMaxSizeEx(pThis->hFile, &cbMax);
457 if (RT_SUCCESS(rc))
458 *pcbMax = cbMax;
459 return rc;
460}
461
462
463/**
464 * Standard file operations.
465 */
466DECL_HIDDEN_CONST(const RTVFSFILEOPS) g_rtVfsStdFileOps =
467{
468 { /* Stream */
469 { /* Obj */
470 RTVFSOBJOPS_VERSION,
471 RTVFSOBJTYPE_FILE,
472 "StdFile",
473 rtVfsStdFile_Close,
474 rtVfsStdFile_QueryInfo,
475 NULL,
476 RTVFSOBJOPS_VERSION
477 },
478 RTVFSIOSTREAMOPS_VERSION,
479 0,
480 rtVfsStdFile_Read,
481 rtVfsStdFile_Write,
482 rtVfsStdFile_Flush,
483 rtVfsStdFile_PollOne,
484 rtVfsStdFile_Tell,
485 rtVfsStdFile_Skip,
486 NULL /*ZeroFill*/,
487 RTVFSIOSTREAMOPS_VERSION,
488 },
489 RTVFSFILEOPS_VERSION,
490 0,
491 { /* ObjSet */
492 RTVFSOBJSETOPS_VERSION,
493 RT_UOFFSETOF(RTVFSFILEOPS, ObjSet) - RT_UOFFSETOF(RTVFSFILEOPS, Stream.Obj),
494 rtVfsStdFile_SetMode,
495 rtVfsStdFile_SetTimes,
496 rtVfsStdFile_SetOwner,
497 RTVFSOBJSETOPS_VERSION
498 },
499 rtVfsStdFile_Seek,
500 rtVfsStdFile_QuerySize,
501 rtVfsStdFile_SetSize,
502 rtVfsStdFile_QueryMaxSize,
503 RTVFSFILEOPS_VERSION
504};
505
506
507/**
508 * Internal worker for RTVfsFileFromRTFile and RTVfsFileOpenNormal.
509 *
510 * @returns IRPT status code.
511 * @param hFile The IPRT file handle.
512 * @param fOpen The RTFILE_O_XXX flags.
513 * @param fLeaveOpen Whether to leave it open or close it.
514 * @param phVfsFile Where to return the handle.
515 */
516static int rtVfsFileFromRTFile(RTFILE hFile, uint64_t fOpen, bool fLeaveOpen, PRTVFSFILE phVfsFile)
517{
518 PRTVFSSTDFILE pThis;
519 RTVFSFILE hVfsFile;
520 int rc = RTVfsNewFile(&g_rtVfsStdFileOps, sizeof(RTVFSSTDFILE), fOpen, NIL_RTVFS, NIL_RTVFSLOCK,
521 &hVfsFile, (void **)&pThis);
522 if (RT_FAILURE(rc))
523 return rc;
524
525 pThis->hFile = hFile;
526 pThis->fLeaveOpen = fLeaveOpen;
527 *phVfsFile = hVfsFile;
528 return VINF_SUCCESS;
529}
530
531
532RTDECL(int) RTVfsFileFromRTFile(RTFILE hFile, uint64_t fOpen, bool fLeaveOpen, PRTVFSFILE phVfsFile)
533{
534 /*
535 * Check the handle validity.
536 */
537 RTFSOBJINFO ObjInfo;
538 int rc = RTFileQueryInfo(hFile, &ObjInfo, RTFSOBJATTRADD_NOTHING);
539 if (RT_FAILURE(rc))
540 return rc;
541
542 /*
543 * Set up some fake fOpen flags if necessary and create a VFS file handle.
544 */
545 if (!fOpen)
546 fOpen = RTFILE_O_READWRITE | RTFILE_O_DENY_NONE | RTFILE_O_OPEN_CREATE;
547
548 return rtVfsFileFromRTFile(hFile, fOpen, fLeaveOpen, phVfsFile);
549}
550
551
552RTDECL(int) RTVfsFileOpenNormal(const char *pszFilename, uint64_t fOpen, PRTVFSFILE phVfsFile)
553{
554 /*
555 * Open the file the normal way and pass it to RTVfsFileFromRTFile.
556 */
557 RTFILE hFile;
558 int rc = RTFileOpen(&hFile, pszFilename, fOpen);
559 if (RT_SUCCESS(rc))
560 {
561 /*
562 * Create a VFS file handle.
563 */
564 rc = rtVfsFileFromRTFile(hFile, fOpen, false /*fLeaveOpen*/, phVfsFile);
565 if (RT_FAILURE(rc))
566 RTFileClose(hFile);
567 }
568 return rc;
569}
570
571
572RTDECL(int) RTVfsIoStrmFromRTFile(RTFILE hFile, uint64_t fOpen, bool fLeaveOpen, PRTVFSIOSTREAM phVfsIos)
573{
574 RTVFSFILE hVfsFile;
575 int rc = RTVfsFileFromRTFile(hFile, fOpen, fLeaveOpen, &hVfsFile);
576 if (RT_SUCCESS(rc))
577 {
578 *phVfsIos = RTVfsFileToIoStream(hVfsFile);
579 RTVfsFileRelease(hVfsFile);
580 }
581 return rc;
582}
583
584
585RTDECL(int) RTVfsIoStrmOpenNormal(const char *pszFilename, uint64_t fOpen, PRTVFSIOSTREAM phVfsIos)
586{
587 RTVFSFILE hVfsFile;
588 int rc = RTVfsFileOpenNormal(pszFilename, fOpen, &hVfsFile);
589 if (RT_SUCCESS(rc))
590 {
591 *phVfsIos = RTVfsFileToIoStream(hVfsFile);
592 RTVfsFileRelease(hVfsFile);
593 }
594 return rc;
595}
596
597
598
599/**
600 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnValidate}
601 */
602static DECLCALLBACK(int) rtVfsChainStdFile_Validate(PCRTVFSCHAINELEMENTREG pProviderReg, PRTVFSCHAINSPEC pSpec,
603 PRTVFSCHAINELEMSPEC pElement, uint32_t *poffError, PRTERRINFO pErrInfo)
604{
605 RT_NOREF(pProviderReg);
606
607 /*
608 * Basic checks.
609 */
610 if (pElement->enmTypeIn != RTVFSOBJTYPE_INVALID)
611 return VERR_VFS_CHAIN_MUST_BE_FIRST_ELEMENT;
612 if ( pElement->enmType != RTVFSOBJTYPE_FILE
613 && pElement->enmType != RTVFSOBJTYPE_IO_STREAM)
614 return VERR_VFS_CHAIN_ONLY_FILE_OR_IOS;
615
616 /*
617 * Join common cause with the 'open' provider.
618 */
619 return RTVfsChainValidateOpenFileOrIoStream(pSpec, pElement, poffError, pErrInfo);
620}
621
622
623/**
624 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnInstantiate}
625 */
626static DECLCALLBACK(int) rtVfsChainStdFile_Instantiate(PCRTVFSCHAINELEMENTREG pProviderReg, PCRTVFSCHAINSPEC pSpec,
627 PCRTVFSCHAINELEMSPEC pElement, RTVFSOBJ hPrevVfsObj,
628 PRTVFSOBJ phVfsObj, uint32_t *poffError, PRTERRINFO pErrInfo)
629{
630 RT_NOREF(pProviderReg, pSpec, poffError, pErrInfo);
631 AssertReturn(hPrevVfsObj == NIL_RTVFSOBJ, VERR_VFS_CHAIN_IPE);
632
633 RTVFSFILE hVfsFile;
634 int rc = RTVfsFileOpenNormal(pElement->paArgs[0].psz, pElement->uProvider, &hVfsFile);
635 if (RT_SUCCESS(rc))
636 {
637 *phVfsObj = RTVfsObjFromFile(hVfsFile);
638 RTVfsFileRelease(hVfsFile);
639 if (*phVfsObj != NIL_RTVFSOBJ)
640 return VINF_SUCCESS;
641 rc = VERR_VFS_CHAIN_CAST_FAILED;
642 }
643 return rc;
644}
645
646
647/**
648 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnCanReuseElement}
649 */
650static DECLCALLBACK(bool) rtVfsChainStdFile_CanReuseElement(PCRTVFSCHAINELEMENTREG pProviderReg,
651 PCRTVFSCHAINSPEC pSpec, PCRTVFSCHAINELEMSPEC pElement,
652 PCRTVFSCHAINSPEC pReuseSpec, PCRTVFSCHAINELEMSPEC pReuseElement)
653{
654 RT_NOREF(pProviderReg, pSpec, pReuseSpec);
655 if (strcmp(pElement->paArgs[0].psz, pReuseElement->paArgs[0].psz) == 0)
656 if (pElement->paArgs[0].uProvider == pReuseElement->paArgs[0].uProvider)
657 return true;
658 return false;
659}
660
661
662/** VFS chain element 'file'. */
663static RTVFSCHAINELEMENTREG g_rtVfsChainStdFileReg =
664{
665 /* uVersion = */ RTVFSCHAINELEMENTREG_VERSION,
666 /* fReserved = */ 0,
667 /* pszName = */ "stdfile",
668 /* ListEntry = */ { NULL, NULL },
669 /* pszHelp = */ "Open a real file, providing either a file or an I/O stream object. Initial element.\n"
670 "First argument is the filename path.\n"
671 "Second argument is access mode, optional: r, w, rw.\n"
672 "Third argument is open disposition, optional: create, create-replace, open, open-create, open-append, open-truncate.\n"
673 "Forth argument is file sharing, optional: nr, nw, nrw, d.",
674 /* pfnValidate = */ rtVfsChainStdFile_Validate,
675 /* pfnInstantiate = */ rtVfsChainStdFile_Instantiate,
676 /* pfnCanReuseElement = */ rtVfsChainStdFile_CanReuseElement,
677 /* uEndMarker = */ RTVFSCHAINELEMENTREG_VERSION
678};
679
680RTVFSCHAIN_AUTO_REGISTER_ELEMENT_PROVIDER(&g_rtVfsChainStdFileReg, rtVfsChainStdFileReg);
681
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