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
|
/* $Id: vfsprogress.cpp $ */
/** @file
* IPRT - Virtual File System, progress filter for files.
*/
/*
* Copyright (C) 2010-2020 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE 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.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/vfs.h>
#include <iprt/vfslowlevel.h>
#include <iprt/assert.h>
#include <iprt/errcore.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 RTVFSPROGRESSFILE
{
/** This is negative (RT_FAILURE) if canceled. */
int rcCanceled;
/** RTVFSPROGRESS_F_XXX. */
uint32_t fFlags;
/** Progress callback. */
PFNRTPROGRESS pfnProgress;
/** User argument for the callback. */
void *pvUser;
/** The I/O stream handle. */
RTVFSIOSTREAM hVfsIos;
/** The file handle. NIL_RTFILE if a pure I/O stream. */
RTVFSFILE hVfsFile;
/** Total number of bytes expected to be read and written. */
uint64_t cbExpected;
/** The number of bytes expected to be read. */
uint64_t cbExpectedRead;
/** The number of bytes expected to be written. */
uint64_t cbExpectedWritten;
/** Number of bytes currently read. */
uint64_t cbCurrentlyRead;
/** Number of bytes currently written. */
uint64_t cbCurrentlyWritten;
/** Current precentage. */
unsigned uCurPct;
} RTVFSPROGRESSFILE;
/** Pointer to the private data of a standard file. */
typedef RTVFSPROGRESSFILE *PRTVFSPROGRESSFILE;
/**
* Update the progress and do the progress callback if necessary.
*
* @returns Callback return code.
* @param pThis The file progress instance.
*/
static int rtVfsProgressFile_UpdateProgress(PRTVFSPROGRESSFILE pThis)
{
uint64_t cbDone = RT_MIN(pThis->cbCurrentlyRead, pThis->cbExpectedRead)
+ RT_MIN(pThis->cbCurrentlyWritten, pThis->cbExpectedWritten);
unsigned uPct = cbDone * 100 / pThis->cbExpected;
if (uPct == pThis->uCurPct)
return pThis->rcCanceled;
pThis->uCurPct = uPct;
int rc = pThis->pfnProgress(uPct, pThis->pvUser);
if (!(pThis->fFlags & RTVFSPROGRESS_F_CANCELABLE))
rc = VINF_SUCCESS;
else if (RT_FAILURE(rc) && RT_SUCCESS(pThis->rcCanceled))
pThis->rcCanceled = rc;
return rc;
}
/**
* @interface_method_impl{RTVFSOBJOPS,pfnClose}
*/
static DECLCALLBACK(int) rtVfsProgressFile_Close(void *pvThis)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
if (pThis->hVfsFile != NIL_RTVFSFILE)
{
RTVfsFileRelease(pThis->hVfsFile);
pThis->hVfsFile = NIL_RTVFSFILE;
}
RTVfsIoStrmRelease(pThis->hVfsIos);
pThis->hVfsIos = NIL_RTVFSIOSTREAM;
pThis->pfnProgress = NULL;
return VINF_SUCCESS;
}
/**
* @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
*/
static DECLCALLBACK(int) rtVfsProgressFile_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
int rc = pThis->rcCanceled;
if (RT_SUCCESS(rc))
rc = RTVfsIoStrmQueryInfo(pThis->hVfsIos, pObjInfo, enmAddAttr);
return rc;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
*/
static DECLCALLBACK(int) rtVfsProgressFile_Read(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
int rc = pThis->rcCanceled;
if (RT_SUCCESS(rc))
{
/* Simplify a little there if a seeks is implied and assume the read goes well. */
if ( off >= 0
&& (pThis->fFlags & RTVFSPROGRESS_F_FORWARD_SEEK_AS_READ))
{
uint64_t offCurrent = RTVfsFileTell(pThis->hVfsFile);
if (offCurrent < (uint64_t)off)
pThis->cbCurrentlyRead += off - offCurrent;
}
/* Calc the request before calling down the stack. */
size_t cbReq = 0;
unsigned i = pSgBuf->cSegs;
while (i-- > 0)
cbReq += pSgBuf->paSegs[i].cbSeg;
/* Do the read. */
rc = RTVfsIoStrmSgRead(pThis->hVfsIos, off, pSgBuf, fBlocking, pcbRead);
if (RT_SUCCESS(rc))
{
/* Update the progress (we cannot cancel here, sorry). */
pThis->cbCurrentlyRead += pcbRead ? *pcbRead : cbReq;
rtVfsProgressFile_UpdateProgress(pThis);
}
}
return rc;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
*/
static DECLCALLBACK(int) rtVfsProgressFile_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
int rc = pThis->rcCanceled;
if (RT_SUCCESS(rc))
{
/* Simplify a little there if a seeks is implied and assume the write goes well. */
if ( off >= 0
&& (pThis->fFlags & RTVFSPROGRESS_F_FORWARD_SEEK_AS_WRITE))
{
uint64_t offCurrent = RTVfsFileTell(pThis->hVfsFile);
if (offCurrent < (uint64_t)off)
pThis->cbCurrentlyWritten += off - offCurrent;
}
/* Calc the request before calling down the stack. */
size_t cbReq = 0;
unsigned i = pSgBuf->cSegs;
while (i-- > 0)
cbReq += pSgBuf->paSegs[i].cbSeg;
/* Do the read. */
rc = RTVfsIoStrmSgWrite(pThis->hVfsIos, off, pSgBuf, fBlocking, pcbWritten);
if (RT_SUCCESS(rc))
{
/* Update the progress (we cannot cancel here, sorry). */
pThis->cbCurrentlyWritten += pcbWritten ? *pcbWritten : cbReq;
rtVfsProgressFile_UpdateProgress(pThis);
}
}
return rc;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
*/
static DECLCALLBACK(int) rtVfsProgressFile_Flush(void *pvThis)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
int rc = pThis->rcCanceled;
if (RT_SUCCESS(rc))
rc = RTVfsIoStrmFlush(pThis->hVfsIos);
return rc;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
*/
static DECLCALLBACK(int)
rtVfsProgressFile_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr, uint32_t *pfRetEvents)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
int rc = pThis->rcCanceled;
if (RT_SUCCESS(rc))
rc = RTVfsIoStrmPoll(pThis->hVfsIos, fEvents, cMillies, fIntr, pfRetEvents);
else
{
*pfRetEvents |= RTPOLL_EVT_ERROR;
rc = VINF_SUCCESS;
}
return rc;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
*/
static DECLCALLBACK(int) rtVfsProgressFile_Tell(void *pvThis, PRTFOFF poffActual)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
*poffActual = RTVfsIoStrmTell(pThis->hVfsIos);
return *poffActual >= 0 ? VINF_SUCCESS : (int)*poffActual;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnSkip}
*/
static DECLCALLBACK(int) rtVfsProgressFile_Skip(void *pvThis, RTFOFF cb)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
int rc = pThis->rcCanceled;
if (RT_SUCCESS(rc))
{
rc = RTVfsIoStrmSkip(pThis->hVfsIos, cb);
if (RT_SUCCESS(rc))
{
pThis->cbCurrentlyRead += cb;
rtVfsProgressFile_UpdateProgress(pThis);
}
}
return rc;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnZeroFill}
*/
static DECLCALLBACK(int) rtVfsProgressFile_ZeroFill(void *pvThis, RTFOFF cb)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
int rc = pThis->rcCanceled;
if (RT_SUCCESS(rc))
{
rc = RTVfsIoStrmZeroFill(pThis->hVfsIos, cb);
if (RT_SUCCESS(rc))
{
pThis->cbCurrentlyWritten += cb;
rtVfsProgressFile_UpdateProgress(pThis);
}
}
return rc;
}
/**
* I/O stream progress operations.
*/
DECL_HIDDEN_CONST(const RTVFSIOSTREAMOPS) g_rtVfsProgressIosOps =
{
{ /* Obj */
RTVFSOBJOPS_VERSION,
RTVFSOBJTYPE_IO_STREAM,
"I/O Stream Progress",
rtVfsProgressFile_Close,
rtVfsProgressFile_QueryInfo,
RTVFSOBJOPS_VERSION
},
RTVFSIOSTREAMOPS_VERSION,
0,
rtVfsProgressFile_Read,
rtVfsProgressFile_Write,
rtVfsProgressFile_Flush,
rtVfsProgressFile_PollOne,
rtVfsProgressFile_Tell,
rtVfsProgressFile_Skip,
rtVfsProgressFile_ZeroFill,
RTVFSIOSTREAMOPS_VERSION,
};
/**
* @interface_method_impl{RTVFSOBJSETOPS,pfnMode}
*/
static DECLCALLBACK(int) rtVfsProgressFile_SetMode(void *pvThis, RTFMODE fMode, RTFMODE fMask)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
//return RTVfsFileSetMode(pThis->hVfsIos, fMode, fMask); - missing
RT_NOREF(pThis, fMode, fMask);
AssertFailedReturn(VERR_NOT_IMPLEMENTED);
}
/**
* @interface_method_impl{RTVFSOBJSETOPS,pfnSetTimes}
*/
static DECLCALLBACK(int) rtVfsProgressFile_SetTimes(void *pvThis, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
//return RTVfsFileSetTimes(pThis->hVfsIos, pAccessTime, pModificationTime, pChangeTime, pBirthTime); - missing
RT_NOREF(pThis, pAccessTime, pModificationTime, pChangeTime, pBirthTime);
AssertFailedReturn(VERR_NOT_IMPLEMENTED);
}
/**
* @interface_method_impl{RTVFSOBJSETOPS,pfnSetOwner}
*/
static DECLCALLBACK(int) rtVfsProgressFile_SetOwner(void *pvThis, RTUID uid, RTGID gid)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
//return RTVfsFileSetOwern(pThis->hVfsIos, uid, gid); - missing
RT_NOREF(pThis, uid, gid);
AssertFailedReturn(VERR_NOT_IMPLEMENTED);
}
/**
* @interface_method_impl{RTVFSFILEOPS,pfnSeek}
*/
static DECLCALLBACK(int) rtVfsProgressFile_Seek(void *pvThis, RTFOFF offSeek, unsigned uMethod, PRTFOFF poffActual)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
uint64_t offPrev = UINT64_MAX;
if (pThis->fFlags & (RTVFSPROGRESS_F_FORWARD_SEEK_AS_READ | RTVFSPROGRESS_F_FORWARD_SEEK_AS_WRITE))
offPrev = RTVfsFileTell(pThis->hVfsFile);
uint64_t offActual = 0;
int rc = RTVfsFileSeek(pThis->hVfsFile, offSeek, uMethod, &offActual);
if (RT_SUCCESS(rc))
{
if (poffActual)
*poffActual = offActual;
/* Do progress updates as requested. */
if (pThis->fFlags & (RTVFSPROGRESS_F_FORWARD_SEEK_AS_READ | RTVFSPROGRESS_F_FORWARD_SEEK_AS_WRITE))
{
if (offActual > offPrev)
{
if (pThis->fFlags & RTVFSPROGRESS_F_FORWARD_SEEK_AS_READ)
pThis->cbCurrentlyRead += offActual - offPrev;
else
pThis->cbCurrentlyWritten += offActual - offPrev;
rtVfsProgressFile_UpdateProgress(pThis);
}
}
}
return rc;
}
/**
* @interface_method_impl{RTVFSFILEOPS,pfnQuerySize}
*/
static DECLCALLBACK(int) rtVfsProgressFile_QuerySize(void *pvThis, uint64_t *pcbFile)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
return RTVfsFileQuerySize(pThis->hVfsFile, pcbFile);
}
/**
* @interface_method_impl{RTVFSFILEOPS,pfnSetSize}
*/
static DECLCALLBACK(int) rtVfsProgressFile_SetSize(void *pvThis, uint64_t cbFile, uint32_t fFlags)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
return RTVfsFileSetSize(pThis->hVfsFile, cbFile, fFlags);
}
/**
* @interface_method_impl{RTVFSFILEOPS,pfnQueryMaxSize}
*/
static DECLCALLBACK(int) rtVfsProgressFile_QueryMaxSize(void *pvThis, uint64_t *pcbMax)
{
PRTVFSPROGRESSFILE pThis = (PRTVFSPROGRESSFILE)pvThis;
return RTVfsFileQueryMaxSize(pThis->hVfsFile, pcbMax);
}
/**
* File progress operations.
*/
DECL_HIDDEN_CONST(const RTVFSFILEOPS) g_rtVfsProgressFileOps =
{
{ /* Stream */
{ /* Obj */
RTVFSOBJOPS_VERSION,
RTVFSOBJTYPE_FILE,
"File Progress",
rtVfsProgressFile_Close,
rtVfsProgressFile_QueryInfo,
RTVFSOBJOPS_VERSION
},
RTVFSIOSTREAMOPS_VERSION,
0,
rtVfsProgressFile_Read,
rtVfsProgressFile_Write,
rtVfsProgressFile_Flush,
rtVfsProgressFile_PollOne,
rtVfsProgressFile_Tell,
rtVfsProgressFile_Skip,
rtVfsProgressFile_ZeroFill,
RTVFSIOSTREAMOPS_VERSION,
},
RTVFSFILEOPS_VERSION,
0,
{ /* ObjSet */
RTVFSOBJSETOPS_VERSION,
RT_UOFFSETOF(RTVFSFILEOPS, ObjSet) - RT_UOFFSETOF(RTVFSFILEOPS, Stream.Obj),
rtVfsProgressFile_SetMode,
rtVfsProgressFile_SetTimes,
rtVfsProgressFile_SetOwner,
RTVFSOBJSETOPS_VERSION
},
rtVfsProgressFile_Seek,
rtVfsProgressFile_QuerySize,
rtVfsProgressFile_SetSize,
rtVfsProgressFile_QueryMaxSize,
RTVFSFILEOPS_VERSION
};
RTDECL(int) RTVfsCreateProgressForIoStream(RTVFSIOSTREAM hVfsIos, PFNRTPROGRESS pfnProgress, void *pvUser, uint32_t fFlags,
uint64_t cbExpectedRead, uint64_t cbExpectedWritten, PRTVFSIOSTREAM phVfsIos)
{
AssertPtrReturn(pfnProgress, VERR_INVALID_POINTER);
AssertReturn(!(fFlags & ~RTVFSPROGRESS_F_VALID_MASK), VERR_INVALID_FLAGS);
AssertReturn(!(fFlags & RTVFSPROGRESS_F_FORWARD_SEEK_AS_READ) || !(fFlags & RTVFSPROGRESS_F_FORWARD_SEEK_AS_WRITE),
VERR_INVALID_FLAGS);
uint32_t cRefs = RTVfsIoStrmRetain(hVfsIos);
AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
PRTVFSPROGRESSFILE pThis;
int rc = RTVfsNewIoStream(&g_rtVfsProgressIosOps, sizeof(*pThis), RTVfsIoStrmGetOpenFlags(hVfsIos),
NIL_RTVFS, NIL_RTVFSLOCK, phVfsIos, (void **)&pThis);
if (RT_SUCCESS(rc))
{
pThis->rcCanceled = VINF_SUCCESS;
pThis->fFlags = fFlags;
pThis->pfnProgress = pfnProgress;
pThis->pvUser = pvUser;
pThis->hVfsIos = hVfsIos;
pThis->hVfsFile = RTVfsIoStrmToFile(hVfsIos);
pThis->cbCurrentlyRead = 0;
pThis->cbCurrentlyWritten = 0;
pThis->cbExpectedRead = cbExpectedRead;
pThis->cbExpectedWritten = cbExpectedWritten;
pThis->cbExpected = cbExpectedRead + cbExpectedWritten;
if (!pThis->cbExpected)
pThis->cbExpected = 1;
pThis->uCurPct = 0;
}
return rc;
}
RTDECL(int) RTVfsCreateProgressForFile(RTVFSFILE hVfsFile, PFNRTPROGRESS pfnProgress, void *pvUser, uint32_t fFlags,
uint64_t cbExpectedRead, uint64_t cbExpectedWritten, PRTVFSFILE phVfsFile)
{
AssertPtrReturn(pfnProgress, VERR_INVALID_POINTER);
AssertReturn(!(fFlags & ~RTVFSPROGRESS_F_VALID_MASK), VERR_INVALID_FLAGS);
AssertReturn(!(fFlags & RTVFSPROGRESS_F_FORWARD_SEEK_AS_READ) || !(fFlags & RTVFSPROGRESS_F_FORWARD_SEEK_AS_WRITE),
VERR_INVALID_FLAGS);
uint32_t cRefs = RTVfsFileRetain(hVfsFile);
AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(hVfsFile);
AssertReturnStmt(hVfsIos != NIL_RTVFSIOSTREAM, RTVfsFileRelease(hVfsFile), VERR_INVALID_HANDLE);
PRTVFSPROGRESSFILE pThis;
int rc = RTVfsNewFile(&g_rtVfsProgressFileOps, sizeof(*pThis), RTVfsFileGetOpenFlags(hVfsFile),
NIL_RTVFS, NIL_RTVFSLOCK, phVfsFile, (void **)&pThis);
if (RT_SUCCESS(rc))
{
pThis->fFlags = fFlags;
pThis->pfnProgress = pfnProgress;
pThis->pvUser = pvUser;
pThis->hVfsIos = hVfsIos;
pThis->hVfsFile = hVfsFile;
pThis->cbCurrentlyRead = 0;
pThis->cbCurrentlyWritten = 0;
pThis->cbExpectedRead = cbExpectedRead;
pThis->cbExpectedWritten = cbExpectedWritten;
pThis->cbExpected = cbExpectedRead + cbExpectedWritten;
if (!pThis->cbExpected)
pThis->cbExpected = 1;
pThis->uCurPct = 0;
}
return rc;
}
|