michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "prio.h" michael@0: #include "prmem.h" michael@0: #include "prprf.h" michael@0: #include "prinit.h" michael@0: #include "prerror.h" michael@0: #include "prthread.h" michael@0: michael@0: #include "plerror.h" michael@0: #include "plgetopt.h" michael@0: michael@0: #define DEFAULT_COUNT 10 michael@0: #define DEFAULT_FILESIZE 1 michael@0: #define BUFFER_SIZE 1000000 michael@0: michael@0: typedef enum {v_silent, v_whisper, v_shout} Verbosity; michael@0: static void Verbose(Verbosity, const char*, const char*, PRIntn); michael@0: michael@0: #define VERBOSE(_l, _m) Verbose(_l, _m, __FILE__, __LINE__) michael@0: michael@0: static PRIntn test_result = 2; michael@0: static PRFileDesc *output = NULL; michael@0: static PRIntn verbose = v_silent; michael@0: static PRIntn filesize = DEFAULT_FILESIZE; michael@0: michael@0: static PRIntn Usage(void) michael@0: { michael@0: PR_fprintf(output, "Bigfile test usage:\n"); michael@0: PR_fprintf(output, ">bigfile [-G] [-d] [-v[*v]] [-s ] \n"); michael@0: PR_fprintf(output, "\td\tdebug mode (equivalent to -vvv)\t(false)\n"); michael@0: PR_fprintf(output, "\tv\tAdditional levels of output\t(none)\n"); michael@0: PR_fprintf(output, "\tk\tKeep data file after exit\t(false)\n"); michael@0: PR_fprintf(output, "\ts \tFile size in megabytes\t\t(1 megabyte)\n"); michael@0: PR_fprintf(output, "\t\tName of test file\t(none)\n"); michael@0: return 2; /* nothing happened */ michael@0: } /* Usage */ michael@0: michael@0: static PRStatus DeleteIfFound(const char *filename) michael@0: { michael@0: PRStatus rv; michael@0: VERBOSE(v_shout, "Checking for existing file"); michael@0: rv = PR_Access(filename, PR_ACCESS_WRITE_OK); michael@0: if (PR_SUCCESS == rv) michael@0: { michael@0: VERBOSE(v_shout, "Deleting existing file"); michael@0: rv = PR_Delete(filename); michael@0: if (PR_FAILURE == rv) VERBOSE(v_shout, "Cannot delete big file"); michael@0: } michael@0: else if (PR_FILE_NOT_FOUND_ERROR != PR_GetError()) michael@0: VERBOSE(v_shout, "Cannot access big file"); michael@0: else rv = PR_SUCCESS; michael@0: return rv; michael@0: } /* DeleteIfFound */ michael@0: michael@0: static PRIntn Error(const char *msg, const char *filename) michael@0: { michael@0: PRInt32 error = PR_GetError(); michael@0: if (NULL != msg) michael@0: { michael@0: if (0 == error) PR_fprintf(output, msg); michael@0: else PL_FPrintError(output, msg); michael@0: } michael@0: (void)DeleteIfFound(filename); michael@0: if (v_shout == verbose) PR_Abort(); michael@0: return 1; michael@0: } /* Error */ michael@0: michael@0: static void Verbose( michael@0: Verbosity level, const char *msg, const char *file, PRIntn line) michael@0: { michael@0: if (level <= verbose) michael@0: PR_fprintf(output, "[%s : %d]: %s\n", file, line, msg); michael@0: } /* Verbose */ michael@0: michael@0: static void PrintInfo(PRFileInfo64 *info, const char *filename) michael@0: { michael@0: PRExplodedTime tm; michael@0: char ctime[40], mtime[40]; michael@0: static const char *types[] = {"FILE", "DIRECTORY", "OTHER"}; michael@0: PR_fprintf( michael@0: output, "[%s : %d]: File info for %s\n", michael@0: __FILE__, __LINE__, filename); michael@0: PR_fprintf( michael@0: output, " type: %s, size: %llu bytes,\n", michael@0: types[info->type - 1], info->size); michael@0: michael@0: PR_ExplodeTime(info->creationTime, PR_GMTParameters, &tm); michael@0: (void)PR_FormatTime(ctime, sizeof(ctime), "%c GMT", &tm); michael@0: PR_ExplodeTime(info->modifyTime, PR_GMTParameters, &tm); michael@0: (void)PR_FormatTime(mtime, sizeof(mtime), "%c GMT", &tm); michael@0: michael@0: PR_fprintf( michael@0: output, " creation: %s,\n modify: %s\n", ctime, mtime); michael@0: } /* PrintInfo */ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRStatus rv; michael@0: char *buffer; michael@0: PLOptStatus os; michael@0: PRInt32 loop, bytes; michael@0: PRFileInfo small_info; michael@0: PRFileInfo64 big_info; michael@0: PRBool keep = PR_FALSE; michael@0: PRFileDesc *file = NULL; michael@0: const char *filename = NULL; michael@0: PRIntn count = DEFAULT_COUNT; michael@0: PRInt64 filesize64, big_answer, big_size, one_meg, zero_meg, big_fragment; michael@0: PRInt64 sevenFox = LL_INIT(0,0x7fffffff); michael@0: michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "dtvhs:"); michael@0: michael@0: output = PR_GetSpecialFD(PR_StandardError); michael@0: PR_STDIO_INIT(); michael@0: michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 0: michael@0: filename = opt->value; michael@0: break; michael@0: case 'd': /* debug mode */ michael@0: verbose = v_shout; michael@0: break; michael@0: case 'k': /* keep file */ michael@0: keep = PR_TRUE; michael@0: break; michael@0: case 'v': /* verbosity */ michael@0: if (v_shout > verbose) verbose += 1; michael@0: break; michael@0: case 'c': /* loop counter */ michael@0: count = atoi(opt->value); michael@0: break; michael@0: case 's': /* filesize */ michael@0: filesize = atoi(opt->value); michael@0: break; michael@0: case 'h': /* confused */ michael@0: default: michael@0: return Usage(); michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: if (0 == count) count = DEFAULT_COUNT; michael@0: if (0 == filesize) filesize = DEFAULT_FILESIZE; michael@0: if (NULL == filename) michael@0: { michael@0: #ifdef SYMBIAN michael@0: #define FILE_NAME "c:\\data\\bigfile.dat" michael@0: #else michael@0: #define FILE_NAME "bigfile.dat" michael@0: #endif michael@0: if (DEFAULT_FILESIZE != filesize) return Usage(); michael@0: else filename = FILE_NAME; michael@0: } michael@0: michael@0: if (PR_FAILURE == DeleteIfFound(filename)) return 1; michael@0: michael@0: test_result = 0; michael@0: michael@0: LL_I2L(zero_meg, 0); michael@0: LL_I2L(one_meg, 1000000); michael@0: LL_I2L(filesize64, filesize); michael@0: buffer = (char*)PR_MALLOC(BUFFER_SIZE); michael@0: LL_I2L(big_fragment, BUFFER_SIZE); michael@0: LL_MUL(filesize64, filesize64, one_meg); michael@0: michael@0: for (loop = 0; loop < BUFFER_SIZE; ++loop) buffer[loop] = (char)loop; michael@0: michael@0: VERBOSE(v_whisper, "Creating big file"); michael@0: file = PR_Open(filename, PR_CREATE_FILE | PR_WRONLY, 0666); michael@0: if (NULL == file) return Error("PR_Open()", filename); michael@0: michael@0: VERBOSE(v_whisper, "Testing available space in empty file"); michael@0: big_answer = file->methods->available64(file); michael@0: if (!LL_IS_ZERO(big_answer)) return Error("empty available64()", filename); michael@0: michael@0: LL_SUB(big_size, filesize64, one_meg); michael@0: VERBOSE(v_whisper, "Creating sparse big file by seeking to end"); michael@0: big_answer = file->methods->seek64(file, big_size, PR_SEEK_SET); michael@0: if (!LL_EQ(big_answer, big_size)) return Error("seek", filename); michael@0: michael@0: VERBOSE(v_whisper, "Writing block at end of sparse file"); michael@0: bytes = file->methods->write(file, buffer, BUFFER_SIZE); michael@0: if (bytes != BUFFER_SIZE) return Error("write", filename); michael@0: michael@0: VERBOSE(v_whisper, "Testing available space at end of sparse file"); michael@0: big_answer = file->methods->available64(file); michael@0: if (!LL_IS_ZERO(big_answer)) return Error("eof available64()", filename); michael@0: michael@0: VERBOSE(v_whisper, "Getting big info on sparse big file"); michael@0: rv = file->methods->fileInfo64(file, &big_info); michael@0: if (PR_FAILURE == rv) return Error("fileInfo64()", filename); michael@0: if (v_shout <= verbose) PrintInfo(&big_info, filename); michael@0: michael@0: VERBOSE(v_whisper, "Getting small info on sparse big file"); michael@0: rv = file->methods->fileInfo(file, &small_info); michael@0: if (LL_CMP(sevenFox, <, filesize64) && (PR_SUCCESS == rv)) michael@0: { michael@0: VERBOSE(v_whisper, "Should have failed and didn't"); michael@0: return Error("fileInfo()", filename); michael@0: } michael@0: else if (LL_CMP(sevenFox, >, filesize64) && (PR_FAILURE == rv)) michael@0: { michael@0: VERBOSE(v_whisper, "Should have succeeded and didn't"); michael@0: return Error("fileInfo()", filename); michael@0: } michael@0: michael@0: VERBOSE(v_whisper, "Rewinding big file"); michael@0: big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_SET); michael@0: if (!LL_IS_ZERO(big_answer)) return Error("rewind seek64()", filename); michael@0: michael@0: VERBOSE(v_whisper, "Establishing available space in rewound file"); michael@0: big_answer = file->methods->available64(file); michael@0: if (LL_NE(filesize64, big_answer)) michael@0: return Error("bof available64()", filename); michael@0: michael@0: VERBOSE(v_whisper, "Closing big file"); michael@0: rv = file->methods->close(file); michael@0: if (PR_FAILURE == rv) return Error("close()", filename); michael@0: michael@0: VERBOSE(v_whisper, "Reopening big file"); michael@0: file = PR_Open(filename, PR_RDWR, 0666); michael@0: if (NULL == file) return Error("open failed", filename); michael@0: michael@0: VERBOSE(v_whisper, "Checking available data in reopened file"); michael@0: big_answer = file->methods->available64(file); michael@0: if (LL_NE(filesize64, big_answer)) michael@0: return Error("reopened available64()", filename); michael@0: michael@0: big_answer = zero_meg; michael@0: VERBOSE(v_whisper, "Rewriting every byte of big file data"); michael@0: do michael@0: { michael@0: bytes = file->methods->write(file, buffer, BUFFER_SIZE); michael@0: if (bytes != BUFFER_SIZE) michael@0: return Error("write", filename); michael@0: LL_ADD(big_answer, big_answer, big_fragment); michael@0: } while (LL_CMP(big_answer, <, filesize64)); michael@0: michael@0: VERBOSE(v_whisper, "Checking position at eof"); michael@0: big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_CUR); michael@0: if (LL_NE(big_answer, filesize64)) michael@0: return Error("file size error", filename); michael@0: michael@0: VERBOSE(v_whisper, "Testing available space at eof"); michael@0: big_answer = file->methods->available64(file); michael@0: if (!LL_IS_ZERO(big_answer)) michael@0: return Error("eof available64()", filename); michael@0: michael@0: VERBOSE(v_whisper, "Rewinding full file"); michael@0: big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_SET); michael@0: if (!LL_IS_ZERO(big_answer)) return Error("bof seek64()", filename); michael@0: michael@0: VERBOSE(v_whisper, "Testing available space in rewound file"); michael@0: big_answer = file->methods->available64(file); michael@0: if (LL_NE(big_answer, filesize64)) return Error("bof available64()", filename); michael@0: michael@0: VERBOSE(v_whisper, "Seeking to end of big file"); michael@0: big_answer = file->methods->seek64(file, filesize64, PR_SEEK_SET); michael@0: if (LL_NE(big_answer, filesize64)) return Error("eof seek64()", filename); michael@0: michael@0: VERBOSE(v_whisper, "Getting info on big file while it's open"); michael@0: rv = file->methods->fileInfo64(file, &big_info); michael@0: if (PR_FAILURE == rv) return Error("fileInfo64()", filename); michael@0: if (v_shout <= verbose) PrintInfo(&big_info, filename); michael@0: michael@0: VERBOSE(v_whisper, "Closing big file"); michael@0: rv = file->methods->close(file); michael@0: if (PR_FAILURE == rv) return Error("close()", filename); michael@0: michael@0: VERBOSE(v_whisper, "Getting info on big file after it's closed"); michael@0: rv = PR_GetFileInfo64(filename, &big_info); michael@0: if (PR_FAILURE == rv) return Error("fileInfo64()", filename); michael@0: if (v_shout <= verbose) PrintInfo(&big_info, filename); michael@0: michael@0: VERBOSE(v_whisper, "Deleting big file"); michael@0: rv = PR_Delete(filename); michael@0: if (PR_FAILURE == rv) return Error("PR_Delete()", filename); michael@0: michael@0: PR_DELETE(buffer); michael@0: return test_result; michael@0: } /* main */ michael@0: michael@0: /* bigfile.c */