OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium OS Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef VOLUME_ARCHIVE_H_ | |
6 #define VOLUME_ARCHIVE_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "volume_reader.h" | |
11 | |
12 // Defines a wrapper for operations executed on an archive. API is not meant | |
13 // to be thread safe and its methods shouldn't be called in parallel. | |
14 class VolumeArchive { | |
15 public: | |
16 explicit VolumeArchive(VolumeReader* reader) : reader_(reader) {} | |
17 | |
18 virtual ~VolumeArchive() {} | |
19 | |
20 // For functions that need to return more than pass/fail results. | |
21 enum Result { | |
22 RESULT_SUCCESS, | |
23 RESULT_EOF, | |
24 RESULT_FAIL, | |
25 }; | |
26 | |
27 // Initializes VolumeArchive. Should be called only once. | |
28 // In case of any errors call VolumeArchive::Cleanup and the error message can | |
29 // be obtained with VolumeArchive::error_message(). Encoding is the default | |
30 // encoding. Note, that other encoding may be used if specified in the | |
31 // archive file. | |
32 virtual bool Init(const std::string& encoding) = 0; | |
33 | |
34 // Gets the next header. In case of failure the error message can be | |
35 // obtained with VolumeArchive::error_message(). | |
36 virtual Result GetNextHeader() = 0; | |
37 virtual Result GetNextHeader(const char** path_name, | |
38 int64_t* size, | |
39 bool* is_directory, | |
40 time_t* modification_time) = 0; | |
41 | |
42 // Seeks to the |index|-th header. | |
43 virtual bool SeekHeader(int64_t index) = 0; | |
44 | |
45 // Gets data from offset to offset + length for the file reached with | |
46 // VolumeArchive::GetNextHeader. The data is stored in an internal buffer | |
47 // in the implementation of VolumeArchive and it will be returned | |
48 // via *buffer parameter to avoid an extra copy. *buffer is owned by | |
49 // VolumeArchive. | |
50 // | |
51 // Supports file seek by using the offset parameter. In case offset is less | |
52 // then last VolumeArchive::ReadData offset, then the read will be restarted | |
53 // from the beginning of the archive. | |
54 // | |
55 // For improving perfomance use VolumeArchive::MaybeDecompressAhead. Using | |
56 // VolumeArchive::MaybeDecompressAhead is not mandatory, but without it | |
57 // performance will suffer. | |
58 // | |
59 // The API assumes offset >= 0 and length > 0. length can be as big as | |
60 // possible, but its up to the implementation to avoid big memory usage. | |
61 // It can return up to length bytes of data, however 0 is returned only in | |
62 // case of EOF. | |
63 // | |
64 // Returns the actual number of read bytes. The API ensures that *buffer will | |
65 // have available as many bytes as returned. In case of failure, returns a | |
66 // negative value and the error message can be obtained with | |
67 // VolumeArchive::error_message(). | |
68 virtual int64_t ReadData(int64_t offset, | |
69 int64_t length, | |
70 const char** buffer) = 0; | |
71 | |
72 // Decompress ahead in case there are no more available bytes in the internal | |
73 // buffer. | |
74 virtual void MaybeDecompressAhead() = 0; | |
75 | |
76 // Cleans all resources. Should be called only once. Returns true if | |
77 // successful. In case of failure the error message can be obtained with | |
78 // VolumeArchive::error_message(). | |
79 virtual bool Cleanup() = 0; | |
80 | |
81 VolumeReader* reader() const { return reader_; } | |
82 std::string error_message() const { return error_message_; } | |
83 | |
84 protected: | |
85 // Cleans up the reader. Can be called multiple times, but once called reader | |
86 // cannot be reinitialized. | |
87 void CleanupReader() { | |
88 delete reader_; | |
89 reader_ = NULL; | |
90 } | |
91 | |
92 void set_error_message(const std::string& error_message) { | |
93 error_message_ = error_message; | |
94 } | |
95 | |
96 private: | |
97 VolumeReader* reader_; // The reader that actually reads the archive data. | |
98 std::string error_message_; // An error message set in case of any errors. | |
99 }; | |
100 | |
101 #endif // VOLUME_ARCHIVE_H_ | |
OLD | NEW |