Preliminary support for external file upload scripts.
[cumulus.git] / remote.h
1 /* LBS: An LFS-inspired filesystem backup system
2  * Copyright (C) 2006  Michael Vrable
3  *
4  * Backup data (segments and backup descriptors) may be stored on a remote
5  * fileserver instead of locally.  The only local storage needed is for the
6  * local database and some temporary space for staging files before they are
7  * transferred to the remote server.
8  *
9  * Like encryption, remote storage is handled through the use of external
10  * scripts that are called when a file is to be transferred. */
11
12 #ifndef _LBS_REMOTE_H
13 #define _LBS_REMOTE_H
14
15 #include <list>
16 #include <string>
17 #include <pthread.h>
18
19 class RemoteFile;
20
21 class RemoteStore {
22 public:
23     static const size_t MAX_QUEUE_SIZE = 4;
24
25     RemoteStore(const std::string &stagedir);
26     ~RemoteStore();
27     void set_script(const std::string &script)
28         { backup_script = script; }
29     RemoteFile *alloc_file(const std::string &name);
30     void enqueue(RemoteFile *file);
31     void sync();
32
33 private:
34     pthread_t thread;
35     pthread_mutex_t lock;
36     pthread_cond_t cond;
37
38     std::string staging_dir, backup_script;
39     bool terminate;             // Set when thread should shut down
40     bool busy;                  // True while there are pending transfers
41     std::list<RemoteFile *> transfer_queue;
42
43     /* For error-checking purposes, track the number of files which have been
44      * allocated but not yet queued to be sent.  This should be zero when the
45      * RemoteStore is destroyed. */
46     int files_outstanding;
47
48     void transfer_thread();
49     static void *start_transfer_thread(void *arg);
50 };
51
52 class RemoteFile {
53 public:
54     /* Get the file descriptor for writing to the (staging copy of the) file.
55      * The _caller_ is responsible for closing this file descriptor once all
56      * data is written, and before send() is called. */
57     int get_fd() const { return fd; }
58
59     const std::string &get_local_path() const { return local_path; }
60
61     /* Called when the file is finished--request that it be sent to the remote
62      * server.  This will delete the RemoteFile object. */
63     void send() { remote_store->enqueue(this); }
64 private:
65     friend class RemoteStore;
66
67     RemoteFile(RemoteStore *remote,
68                const std::string &name, const std::string &local_path);
69
70     RemoteStore *remote_store;
71
72     int fd;
73     std::string local_path;
74     std::string remote_path;
75 };
76
77 #endif // _LBS_REMOTE_H