Changeset 456 for branches


Ignore:
Timestamp:
Aug 21, 2007, 9:56:06 AM (16 years ago)
Author:
Jari Häkkinen
Message:

Refs #251.

Location:
branches/0.6-stable/lib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/0.6-stable/lib/File.cc

    r430 r456  
    3434
    3535#include <cassert>
     36#include <cstdio>
    3637#include <ctime>
    3738#include <fstream>
     
    254255    tmp.close();
    255256    close(fd);
    256     // finally move printed temporary file to original file
    257     rename(tmpname, path().c_str());
     257    // finally copy temporary file to replace original file, and
     258    // remove the temporary file
     259    try {
     260      copy_file(tmpname, path());
     261    }
     262    catch (std::runtime_error e) {
     263      // catch exception, cleanup, and rethrow
     264      std::cerr << "File::print_copyright: Exception caught, "
     265                << "removing temporary file " << tmpname << std::endl;
     266      if (unlink(tmpname))
     267        throw runtime_error(std::string("File::print_copyright: ") +
     268                            "failed to unlink temporary file" + tmpname);
     269      throw;
     270    }
     271    if (unlink(tmpname))
     272      throw runtime_error(std::string("File::print_copyright: ") +
     273                          "failed to unlink temporary file" + tmpname);
    258274  }
    259275
  • branches/0.6-stable/lib/File.h

    r430 r456  
    6666    const Stats& parse(const bool verbose=false);
    6767
     68    /**
     69       @throw std::runtime_error when a file error is encountered
     70       updating the copyrights.
     71    */
    6872    void print_copyright(std::map<std::string, Alias>&) const;
    6973
  • branches/0.6-stable/lib/utility.cc

    r430 r456  
    6161
    6262
     63  void copy_file(const std::string& source, const std::string& target)
     64  {
     65    std::ofstream o(target.c_str());
     66    std::ifstream i(source.c_str());
     67    while (i.good()) {
     68      char ch=i.get();
     69      if (i.good())
     70        o.put(ch);
     71      if (!o.good())
     72        throw std::runtime_error(std::string("copy_file: ") +
     73                                 "writing target file failed '" + target + "'");
     74    }
     75    if (!i.eof() && (i.bad() || i.fail()))  // fail on everything except eof
     76      throw std::runtime_error(std::string("copy_file: ") +
     77                               "error reading source file '" + source + "'");
     78    i.close(); o.close();
     79  }
     80
     81
    6382  std::string file_name(const std::string& full_path)
    6483  {
  • branches/0.6-stable/lib/utility.h

    r430 r456  
    5353  ///
    5454  int access_rights(const std::string& path,const std::string& bits);
     55
     56  /**
     57     @brief Copy file \a source to \a target.
     58
     59     @throw std::runtime_error when read error of \a source or write
     60     error for \a target is encountered.
     61  */
     62  void copy_file(const std::string& source, const std::string& target);
    5563
    5664  ///
Note: See TracChangeset for help on using the changeset viewer.