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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/* <!-- copyright */
/*
* aria2 - a simple utility for downloading files faster
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* copyright --> */
#include "MultiDiskWriter.h"
#include "DlAbortEx.h"
#include "Util.h"
#include "message.h"
#include <errno.h>
MultiDiskWriter::MultiDiskWriter(int pieceLength):
pieceLength(pieceLength),
ctx(DIGEST_ALGO_SHA1) {
ctx.digestInit();
}
MultiDiskWriter::~MultiDiskWriter() {
clearEntries();
ctx.digestFree();
}
void MultiDiskWriter::clearEntries() {
for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
itr != diskWriterEntries.end(); itr++) {
delete *itr;
}
diskWriterEntries.clear();
}
void MultiDiskWriter::setFileEntries(const FileEntries& fileEntries) {
clearEntries();
for(FileEntries::const_iterator itr = fileEntries.begin();
itr != fileEntries.end(); itr++) {
diskWriterEntries.push_back(new DiskWriterEntry(*itr));
}
}
void MultiDiskWriter::openFile(const string& filename) {
for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
itr != diskWriterEntries.end(); itr++) {
(*itr)->diskWriter->openFile(filename+"/"+(*itr)->fileEntry.path);
}
}
// filename is a directory which is specified by the user in the option.
void MultiDiskWriter::initAndOpenFile(const string& filename) {
for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
itr != diskWriterEntries.end(); itr++) {
(*itr)->diskWriter->initAndOpenFile(filename+"/"+(*itr)->fileEntry.path);
}
}
void MultiDiskWriter::closeFile() {
for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
itr != diskWriterEntries.end(); itr++) {
(*itr)->diskWriter->closeFile();
}
}
void MultiDiskWriter::openExistingFile(const string& filename) {
for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
itr != diskWriterEntries.end(); itr++) {
(*itr)->diskWriter->openExistingFile(filename+"/"+(*itr)->fileEntry.path);
}
}
void MultiDiskWriter::writeData(const char* data, int len, long long int offset) {
long long int fileOffset = offset;
bool writing = false;
int rem = len;
for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
itr != diskWriterEntries.end() && rem != 0; itr++) {
if(isInRange(*itr, offset) || writing) {
int writeLength = calculateLength(*itr, fileOffset, rem);
(*itr)->diskWriter->writeData(data+(len-rem), writeLength, fileOffset);
rem -= writeLength;
writing = true;
fileOffset = 0;
} else {
fileOffset -= (*itr)->fileEntry.length;
}
}
if(!writing) {
throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, offset);
}
}
bool MultiDiskWriter::isInRange(const DiskWriterEntry* entry, long long int offset) const {
return entry->fileEntry.offset <= offset &&
offset < entry->fileEntry.offset+entry->fileEntry.length;
}
int MultiDiskWriter::calculateLength(const DiskWriterEntry* entry, long long int fileOffset, int rem) const {
int length;
if(entry->fileEntry.length < fileOffset+rem) {
length = entry->fileEntry.length-fileOffset;
} else {
length = rem;
}
return length;
}
int MultiDiskWriter::readData(char* data, int len, long long int offset) {
long long int fileOffset = offset;
bool reading = false;
int rem = len;
int totalReadLength = 0;
for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
itr != diskWriterEntries.end() && rem != 0; itr++) {
if(isInRange(*itr, offset) || reading) {
int readLength = calculateLength((*itr), fileOffset, rem);
totalReadLength += (*itr)->diskWriter->readData(data+(len-rem), readLength, fileOffset);
rem -= readLength;
reading = true;
fileOffset = 0;
} else {
fileOffset -= (*itr)->fileEntry.length;
}
}
if(!reading) {
throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, offset);
}
return totalReadLength;
}
void MultiDiskWriter::hashUpdate(DiskWriterEntry* entry, long long int offset, long long int length) {
int BUFSIZE = 16*1024;
char buf[BUFSIZE];
for(int i = 0; i < length/BUFSIZE; i++) {
if(BUFSIZE != entry->diskWriter->readData(buf, BUFSIZE, offset)) {
throw string("error");
}
ctx.digestUpdate(buf, BUFSIZE);
offset += BUFSIZE;
}
int r = length%BUFSIZE;
if(r > 0) {
if(r != entry->diskWriter->readData(buf, r, offset)) {
throw string("error");
}
ctx.digestUpdate(buf, r);
}
}
string MultiDiskWriter::sha1Sum(long long int offset, long long int length) {
long long int fileOffset = offset;
bool reading = false;
int rem = length;
ctx.digestReset();
try {
for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
itr != diskWriterEntries.end() && rem != 0; itr++) {
if(isInRange(*itr, offset) || reading) {
int readLength = calculateLength((*itr), fileOffset, rem);
hashUpdate(*itr, fileOffset, readLength);
rem -= readLength;
reading = true;
fileOffset = 0;
} else {
fileOffset -= (*itr)->fileEntry.length;
}
}
if(!reading) {
throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, offset);
}
unsigned char hashValue[20];
ctx.digestFinal(hashValue);
return Util::toHex(hashValue, 20);
} catch(string ex) {
throw new DlAbortEx(EX_FILE_SHA1SUM, "", strerror(errno));
}
}
|