1 | /* $Id: tstRTFilesystem.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - IPRT Filesystem API (Fileystem)
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2020 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 | #include <iprt/vfs.h>
|
---|
32 | #include <iprt/errcore.h>
|
---|
33 | #include <iprt/test.h>
|
---|
34 | #include <iprt/file.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Structures and Typedefs *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 |
|
---|
42 | static int tstRTFilesystem(RTTEST hTest, RTVFSFILE hVfsFile)
|
---|
43 | {
|
---|
44 | int rc = VINF_SUCCESS;
|
---|
45 | RTVFS hVfs = NIL_RTVFS;
|
---|
46 |
|
---|
47 | RTTestSubF(hTest, "Create filesystem object");
|
---|
48 |
|
---|
49 | rc = RTVfsMountVol(hVfsFile, RTVFSMNT_F_READ_ONLY | RTVFSMNT_F_FOR_RANGE_IN_USE, &hVfs, NULL);
|
---|
50 | if (RT_FAILURE(rc))
|
---|
51 | {
|
---|
52 | RTTestIFailed("RTVfsMountVol -> %Rrc", rc);
|
---|
53 | return rc;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /* Check all blocks. */
|
---|
57 | uint64_t off = 0;
|
---|
58 | uint32_t cBlocksUsed = 0;
|
---|
59 | uint32_t cBlocksUnused = 0;
|
---|
60 | uint64_t cbFs = 0;
|
---|
61 |
|
---|
62 | rc = RTVfsFileQuerySize(hVfsFile, &cbFs);
|
---|
63 | if (RT_FAILURE(rc))
|
---|
64 | {
|
---|
65 | RTTestIFailed("RTVfsFileQuerySize -> %Rrc", rc);
|
---|
66 | return rc;
|
---|
67 | }
|
---|
68 |
|
---|
69 | while (off < cbFs)
|
---|
70 | {
|
---|
71 | bool fUsed = false;
|
---|
72 |
|
---|
73 | rc = RTVfsQueryRangeState(hVfs, off, 1024, &fUsed);
|
---|
74 | if (RT_FAILURE(rc))
|
---|
75 | {
|
---|
76 | RTTestIFailed("RTVfsIsRangeInUse -> %Rrc", rc);
|
---|
77 | break;
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (fUsed)
|
---|
81 | cBlocksUsed++;
|
---|
82 | else
|
---|
83 | cBlocksUnused++;
|
---|
84 |
|
---|
85 | off += 1024;
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (RT_SUCCESS(rc))
|
---|
89 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%u blocks used and %u blocks unused\n",
|
---|
90 | cBlocksUsed, cBlocksUnused);
|
---|
91 |
|
---|
92 | RTVfsRelease(hVfs);
|
---|
93 |
|
---|
94 | return rc;
|
---|
95 | }
|
---|
96 |
|
---|
97 | int main(int argc, char **argv)
|
---|
98 | {
|
---|
99 | /*
|
---|
100 | * Initialize IPRT and create the test.
|
---|
101 | */
|
---|
102 | RTTEST hTest;
|
---|
103 | int rc = RTTestInitAndCreate("tstRTFilesystem", &hTest);
|
---|
104 | if (rc)
|
---|
105 | return rc;
|
---|
106 | RTTestBanner(hTest);
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * If no args, display usage.
|
---|
110 | */
|
---|
111 | if (argc < 2)
|
---|
112 | {
|
---|
113 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Syntax: %s <image>\n", argv[0]);
|
---|
114 | return RTTestSkipAndDestroy(hTest, "Missing required arguments\n");
|
---|
115 | }
|
---|
116 |
|
---|
117 | /* Open image. */
|
---|
118 | RTFILE hFile;
|
---|
119 | RTVFSFILE hVfsFile;
|
---|
120 | rc = RTFileOpen(&hFile, argv[1], RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ);
|
---|
121 | if (RT_FAILURE(rc))
|
---|
122 | {
|
---|
123 | RTTestIFailed("RTFileOpen -> %Rrc", rc);
|
---|
124 | return RTTestSummaryAndDestroy(hTest);
|
---|
125 | }
|
---|
126 |
|
---|
127 | rc = RTVfsFileFromRTFile(hFile, 0, false, &hVfsFile);
|
---|
128 | if (RT_FAILURE(rc))
|
---|
129 | {
|
---|
130 | RTTestIFailed("RTVfsFileFromRTFile -> %Rrc", rc);
|
---|
131 | return RTTestSummaryAndDestroy(hTest);
|
---|
132 | }
|
---|
133 |
|
---|
134 | rc = tstRTFilesystem(hTest, hVfsFile);
|
---|
135 |
|
---|
136 | RTTESTI_CHECK(rc == VINF_SUCCESS);
|
---|
137 |
|
---|
138 | RTVfsFileRelease(hVfsFile);
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * Summary
|
---|
142 | */
|
---|
143 | return RTTestSummaryAndDestroy(hTest);
|
---|
144 | }
|
---|
145 |
|
---|