| 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/. */ |
| 5 |
|
| 6 |
#include "prio.h" |
| 7 |
#include "prmem.h" |
| 8 |
#include "prprf.h" |
| 9 |
#include "prinit.h" |
| 10 |
#include "prerror.h" |
| 11 |
#include "prthread.h" |
| 12 |
|
| 13 |
#include "plerror.h" |
| 14 |
#include "plgetopt.h" |
| 15 |
|
| 16 |
#define DEFAULT_COUNT 10 |
| 17 |
#define DEFAULT_FILESIZE 1 |
| 18 |
#define BUFFER_SIZE 1000000 |
| 19 |
|
| 20 |
typedef enum { v_silent, v_whisper, v_shout } Verbosity; |
| 21 |
static void Verbose(Verbosity, const char*, const char*, PRIntn); |
| 22 |
|
| 23 |
#define VERBOSE(_l, _m) Verbose(_l, _m, __FILE__, __LINE__) |
| 24 |
|
| 25 |
static PRIntn test_result = 2; |
| 26 |
static PRFileDesc* output = NULL; |
| 27 |
static PRIntn verbose = v_silent; |
| 28 |
static PRIntn filesize = DEFAULT_FILESIZE; |
| 29 |
|
| 30 |
static PRIntn Usage(void) { |
| 31 |
PR_fprintf(output, "Bigfile test usage:\n"); |
| 32 |
PR_fprintf(output, ">bigfile [-G] [-d] [-v[*v]] [-s <n>] <filename>\n"); |
| 33 |
PR_fprintf(output, "\td\tdebug mode (equivalent to -vvv)\t(false)\n"); |
| 34 |
PR_fprintf(output, "\tv\tAdditional levels of output\t(none)\n"); |
| 35 |
PR_fprintf(output, "\tk\tKeep data file after exit\t(false)\n"); |
| 36 |
PR_fprintf(output, "\ts <n>\tFile size in megabytes\t\t(1 megabyte)\n"); |
| 37 |
PR_fprintf(output, "\t<filename>\tName of test file\t(none)\n"); |
| 38 |
return 2; /* nothing happened */ |
| 39 |
} /* Usage */ |
| 40 |
|
| 41 |
static PRStatus DeleteIfFound(const char* filename) { |
| 42 |
PRStatus rv; |
| 43 |
VERBOSE(v_shout, "Checking for existing file"); |
| 44 |
rv = PR_Access(filename, PR_ACCESS_WRITE_OK); |
| 45 |
if (PR_SUCCESS == rv) { |
| 46 |
VERBOSE(v_shout, "Deleting existing file"); |
| 47 |
rv = PR_Delete(filename); |
| 48 |
if (PR_FAILURE == rv) { |
| 49 |
VERBOSE(v_shout, "Cannot delete big file"); |
| 50 |
} |
| 51 |
} else if (PR_FILE_NOT_FOUND_ERROR != PR_GetError()) { |
| 52 |
VERBOSE(v_shout, "Cannot access big file"); |
| 53 |
} else { |
| 54 |
rv = PR_SUCCESS; |
| 55 |
} |
| 56 |
return rv; |
| 57 |
} /* DeleteIfFound */ |
| 58 |
|
| 59 |
static PRIntn Error(const char* msg, const char* filename) { |
| 60 |
PRInt32 error = PR_GetError(); |
| 61 |
if (NULL != msg) { |
| 62 |
if (0 == error) { |
| 63 |
PR_fprintf(output, msg); |
| 64 |
} else { |
| 65 |
PL_FPrintError(output, msg); |
| 66 |
} |
| 67 |
} |
| 68 |
(void)DeleteIfFound(filename); |
| 69 |
if (v_shout == verbose) { |
| 70 |
PR_Abort(); |
| 71 |
} |
| 72 |
return 1; |
| 73 |
} /* Error */ |
| 74 |
|
| 75 |
static void Verbose(Verbosity level, const char* msg, const char* file, |
| 76 |
PRIntn line) { |
| 77 |
if (level <= verbose) { |
| 78 |
PR_fprintf(output, "[%s : %d]: %s\n", file, line, msg); |
| 79 |
} |
| 80 |
} /* Verbose */ |
| 81 |
|
| 82 |
static void PrintInfo(PRFileInfo64* info, const char* filename) { |
| 83 |
PRExplodedTime tm; |
| 84 |
char ctime[40], mtime[40]; |
| 85 |
static const char* types[] = {"FILE", "DIRECTORY", "OTHER"}; |
| 86 |
PR_fprintf(output, "[%s : %d]: File info for %s\n", __FILE__, __LINE__, |
| 87 |
filename); |
| 88 |
PR_fprintf(output, " type: %s, size: %llu bytes,\n", types[info->type - 1], |
| 89 |
info->size); |
| 90 |
|
| 91 |
PR_ExplodeTime(info->creationTime, PR_GMTParameters, &tm); |
| 92 |
(void)PR_FormatTime(ctime, sizeof(ctime), "%c GMT", &tm); |
| 93 |
PR_ExplodeTime(info->modifyTime, PR_GMTParameters, &tm); |
| 94 |
(void)PR_FormatTime(mtime, sizeof(mtime), "%c GMT", &tm); |
| 95 |
|
| 96 |
PR_fprintf(output, " creation: %s,\n modify: %s\n", ctime, mtime); |
| 97 |
} /* PrintInfo */ |
| 98 |
|
| 99 |
int main(int argc, char** argv) { |
| 100 |
PRStatus rv; |
| 101 |
char* buffer; |
| 102 |
PLOptStatus os; |
| 103 |
PRInt32 loop, bytes; |
| 104 |
PRFileInfo small_info; |
| 105 |
PRFileInfo64 big_info; |
| 106 |
PRBool keep = PR_FALSE; |
| 107 |
PRFileDesc* file = NULL; |
| 108 |
const char* filename = NULL; |
| 109 |
PRIntn count = DEFAULT_COUNT; |
| 110 |
PRInt64 filesize64, big_answer, big_size, one_meg, zero_meg, big_fragment; |
| 111 |
PRInt64 sevenFox = LL_INIT(0, 0x7fffffff); |
| 112 |
|
| 113 |
PLOptState* opt = PL_CreateOptState(argc, argv, "dtvhs:"); |
| 114 |
|
| 115 |
output = PR_GetSpecialFD(PR_StandardError); |
| 116 |
PR_STDIO_INIT(); |
| 117 |
|
| 118 |
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { |
| 119 |
if (PL_OPT_BAD == os) { |
| 120 |
continue; |
| 121 |
} |
| 122 |
switch (opt->option) { |
| 123 |
case 0: |
| 124 |
filename = opt->value; |
| 125 |
break; |
| 126 |
case 'd': /* debug mode */ |
| 127 |
verbose = v_shout; |
| 128 |
break; |
| 129 |
case 'k': /* keep file */ |
| 130 |
keep = PR_TRUE; |
| 131 |
break; |
| 132 |
case 'v': /* verbosity */ |
| 133 |
if (v_shout > verbose) { |
| 134 |
verbose += 1; |
| 135 |
} |
| 136 |
break; |
| 137 |
case 'c': /* loop counter */ |
| 138 |
count = atoi(opt->value); |
| 139 |
break; |
| 140 |
case 's': /* filesize */ |
| 141 |
filesize = atoi(opt->value); |
| 142 |
break; |
| 143 |
case 'h': /* confused */ |
| 144 |
default: |
| 145 |
return Usage(); |
| 146 |
} |
| 147 |
} |
| 148 |
PL_DestroyOptState(opt); |
| 149 |
|
| 150 |
if (0 == count) { |
| 151 |
count = DEFAULT_COUNT; |
| 152 |
} |
| 153 |
if (0 == filesize) { |
| 154 |
filesize = DEFAULT_FILESIZE; |
| 155 |
} |
| 156 |
if (NULL == filename) { |
| 157 |
#define FILE_NAME "bigfile.dat" |
| 158 |
if (DEFAULT_FILESIZE != filesize) { |
| 159 |
return Usage(); |
| 160 |
} else { |
| 161 |
filename = FILE_NAME; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if (PR_FAILURE == DeleteIfFound(filename)) { |
| 166 |
return 1; |
| 167 |
} |
| 168 |
|
| 169 |
test_result = 0; |
| 170 |
|
| 171 |
LL_I2L(zero_meg, 0); |
| 172 |
LL_I2L(one_meg, 1000000); |
| 173 |
LL_I2L(filesize64, filesize); |
| 174 |
buffer = (char*)PR_MALLOC(BUFFER_SIZE); |
| 175 |
LL_I2L(big_fragment, BUFFER_SIZE); |
| 176 |
LL_MUL(filesize64, filesize64, one_meg); |
| 177 |
|
| 178 |
for (loop = 0; loop < BUFFER_SIZE; ++loop) { |
| 179 |
buffer[loop] = (char)loop; |
| 180 |
} |
| 181 |
|
| 182 |
VERBOSE(v_whisper, "Creating big file"); |
| 183 |
file = PR_Open(filename, PR_CREATE_FILE | PR_WRONLY, 0666); |
| 184 |
if (NULL == file) { |
| 185 |
return Error("PR_Open()", filename); |
| 186 |
} |
| 187 |
|
| 188 |
VERBOSE(v_whisper, "Testing available space in empty file"); |
| 189 |
big_answer = file->methods->available64(file); |
| 190 |
if (!LL_IS_ZERO(big_answer)) { |
| 191 |
return Error("empty available64()", filename); |
| 192 |
} |
| 193 |
|
| 194 |
LL_SUB(big_size, filesize64, one_meg); |
| 195 |
VERBOSE(v_whisper, "Creating sparse big file by seeking to end"); |
| 196 |
big_answer = file->methods->seek64(file, big_size, PR_SEEK_SET); |
| 197 |
if (!LL_EQ(big_answer, big_size)) { |
| 198 |
return Error("seek", filename); |
| 199 |
} |
| 200 |
|
| 201 |
VERBOSE(v_whisper, "Writing block at end of sparse file"); |
| 202 |
bytes = file->methods->write(file, buffer, BUFFER_SIZE); |
| 203 |
if (bytes != BUFFER_SIZE) { |
| 204 |
return Error("write", filename); |
| 205 |
} |
| 206 |
|
| 207 |
VERBOSE(v_whisper, "Testing available space at end of sparse file"); |
| 208 |
big_answer = file->methods->available64(file); |
| 209 |
if (!LL_IS_ZERO(big_answer)) { |
| 210 |
return Error("eof available64()", filename); |
| 211 |
} |
| 212 |
|
| 213 |
VERBOSE(v_whisper, "Getting big info on sparse big file"); |
| 214 |
rv = file->methods->fileInfo64(file, &big_info); |
| 215 |
if (PR_FAILURE == rv) { |
| 216 |
return Error("fileInfo64()", filename); |
| 217 |
} |
| 218 |
if (v_shout <= verbose) { |
| 219 |
PrintInfo(&big_info, filename); |
| 220 |
} |
| 221 |
|
| 222 |
VERBOSE(v_whisper, "Getting small info on sparse big file"); |
| 223 |
rv = file->methods->fileInfo(file, &small_info); |
| 224 |
if (LL_CMP(sevenFox, <, filesize64) && (PR_SUCCESS == rv)) { |
| 225 |
VERBOSE(v_whisper, "Should have failed and didn't"); |
| 226 |
return Error("fileInfo()", filename); |
| 227 |
} else if (LL_CMP(sevenFox, >, filesize64) && (PR_FAILURE == rv)) { |
| 228 |
VERBOSE(v_whisper, "Should have succeeded and didn't"); |
| 229 |
return Error("fileInfo()", filename); |
| 230 |
} |
| 231 |
|
| 232 |
VERBOSE(v_whisper, "Rewinding big file"); |
| 233 |
big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_SET); |
| 234 |
if (!LL_IS_ZERO(big_answer)) { |
| 235 |
return Error("rewind seek64()", filename); |
| 236 |
} |
| 237 |
|
| 238 |
VERBOSE(v_whisper, "Establishing available space in rewound file"); |
| 239 |
big_answer = file->methods->available64(file); |
| 240 |
if (LL_NE(filesize64, big_answer)) { |
| 241 |
return Error("bof available64()", filename); |
| 242 |
} |
| 243 |
|
| 244 |
VERBOSE(v_whisper, "Closing big file"); |
| 245 |
rv = file->methods->close(file); |
| 246 |
if (PR_FAILURE == rv) { |
| 247 |
return Error("close()", filename); |
| 248 |
} |
| 249 |
|
| 250 |
VERBOSE(v_whisper, "Reopening big file"); |
| 251 |
file = PR_Open(filename, PR_RDWR, 0666); |
| 252 |
if (NULL == file) { |
| 253 |
return Error("open failed", filename); |
| 254 |
} |
| 255 |
|
| 256 |
VERBOSE(v_whisper, "Checking available data in reopened file"); |
| 257 |
big_answer = file->methods->available64(file); |
| 258 |
if (LL_NE(filesize64, big_answer)) { |
| 259 |
return Error("reopened available64()", filename); |
| 260 |
} |
| 261 |
|
| 262 |
big_answer = zero_meg; |
| 263 |
VERBOSE(v_whisper, "Rewriting every byte of big file data"); |
| 264 |
do { |
| 265 |
bytes = file->methods->write(file, buffer, BUFFER_SIZE); |
| 266 |
if (bytes != BUFFER_SIZE) { |
| 267 |
return Error("write", filename); |
| 268 |
} |
| 269 |
LL_ADD(big_answer, big_answer, big_fragment); |
| 270 |
} while (LL_CMP(big_answer, <, filesize64)); |
| 271 |
|
| 272 |
VERBOSE(v_whisper, "Checking position at eof"); |
| 273 |
big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_CUR); |
| 274 |
if (LL_NE(big_answer, filesize64)) { |
| 275 |
return Error("file size error", filename); |
| 276 |
} |
| 277 |
|
| 278 |
VERBOSE(v_whisper, "Testing available space at eof"); |
| 279 |
big_answer = file->methods->available64(file); |
| 280 |
if (!LL_IS_ZERO(big_answer)) { |
| 281 |
return Error("eof available64()", filename); |
| 282 |
} |
| 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)) { |
| 287 |
return Error("bof seek64()", filename); |
| 288 |
} |
| 289 |
|
| 290 |
VERBOSE(v_whisper, "Testing available space in rewound file"); |
| 291 |
big_answer = file->methods->available64(file); |
| 292 |
if (LL_NE(big_answer, filesize64)) { |
| 293 |
return Error("bof available64()", filename); |
| 294 |
} |
| 295 |
|
| 296 |
VERBOSE(v_whisper, "Seeking to end of big file"); |
| 297 |
big_answer = file->methods->seek64(file, filesize64, PR_SEEK_SET); |
| 298 |
if (LL_NE(big_answer, filesize64)) { |
| 299 |
return Error("eof seek64()", filename); |
| 300 |
} |
| 301 |
|
| 302 |
VERBOSE(v_whisper, "Getting info on big file while it's open"); |
| 303 |
rv = file->methods->fileInfo64(file, &big_info); |
| 304 |
if (PR_FAILURE == rv) { |
| 305 |
return Error("fileInfo64()", filename); |
| 306 |
} |
| 307 |
if (v_shout <= verbose) { |
| 308 |
PrintInfo(&big_info, filename); |
| 309 |
} |
| 310 |
|
| 311 |
VERBOSE(v_whisper, "Closing big file"); |
| 312 |
rv = file->methods->close(file); |
| 313 |
if (PR_FAILURE == rv) { |
| 314 |
return Error("close()", filename); |
| 315 |
} |
| 316 |
|
| 317 |
VERBOSE(v_whisper, "Getting info on big file after it's closed"); |
| 318 |
rv = PR_GetFileInfo64(filename, &big_info); |
| 319 |
if (PR_FAILURE == rv) { |
| 320 |
return Error("fileInfo64()", filename); |
| 321 |
} |
| 322 |
if (v_shout <= verbose) { |
| 323 |
PrintInfo(&big_info, filename); |
| 324 |
} |
| 325 |
|
| 326 |
VERBOSE(v_whisper, "Deleting big file"); |
| 327 |
rv = PR_Delete(filename); |
| 328 |
if (PR_FAILURE == rv) { |
| 329 |
return Error("PR_Delete()", filename); |
| 330 |
} |
| 331 |
|
| 332 |
PR_DELETE(buffer); |
| 333 |
return test_result; |
| 334 |
} /* main */ |
| 335 |
|
| 336 |
/* bigfile.c */ |