VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/bigfile.c@ 22683

最後變更 在這個檔案從22683是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 11.3 KB
 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#include "prio.h"
39#include "prmem.h"
40#include "prprf.h"
41#include "prinit.h"
42#include "prerror.h"
43#include "prthread.h"
44
45#include "plerror.h"
46#include "plgetopt.h"
47
48#define DEFAULT_COUNT 10
49#define DEFAULT_FILESIZE 1
50#define BUFFER_SIZE 1000000
51
52typedef enum {v_silent, v_whisper, v_shout} Verbosity;
53static void Verbose(Verbosity, const char*, const char*, PRIntn);
54
55#define VERBOSE(_l, _m) Verbose(_l, _m, __FILE__, __LINE__)
56
57static PRIntn test_result = 2;
58static PRFileDesc *output = NULL;
59static PRIntn verbose = v_silent;
60static PRIntn filesize = DEFAULT_FILESIZE;
61
62static PRIntn Usage(void)
63{
64 PR_fprintf(output, "Bigfile test usage:\n");
65 PR_fprintf(output, ">bigfile [-G] [-d] [-v[*v]] [-s <n>] <filename>\n");
66 PR_fprintf(output, "\td\tdebug mode (equivalent to -vvv)\t(false)\n");
67 PR_fprintf(output, "\tv\tAdditional levels of output\t(none)\n");
68 PR_fprintf(output, "\tk\tKeep data file after exit\t(false)\n");
69 PR_fprintf(output, "\ts <n>\tFile size in megabytes\t\t(1 megabyte)\n");
70 PR_fprintf(output, "\t<filename>\tName of test file\t(none)\n");
71 return 2; /* nothing happened */
72} /* Usage */
73
74static PRStatus DeleteIfFound(const char *filename)
75{
76 PRStatus rv;
77 VERBOSE(v_shout, "Checking for existing file");
78 rv = PR_Access(filename, PR_ACCESS_WRITE_OK);
79 if (PR_SUCCESS == rv)
80 {
81 VERBOSE(v_shout, "Deleting existing file");
82 rv = PR_Delete(filename);
83 if (PR_FAILURE == rv) VERBOSE(v_shout, "Cannot delete big file");
84 }
85 else if (PR_FILE_NOT_FOUND_ERROR != PR_GetError())
86 VERBOSE(v_shout, "Cannot access big file");
87 else rv = PR_SUCCESS;
88 return rv;
89} /* DeleteIfFound */
90
91static PRIntn Error(const char *msg, const char *filename)
92{
93 PRInt32 error = PR_GetError();
94 if (NULL != msg)
95 {
96 if (0 == error) PR_fprintf(output, msg);
97 else PL_FPrintError(output, msg);
98 }
99 (void)DeleteIfFound(filename);
100 if (v_shout == verbose) PR_Abort();
101 return 1;
102} /* Error */
103
104static void Verbose(
105 Verbosity level, const char *msg, const char *file, PRIntn line)
106{
107 if (level <= verbose)
108 PR_fprintf(output, "[%s : %d]: %s\n", file, line, msg);
109} /* Verbose */
110
111static void PrintInfo(PRFileInfo64 *info, const char *filename)
112{
113 PRExplodedTime tm;
114 char ctime[40], mtime[40];
115 static const char *types[] = {"FILE", "DIRECTORY", "OTHER"};
116 PR_fprintf(
117 output, "[%s : %d]: File info for %s\n",
118 __FILE__, __LINE__, filename);
119 PR_fprintf(
120 output, " type: %s, size: %llu bytes,\n",
121 types[info->type - 1], info->size);
122
123 PR_ExplodeTime(info->creationTime, PR_GMTParameters, &tm);
124 (void)PR_FormatTime(ctime, sizeof(ctime), "%c GMT", &tm);
125 PR_ExplodeTime(info->modifyTime, PR_GMTParameters, &tm);
126 (void)PR_FormatTime(mtime, sizeof(mtime), "%c GMT", &tm);
127
128 PR_fprintf(
129 output, " creation: %s,\n modify: %s\n", ctime, mtime);
130} /* PrintInfo */
131
132PRIntn main(PRIntn argc, char **argv)
133{
134 PRStatus rv;
135 char *buffer;
136 PLOptStatus os;
137 PRInt32 loop, bytes;
138 PRFileInfo small_info;
139 PRFileInfo64 big_info;
140 PRBool keep = PR_FALSE;
141 PRFileDesc *file = NULL;
142 const char *filename = NULL;
143 PRIntn count = DEFAULT_COUNT;
144 PRInt64 filesize64, big_answer, big_size, one_meg, zero_meg, big_fragment;
145 PRInt64 sevenFox = LL_INIT(0,0x7fffffff);
146
147 PLOptState *opt = PL_CreateOptState(argc, argv, "dtvhs:");
148
149 output = PR_GetSpecialFD(PR_StandardError);
150 PR_STDIO_INIT();
151
152 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
153 {
154 if (PL_OPT_BAD == os) continue;
155 switch (opt->option)
156 {
157 case 0:
158 filename = opt->value;
159 break;
160 case 'd': /* debug mode */
161 verbose = v_shout;
162 break;
163 case 'k': /* keep file */
164 keep = PR_TRUE;
165 break;
166 case 'v': /* verbosity */
167 if (v_shout > verbose) verbose += 1;
168 break;
169 case 'c': /* loop counter */
170 count = atoi(opt->value);
171 break;
172 case 's': /* filesize */
173 filesize = atoi(opt->value);
174 break;
175 case 'h': /* confused */
176 default:
177 return Usage();
178 }
179 }
180 PL_DestroyOptState(opt);
181
182 if (0 == count) count = DEFAULT_COUNT;
183 if (0 == filesize) filesize = DEFAULT_FILESIZE;
184 if (NULL == filename)
185 {
186 if (DEFAULT_FILESIZE != filesize) return Usage();
187 else filename = "bigfile.dat";
188 }
189
190 if (PR_FAILURE == DeleteIfFound(filename)) return 1;
191
192 test_result = 0;
193
194 LL_I2L(zero_meg, 0);
195 LL_I2L(one_meg, 1000000);
196 LL_I2L(filesize64, filesize);
197 buffer = (char*)PR_MALLOC(BUFFER_SIZE);
198 LL_I2L(big_fragment, BUFFER_SIZE);
199 LL_MUL(filesize64, filesize64, one_meg);
200
201 for (loop = 0; loop < BUFFER_SIZE; ++loop) buffer[loop] = (char)loop;
202
203 VERBOSE(v_whisper, "Creating big file");
204 file = PR_Open(filename, PR_CREATE_FILE | PR_WRONLY, 0666);
205 if (NULL == file) return Error("PR_Open()", filename);
206
207 VERBOSE(v_whisper, "Testing available space in empty file");
208 big_answer = file->methods->available64(file);
209 if (!LL_IS_ZERO(big_answer)) return Error("empty available64()", filename);
210
211 LL_SUB(big_size, filesize64, one_meg);
212 VERBOSE(v_whisper, "Creating sparse big file by seeking to end");
213 big_answer = file->methods->seek64(file, big_size, PR_SEEK_SET);
214 if (!LL_EQ(big_answer, big_size)) return Error("seek", filename);
215
216 VERBOSE(v_whisper, "Writing block at end of sparse file");
217 bytes = file->methods->write(file, buffer, BUFFER_SIZE);
218 if (bytes != BUFFER_SIZE) return Error("write", filename);
219
220 VERBOSE(v_whisper, "Testing available space at end of sparse file");
221 big_answer = file->methods->available64(file);
222 if (!LL_IS_ZERO(big_answer)) return Error("eof available64()", filename);
223
224 VERBOSE(v_whisper, "Getting big info on sparse big file");
225 rv = file->methods->fileInfo64(file, &big_info);
226 if (PR_FAILURE == rv) return Error("fileInfo64()", filename);
227 if (v_shout <= verbose) PrintInfo(&big_info, filename);
228
229 VERBOSE(v_whisper, "Getting small info on sparse big file");
230 rv = file->methods->fileInfo(file, &small_info);
231 if (LL_CMP(sevenFox, <, filesize64) && (PR_SUCCESS == rv))
232 {
233 VERBOSE(v_whisper, "Should have failed and didn't");
234 return Error("fileInfo()", filename);
235 }
236 else if (LL_CMP(sevenFox, >, filesize64) && (PR_FAILURE == rv))
237 {
238 VERBOSE(v_whisper, "Should have succeeded and didn't");
239 return Error("fileInfo()", filename);
240 }
241
242 VERBOSE(v_whisper, "Rewinding big file");
243 big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_SET);
244 if (!LL_IS_ZERO(big_answer)) return Error("rewind seek64()", filename);
245
246 VERBOSE(v_whisper, "Establishing available space in rewound file");
247 big_answer = file->methods->available64(file);
248 if (LL_NE(filesize64, big_answer))
249 return Error("bof available64()", filename);
250
251 VERBOSE(v_whisper, "Closing big file");
252 rv = file->methods->close(file);
253 if (PR_FAILURE == rv) return Error("close()", filename);
254
255 VERBOSE(v_whisper, "Reopening big file");
256 file = PR_Open(filename, PR_RDWR, 0666);
257 if (NULL == file) return Error("open failed", filename);
258
259 VERBOSE(v_whisper, "Checking available data in reopened file");
260 big_answer = file->methods->available64(file);
261 if (LL_NE(filesize64, big_answer))
262 return Error("reopened available64()", filename);
263
264 big_answer = zero_meg;
265 VERBOSE(v_whisper, "Rewriting every byte of big file data");
266 do
267 {
268 bytes = file->methods->write(file, buffer, BUFFER_SIZE);
269 if (bytes != BUFFER_SIZE)
270 return Error("write", filename);
271 LL_ADD(big_answer, big_answer, big_fragment);
272 } while (LL_CMP(big_answer, <, filesize64));
273
274 VERBOSE(v_whisper, "Checking position at eof");
275 big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_CUR);
276 if (LL_NE(big_answer, filesize64))
277 return Error("file size error", filename);
278
279 VERBOSE(v_whisper, "Testing available space at eof");
280 big_answer = file->methods->available64(file);
281 if (!LL_IS_ZERO(big_answer))
282 return Error("eof available64()", filename);
283
284 VERBOSE(v_whisper, "Rewinding full file");
285 big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_SET);
286 if (!LL_IS_ZERO(big_answer)) return Error("bof seek64()", filename);
287
288 VERBOSE(v_whisper, "Testing available space in rewound file");
289 big_answer = file->methods->available64(file);
290 if (LL_NE(big_answer, filesize64)) return Error("bof available64()", filename);
291
292 VERBOSE(v_whisper, "Seeking to end of big file");
293 big_answer = file->methods->seek64(file, filesize64, PR_SEEK_SET);
294 if (LL_NE(big_answer, filesize64)) return Error("eof seek64()", filename);
295
296 VERBOSE(v_whisper, "Getting info on big file while it's open");
297 rv = file->methods->fileInfo64(file, &big_info);
298 if (PR_FAILURE == rv) return Error("fileInfo64()", filename);
299 if (v_shout <= verbose) PrintInfo(&big_info, filename);
300
301 VERBOSE(v_whisper, "Closing big file");
302 rv = file->methods->close(file);
303 if (PR_FAILURE == rv) return Error("close()", filename);
304
305 VERBOSE(v_whisper, "Getting info on big file after it's closed");
306 rv = PR_GetFileInfo64(filename, &big_info);
307 if (PR_FAILURE == rv) return Error("fileInfo64()", filename);
308 if (v_shout <= verbose) PrintInfo(&big_info, filename);
309
310 VERBOSE(v_whisper, "Deleting big file");
311 rv = PR_Delete(filename);
312 if (PR_FAILURE == rv) return Error("PR_Delete()", filename);
313
314 PR_DELETE(buffer);
315 return test_result;
316} /* main */
317
318/* bigfile.c */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette