1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
16 #define DEFAULT_COUNT 10
17 #define DEFAULT_FILESIZE 1
18 #define BUFFER_SIZE 1000000
20 typedef enum {v_silent
, v_whisper
, v_shout
} Verbosity
;
21 static void Verbose(Verbosity
, const char*, const char*, PRIntn
);
23 #define VERBOSE(_l, _m) Verbose(_l, _m, __FILE__, __LINE__)
25 static PRIntn test_result
= 2;
26 static PRFileDesc
*output
= NULL
;
27 static PRIntn verbose
= v_silent
;
28 static PRIntn filesize
= DEFAULT_FILESIZE
;
30 static PRIntn
Usage(void)
32 PR_fprintf(output
, "Bigfile test usage:\n");
33 PR_fprintf(output
, ">bigfile [-G] [-d] [-v[*v]] [-s <n>] <filename>\n");
34 PR_fprintf(output
, "\td\tdebug mode (equivalent to -vvv)\t(false)\n");
35 PR_fprintf(output
, "\tv\tAdditional levels of output\t(none)\n");
36 PR_fprintf(output
, "\tk\tKeep data file after exit\t(false)\n");
37 PR_fprintf(output
, "\ts <n>\tFile size in megabytes\t\t(1 megabyte)\n");
38 PR_fprintf(output
, "\t<filename>\tName of test file\t(none)\n");
39 return 2; /* nothing happened */
42 static PRStatus
DeleteIfFound(const char *filename
)
45 VERBOSE(v_shout
, "Checking for existing file");
46 rv
= PR_Access(filename
, PR_ACCESS_WRITE_OK
);
49 VERBOSE(v_shout
, "Deleting existing file");
50 rv
= PR_Delete(filename
);
51 if (PR_FAILURE
== rv
) {
52 VERBOSE(v_shout
, "Cannot delete big file");
55 else if (PR_FILE_NOT_FOUND_ERROR
!= PR_GetError()) {
56 VERBOSE(v_shout
, "Cannot access big file");
64 static PRIntn
Error(const char *msg
, const char *filename
)
66 PRInt32 error
= PR_GetError();
70 PR_fprintf(output
, msg
);
73 PL_FPrintError(output
, msg
);
76 (void)DeleteIfFound(filename
);
77 if (v_shout
== verbose
) {
84 Verbosity level
, const char *msg
, const char *file
, PRIntn line
)
86 if (level
<= verbose
) {
87 PR_fprintf(output
, "[%s : %d]: %s\n", file
, line
, msg
);
91 static void PrintInfo(PRFileInfo64
*info
, const char *filename
)
94 char ctime
[40], mtime
[40];
95 static const char *types
[] = {"FILE", "DIRECTORY", "OTHER"};
97 output
, "[%s : %d]: File info for %s\n",
98 __FILE__
, __LINE__
, filename
);
100 output
, " type: %s, size: %llu bytes,\n",
101 types
[info
->type
- 1], info
->size
);
103 PR_ExplodeTime(info
->creationTime
, PR_GMTParameters
, &tm
);
104 (void)PR_FormatTime(ctime
, sizeof(ctime
), "%c GMT", &tm
);
105 PR_ExplodeTime(info
->modifyTime
, PR_GMTParameters
, &tm
);
106 (void)PR_FormatTime(mtime
, sizeof(mtime
), "%c GMT", &tm
);
109 output
, " creation: %s,\n modify: %s\n", ctime
, mtime
);
112 int main(int argc
, char **argv
)
118 PRFileInfo small_info
;
119 PRFileInfo64 big_info
;
120 PRBool keep
= PR_FALSE
;
121 PRFileDesc
*file
= NULL
;
122 const char *filename
= NULL
;
123 PRIntn count
= DEFAULT_COUNT
;
124 PRInt64 filesize64
, big_answer
, big_size
, one_meg
, zero_meg
, big_fragment
;
125 PRInt64 sevenFox
= LL_INIT(0,0x7fffffff);
127 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "dtvhs:");
129 output
= PR_GetSpecialFD(PR_StandardError
);
132 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
134 if (PL_OPT_BAD
== os
) {
140 filename
= opt
->value
;
142 case 'd': /* debug mode */
145 case 'k': /* keep file */
148 case 'v': /* verbosity */
149 if (v_shout
> verbose
) {
153 case 'c': /* loop counter */
154 count
= atoi(opt
->value
);
156 case 's': /* filesize */
157 filesize
= atoi(opt
->value
);
159 case 'h': /* confused */
164 PL_DestroyOptState(opt
);
167 count
= DEFAULT_COUNT
;
170 filesize
= DEFAULT_FILESIZE
;
172 if (NULL
== filename
)
174 #define FILE_NAME "bigfile.dat"
175 if (DEFAULT_FILESIZE
!= filesize
) {
179 filename
= FILE_NAME
;
183 if (PR_FAILURE
== DeleteIfFound(filename
)) {
190 LL_I2L(one_meg
, 1000000);
191 LL_I2L(filesize64
, filesize
);
192 buffer
= (char*)PR_MALLOC(BUFFER_SIZE
);
193 LL_I2L(big_fragment
, BUFFER_SIZE
);
194 LL_MUL(filesize64
, filesize64
, one_meg
);
196 for (loop
= 0; loop
< BUFFER_SIZE
; ++loop
) {
197 buffer
[loop
] = (char)loop
;
200 VERBOSE(v_whisper
, "Creating big file");
201 file
= PR_Open(filename
, PR_CREATE_FILE
| PR_WRONLY
, 0666);
203 return Error("PR_Open()", filename
);
206 VERBOSE(v_whisper
, "Testing available space in empty file");
207 big_answer
= file
->methods
->available64(file
);
208 if (!LL_IS_ZERO(big_answer
)) {
209 return Error("empty available64()", filename
);
212 LL_SUB(big_size
, filesize64
, one_meg
);
213 VERBOSE(v_whisper
, "Creating sparse big file by seeking to end");
214 big_answer
= file
->methods
->seek64(file
, big_size
, PR_SEEK_SET
);
215 if (!LL_EQ(big_answer
, big_size
)) {
216 return Error("seek", filename
);
219 VERBOSE(v_whisper
, "Writing block at end of sparse file");
220 bytes
= file
->methods
->write(file
, buffer
, BUFFER_SIZE
);
221 if (bytes
!= BUFFER_SIZE
) {
222 return Error("write", filename
);
225 VERBOSE(v_whisper
, "Testing available space at end of sparse file");
226 big_answer
= file
->methods
->available64(file
);
227 if (!LL_IS_ZERO(big_answer
)) {
228 return Error("eof available64()", filename
);
231 VERBOSE(v_whisper
, "Getting big info on sparse big file");
232 rv
= file
->methods
->fileInfo64(file
, &big_info
);
233 if (PR_FAILURE
== rv
) {
234 return Error("fileInfo64()", filename
);
236 if (v_shout
<= verbose
) {
237 PrintInfo(&big_info
, filename
);
240 VERBOSE(v_whisper
, "Getting small info on sparse big file");
241 rv
= file
->methods
->fileInfo(file
, &small_info
);
242 if (LL_CMP(sevenFox
, <, filesize64
) && (PR_SUCCESS
== rv
))
244 VERBOSE(v_whisper
, "Should have failed and didn't");
245 return Error("fileInfo()", filename
);
247 else if (LL_CMP(sevenFox
, >, filesize64
) && (PR_FAILURE
== rv
))
249 VERBOSE(v_whisper
, "Should have succeeded and didn't");
250 return Error("fileInfo()", filename
);
253 VERBOSE(v_whisper
, "Rewinding big file");
254 big_answer
= file
->methods
->seek64(file
, zero_meg
, PR_SEEK_SET
);
255 if (!LL_IS_ZERO(big_answer
)) {
256 return Error("rewind seek64()", filename
);
259 VERBOSE(v_whisper
, "Establishing available space in rewound file");
260 big_answer
= file
->methods
->available64(file
);
261 if (LL_NE(filesize64
, big_answer
)) {
262 return Error("bof available64()", filename
);
265 VERBOSE(v_whisper
, "Closing big file");
266 rv
= file
->methods
->close(file
);
267 if (PR_FAILURE
== rv
) {
268 return Error("close()", filename
);
271 VERBOSE(v_whisper
, "Reopening big file");
272 file
= PR_Open(filename
, PR_RDWR
, 0666);
274 return Error("open failed", filename
);
277 VERBOSE(v_whisper
, "Checking available data in reopened file");
278 big_answer
= file
->methods
->available64(file
);
279 if (LL_NE(filesize64
, big_answer
)) {
280 return Error("reopened available64()", filename
);
283 big_answer
= zero_meg
;
284 VERBOSE(v_whisper
, "Rewriting every byte of big file data");
287 bytes
= file
->methods
->write(file
, buffer
, BUFFER_SIZE
);
288 if (bytes
!= BUFFER_SIZE
) {
289 return Error("write", filename
);
291 LL_ADD(big_answer
, big_answer
, big_fragment
);
292 } while (LL_CMP(big_answer
, <, filesize64
));
294 VERBOSE(v_whisper
, "Checking position at eof");
295 big_answer
= file
->methods
->seek64(file
, zero_meg
, PR_SEEK_CUR
);
296 if (LL_NE(big_answer
, filesize64
)) {
297 return Error("file size error", filename
);
300 VERBOSE(v_whisper
, "Testing available space at eof");
301 big_answer
= file
->methods
->available64(file
);
302 if (!LL_IS_ZERO(big_answer
)) {
303 return Error("eof available64()", filename
);
306 VERBOSE(v_whisper
, "Rewinding full file");
307 big_answer
= file
->methods
->seek64(file
, zero_meg
, PR_SEEK_SET
);
308 if (!LL_IS_ZERO(big_answer
)) {
309 return Error("bof seek64()", filename
);
312 VERBOSE(v_whisper
, "Testing available space in rewound file");
313 big_answer
= file
->methods
->available64(file
);
314 if (LL_NE(big_answer
, filesize64
)) {
315 return Error("bof available64()", filename
);
318 VERBOSE(v_whisper
, "Seeking to end of big file");
319 big_answer
= file
->methods
->seek64(file
, filesize64
, PR_SEEK_SET
);
320 if (LL_NE(big_answer
, filesize64
)) {
321 return Error("eof seek64()", filename
);
324 VERBOSE(v_whisper
, "Getting info on big file while it's open");
325 rv
= file
->methods
->fileInfo64(file
, &big_info
);
326 if (PR_FAILURE
== rv
) {
327 return Error("fileInfo64()", filename
);
329 if (v_shout
<= verbose
) {
330 PrintInfo(&big_info
, filename
);
333 VERBOSE(v_whisper
, "Closing big file");
334 rv
= file
->methods
->close(file
);
335 if (PR_FAILURE
== rv
) {
336 return Error("close()", filename
);
339 VERBOSE(v_whisper
, "Getting info on big file after it's closed");
340 rv
= PR_GetFileInfo64(filename
, &big_info
);
341 if (PR_FAILURE
== rv
) {
342 return Error("fileInfo64()", filename
);
344 if (v_shout
<= verbose
) {
345 PrintInfo(&big_info
, filename
);
348 VERBOSE(v_whisper
, "Deleting big file");
349 rv
= PR_Delete(filename
);
350 if (PR_FAILURE
== rv
) {
351 return Error("PR_Delete()", filename
);