Ardour  9.0-pre0-582-g084a23a80d
file_archive.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 #ifndef _pbd_archive_h_
19 #define _pbd_archive_h_
20 
21 #include <atomic>
22 
23 #include <archive.h>
24 #include <archive_entry.h>
25 #include <pthread.h>
26 
27 #include "pbd/signals.h"
28 
29 #ifndef LIBPBD_API
30 #include "pbd/libpbd_visibility.h"
31 #endif
32 
33 
34 namespace PBD {
35 class Progress;
36 
38 {
39  public:
40  FileArchive (const std::string& url, Progress* p = NULL);
42 
43  int inflate (const std::string& destdir);
44  std::vector<std::string> contents ();
45 
46  std::string next_file_name ();
47  int extract_current_file (const std::string& destpath);
48 
49  /* these are mapped to libarchive's lzmaz
50  * compression level 0..9
51  */
53  CompressNone = -1,
54  CompressFast = 0,
55  CompressGood = 6
56  };
57 
58  int create (const std::string& srcdir, CompressionLevel compression_level = CompressGood);
59  int create (const std::map <std::string, std::string>& filemap, CompressionLevel compression_level = CompressGood);
60 
61  struct MemPipe {
62  public:
64  : data (NULL)
65  , query_length (false)
66  , progress (p)
67  {
68  pthread_mutex_init (&_lock, NULL);
69  pthread_cond_init (&_ready, NULL);
70  reset ();
71  }
72 
74  {
75  lock ();
76  free (data);
77  unlock ();
78 
79  pthread_mutex_destroy (&_lock);
80  pthread_cond_destroy (&_ready);
81  }
82 
83  void reset ()
84  {
85  lock ();
86  free (data);
87  data = 0;
88  size = 0;
89  done = false;
90  processed = 0;
91  length = 0;
92  unlock ();
93  }
94 
95  void lock () { pthread_mutex_lock (&_lock); }
96  void unlock () { pthread_mutex_unlock (&_lock); }
97  void signal () { pthread_cond_signal (&_ready); }
98  void wait () { pthread_cond_wait (&_ready, &_lock); }
99 
100  uint8_t buf[8192];
101  uint8_t* data;
102  size_t size;
103  bool done;
104 
105  size_t processed;
106  size_t length;
108 
110 
111  private:
112  pthread_mutex_t _lock;
113  pthread_cond_t _ready;
114  };
115 
116  struct Request {
117  public:
118  Request (const std::string& u, Progress* p)
119  : mp (p)
120  {
121  if (u.size () > 0) {
122  url = strdup (u.c_str());
123  } else {
124  url = NULL;
125  }
126  }
127 
129  {
130  free (url);
131  }
132 
133  bool is_remote () const
134  {
135  if (!strncmp (url, "https://", 8) || !strncmp (url, "http://", 7) || !strncmp (url, "ftp://", 6)) {
136  return true;
137  }
138  return false;
139  }
140 
141  char* url;
143  };
144 
145  private:
146  int process_file ();
147  int process_url ();
148 
149  std::vector<std::string> contents_url ();
150  std::vector<std::string> contents_file ();
151 
152  int extract_url ();
153  int extract_file ();
154 
155  int do_extract (struct archive* a);
156  std::vector<std::string> get_contents (struct archive *a);
157 
158  bool is_url ();
159 
160  struct archive* setup_file_archive ();
161 
162  std::string fetch (const std::string & url, const std::string& destdir) const;
163 
165  pthread_t _tid;
166 
168 
169  struct archive_entry* _current_entry;
170  struct archive* _archive;
171 };
172 
173 } /* namespace */
174 #endif // _reallocpool_h_
std::vector< std::string > contents_file()
int inflate(const std::string &destdir)
std::vector< std::string > contents()
FileArchive(const std::string &url, Progress *p=NULL)
int do_extract(struct archive *a)
std::string next_file_name()
int extract_current_file(const std::string &destpath)
int create(const std::string &srcdir, CompressionLevel compression_level=CompressGood)
std::string fetch(const std::string &url, const std::string &destdir) const
std::vector< std::string > get_contents(struct archive *a)
Progress * _progress
Definition: file_archive.h:167
struct archive_entry * _current_entry
Definition: file_archive.h:169
struct archive * setup_file_archive()
struct archive * _archive
Definition: file_archive.h:170
int create(const std::map< std::string, std::string > &filemap, CompressionLevel compression_level=CompressGood)
std::vector< std::string > contents_url()
#define LIBPBD_API
PBD::PropertyDescriptor< timecnt_t > length
Definition: axis_view.h:42
Request(const std::string &u, Progress *p)
Definition: file_archive.h:118