home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / bigfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.3 KB  |  243 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "prio.h"
  20. #include "prmem.h"
  21. #include "prprf.h"
  22. #include "prinit.h"
  23. #include "prerror.h"
  24. #include "prthread.h"
  25.  
  26. #include "plerror.h"
  27. #include "plgetopt.h"
  28.  
  29. #define DEFAULT_COUNT 10
  30. #define DEFAULT_FILESIZE 1
  31. #define BUFFER_SIZE 1000000
  32.  
  33. typedef enum {v_silent, v_whisper, v_shout} Verbosity;
  34. static void Verbose(Verbosity, const char*, const char*, PRIntn);
  35.  
  36. #define VERBOSE(_l, _m) Verbose(_l, _m, __FILE__, __LINE__)
  37.  
  38. static PRIntn filesize = 1;
  39. static PRIntn test_result = 2;
  40. static PRFileDesc *output = NULL;
  41. static PRIntn verbose = v_silent;
  42.  
  43. static PRIntn Usage(void)
  44. {
  45.     PR_fprintf(output, "Bigfile test usage:\n");
  46.     PR_fprintf(output, ">bigfile [-G] [-d] [-v[*v]] [-s <n>] <filename>\n");
  47.     PR_fprintf(output, "\td\tdebug mode (equivalent to -vvv)\t(false)\n");
  48.     PR_fprintf(output, "\tv\tAdditional levels of output\t(none)\n");
  49.     PR_fprintf(output, "\ts <n>\tFile size in megabytes\t\t(1 megabyte)\n");
  50.     PR_fprintf(output, "\t<filename>\tName of test file\t(none)\n");
  51.     return 2;  /* nothing happened */
  52. }  /* Usage */
  53.  
  54. static PRStatus DeleteIfFound(const char *filename)
  55. {
  56.     PRStatus rv;
  57.     VERBOSE(v_shout, "Checking for existing file");
  58.     rv = PR_Access(filename, PR_ACCESS_WRITE_OK);
  59.     if (PR_SUCCESS == rv)
  60.     {
  61.         VERBOSE(v_shout, "Deleting existing file");
  62.         rv = PR_Delete(filename);
  63.         if (PR_FAILURE == rv) VERBOSE(v_shout, "Cannot delete big file");
  64.     }
  65.     else if (PR_FILE_NOT_FOUND_ERROR !=  PR_GetError())
  66.         VERBOSE(v_shout, "Cannot access big file");
  67.     return rv;
  68. }  /* DeleteIfFound */
  69.  
  70. static PRIntn Error(const char *msg, const char *filename)
  71. {
  72.     PRInt32 error = PR_GetError();
  73.     if (NULL != msg)
  74.     {
  75.         if (0 == error) PR_fprintf(output, msg);
  76.         else PL_FPrintError(output, msg);
  77.     }
  78.     (void)DeleteIfFound(filename);
  79.     if (v_shout == verbose) PR_Abort();
  80.     return 1;
  81. }  /* Error */
  82.  
  83. static void Verbose(
  84.     Verbosity level, const char *msg, const char *file, PRIntn line)
  85. {
  86.     if (level <= verbose)
  87.         PR_fprintf(output, "[%s : %d]: %s\n", file, line, msg);
  88. }  /* Verbose */
  89.  
  90. PRIntn main(PRIntn argc, char **argv)
  91. {
  92.     PRStatus rv;
  93.     char *buffer;
  94.     PLOptStatus os;
  95.     PRInt32 loop, bytes;
  96.     PRFileDesc *file = NULL;
  97.     const char *filename = NULL;
  98.     PRIntn count = DEFAULT_COUNT;
  99.     PRFileInfo64 *big_info = NULL;
  100.     PRInt64 big_answer, big_size, one_meg, zero_meg, big_fragment;
  101.     PRInt64 filesize64;
  102.  
  103.     PLOptState *opt = PL_CreateOptState(argc, argv, "dvhs:");
  104.  
  105.     output = PR_GetSpecialFD(PR_StandardError);
  106.     PR_STDIO_INIT();
  107.  
  108.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  109.     {
  110.         if (PL_OPT_BAD == os) continue;
  111.         switch (opt->option)
  112.         {
  113.         case 0:
  114.             filename = opt->value;
  115.             break;
  116.         case 'd':  /* debug mode */
  117.             verbose = v_shout;
  118.             break;
  119.         case 'v':  /* verbosity */
  120.             if (v_shout > verbose) verbose += 1;
  121.             break;
  122.         case 'c':  /* loop counter */
  123.             count = atoi(opt->value);
  124.             break;
  125.         case 's':  /* filesize */
  126.             filesize = atoi(opt->value);
  127.             break;
  128.         case 'h':  /* confused */
  129.         default:
  130.             return Usage();
  131.         }
  132.     }
  133.     PL_DestroyOptState(opt);
  134.  
  135.     if (NULL == filename) return Usage();
  136.     if (0 == count) count = DEFAULT_COUNT;
  137.     if (0 == filesize) filesize = DEFAULT_FILESIZE;
  138.  
  139.     if (PR_FAILURE == DeleteIfFound(filename)) return 1;
  140.  
  141.     test_result = 0;
  142.  
  143.     LL_I2L(zero_meg, 0);
  144.     LL_I2L(one_meg, 1000000);
  145.     LL_I2L(filesize64, filesize);
  146.     buffer = (char*)PR_MALLOC(BUFFER_SIZE);
  147.     LL_I2L(big_fragment, BUFFER_SIZE);
  148.     LL_MUL(big_size, filesize64, one_meg); 
  149.  
  150.     for (loop = 0; loop < BUFFER_SIZE; ++loop) buffer[loop] = (char)loop;
  151.  
  152.     VERBOSE(v_whisper, "Creating big file");
  153.     file = PR_Open(filename, PR_CREATE_FILE | PR_WRONLY, 0666);
  154.     if (NULL == file) return Error("PR_Open()", filename);
  155.     
  156.     VERBOSE(v_whisper, "Testing available space in empty file");
  157.     big_answer = file->methods->available64(file);
  158.     if (!LL_IS_ZERO(big_answer)) return Error("empty available64()", filename);
  159.  
  160. #if 0
  161.     VERBOSE(v_whisper, "Filling big file with data");
  162.     while (LL_CMP(big_answer, <, big_size))
  163.     {
  164.         bytes = file->methods->write(file, buffer, BUFFER_SIZE);
  165.         if (bytes != BUFFER_SIZE) return Error("write", filename);
  166.         LL_ADD(big_answer, big_answer, big_fragment);
  167.     }
  168. #else
  169.     LL_SUB(big_size, big_size, one_meg);
  170.     big_answer = file->methods->seek64(file, big_size, PR_SEEK_SET);
  171.     bytes = file->methods->write(file, buffer, BUFFER_SIZE);
  172.     if (bytes != BUFFER_SIZE) return Error("write", filename);
  173. #endif
  174.  
  175.     VERBOSE(v_whisper, "Testing available space in filled file");
  176.     big_answer = file->methods->available64(file);
  177.     if (LL_NE(big_answer, zero_meg)) return Error("eof available64()", filename);
  178.  
  179.     VERBOSE(v_whisper, "Rewinding big file");
  180.     big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_SET);
  181.     if (LL_NE(big_answer, zero_meg)) return Error("rewind seek64()", filename);
  182.  
  183.     VERBOSE(v_whisper, "Establishing available space in rewound file");
  184.     big_size = file->methods->available64(file);
  185.     if (!LL_GE_ZERO(big_size)) return Error("bof available64()", filename);
  186.  
  187.     VERBOSE(v_whisper, "Closing big file");
  188.     rv = file->methods->close(file);
  189.     if (PR_FAILURE == rv) return Error("close()", filename);
  190.  
  191.     VERBOSE(v_whisper, "Reopening big file");
  192.     file = PR_Open(filename, PR_RDWR, 0666);
  193.     if (NULL == file) return Error("bof seek64()", filename);
  194.  
  195.     VERBOSE(v_whisper, "Checking available data in reopened file");
  196.     big_answer = file->methods->available64(file);
  197.     if (LL_NE(big_size, big_answer)) return Error("reopened available64()", filename);
  198.  
  199.     big_answer = zero_meg;
  200.     VERBOSE(v_whisper, "Rewriting big file data");
  201.     while (LL_CMP(big_answer, <, big_size))
  202.     {
  203.         bytes = file->methods->write(file, buffer, BUFFER_SIZE);
  204.         if (bytes != BUFFER_SIZE) return Error("write", filename);
  205.         LL_ADD(big_answer, big_answer, big_fragment);
  206.     }
  207.  
  208.     VERBOSE(v_whisper, "Testing available space at eof");
  209.     big_answer = file->methods->available64(file);
  210.     if (LL_NE(big_answer, zero_meg)) return Error("eof available64()", filename);
  211.  
  212.     VERBOSE(v_whisper, "Rewinding full file file");
  213.     big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_SET);
  214.     if (LL_NE(big_answer, zero_meg)) return Error("bof seek64()", filename);
  215.  
  216.     VERBOSE(v_whisper, "Testing available space in rewound file");
  217.     big_answer = file->methods->available64(file);
  218.     if (LL_NE(big_answer, big_size)) return Error("bof available64()", filename);
  219.  
  220.     VERBOSE(v_whisper, "Seeking to end of big file");
  221.     big_answer = file->methods->seek64(file, big_size, PR_SEEK_SET);
  222.     if (LL_NE(big_answer, big_size)) return Error("eof seek64()", filename);
  223.  
  224.     VERBOSE(v_whisper, "Getting info on big file");
  225.     big_info = PR_NEWZAP(PRFileInfo64);
  226.     rv = file->methods->fileInfo64(file, big_info);
  227.     if (PR_FAILURE == rv) return Error("fileInfo64()", filename);
  228.     PR_DELETE(big_info);
  229.  
  230.     VERBOSE(v_whisper, "Closing big file");
  231.     rv = file->methods->close(file);
  232.     if (PR_FAILURE == rv) return Error("close()", filename);
  233.  
  234.     VERBOSE(v_whisper, "Deleting big file");
  235.     rv = PR_Delete(filename);
  236.     if (PR_FAILURE == rv) return Error("PR_Delete()", filename);
  237.  
  238.     PR_DELETE(buffer);
  239.     return test_result;
  240. } /* main */
  241.  
  242. /* bigfile.c */
  243.