Ardour  9.2-654-gd2ed0bd940
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 #if defined(COMPILER_MSVC) && defined(WAF_BUILD)
24 #include <winsock2.h> /*Not needed for file_archive itself, but prevents archive_entry.h from including winsock.h. */
25 #endif
26 
27 #include <archive.h>
28 #include <archive_entry.h>
29 #include <pthread.h>
30 
31 #include "pbd/signals.h"
32 
33 #ifndef LIBPBD_API
34 #include "pbd/libpbd_visibility.h"
35 #endif
36 
37 
38 namespace PBD {
39 class Progress;
40 
42 {
43  public:
44  FileArchive (const std::string& url, Progress* p = NULL);
46 
47  int inflate (const std::string& destdir);
48  std::vector<std::string> contents ();
49 
50  std::string next_file_name ();
51  int extract_current_file (const std::string& destpath);
52 
53  /* these are mapped to libarchive's lzmaz
54  * compression level 0..9
55  */
57  CompressNone = -1,
58  CompressFast = 0,
59  CompressGood = 6
60  };
61 
62  int create (const std::string& srcdir, CompressionLevel compression_level = CompressGood);
63  int create (const std::map <std::string, std::string>& filemap, CompressionLevel compression_level = CompressGood);
64 
65  struct MemPipe {
66  public:
68  : data (NULL)
69  , query_length (false)
70  , progress (p)
71  {
72  pthread_mutex_init (&_lock, NULL);
73  pthread_cond_init (&_ready, NULL);
74  reset ();
75  }
76 
78  {
79  lock ();
80  free (data);
81  unlock ();
82 
83  pthread_mutex_destroy (&_lock);
84  pthread_cond_destroy (&_ready);
85  }
86 
87  void reset ()
88  {
89  lock ();
90  free (data);
91  data = 0;
92  size = 0;
93  done = false;
94  processed = 0;
95  length = 0;
96  unlock ();
97  }
98 
99  void lock () { pthread_mutex_lock (&_lock); }
100  void unlock () { pthread_mutex_unlock (&_lock); }
101  void signal () { pthread_cond_signal (&_ready); }
102  void wait () { pthread_cond_wait (&_ready, &_lock); }
103 
104  uint8_t buf[8192];
105  uint8_t* data;
106  size_t size;
107  bool done;
108 
109  size_t processed;
110  size_t length;
112 
114 
115  private:
116  pthread_mutex_t _lock;
117  pthread_cond_t _ready;
118  };
119 
120  struct Request {
121  public:
122  Request (const std::string& u, Progress* p)
123  : mp (p)
124  {
125  if (u.size () > 0) {
126  url = strdup (u.c_str());
127  } else {
128  url = NULL;
129  }
130  }
131 
133  {
134  free (url);
135  }
136 
137  bool is_remote () const
138  {
139  if (!strncmp (url, "https://", 8) || !strncmp (url, "http://", 7) || !strncmp (url, "ftp://", 6)) {
140  return true;
141  }
142  return false;
143  }
144 
145  char* url;
147  };
148 
149  private:
150  int process_file ();
151  int process_url ();
152 
153  std::vector<std::string> contents_url ();
154  std::vector<std::string> contents_file ();
155 
156  int extract_url ();
157  int extract_file ();
158 
159  int do_extract (struct archive* a);
160  std::vector<std::string> get_contents (struct archive *a);
161 
162  bool is_url ();
163 
164  struct archive* setup_file_archive ();
165 
166  std::string fetch (const std::string & url, const std::string& destdir) const;
167 
169  pthread_t _tid;
170 
172 
173  struct archive_entry* _current_entry;
174  struct archive* _archive;
175 };
176 
177 } /* namespace */
178 #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:171
struct archive_entry * _current_entry
Definition: file_archive.h:173
struct archive * setup_file_archive()
struct archive * _archive
Definition: file_archive.h:174
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:122