-/* LBS: An LFS-inspired filesystem backup system
- * Copyright (C) 2007 Michael Vrable
+/* Cumulus: Smart Filesystem Backup to Dumb Servers
*
- * Utility functions for converting various datatypes to text format (and
- * later, for parsing them back, perhaps).
+ * Copyright (C) 2007 The Regents of the University of California
+ * Written by Michael Vrable <mvrable@cs.ucsd.edu>
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
#include <uuid/uuid.h>
#include <iostream>
for (size_t i = 0; i < in.length(); i++) {
unsigned char c = in[i];
- if (c >= '+' && c < 0x7f) {
+ if (c >= '+' && c < 0x7f && c != '@') {
out += c;
} else {
char buf[4];
*output = '\0';
string result(buf);
- delete buf;
+ delete[] buf;
return result;
}
-/* Return the string representation of an integer. */
-string encode_int(long long n)
+/* Return the string representation of an integer. Will try to produce output
+ * in decimal, hexadecimal, or octal according to base, though this is just
+ * advisory. For negative numbers, will always use decimal. */
+string encode_int(long long n, int base)
{
char buf[64];
+
+ if (n >= 0 && base == 16) {
+ sprintf(buf, "0x%llx", n);
+ return buf;
+ }
+
+ if (n > 0 && base == 8) {
+ sprintf(buf, "0%llo", n);
+ return buf;
+ }
+
sprintf(buf, "%lld", n);
return buf;
}
-/* Return the string representation of an integer. */
+/* Parse the string representation of an integer. Accepts decimal, octal, and
+ * hexadecimal, just as C would (recognizes the 0 and 0x prefixes). */
long long parse_int(const string &s)
{
- return strtoll(s.c_str(), NULL, 10);
+ 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<string, string> dict)
+/* Mark a file descriptor as close-on-exec. */
+void cloexec(int fd)
{
- for (map<string, string>::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);
}