From 8934a58f919a4d40d81c4ef055b7ee6d2f00fc93 Mon Sep 17 00:00:00 2001 From: Da Zheng Date: Thu, 30 Jun 2016 17:49:44 -0400 Subject: [PATCH] [SAFS]: print err msgs when renaming fails. --- libsafs/native_file.cpp | 15 +++++++++++++++ libsafs/native_file.h | 9 +-------- libsafs/safs_file.cpp | 5 ++++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/libsafs/native_file.cpp b/libsafs/native_file.cpp index de106b02..53567841 100644 --- a/libsafs/native_file.cpp +++ b/libsafs/native_file.cpp @@ -19,6 +19,8 @@ #include #include +#include +#include #include "common.h" #include "native_file.h" @@ -121,4 +123,17 @@ bool native_dir::delete_dir(bool recursive) } } +bool native_file::rename(const std::string &new_name) +{ + if (::rename(file_name.c_str(), new_name.c_str()) == 0) { + file_name = new_name; + return true; + } + else { + fprintf(stderr, "can't renmae to %s: %s\n", new_name.c_str(), + strerror(errno)); + return false; + } +} + } diff --git a/libsafs/native_file.h b/libsafs/native_file.h index 84fabd4e..c87e00bc 100644 --- a/libsafs/native_file.h +++ b/libsafs/native_file.h @@ -90,14 +90,7 @@ class native_file return file_name; } - virtual bool rename(const std::string &new_name) { - if (::rename(file_name.c_str(), new_name.c_str()) == 0) { - file_name = new_name; - return true; - } - else - return false; - } + virtual bool rename(const std::string &new_name); /** * Create/delete a file on the native file system. diff --git a/libsafs/safs_file.cpp b/libsafs/safs_file.cpp index 8b7ac475..a27f8d73 100644 --- a/libsafs/safs_file.cpp +++ b/libsafs/safs_file.cpp @@ -105,8 +105,11 @@ ssize_t safs_file::get_size() const bool safs_file::rename(const std::string &new_name) { - if (!exist()) + if (!exist()) { + fprintf(stderr, "can't rename: the new name %s exists\n", + new_name.c_str()); return false; + } for (unsigned i = 0; i < native_dirs.size(); i++) { native_file f(native_dirs[i].get_file_name());