Replace boost::scoped_ptr with std::unique_ptr.
[cumulus.git] / remote.h
1 /* Cumulus: Efficient Filesystem Backup to the Cloud
2  * Copyright (C) 2008 The Cumulus Developers
3  * See the AUTHORS file for a list of contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 /* Backup data (segments and backup descriptors) may be stored on a remote
21  * fileserver instead of locally.  The only local storage needed is for the
22  * local database and some temporary space for staging files before they are
23  * transferred to the remote server.
24  *
25  * Like encryption, remote storage is handled through the use of external
26  * scripts that are called when a file is to be transferred. */
27
28 #ifndef _LBS_REMOTE_H
29 #define _LBS_REMOTE_H
30
31 #include <list>
32 #include <string>
33 #include <pthread.h>
34
35 class RemoteFile;
36
37 class RemoteStore {
38 public:
39     static const size_t MAX_QUEUE_SIZE = 4;
40
41     RemoteStore(const std::string &stagedir, const std::string &script = "");
42     ~RemoteStore();
43     RemoteFile *alloc_file(const std::string &name, const std::string &type);
44     void enqueue(RemoteFile *file);
45     void sync();
46
47 private:
48     pthread_t thread;
49     pthread_mutex_t lock;
50     pthread_cond_t cond;
51
52     std::string staging_dir, backup_script;
53     bool terminate;             // Set when thread should shut down
54     bool busy;                  // True while there are pending transfers
55     std::list<RemoteFile *> transfer_queue;
56
57     /* For error-checking purposes, track the number of files which have been
58      * allocated but not yet queued to be sent.  This should be zero when the
59      * RemoteStore is destroyed. */
60     int files_outstanding;
61
62     void transfer_thread();
63     static void *start_transfer_thread(void *arg);
64 };
65
66 class RemoteFile {
67 public:
68     /* Get the file descriptor for writing to the (staging copy of the) file.
69      * The _caller_ is responsible for closing this file descriptor once all
70      * data is written, and before send() is called. */
71     int get_fd() const { return fd; }
72
73     const std::string &get_local_path() const { return local_path; }
74     const std::string &get_remote_path() const { return remote_path; }
75
76     /* Called when the file is finished--request that it be sent to the remote
77      * server.  This will delete the RemoteFile object. */
78     void send() { remote_store->enqueue(this); }
79 private:
80     friend class RemoteStore;
81
82     RemoteFile(RemoteStore *remote,
83                const std::string &name, const std::string &type,
84                const std::string &local_path);
85
86     RemoteStore *remote_store;
87
88     int fd;
89     std::string type, local_path;
90     std::string remote_path;
91 };
92
93 #endif // _LBS_REMOTE_H