Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: utils/archive/reader.dart

Issue 10842002: Add the beginnings of a Dart wrapper for libarchive. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Merge Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « utils/archive/reader.c ('k') | utils/archive/utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 #library("reader");
6
7 #import("input_stream.dart");
8 #import("options.dart");
9 #import("read_request.dart");
10 #import("utils.dart");
11
12 /**
13 * A class for extracting and decompressing an archive.
14 *
15 * Each instance of this class represents a specific set of options for
16 * extracting an archive. These options can be used to create multiple input
17 * streams using the [reader.ArchiveReader.openFilename] and
18 * [reader.ArchiveReader.openMemory] methods.
19 *
20 * Before opening an archive, this needs to be configured. [filter] should be
21 * used to enable specific decompression algorithms, and [format] should be used
22 * to enable specific archive formats.
23 */
24 class ArchiveReader {
25 /**
26 * The configuration for the filter(s) to use when decompressing the contents
27 * of an archive. The precise compression used is auto-detected from among all
28 * enabled options.
29 */
30 final Filter filter;
31
32 /**
33 * The configuration for the archive format(s) to look for when extracting the
34 * contents of an archive. The format used is auto-detected from among all
35 * enabled options.
36 */
37 final Format format;
38
39 /**
40 * Options for both [filter] and [format]. See [the libarchive
41 * documentation][wiki] for a list of available options.
42 *
43 * [wiki]: https://github.com/libarchive/libarchive/wiki/ManPageArchiveReadSet Options3
44 */
45 final ArchiveOptions options;
46
47 /** Creates a new, unconfigured archive reader. */
48 ArchiveReader() : filter = new Filter._(),
49 format = new Format._(),
50 options = new ArchiveOptions();
51
52 /**
53 * Begins extracting from [file].
54 *
55 * [block_size] only needs to be specified for reading from devices that
56 * require strict I/O blocking.
57 */
58 Future<ArchiveInputStream> openFilename(String file, [int block_size=16384]) {
59 var id;
60 return _createArchive().chain((_id) {
61 id = _id;
62 return call(OPEN_FILENAME, id, [file, block_size]);
63 }).transform((_) => new ArchiveInputStream(id));
64 }
65
66 /** Begins extracting from [data], which should be a list of bytes. */
67 Future<ArchiveInputStream> openData(List<int> data) {
68 var id;
69 return _createArchive().chain((_id) {
70 id = _id;
71 return call(OPEN_MEMORY, id, [bytesForC(data)]);
72 }).transform((_) => new ArchiveInputStream(id));
73 }
74
75 /**
76 * Creates an archive struct, applies all the configuration options to it, and
77 * returns its id.
78 */
79 Future<int> _createArchive() {
80 return call(NEW, null).chain((id) {
81 if (id == 0 || id == null) {
82 throw new ArchiveException("Archive is invalid or closed.");
83 }
84 return _pushConfiguration(id).transform((_) => id);
85 });
86 }
87
88 /**
89 * Applies all configuration in this archive to the archive identified by
90 * [id]. Returns a future that completes once all the configuration is
91 * applied.
92 */
93 Future _pushConfiguration(int id) {
94 var pending = <Future>[];
95 if (filter.program != null) {
96 if (filter.programSignature != null) {
97 var signature = bytesForC(filter.programSignature);
98 pending.add(call(SUPPORT_FILTER_PROGRAM_SIGNATURE, id,
99 [filter.program, signature]));
100 } else {
101 pending.add(call(SUPPORT_FILTER_PROGRAM, id, [filter.program]));
102 }
103 } else if (filter.all) {
104 pending.add(call(SUPPORT_FILTER_ALL, id));
105 } else {
106 if (filter.bzip2) pending.add(call(SUPPORT_FILTER_BZIP2, id));
107 if (filter.compress) {
108 pending.add(call(SUPPORT_FILTER_COMPRESS, id));
109 }
110 if (filter.gzip) pending.add(call(SUPPORT_FILTER_GZIP, id));
111 if (filter.lzma) pending.add(call(SUPPORT_FILTER_LZMA, id));
112 if (filter.xz) pending.add(call(SUPPORT_FILTER_XZ, id));
113 }
114
115 if (format.all) {
116 pending.add(call(SUPPORT_FORMAT_ALL, id));
117 } else {
118 if (format.ar) pending.add(call(SUPPORT_FORMAT_AR, id));
119 if (format.cpio) pending.add(call(SUPPORT_FORMAT_CPIO, id));
120 if (format.empty) pending.add(call(SUPPORT_FORMAT_EMPTY, id));
121 if (format.iso9660) pending.add(call(SUPPORT_FORMAT_ISO9660, id));
122 if (format.mtree) pending.add(call(SUPPORT_FORMAT_MTREE, id));
123 if (format.raw) pending.add(call(SUPPORT_FORMAT_RAW, id));
124 if (format.tar) pending.add(call(SUPPORT_FORMAT_TAR, id));
125 if (format.zip) pending.add(call(SUPPORT_FORMAT_ZIP, id));
126 }
127
128 if (!filter.options.isEmpty) {
129 pending.add(
130 call(SET_FILTER_OPTIONS, id, [filter.options.serialize()]));
131 }
132
133 if (!format.options.isEmpty) {
134 pending.add(
135 call(SET_FORMAT_OPTIONS, id, [format.options.serialize()]));
136 }
137
138 if (!options.isEmpty) {
139 pending.add(call(SET_OPTIONS, id, [options.serialize()]));
140 }
141
142 return Futures.wait(pending);
143 }
144 }
145
146 /**
147 * The configuration for the filter(s) to use when decompressing the contents
148 * of an archive. The precise compression used is auto-detected from among all
149 * enabled options.
150 */
151 class Filter {
152 /**
153 * Auto-detect among all possible filters. If this is set, all other filter
154 * flags are ignored. [program] takes precedence over this.
155 */
156 bool all = false;
157
158 /**
159 * Enable [bzip2][wp] compression.
160 *
161 * [wp]: http://en.wikipedia.org/wiki/Bzip2
162 */
163 bool bzip2 = false;
164
165 /**
166 * Enable the compression used by [the `compress` utility][wp].
167 *
168 * [wp]: http://en.wikipedia.org/wiki/Compress
169 */
170 bool compress = false;
171
172 /**
173 * Enable [gzip][wp] compression.
174 *
175 * [wp]: http://en.wikipedia.org/wiki/Gzip
176 */
177 bool gzip = false;
178
179 /**
180 * Enable [lzma][wp] compression.
181 *
182 * [wp]: http://en.wikipedia.org/wiki/Lzma
183 */
184 bool lzma = false;
185
186 /**
187 * Enable [xz][wp] compression.
188 *
189 * [wp]: http://en.wikipedia.org/wiki/Xz
190 */
191 bool xz = false;
192
193 /**
194 * Compress using the command-line program `program`. If this is specified and
195 * [programSignature] is not, all other filter flags are ignored. This takes
196 * precedence over [all].
197 */
198 String program;
199
200 // TODO(nweiz): allow multiple programs with signatures to be specified.
201 /**
202 * If set, `program` will be applied only to files whose initial bytes match
203 * [programSignature].
204 */
205 List<int> programSignature;
206
207 /**
208 * Options for individual filters. See [the libarchive documentation][wiki]
209 * for a list of available options.
210 *
211 * [wiki]: https://github.com/libarchive/libarchive/wiki/ManPageArchiveReadSet Options3
212 */
213 final ArchiveOptions options;
214
215 Filter._() : options = new ArchiveOptions();
216 }
217
218 /**
219 * The configuration for the archive format(s) to look for when extracting the
220 * contents of an archive. The format used is auto-detected from among all
221 * enabled options.
222 */
223 class Format {
224 /**
225 * Auto-detect among all possible formats. If this is set, all other format
226 * flags are ignored.
227 */
228 bool all = false;
229
230 /**
231 * Enable the [ar][wp] format.
232 *
233 * [wp]: http://en.wikipedia.org/wiki/Ar_(Unix)
234 */
235 bool ar = false;
236
237 /**
238 * Enable the [cpio][wp] format.
239 *
240 * [wp]: http://en.wikipedia.org/wiki/Cpio
241 */
242 bool cpio = false;
243
244 /** Enable treating empty files as archives with no entries. */
245 bool empty = false;
246
247 /**
248 * Enable the [ISO 9660][wp] format.
249 *
250 * [wp]: http://en.wikipedia.org/wiki/ISO_9660
251 */
252 bool iso9660 = false;
253
254 /**
255 * Enable the [mtree][wiki] format.
256 *
257 * [wiki]: https://github.com/libarchive/libarchive/wiki/ManPageMtree5
258 */
259 bool mtree = false;
260
261 /** Enable treating unknown files as archives containing a single file. */
262 bool raw = false;
263
264 /**
265 * Enable the [tar][wp] format.
266 *
267 * [wp]: http://en.wikipedia.org/wiki/Tar_(file_format)
268 */
269 bool tar = false;
270
271 /**
272 * Enable the [zip][wp] format.
273 *
274 * [wp]: http://en.wikipedia.org/wiki/ZIP_(file_format)
275 */
276 bool zip = false;
277
278
279 /**
280 * Options for individual formats. See [the libarchive documentation][wiki]
281 * for a list of available options.
282 *
283 * [wiki]: https://github.com/libarchive/libarchive/wiki/ManPageArchiveReadSet Options3
284 */
285 final ArchiveOptions options;
286
287 Format._() : options = new ArchiveOptions();
288 }
OLDNEW
« no previous file with comments | « utils/archive/reader.c ('k') | utils/archive/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698