X-Git-Url: http://git.vrable.net/?p=cumulus.git;a=blobdiff_plain;f=util.cc;h=a7479fcf3a754428edf39f56f7eebd34838aa0b7;hp=13ad9918d5a21e69dedeb3e9636bc34a99c9897f;hb=f546e4df51cde51dd984e4ad26e70dd7533f9791;hpb=90073d1fa61f5ac3f033dc6aab351b9add40dcaf diff --git a/util.cc b/util.cc index 13ad991..a7479fc 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 @@ -67,7 +85,7 @@ string uri_decode(const string &in) *output = '\0'; string result(buf); - delete buf; + delete[] buf; return result; } @@ -99,12 +117,20 @@ 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); }