diff options
Diffstat (limited to 'xdelta3/testing/file.h')
-rw-r--r-- | xdelta3/testing/file.h | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/xdelta3/testing/file.h b/xdelta3/testing/file.h index 9511d1c..1205bc0 100644 --- a/xdelta3/testing/file.h +++ b/xdelta3/testing/file.h | |||
@@ -3,6 +3,7 @@ namespace regtest { | |||
3 | 3 | ||
4 | class Block; | 4 | class Block; |
5 | class BlockIterator; | 5 | class BlockIterator; |
6 | class TmpFile; | ||
6 | 7 | ||
7 | class FileSpec { | 8 | class FileSpec { |
8 | public: | 9 | public: |
@@ -82,6 +83,8 @@ class FileSpec { | |||
82 | 83 | ||
83 | void PrintData() const; | 84 | void PrintData() const; |
84 | 85 | ||
86 | void WriteTmpFile(TmpFile *f) const; | ||
87 | |||
85 | typedef BlockIterator iterator; | 88 | typedef BlockIterator iterator; |
86 | 89 | ||
87 | private: | 90 | private: |
@@ -125,7 +128,9 @@ public: | |||
125 | size_ = 0; | 128 | size_ = 0; |
126 | } | 129 | } |
127 | 130 | ||
128 | void Block::Print() const; | 131 | void Print() const; |
132 | |||
133 | void WriteTmpFile(TmpFile *f) const; | ||
129 | 134 | ||
130 | private: | 135 | private: |
131 | void SetSize(size_t size) { | 136 | void SetSize(size_t size) { |
@@ -208,6 +213,39 @@ private: | |||
208 | size_t blksize_; | 213 | size_t blksize_; |
209 | }; | 214 | }; |
210 | 215 | ||
216 | class TmpFile { | ||
217 | public: | ||
218 | // TODO this is a little unportable! | ||
219 | TmpFile() { | ||
220 | static int static_counter = 0; | ||
221 | char buf[32]; | ||
222 | snprintf(buf, 32, "/tmp/regtest.%d", static_counter++); | ||
223 | filename_.append(buf); | ||
224 | main_file_init(&file_); | ||
225 | CHECK_EQ(0, main_file_open(&file_, filename_.c_str(), XO_WRITE)); | ||
226 | } | ||
227 | |||
228 | ~TmpFile() { | ||
229 | unlink(filename_.c_str()); | ||
230 | main_file_cleanup(&file_); | ||
231 | } | ||
232 | |||
233 | void Append(const Block *block) { | ||
234 | CHECK_EQ(0, main_file_write(&file_, | ||
235 | block->Data(), block->Size(), | ||
236 | "tmpfile write failed")); | ||
237 | } | ||
238 | |||
239 | const char* Name() const { | ||
240 | CHECK_EQ(0, main_file_close(&file_)); | ||
241 | return filename_.c_str(); | ||
242 | } | ||
243 | |||
244 | private: | ||
245 | string filename_; | ||
246 | mutable main_file file_; | ||
247 | }; | ||
248 | |||
211 | inline void BlockIterator::Get(Block *block) const { | 249 | inline void BlockIterator::Get(Block *block) const { |
212 | xoff_t offset = blkno_ * blksize_; | 250 | xoff_t offset = blkno_ * blksize_; |
213 | const SegmentMap &table = spec_.table_; | 251 | const SegmentMap &table = spec_.table_; |
@@ -287,5 +325,17 @@ inline void Block::Print() const { | |||
287 | DP(RINT "\n"); | 325 | DP(RINT "\n"); |
288 | } | 326 | } |
289 | 327 | ||
328 | inline void FileSpec::WriteTmpFile(TmpFile *f) const { | ||
329 | Block block; | ||
330 | for (BlockIterator iter(*this); !iter.Done(); iter.Next()) { | ||
331 | iter.Get(&block); | ||
332 | f->Append(&block); | ||
333 | } | ||
334 | } | ||
335 | |||
336 | inline void Block::WriteTmpFile(TmpFile *f) const { | ||
337 | f->Append(this); | ||
338 | } | ||
339 | |||
290 | } // namespace regtest | 340 | } // namespace regtest |
291 | 341 | ||