Initial framework for direct transfer of backups to remote storage.
[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     RemoteFile *alloc_file(const std::string &name);
28     void enqueue(RemoteFile *file);
29     void sync();
30
31 private:
32     pthread_t thread;
33     pthread_mutex_t lock;
34     pthread_cond_t cond;
35
36     std::string staging_dir;
37     bool terminate;             // Set when thread should shut down
38     bool busy;                  // True while there are pending transfers
39     std::list<RemoteFile *> transfer_queue;
40
41     /* For error-checking purposes, track the number of files which have been
42      * allocated but not yet queued to be sent.  This should be zero when the
43      * RemoteStore is destroyed. */
44     int files_outstanding;
45
46     void transfer_thread();
47     static void *start_transfer_thread(void *arg);
48 };
49
50 class RemoteFile {
51 public:
52     /* Get the file descriptor for writing to the (staging copy of the) file.
53      * The _caller_ is responsible for closing this file descriptor once all
54      * data is written, and before send() is called. */
55     int get_fd() const { return fd; }
56
57     const std::string &get_local_path() const { return local_path; }
58
59     /* Called when the file is finished--request that it be sent to the remote
60      * server.  This will delete the RemoteFile object. */
61     void send() { remote_store->enqueue(this); }
62 private:
63     friend class RemoteStore;
64
65     RemoteFile(RemoteStore *remote,
66                const std::string &name, const std::string &local_path);
67
68     RemoteStore *remote_store;
69
70     int fd;
71     std::string local_path;
72     std::string remote_path;
73 };
74
75 #endif // _LBS_REMOTE_H