Prepare support for compression levels (archive + flac)
[ardour.git] / libs / pbd / pbd / file_archive.h
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19 #ifndef _pbd_archive_h_
20 #define _pbd_archive_h_
21
22 #include <pthread.h>
23
24 #include "pbd/signals.h"
25
26 #ifndef LIBPBD_API
27 #include "pbd/libpbd_visibility.h"
28 #endif
29
30
31 namespace PBD {
32
33 class LIBPBD_API FileArchive
34 {
35         public:
36                 FileArchive (const std::string& url);
37
38                 int inflate (const std::string& destdir);
39                 std::vector<std::string> contents ();
40
41                 /* these are mapped to libarchive's lzmaz
42                  * compression level 0..9
43                  */
44                 enum CompressionLevel {
45                         CompressNone = -1,
46                         CompressFast = 0,
47                         CompressGood = 6
48                 };
49
50                 int create (const std::string& srcdir, CompressionLevel compression_level = CompressGood);
51                 int create (const std::map <std::string, std::string>& filemap, CompressionLevel compression_level = CompressGood);
52
53                 PBD::Signal2<void, size_t, size_t> progress; // TODO
54
55                 struct MemPipe {
56                         public:
57                                 MemPipe ()
58                                         : data (NULL)
59                                         , progress (0)
60                                 {
61                                         pthread_mutex_init (&_lock, NULL);
62                                         pthread_cond_init (&_ready, NULL);
63                                         reset ();
64                                 }
65
66                                 ~MemPipe ()
67                                 {
68                                         lock ();
69                                         free (data);
70                                         unlock ();
71
72                                         pthread_mutex_destroy (&_lock);
73                                         pthread_cond_destroy (&_ready);
74                                 }
75
76                                 void reset ()
77                                 {
78                                         lock ();
79                                         free (data);
80                                         data = 0;
81                                         size = 0;
82                                         done = false;
83                                         processed = 0;
84                                         length = -1;
85                                         unlock ();
86                                 }
87
88                                 void lock ()   { pthread_mutex_lock (&_lock); }
89                                 void unlock () { pthread_mutex_unlock (&_lock); }
90                                 void signal () { pthread_cond_signal (&_ready); }
91                                 void wait ()   { pthread_cond_wait (&_ready, &_lock); }
92
93                                 uint8_t  buf[8192];
94                                 uint8_t* data;
95                                 size_t   size;
96                                 bool     done;
97
98                                 double   processed;
99                                 double   length;
100                                 FileArchive* progress;
101
102                         private:
103                                 pthread_mutex_t _lock;
104                                 pthread_cond_t  _ready;
105                 };
106
107                 struct Request {
108                         public:
109                                 Request (const std::string& u)
110                                 {
111                                         if (u.size () > 0) {
112                                                 url = strdup (u.c_str());
113                                         } else {
114                                                 url = NULL;
115                                         }
116                                 }
117
118                                 ~Request ()
119                                 {
120                                         free (url);
121                                 }
122
123                                 bool is_remote () const
124                                 {
125                                         if (!strncmp (url, "https://", 8) || !strncmp (url, "http://", 7) || !strncmp (url, "ftp://", 6)) {
126                                                 return true;
127                                         }
128                                         return false;
129                                 }
130
131                                 char* url;
132                                 MemPipe mp;
133                 };
134
135         private:
136
137                 int process_file ();
138                 int process_url ();
139
140                 std::vector<std::string> contents_url ();
141                 std::vector<std::string> contents_file ();
142
143                 int extract_url ();
144                 int extract_file ();
145
146                 int do_extract (struct archive* a);
147                 std::vector<std::string> get_contents (struct archive *a);
148
149                 bool is_url ();
150
151                 Request   _req;
152                 pthread_t _tid;
153 };
154
155 } /* namespace */
156 #endif // _reallocpool_h_