X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=util.cc;h=76efe0f35e7226b65e516c58daacf1dce4f9eca9;hb=5d386960f207f991833f1204748bc4323934f761;hp=a51fa3d744b1d702d4bbd231cb783a4d65598142;hpb=7eff3edfed612e90c5bdb02c05644e550f2270be;p=cumulus.git diff --git a/util.cc b/util.cc index a51fa3d..76efe0f 100644 --- a/util.cc +++ b/util.cc @@ -1,11 +1,29 @@ -/* LBS: An LFS-inspired filesystem backup system - * Copyright (C) 2007 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. * - * Utility functions for converting various datatypes to text format (and - * later, for parsing them back, perhaps). + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +/* Utility functions for converting various datatypes to text format (and + * later, for parsing them back, perhaps). */ + #include +#include +#include +#include #include #include @@ -13,6 +31,8 @@ #include #include +#include "util.h" + using std::map; using std::ostream; using std::string; @@ -99,12 +119,41 @@ long long parse_int(const string &s) return strtoll(s.c_str(), NULL, 0); } -/* Output a dictionary of string key/value pairs to the given output stream. - * The format is a sequence of lines of the form "key: value". */ -void dict_output(ostream &o, map dict) +/* Mark a file descriptor as close-on-exec. */ +void cloexec(int fd) { - for (map::const_iterator i = dict.begin(); - i != dict.end(); ++i) { - o << i->first << ": " << i->second << "\n"; - } + 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); }