/* LBS: An LFS-inspired filesystem backup system
- * Copyright (C) 2006 Michael Vrable
+ * Copyright (C) 2008 Michael Vrable
*
* Backup data (segments and backup descriptors) may be stored on a remote
* fileserver instead of locally. The only local storage needed is for the
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/wait.h>
#include <list>
#include <string>
* will initially be created in a temporary directory. When the file is
* written out, the RemoteFile object should be passed to RemoteStore::enqueue,
* which will upload it to the remote server. */
-RemoteFile *RemoteStore::alloc_file(const string &name)
+RemoteFile *RemoteStore::alloc_file(const string &name, const string &type)
{
fprintf(stderr, "Allocate file: %s\n", name.c_str());
pthread_mutex_lock(&lock);
files_outstanding++;
pthread_mutex_unlock(&lock);
- return new RemoteFile(this, name, staging_dir + "/" + name);
+ return new RemoteFile(this, name, type, staging_dir + "/" + name);
}
/* Request that a file be transferred to the remote server. The actual
// Transfer the file
fprintf(stderr, "Start transfer: %s\n", file->remote_path.c_str());
- // TODO
+ if (backup_script != "") {
+ pid_t pid = fork();
+ if (pid < 0) {
+ fprintf(stderr, "Unable to fork for upload script: %m\n");
+ throw IOException("fork: upload script");
+ }
+ if (pid == 0) {
+ string cmd = backup_script;
+ cmd += " " + file->local_path + " " + file->type + " "
+ + file->remote_path;
+ execlp("/bin/sh", "/bin/sh", "-c", cmd.c_str(), NULL);
+ throw IOException("exec failed");
+ }
+
+ int status = 0;
+ waitpid(pid, &status, 0);
+ if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
+ fprintf(stderr, "Warning: error code from upload script: %d\n",
+ status);
+ }
+
+ if (unlink(file->local_path.c_str()) < 0) {
+ fprintf(stderr, "Warning: Deleting temporary file %s: %m\n",
+ file->local_path.c_str());
+ }
+ }
fprintf(stderr, "Finish transfer: %s\n", file->remote_path.c_str());
delete file;
}
RemoteFile::RemoteFile(RemoteStore *remote,
- const string &name, const string &local_path)
+ const string &name, const string &type,
+ const string &local_path)
{
remote_store = remote;
+ this->type = type;
this->local_path = local_path;
this->remote_path = name;