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
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/
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
15 * The Original Code is the Netscape Portable Runtime (NSPR).
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.
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.
36 * ***** END LICENSE BLOCK ***** */
48 #define DEFAULT_COUNT 10
49 #define DEFAULT_FILESIZE 1
50 #define BUFFER_SIZE 1000000
52 typedef enum {v_silent
, v_whisper
, v_shout
} Verbosity
;
53 static void Verbose(Verbosity
, const char*, const char*, PRIntn
);
55 #define VERBOSE(_l, _m) Verbose(_l, _m, __FILE__, __LINE__)
57 static PRIntn test_result
= 2;
58 static PRFileDesc
*output
= NULL
;
59 static PRIntn verbose
= v_silent
;
60 static PRIntn filesize
= DEFAULT_FILESIZE
;
62 static PRIntn
Usage(void)
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 */
74 static PRStatus
DeleteIfFound(const char *filename
)
77 VERBOSE(v_shout
, "Checking for existing file");
78 rv
= PR_Access(filename
, PR_ACCESS_WRITE_OK
);
81 VERBOSE(v_shout
, "Deleting existing file");
82 rv
= PR_Delete(filename
);
83 if (PR_FAILURE
== rv
) VERBOSE(v_shout
, "Cannot delete big file");
85 else if (PR_FILE_NOT_FOUND_ERROR
!= PR_GetError())
86 VERBOSE(v_shout
, "Cannot access big file");
91 static PRIntn
Error(const char *msg
, const char *filename
)
93 PRInt32 error
= PR_GetError();
96 if (0 == error
) PR_fprintf(output
, msg
);
97 else PL_FPrintError(output
, msg
);
99 (void)DeleteIfFound(filename
);
100 if (v_shout
== verbose
) PR_Abort();
105 Verbosity level
, const char *msg
, const char *file
, PRIntn line
)
107 if (level
<= verbose
)
108 PR_fprintf(output
, "[%s : %d]: %s\n", file
, line
, msg
);
111 static void PrintInfo(PRFileInfo64
*info
, const char *filename
)
114 char ctime
[40], mtime
[40];
115 static const char *types
[] = {"FILE", "DIRECTORY", "OTHER"};
117 output
, "[%s : %d]: File info for %s\n",
118 __FILE__
, __LINE__
, filename
);
120 output
, " type: %s, size: %llu bytes,\n",
121 types
[info
->type
- 1], info
->size
);
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
);
129 output
, " creation: %s,\n modify: %s\n", ctime
, mtime
);
132 PRIntn
main(PRIntn argc
, char **argv
)
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);
147 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "dtvhs:");
149 output
= PR_GetSpecialFD(PR_StandardError
);
152 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
154 if (PL_OPT_BAD
== os
) continue;
158 filename
= opt
->value
;
160 case 'd': /* debug mode */
163 case 'k': /* keep file */
166 case 'v': /* verbosity */
167 if (v_shout
> verbose
) verbose
+= 1;
169 case 'c': /* loop counter */
170 count
= atoi(opt
->value
);
172 case 's': /* filesize */
173 filesize
= atoi(opt
->value
);
175 case 'h': /* confused */
180 PL_DestroyOptState(opt
);
182 if (0 == count
) count
= DEFAULT_COUNT
;
183 if (0 == filesize
) filesize
= DEFAULT_FILESIZE
;
184 if (NULL
== filename
)
186 if (DEFAULT_FILESIZE
!= filesize
) return Usage();
187 else filename
= "bigfile.dat";
190 if (PR_FAILURE
== DeleteIfFound(filename
)) return 1;
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
);
201 for (loop
= 0; loop
< BUFFER_SIZE
; ++loop
) buffer
[loop
] = (char)loop
;
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
);
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
);
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
);
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
);
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
);
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
);
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
))
233 VERBOSE(v_whisper
, "Should have failed and didn't");
234 return Error("fileInfo()", filename
);
236 else if (LL_CMP(sevenFox
, >, filesize64
) && (PR_FAILURE
== rv
))
238 VERBOSE(v_whisper
, "Should have succeeded and didn't");
239 return Error("fileInfo()", filename
);
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
);
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
);
251 VERBOSE(v_whisper
, "Closing big file");
252 rv
= file
->methods
->close(file
);
253 if (PR_FAILURE
== rv
) return Error("close()", filename
);
255 VERBOSE(v_whisper
, "Reopening big file");
256 file
= PR_Open(filename
, PR_RDWR
, 0666);
257 if (NULL
== file
) return Error("open failed", filename
);
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
);
264 big_answer
= zero_meg
;
265 VERBOSE(v_whisper
, "Rewriting every byte of big file data");
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
));
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
);
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
);
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
);
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
);
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
);
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
);
301 VERBOSE(v_whisper
, "Closing big file");
302 rv
= file
->methods
->close(file
);
303 if (PR_FAILURE
== rv
) return Error("close()", filename
);
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
);
310 VERBOSE(v_whisper
, "Deleting big file");
311 rv
= PR_Delete(filename
);
312 if (PR_FAILURE
== rv
) return Error("PR_Delete()", filename
);