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 int main(int 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)
187 #define FILE_NAME "c:\\data\\bigfile.dat"
189 #define FILE_NAME "bigfile.dat"
191 if (DEFAULT_FILESIZE != filesize) return Usage();
192 else filename = FILE_NAME;
195 if (PR_FAILURE == DeleteIfFound(filename)) return 1;
200 LL_I2L(one_meg, 1000000);
201 LL_I2L(filesize64, filesize);
202 buffer = (char*)PR_MALLOC(BUFFER_SIZE);
203 LL_I2L(big_fragment, BUFFER_SIZE);
204 LL_MUL(filesize64, filesize64, one_meg);
206 for (loop = 0; loop < BUFFER_SIZE; ++loop) buffer[loop] = (char)loop;
208 VERBOSE(v_whisper, "Creating big file");
209 file = PR_Open(filename, PR_CREATE_FILE | PR_WRONLY, 0666);
210 if (NULL == file) return Error("PR_Open()", filename);
212 VERBOSE(v_whisper, "Testing available space in empty file");
213 big_answer = file->methods->available64(file);
214 if (!LL_IS_ZERO(big_answer)) return Error("empty available64()", filename);
216 LL_SUB(big_size, filesize64, one_meg);
217 VERBOSE(v_whisper, "Creating sparse big file by seeking to end");
218 big_answer = file->methods->seek64(file, big_size, PR_SEEK_SET);
219 if (!LL_EQ(big_answer, big_size)) return Error("seek", filename);
221 VERBOSE(v_whisper, "Writing block at end of sparse file");
222 bytes = file->methods->write(file, buffer, BUFFER_SIZE);
223 if (bytes != BUFFER_SIZE) 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)) return Error("eof available64()", filename);
229 VERBOSE(v_whisper, "Getting big info on sparse big file");
230 rv = file->methods->fileInfo64(file, &big_info);
231 if (PR_FAILURE == rv) return Error("fileInfo64()", filename);
232 if (v_shout <= verbose) PrintInfo(&big_info, filename);
234 VERBOSE(v_whisper, "Getting small info on sparse big file");
235 rv = file->methods->fileInfo(file, &small_info);
236 if (LL_CMP(sevenFox, <, filesize64) && (PR_SUCCESS == rv))
238 VERBOSE(v_whisper, "Should have failed and didn't");
239 return Error("fileInfo()", filename);
241 else if (LL_CMP(sevenFox, >, filesize64) && (PR_FAILURE == rv))
243 VERBOSE(v_whisper, "Should have succeeded and didn't");
244 return Error("fileInfo()", filename);
247 VERBOSE(v_whisper, "Rewinding big file");
248 big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_SET);
249 if (!LL_IS_ZERO(big_answer)) return Error("rewind seek64()", filename);
251 VERBOSE(v_whisper, "Establishing available space in rewound file");
252 big_answer = file->methods->available64(file);
253 if (LL_NE(filesize64, big_answer))
254 return Error("bof available64()", filename);
256 VERBOSE(v_whisper, "Closing big file");
257 rv = file->methods->close(file);
258 if (PR_FAILURE == rv) return Error("close()", filename);
260 VERBOSE(v_whisper, "Reopening big file");
261 file = PR_Open(filename, PR_RDWR, 0666);
262 if (NULL == file) return Error("open failed", filename);
264 VERBOSE(v_whisper, "Checking available data in reopened file");
265 big_answer = file->methods->available64(file);
266 if (LL_NE(filesize64, big_answer))
267 return Error("reopened available64()", filename);
269 big_answer = zero_meg;
270 VERBOSE(v_whisper, "Rewriting every byte of big file data");
273 bytes = file->methods->write(file, buffer, BUFFER_SIZE);
274 if (bytes != BUFFER_SIZE)
275 return Error("write", filename);
276 LL_ADD(big_answer, big_answer, big_fragment);
277 } while (LL_CMP(big_answer, <, filesize64));
279 VERBOSE(v_whisper, "Checking position at eof");
280 big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_CUR);
281 if (LL_NE(big_answer, filesize64))
282 return Error("file size error", filename);
284 VERBOSE(v_whisper, "Testing available space at eof");
285 big_answer = file->methods->available64(file);
286 if (!LL_IS_ZERO(big_answer))
287 return Error("eof available64()", filename);
289 VERBOSE(v_whisper, "Rewinding full file");
290 big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_SET);
291 if (!LL_IS_ZERO(big_answer)) return Error("bof seek64()", filename);
293 VERBOSE(v_whisper, "Testing available space in rewound file");
294 big_answer = file->methods->available64(file);
295 if (LL_NE(big_answer, filesize64)) return Error("bof available64()", filename);
297 VERBOSE(v_whisper, "Seeking to end of big file");
298 big_answer = file->methods->seek64(file, filesize64, PR_SEEK_SET);
299 if (LL_NE(big_answer, filesize64)) return Error("eof seek64()", filename);
301 VERBOSE(v_whisper, "Getting info on big file while it's open");
302 rv = file->methods->fileInfo64(file, &big_info);
303 if (PR_FAILURE == rv) return Error("fileInfo64()", filename);
304 if (v_shout <= verbose) PrintInfo(&big_info, filename);
306 VERBOSE(v_whisper, "Closing big file");
307 rv = file->methods->close(file);
308 if (PR_FAILURE == rv) return Error("close()", filename);
310 VERBOSE(v_whisper, "Getting info on big file after it's closed");
311 rv = PR_GetFileInfo64(filename, &big_info);
312 if (PR_FAILURE == rv) return Error("fileInfo64()", filename);
313 if (v_shout <= verbose) PrintInfo(&big_info, filename);
315 VERBOSE(v_whisper, "Deleting big file");
316 rv = PR_Delete(filename);
317 if (PR_FAILURE == rv) return Error("PR_Delete()", filename);