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