void DeleteFileOrDirectory(const std::vector<std::string>& files) { if (files.empty()) { return; } FTS* fts; FTSENT* p; int flags = FTS_PHYSICAL | FTS_NOSTAT; size_t n = 0; char** const entries = new char*[files.size() + 1]; entries[files.size()] = 0; for (std::vector<std::string>::const_iterator i = files.begin(); i != files.end(); ++i) { char* path = new char[i->length() + 1]; ::strncpy(path, i->c_str(), i->length() + 1); entries[n++] = path; } fts = ::fts_open(entries, flags, 0); if (!fts) { base_throw(IOException, c_format("fts_open failed: %s", ::strerror(errno))); } while ((p = ::fts_read(fts)) != 0) { if (p->fts_info == FTS_ERR) { base_throw(IOException, c_format("%s: %s", p->fts_path, ::strerror(p->fts_errno))); } if (p->fts_info == FTS_D) { continue; } else if (p->fts_info == FTS_DP || p->fts_info == FTS_DNR) { if (::rmdir(p->fts_accpath) == -1) { if (errno != ENOENT) { base_throw(IOException, c_format("%s: %s", p->fts_path, ::strerror(errno))); } errno = 0; } } else { if (::unlink(p->fts_accpath) == -1) { if (errno != ENOENT) { base_throw(IOException, c_format("%s: %s", p->fts_path, ::strerror(errno))); } errno = 0; } } } for (size_t i = 0; i != files.size(); ++i) { delete[] entries[i]; } delete[] entries; }
void PollEPoll::Close(Event* event) { if (EventMask_(event) != 0) { string ev; if (InRead(event)) { ev += "r"; } if (InWrite(event)) { ev += "w"; } if (InError(event)) { ev += "e"; } base_throw(IOException, c_format("PollEPoll::Close(...) called but the file descriptor %d:%s is active", event->Descriptor(), ev.c_str())); } for (epoll_event *itr = events_, *last = events_ + waiting_events_; itr != last; ++itr) { if (itr->data.ptr == event) { itr->data.ptr = 0; } } }
std::string Clock::GetHumanReadableDuration(const unsigned int duration) { unsigned int d = duration; unsigned int days = d / 86400; d -= days * 86400; unsigned int hours = d / 3600; d -= hours * 3600; unsigned int minutes = d / 60; d -= minutes * 60; unsigned int seconds = d; return c_format("%ud %uh %um %us", days, hours, minutes, seconds); }
// Will run simple "Hello World" non-demonized webserver. int main() { ::signal(SIGHUP, signal_handler); ::signal(SIGTERM, signal_handler); ::signal(SIGINT, signal_handler); ::signal(SIGPIPE, SIG_IGN); if (GOT_STOP_SIGNAL) { goto exitpoint; } try { PAST_SCHEDULER_START = true; demo::Scheduler::Init(); demo::Scheduler::Self()->Setup(); demo::Scheduler::Self()->Start(); demo::Scheduler::Self()->Join(); demo::Scheduler::Destroy(); } catch (const IOException& e) { Exit(1, e.what()); } catch (const std::runtime_error& e) { Exit(1, c_format("Runtime error: %s.", e.what())); } catch (const std::exception& e) { Exit(1, c_format("Exception: %s.", e.what())); } catch (...) { Exit(1, "Unhandled exception. Performing planejump into another dimension..."); } exitpoint: // Prints message and removes pid only. Exit(0, "Shutdown complete."); return 0; }
void CreateDirectoryIfNotExists(const char* path) { struct stat dirstat; if (::stat(path, &dirstat) == -1) { if (errno == ENOENT) { if (::mkdir(path, 0755) == -1) { base_throw(IOException, ::strerror(errno)); } } else { base_throw(IOException, ::strerror(errno)); } } else if ((dirstat.st_mode & S_IFDIR) == 0) { base_throw(IOException, c_format("File with name '%s' prevents directory creation", path)); } }
void PollEPoll::Open(Event* event) { if (EventMask_(event) != 0) { std::string ev; if (InRead(event)) { ev += "r"; } if (InWrite(event)) { ev += "w"; } if (InError(event)) { ev += "e"; } base_throw(IOException, c_format("PollEPoll::Open(...) called but the file descriptor %d:%s is active ", event->Descriptor(), ev.c_str())); } }
string TimeVal:: fmt_unix () const { return string (c_format("%ld.%ld", tv_sec, tv_usec)); }
const std::string BaseException::where() const { return c_format("line %lu of %s", line_, file_); }