Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

TestFullDisk.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 
00003 // This file is part of Vesta.
00004 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Thu May 27 14:54:35 EDT 2004 by ken@xorian.net
00020 //      modified on Wed Jan 26 17:10:36 PST 2000 by heydon
00021 
00022 // TestFullDisk.C
00023 //
00024 // This program tests if the AtomicFile implementation behaves
00025 // correctly in the face of a full disk.
00026 //
00027 // Syntax: TestFullDisk [ -useFS ] filename size
00028 //
00029 // The program opens the file named 'filename', and writes
00030 // 'size' null bytes to it. It then commits the writes using
00031 // the AtomicFile::close method.
00032 //
00033 // If the '-useFS' option is specified, then all writes
00034 // are performed via calls to FS::Write, which should
00035 // halt and report an error whenever any of the writes
00036 // fails.
00037 
00038 #include <Basics.H>
00039 #include "FS.H"
00040 #include "AtomicFile.H"
00041 
00042 using std::cerr;
00043 using std::endl;
00044 using std::ios;
00045 
00046 void Syntax(char *msg) {
00047     cerr << "Error: " << msg << endl;
00048     cerr << "Syntax: TestFullDisk [ -useFS ] filename size" << endl;
00049     exit(1);
00050 }
00051 
00052 void Test(bool useFS, char *filename, int size) throw (FS::Failure) {
00053     // open the file
00054     AtomicFile ofs;
00055     ofs.open(filename, ios::out);
00056     if (ofs.fail()) {
00057         throw(FS::Failure(Text("AtomicFile::open"), filename));
00058     }
00059 
00060     // write to it
00061     const int BuffSz = 1024 * 8;
00062     char buff[BuffSz];
00063     while (size > 0) {
00064         int toWrite = min(BuffSz, size);
00065         if (useFS) {
00066             FS::Write(ofs, buff, toWrite);
00067         } else {
00068             ofs.write(buff, toWrite);
00069         }
00070         size -= toWrite;
00071     }
00072 
00073     // close the file (rename it atomically)
00074     FS::Close(ofs);
00075 }
00076 
00077 int main(int argc, char *argv[]) {
00078     // parse command-line
00079     if (argc != 3 && argc != 4) {
00080         Syntax("incorrect number of arguments");
00081     }
00082     int arg = 1;
00083     bool useFS = false;
00084     if (strcmp(argv[arg], "-useFS") == 0) {
00085         arg++;
00086         useFS = true;
00087     }
00088     char *filename = argv[arg++];
00089     int size;
00090     if (sscanf(argv[arg++], "%d", &size) != 1) {
00091         Syntax("'size' argument not an integer");
00092     }
00093 
00094     // perform the test
00095     try {
00096         Test(useFS, filename, size);
00097     } catch (const FS::Failure &f) {
00098         // fatal error
00099         cerr << f;
00100         assert(false);
00101     }
00102     return 0;
00103 }
00104     

Generated on Mon May 8 00:48:30 2006 for Vesta by  doxygen 1.4.2