forked from yeyouqun/xdeltalib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconstruct.cpp
150 lines (133 loc) · 3.55 KB
/
reconstruct.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// authour:[email protected]
#include <set>
#include <string>
#include <list>
#include <algorithm>
#ifdef _WIN32
#include <windows.h>
#include <functional>
#include <hash_map>
#include <errno.h>
#else
#include <unistd.h>
#include <memory>
#include <ext/functional>
#if !defined (__CXX_11__)
#include <ext/hash_map>
#else
#include <unordered_map>
#endif
#include <memory.h>
#include <stdio.h>
#endif
#include "mytypes.h"
#include "digest.h"
#include "rw.h"
#include "rollsum.h"
#include "buffer.h"
#include "xdeltalib.h"
#include "reconstruct.h"
namespace xdelta {
void reconstructor::start_hash_stream (const std::string & fname
, const int32_t blk_len)
{
reader_ = foperator_.create_reader (fname);
if (!reader_->exist_file ()) {
foperator_.release (reader_);
reader_ = 0;
writer_ = foperator_.create_writer (fname);
}
else {
reader_->open_file ();
ftmpname_ = get_tmp_fname (fname);
writer_ = foperator_.create_writer (ftmpname_);
}
if (writer_ == 0) {
std::string errmsg = fmt_string ("Can't create file %s."
, fname.c_str ());
THROW_XDELTA_EXCEPTION_NO_ERRNO (errmsg);
}
writer_->open_file ();
fname_ = fname;
}
void reconstructor::add_block (const target_pos & tpos
, uint32_t blk_len
, uint64_t s_offset)
{
if (reader_ == 0) {
std::string errmsg = fmt_string ("Original file %s not open.", fname_.c_str ());
THROW_XDELTA_EXCEPTION_NO_ERRNO (errmsg);
}
uint64_t t_offset = tpos.t_offset + tpos.index * blk_len;
uint64_t offset = reader_->seek_file (t_offset, FILE_BEGIN);
if (offset != t_offset) {
std::string errmsg = fmt_string ("Can't seek file %s(%s)."
, fname_.c_str ()
, error_msg ().c_str ());
THROW_XDELTA_EXCEPTION (errmsg);
}
uint32_t ret = reader_->read_file (buff_.begin (), blk_len);
if (ret < blk_len) {
std::string errmsg = fmt_string ("Can't read file %s(%s)."
, fname_.c_str ()
, error_msg ().c_str ());
THROW_XDELTA_EXCEPTION (errmsg);
}
offset = writer_->seek_file (s_offset, FILE_BEGIN);
if (offset != s_offset) {
std::string errmsg = fmt_string ("Can't seek file %s(%s)."
, fname_.c_str ()
, error_msg ().c_str ());
THROW_XDELTA_EXCEPTION (errmsg);
}
ret = writer_->write_file (buff_.begin (), blk_len);
if (ret < blk_len) {
std::string errmsg = fmt_string ("Can't write file %s(%s)."
, fname_.c_str ()
, error_msg ().c_str ());
THROW_XDELTA_EXCEPTION (errmsg);
}
}
void reconstructor::add_block (const uchar_t * data, uint32_t blk_len, uint64_t s_offset)
{
int ret = writer_->write_file (data, blk_len);
if (ret < 0) {
std::string errmsg = fmt_string ("Can't not write file %s(%s)."
, fname_.c_str ()
, error_msg ().c_str ());
THROW_XDELTA_EXCEPTION (errmsg);
}
}
void reconstructor::end_hash_stream (uint64_t filsize)
{
if (reader_) {
reader_->close_file ();
foperator_.release (reader_);
}
if (writer_) {
writer_->close_file ();
foperator_.release (writer_);
}
if (!ftmpname_.empty ())
foperator_.rename (ftmpname_, fname_);
fname_.clear ();
ftmpname_.clear ();
}
void reconstructor::on_error (const std::string & errmsg, int errorno)
{
if (errorno != 0 && errorno == ENOENT
#ifdef _WIN32
|| errorno == ERROR_FILE_NOT_FOUND
|| errorno == ERROR_PATH_NOT_FOUND
#endif
) {
return;
}
if (reader_)
reader_->close_file ();
if (writer_)
writer_->close_file ();
if (!ftmpname_.empty ())
foperator_.rm_file (ftmpname_);
}
} //namespace xdelta