Changeset 1132


Ignore:
Timestamp:
Jul 16, 2010, 5:24:35 AM (13 years ago)
Author:
Peter Johansson
Message:

when updating copyright, write to #file# and then rename that file rather than writing in /tmp which might be a a different device and rename doesn't work. fixes #285

Location:
trunk/lib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/File.cc

    r1097 r1132  
    401401                              size_t start_at_line, size_t end_at_line) const
    402402  {
    403     // Code copied from Gnuplot -r70
    404     char tmpname[]="/tmp/svndigestXXXXXX";
    405     int fd=mkstemp(tmpname);  // mkstemp return a file descriptor
    406     if (fd == -1)
    407       throw std::runtime_error(std::string("Failed to get unique filename: ") +
    408                                tmpname);
    409     // Jari would like to do something like 'std::ofstream tmp(fd);'
    410     // but has to settle for (which is stupid since the file is
    411     // already open for writing.
    412     std::ofstream tmp(tmpname);
     403    // embrace filename with brackets #filename#
     404    std::string tmpname = concatenate_path(directory_name(path()),
     405                                           "#" + file_name(path()) + "#");
     406    std::ofstream tmp(tmpname.c_str());
    413407
    414408    using namespace std;
     
    438432    is.close();
    439433    tmp.close();
    440     close(fd);
    441     // get file permission
    442     struct stat nodestat;
    443     stat(path().c_str(),&nodestat);
    444434   
    445     // finally copy temporary file to replace original file, and
    446     // remove the temporary file
    447     try {
    448       copy_file(tmpname, path());
    449     }
    450     catch (std::runtime_error e) {
    451       // catch exception, cleanup, and rethrow
    452       std::cerr << "svncopyright: File::print_copyright: Exception caught, "
    453                 << "removing temporary file " << tmpname << std::endl;
    454       if (unlink(tmpname))
    455         throw runtime_error(std::string("File::print_copyright: ") +
    456                             "failed to unlink temporary file" + tmpname);
    457       throw;
    458     }
    459     if (unlink(tmpname))
    460       throw runtime_error(std::string("File::print_copyright: ") +
    461                           "failed to unlink temporary file" + tmpname);
    462 
    463     chmod(path().c_str(), nodestat.st_mode);
     435    // finally rename file
     436    rename(tmpname, path());
    464437  }
    465438
  • trunk/lib/utility.cc

    r1101 r1132  
    289289
    290290
     291  void rename(const std::string& from, const std::string to)
     292  {
     293    int code = ::rename(from.c_str(), to.c_str());
     294    if (code){
     295      std::stringstream ss;
     296      ss << "rename(" << from << ", " << to << "): failed\n" << strerror(errno);
     297      throw std::runtime_error(ss.str());
     298    }
     299  }
     300
     301
    291302  void replace(std::string& str, std::string old_str, std::string new_str)
    292303  {
  • trunk/lib/utility.h

    r1098 r1132  
    176176  bool regexp(const std::string& pattern, const std::string& str,
    177177              std::vector<std::string>& vec);
     178
     179  /**
     180     same as rename(2) but throw errno if error is encountered
     181
     182     \see man rename
     183   */
     184  void rename(const std::string& from, const std::string to);
    178185
    179186  /**
Note: See TracChangeset for help on using the changeset viewer.