From 55ab4264dc3f42a4588de0cf52e8f0f88e4fd90e Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Fri, 18 Aug 2017 07:44:42 +0200 Subject: Modernize various files * range-based for loops * emplace_back instead of push_back * code style * C++ headers instead of C headers * Default operators * empty stl function --- src/filesys.cpp | 76 ++++++++++++++++++++++++++------------------------------- 1 file changed, 35 insertions(+), 41 deletions(-) (limited to 'src/filesys.cpp') diff --git a/src/filesys.cpp b/src/filesys.cpp index bd8b94aff..d965384a2 100644 --- a/src/filesys.cpp +++ b/src/filesys.cpp @@ -20,9 +20,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "filesys.h" #include "util/string.h" #include -#include -#include -#include +#include +#include +#include #include #include "log.h" #include "config.h" @@ -246,7 +246,7 @@ std::vector GetDirListing(const std::string &pathstring) If so, try stat(). */ if(isdir == -1) { - struct stat statbuf; + struct stat statbuf{}; if (stat((pathstring + "/" + node.name).c_str(), &statbuf)) continue; isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR); @@ -262,22 +262,20 @@ std::vector GetDirListing(const std::string &pathstring) bool CreateDir(const std::string &path) { int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - if(r == 0) - { + if (r == 0) { return true; } - else - { - // If already exists, return true - if(errno == EEXIST) - return true; - return false; - } + + // If already exists, return true + if (errno == EEXIST) + return true; + return false; + } bool PathExists(const std::string &path) { - struct stat st; + struct stat st{}; return (stat(path.c_str(),&st) == 0); } @@ -288,7 +286,7 @@ bool IsPathAbsolute(const std::string &path) bool IsDir(const std::string &path) { - struct stat statbuf; + struct stat statbuf{}; if(stat(path.c_str(), &statbuf)) return false; // Actually error; but certainly not a directory return ((statbuf.st_mode & S_IFDIR) == S_IFDIR); @@ -347,19 +345,19 @@ bool RecursiveDelete(const std::string &path) bool DeleteSingleFileOrEmptyDirectory(const std::string &path) { - if(IsDir(path)){ + if (IsDir(path)) { bool did = (rmdir(path.c_str()) == 0); - if(!did) - errorstream<<"rmdir errno: "< &dst) { std::vector content = GetDirListing(path); - for(unsigned int i=0; i list = GetDirListing(path); - for(unsigned int i=0; i content = fs::GetDirListing(source); - for(unsigned int i=0; i < content.size(); i++){ - std::string sourcechild = source + DIR_DELIM + content[i].name; - std::string targetchild = target + DIR_DELIM + content[i].name; - if(content[i].dir){ + for (const auto &dln : content) { + std::string sourcechild = source + DIR_DELIM + dln.name; + std::string targetchild = target + DIR_DELIM + dln.name; + if(dln.dir){ if(!fs::CopyDir(sourcechild, targetchild)){ retval = false; } @@ -526,9 +521,8 @@ bool CopyDir(const std::string &source, const std::string &target) } return retval; } - else { - return false; - } + + return false; } bool PathStartsWith(const std::string &path, const std::string &prefix) -- cgit v1.2.3