X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=util.cc;h=76efe0f35e7226b65e516c58daacf1dce4f9eca9;hb=262f66f165516f2ae3923b708d860a8711fc7c58;hp=b595eea602f1f11f9e76958afd5efe4a04501c85;hpb=915e73b3fa1f245541611f26268cdd574798e1e2;p=cumulus.git diff --git a/util.cc b/util.cc index b595eea..76efe0f 100644 --- a/util.cc +++ b/util.cc @@ -1,7 +1,6 @@ -/* Cumulus: Smart Filesystem Backup to Dumb Servers - * - * Copyright (C) 2007 The Regents of the University of California - * Written by Michael Vrable +/* Cumulus: Efficient Filesystem Backup to the Cloud + * Copyright (C) 2007-2008 The Cumulus Developers + * See the AUTHORS file for a list of contributors. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,6 +22,8 @@ #include #include +#include +#include #include #include @@ -30,6 +31,8 @@ #include #include +#include "util.h" + using std::map; using std::ostream; using std::string; @@ -115,3 +118,42 @@ long long parse_int(const string &s) { return strtoll(s.c_str(), NULL, 0); } + +/* Mark a file descriptor as close-on-exec. */ +void cloexec(int fd) +{ + long flags = fcntl(fd, F_GETFD); + + if (flags < 0) + return; + + fcntl(fd, F_SETFD, flags | FD_CLOEXEC); +} + +/* Report a fatal error and exit. */ +void fatal(string msg) +{ + fprintf(stderr, "FATAL: %s\n", msg.c_str()); + exit(1); +} + +/* Available time formats. */ +const char TimeFormat::FORMAT_FILENAME[] = "%Y%m%dT%H%M%S"; +const char TimeFormat::FORMAT_ISO8601[] = "%Y-%m-%d %H:%M:%S"; +const char TimeFormat::FORMAT_LOCALTIME[] = "%Y-%m-%d %H:%M:%S %z"; + +static size_t MAX_TIMESTAMP_LENGTH = 1024; + +std::string TimeFormat::format(time_t timestamp, const char *format, bool utc) +{ + struct tm time_buf; + + if (utc) + gmtime_r(×tamp, &time_buf); + else + localtime_r(×tamp, &time_buf); + + char buffer[MAX_TIMESTAMP_LENGTH]; + strftime(buffer, MAX_TIMESTAMP_LENGTH, format, &time_buf); + return string(buffer); +}