/* Store the time when the backup started, so it can be included in the
* snapshot name. */
time_t now;
- struct tm time_buf_local, time_buf_utc;
- char desc_buf[256];
time(&now);
- localtime_r(&now, &time_buf_local);
- gmtime_r(&now, &time_buf_utc);
- strftime(desc_buf, sizeof(desc_buf), "%Y%m%dT%H%M%S", &time_buf_utc);
+ string timestamp
+ = TimeFormat::format(now, TimeFormat::FORMAT_FILENAME, true);
/* Open the local database which tracks all objects that are stored
* remotely, for efficient incrementals. Provide it with the name of this
* snapshot. */
string database_path = localdb_dir + "/localdb.sqlite";
db = new LocalDb;
- db->Open(database_path.c_str(), desc_buf, backup_scheme.c_str());
+ db->Open(database_path.c_str(), timestamp.c_str(), backup_scheme.c_str());
tss = new TarSegmentStore(remote, db);
/* Initialize the stat cache, for skipping over unchanged files. */
- metawriter = new MetadataWriter(tss, localdb_dir.c_str(), desc_buf,
+ metawriter = new MetadataWriter(tss, localdb_dir.c_str(), timestamp.c_str(),
backup_scheme.c_str());
for (int i = optind; i < argc; i++) {
string checksum_filename = "snapshot-";
if (backup_scheme.size() > 0)
checksum_filename += backup_scheme + "-";
- checksum_filename = checksum_filename + desc_buf + "." + csum_type + "sums";
+ checksum_filename
+ = checksum_filename + timestamp + "." + csum_type + "sums";
RemoteFile *checksum_file = remote->alloc_file(checksum_filename,
"meta");
FILE *checksums = fdopen(checksum_file->get_fd(), "w");
string desc_filename = "snapshot-";
if (backup_scheme.size() > 0)
desc_filename += backup_scheme + "-";
- desc_filename = desc_filename + desc_buf + ".cumulus";
+ desc_filename = desc_filename + timestamp + ".cumulus";
RemoteFile *descriptor_file = remote->alloc_file(desc_filename,
"snapshots");
fprintf(descriptor, "Format: Cumulus Snapshot v0.11\n");
fprintf(descriptor, "Producer: Cumulus %s\n", cumulus_version);
- strftime(desc_buf, sizeof(desc_buf), "%Y-%m-%d %H:%M:%S %z",
- &time_buf_local);
- fprintf(descriptor, "Date: %s\n", desc_buf);
+ string timestamp_local
+ = TimeFormat::format(now, TimeFormat::FORMAT_LOCALTIME, false);
+ fprintf(descriptor, "Date: %s\n", timestamp_local.c_str());
if (backup_scheme.size() > 0)
fprintf(descriptor, "Scheme: %s\n", backup_scheme.c_str());
fprintf(descriptor, "Root: %s\n", backup_root.c_str());
#include <string>
#include <sstream>
+#include "util.h"
+
using std::map;
using std::ostream;
using std::string;
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);
+}
void fatal(std::string msg) __attribute__((noreturn));
+/* Date/time string formatting and parsing utility functions. All data and
+ * methods are static, so this class should not be instantiated. */
+class TimeFormat {
+public:
+ // Abbreviated time format encoded in snapshot file names.
+ static const char FORMAT_FILENAME[];
+ // A timestamp, in UTC, written out in an ISO 8601 format (compatible with
+ // the SQLite datetime function).
+ static const char FORMAT_ISO8601[];
+ // Similar to the above, but including a timezone offset.
+ static const char FORMAT_LOCALTIME[];
+
+ static std::string format(time_t timestamp, const char *format, bool utc);
+
+ static std::string isoformat(time_t timestamp)
+ { return format(timestamp, FORMAT_ISO8601, true); }
+
+private:
+ TimeFormat() { }
+};
+
#endif // _LBS_TARSTORE_H