diff options
author | Josh Stone <jistone@redhat.com> | 2009-09-28 18:49:51 -0700 |
---|---|---|
committer | Josh Stone <jistone@redhat.com> | 2009-09-28 18:49:51 -0700 |
commit | e16dc041ec7dbcc83c64533ee4cf8759f6ae0be5 (patch) | |
tree | 2e5a018c8057e930bcdf27afcc290effb0b25593 /util.cxx | |
parent | 55e50c24c9176f1b3d15af94e145456a68e7ecf6 (diff) | |
download | systemtap-steved-e16dc041ec7dbcc83c64533ee4cf8759f6ae0be5.tar.gz systemtap-steved-e16dc041ec7dbcc83c64533ee4cf8759f6ae0be5.tar.xz systemtap-steved-e16dc041ec7dbcc83c64533ee4cf8759f6ae0be5.zip |
Simplify copy_file calls
Every single copy_file call we had was converting strings to char*,
printing the same error message, and optionally printing the same
verbose string. Let's canonicalize that.
* util.cxx (copy_file): Take string filenames, add a verbose flag, and
consolidate the message printing.
* cache.cxx (add_to_cache): Pass strings and remove message printing.
(get_from_cache): Ditto.
* main.cxx (main): Ditto.
* tapsets.cxx (tracepoint_builder::get_tracequery_module): Ditto.
(dwarf_cast_expanding_visitor::filter_special_modules): Ditto.
Diffstat (limited to 'util.cxx')
-rw-r--r-- | util.cxx | 30 |
1 files changed, 19 insertions, 11 deletions
@@ -78,8 +78,8 @@ file_exists (const string &path) // Copy a file. The copy is done via a temporary file and atomic // rename. -int -copy_file(const char *src, const char *dest) +bool +copy_file(const string& src, const string& dest, bool verbose) { int fd1, fd2; char buf[10240]; @@ -88,10 +88,13 @@ copy_file(const char *src, const char *dest) char *tmp_name; mode_t mask; + if (verbose) + clog << "Copying " << src << " to " << dest << endl; + // Open the src file. - fd1 = open(src, O_RDONLY); + fd1 = open(src.c_str(), O_RDONLY); if (fd1 == -1) - return -1; + goto error; // Open the temporary output file. tmp = dest + string(".XXXXXX"); @@ -100,7 +103,7 @@ copy_file(const char *src, const char *dest) if (fd2 == -1) { close(fd1); - return -1; + goto error; } // Copy the src file to the temporary output file. @@ -111,7 +114,7 @@ copy_file(const char *src, const char *dest) close(fd2); close(fd1); unlink(tmp_name); - return -1; + goto error; } } close(fd1); @@ -126,18 +129,23 @@ copy_file(const char *src, const char *dest) if (close(fd2) == -1) { unlink(tmp_name); - return -1; + goto error; } // Rename the temporary output file to the destination file. - unlink(dest); - if (rename(tmp_name, dest) == -1) + unlink(dest.c_str()); + if (rename(tmp_name, dest.c_str()) == -1) { unlink(tmp_name); - return -1; + goto error; } - return 0; + return true; + +error: + cerr << "Copy failed (\"" << src << "\" to \"" << dest << "\"): " + << strerror(errno) << endl; + return false; } |