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