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