Browse Source

libtransport: get rid of dodgy filesystem manipulation code

master
eta 3 years ago
parent
commit
a16e75ef40
  1. 4
      include/transport/Util.h
  2. 60
      libtransport/Util.cpp

4
include/transport/Util.h

@ -35,10 +35,6 @@ class Config; @@ -35,10 +35,6 @@ class Config;
namespace Util {
void createDirectories(Transport::Config *config, const boost::filesystem::path &ph);
void removeEverythingOlderThan(const std::vector<std::string> &dirs, time_t t);
int getRandomPort(const std::string &s);
std::string char2hex(char dec);

60
libtransport/Util.cpp

@ -44,66 +44,6 @@ namespace Transport { @@ -44,66 +44,6 @@ namespace Transport {
namespace Util {
void createDirectories(Transport::Config *config, const boost::filesystem::path &ph) {
if (ph.empty() || exists(ph)) {
return;
}
// First create branch, by calling ourself recursively
createDirectories(config, ph.branch_path());
// Now that parent's path exists, create the directory
create_directory(ph);
if (!CONFIG_STRING(config, "service.group").empty() && !CONFIG_STRING(config, "service.user").empty()) {
struct group *gr;
if ((gr = getgrnam(CONFIG_STRING(config, "service.group").c_str())) == NULL) {
std::cerr << "Invalid service.group name " << CONFIG_STRING(config, "service.group") << "\n";
}
struct passwd *pw;
if ((pw = getpwnam(CONFIG_STRING(config, "service.user").c_str())) == NULL) {
std::cerr << "Invalid service.user name " << CONFIG_STRING(config, "service.user") << "\n";
}
chown(ph.string().c_str(), pw->pw_uid, gr->gr_gid);
}
}
void removeEverythingOlderThan(const std::vector<std::string> &dirs, time_t t) {
BOOST_FOREACH (const std::string &dir, dirs) {
path p(dir);
try {
if (!exists(p)) {
continue;
}
if (!is_directory(p)) {
continue;
}
directory_iterator end_itr;
for (directory_iterator itr(p); itr != end_itr; ++itr) {
if (last_write_time(itr->path()) < t) {
try {
if (is_regular(itr->path())) {
remove(itr->path());
} else if (is_directory(itr->path())) {
std::vector<std::string> nextDirs;
nextDirs.push_back(itr->path().string());
removeEverythingOlderThan(nextDirs, t);
if (boost::filesystem::is_empty(itr->path())) {
remove_all(itr->path());
}
}
} catch (const filesystem_error &ex) {
}
}
}
} catch (const filesystem_error &ex) {
}
}
}
int getRandomPort(const std::string &s) {
unsigned long r = 0;
BOOST_FOREACH (char c, s) { r += (int)c; }

Loading…
Cancel
Save