Silence -Wunused-value
[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                 int create (const std::string& srcdir);
42                 int create (const std::map <std::string, std::string>& filemap);
43
44                 PBD::Signal2<void, size_t, size_t> progress; // TODO
45
46                 struct MemPipe {
47                         public:
48                                 MemPipe ()
49                                         : data (NULL)
50                                         , progress (0)
51                                 {
52                                         pthread_mutex_init (&_lock, NULL);
53                                         pthread_cond_init (&_ready, NULL);
54                                         reset ();
55                                 }
56
57                                 ~MemPipe ()
58                                 {
59                                         lock ();
60                                         free (data);
61                                         unlock ();
62
63                                         pthread_mutex_destroy (&_lock);
64                                         pthread_cond_destroy (&_ready);
65                                 }
66
67                                 void reset ()
68                                 {
69                                         lock ();
70                                         free (data);
71                                         data = 0;
72                                         size = 0;
73                                         done = false;
74                                         processed = 0;
75                                         length = -1;
76                                         unlock ();
77                                 }
78
79                                 void lock ()   { pthread_mutex_lock (&_lock); }
80                                 void unlock () { pthread_mutex_unlock (&_lock); }
81                                 void signal () { pthread_cond_signal (&_ready); }
82                                 void wait ()   { pthread_cond_wait (&_ready, &_lock); }
83
84                                 uint8_t  buf[8192];
85                                 uint8_t* data;
86                                 size_t   size;
87                                 bool     done;
88
89                                 double   processed;
90                                 double   length;
91                                 FileArchive* progress;
92
93                         private:
94                                 pthread_mutex_t _lock;
95                                 pthread_cond_t  _ready;
96                 };
97
98                 struct Request {
99                         public:
100                                 Request (const std::string& u)
101                                 {
102                                         if (u.size () > 0) {
103                                                 url = strdup (u.c_str());
104                                         } else {
105                                                 url = NULL;
106                                         }
107                                 }
108
109                                 ~Request ()
110                                 {
111                                         free (url);
112                                 }
113
114                                 bool is_remote () const
115                                 {
116                                         if (!strncmp (url, "https://", 8) || !strncmp (url, "http://", 7) || !strncmp (url, "ftp://", 6)) {
117                                                 return true;
118                                         }
119                                         return false;
120                                 }
121
122                                 char* url;
123                                 MemPipe mp;
124                 };
125
126         private:
127
128                 int process_file ();
129                 int process_url ();
130
131                 std::vector<std::string> contents_url ();
132                 std::vector<std::string> contents_file ();
133
134                 int extract_url ();
135                 int extract_file ();
136
137                 int do_extract (struct archive* a);
138                 std::vector<std::string> get_contents (struct archive *a);
139
140                 bool is_url ();
141
142                 Request   _req;
143                 pthread_t _tid;
144 };
145
146 } /* namespace */
147 #endif // _reallocpool_h_