Changeset 1132 for trunk/lib/File.cc


Ignore:
Timestamp:
Jul 16, 2010, 5:24:35 AM (12 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

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.