Update copyright dates in source files.
[cumulus.git] / remote.cc
index 1e77333..92b1f89 100644 (file)
--- a/remote.cc
+++ b/remote.cc
@@ -1,5 +1,5 @@
 /* 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
@@ -14,6 +14,7 @@
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/wait.h>
 
 #include <list>
 #include <string>
@@ -66,13 +67,13 @@ RemoteStore::~RemoteStore()
  * 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
@@ -144,7 +145,32 @@ void RemoteStore::transfer_thread()
 
         // 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;
@@ -152,9 +178,11 @@ void RemoteStore::transfer_thread()
 }
 
 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;