summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2006-04-20 15:58:05 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2006-04-20 15:58:05 +0000
commit536c07488b6ba3cae81b58813b665b742531f64c (patch)
tree14bd7efa2674f358f0915032a417d8962ec2e342 /src
parent8203364972fede7b9a2bca09a935fee55efceb8e (diff)
To add some useful information to the exception message:
* src/SocketCore.cc : Updated messages. * src/SegmentMan.cc : * src/TorrentMan.cc : Updated messages. * src/MultiDiskWriter.cc : Updated messages. * src/Util.cc : Updated messages. * src/AbstractDiskWriter.cc : Updated messages. * src/PreAllocationDiskWriter.cc : Updated messages. * src/message.cc : Added new messages. Updated some messages. * src/TrackerWatcherCommand.h (MIN_PEERS): New definition. * src/TrackerWatcherCommand.cc (execute): Use MIN_PEERS. Updated threshold from 30 to 15. * src/AbstractDiskWriter.h (filename): New variable. * src/AbstractDiskWriter.cc (openExistingFile): Assigned filename. (createFile): Assigned filename. * src/main.cc: Updated the help message for http-auth-scheme.
Diffstat (limited to 'src')
-rw-r--r--src/AbstractDiskWriter.cc14
-rw-r--r--src/AbstractDiskWriter.h2
-rw-r--r--src/Directory.cc6
-rw-r--r--src/MultiDiskWriter.cc19
-rw-r--r--src/PreAllocationDiskWriter.cc5
-rw-r--r--src/SegmentMan.cc39
-rw-r--r--src/SocketCore.cc43
-rw-r--r--src/TorrentMan.cc61
-rw-r--r--src/TrackerWatcherCommand.cc2
-rw-r--r--src/TrackerWatcherCommand.h2
-rw-r--r--src/Util.cc15
-rw-r--r--src/main.cc5
-rw-r--r--src/message.h45
13 files changed, 157 insertions, 101 deletions
diff --git a/src/AbstractDiskWriter.cc b/src/AbstractDiskWriter.cc
index f5682a68..42f23b03 100644
--- a/src/AbstractDiskWriter.cc
+++ b/src/AbstractDiskWriter.cc
@@ -60,6 +60,7 @@ void AbstractDiskWriter::closeFile() {
}
void AbstractDiskWriter::openExistingFile(const string& filename) {
+ this->filename = filename;
File f(filename);
if(!f.isFile()) {
throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), "file not found");
@@ -71,6 +72,7 @@ void AbstractDiskWriter::openExistingFile(const string& filename) {
}
void AbstractDiskWriter::createFile(const string& filename, int addFlags) {
+ this->filename = filename;
// TODO proper filename handling needed
assert(filename.size());
// if(filename.empty()) {
@@ -83,14 +85,14 @@ void AbstractDiskWriter::createFile(const string& filename, int addFlags) {
void AbstractDiskWriter::writeDataInternal(const char* data, int len) {
if(write(fd, data, len) < 0) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_FILE_WRITE, filename.c_str(), strerror(errno));
}
}
int AbstractDiskWriter::readDataInternal(char* data, int len) {
int ret;
if((ret = read(fd, data, len)) < 0) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_FILE_READ, filename.c_str(), strerror(errno));
}
return ret;
}
@@ -103,7 +105,7 @@ string AbstractDiskWriter::sha1Sum(long long int offset, long long int length) {
char buf[BUFSIZE];
for(int i = 0; i < length/BUFSIZE; i++) {
if(BUFSIZE != readData(buf, BUFSIZE, offset)) {
- throw "error";
+ throw string("error");
}
sha1DigestUpdate(ctx, buf, BUFSIZE);
offset += BUFSIZE;
@@ -111,7 +113,7 @@ string AbstractDiskWriter::sha1Sum(long long int offset, long long int length) {
int r = length%BUFSIZE;
if(r > 0) {
if(r != readData(buf, r, offset)) {
- throw "error";
+ throw string("error");
}
sha1DigestUpdate(ctx, buf, r);
}
@@ -119,7 +121,7 @@ string AbstractDiskWriter::sha1Sum(long long int offset, long long int length) {
sha1DigestFinal(ctx, hashValue);
return Util::toHex(hashValue, 20);
} catch(string ex) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_FILE_SHA1SUM, filename.c_str(), strerror(errno));
}
#else
return "";
@@ -128,7 +130,7 @@ string AbstractDiskWriter::sha1Sum(long long int offset, long long int length) {
void AbstractDiskWriter::seek(long long int offset) {
if(offset != lseek(fd, offset, SEEK_SET)) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_FILE_SEEK, filename.c_str(), strerror(errno));
}
}
diff --git a/src/AbstractDiskWriter.h b/src/AbstractDiskWriter.h
index 02d1ff06..41c172e3 100644
--- a/src/AbstractDiskWriter.h
+++ b/src/AbstractDiskWriter.h
@@ -29,8 +29,8 @@
class AbstractDiskWriter:public DiskWriter {
protected:
+ string filename;
int fd;
-
#ifdef ENABLE_SHA1DIGEST
MessageDigestContext ctx;
#endif // ENABLE_SHA1DIGEST
diff --git a/src/Directory.cc b/src/Directory.cc
index d35e67c8..d86b41f2 100644
--- a/src/Directory.cc
+++ b/src/Directory.cc
@@ -22,6 +22,7 @@
#include "Directory.h"
#include "File.h"
#include "DlAbortEx.h"
+#include "message.h"
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
@@ -39,12 +40,11 @@ void Directory::createDir(const string& parentDir, bool recursive) const {
File f(path);
if(f.exists()) {
if(!f.isDir()) {
- throw new DlAbortEx("%s is already exists and it is not a directory.",
- path.c_str());
+ throw new DlAbortEx(EX_NOT_DIRECTORY, path.c_str());
}
} else {
if(mkdir(path.c_str(), S_IRUSR|S_IWUSR|S_IXUSR) == -1) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_MAKE_DIR, path.c_str(), strerror(errno));
}
}
if(recursive) {
diff --git a/src/MultiDiskWriter.cc b/src/MultiDiskWriter.cc
index d21cfd60..e3543fe2 100644
--- a/src/MultiDiskWriter.cc
+++ b/src/MultiDiskWriter.cc
@@ -22,6 +22,7 @@
#include "MultiDiskWriter.h"
#include "DlAbortEx.h"
#include "Util.h"
+#include "message.h"
#include <errno.h>
MultiDiskWriter::MultiDiskWriter(int pieceLength):pieceLength(pieceLength) {
@@ -82,8 +83,7 @@ void MultiDiskWriter::openExistingFile(const string& filename) {
}
}
-void MultiDiskWriter::writeData(const char* data, int len, long long int position) {
- long long int offset = position;
+void MultiDiskWriter::writeData(const char* data, int len, long long int offset) {
long long int fileOffset = offset;
bool writing = false;
int rem = len;
@@ -100,7 +100,7 @@ void MultiDiskWriter::writeData(const char* data, int len, long long int positio
}
}
if(!writing) {
- throw new DlAbortEx("offset out of range");
+ throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, offset);
}
}
@@ -119,8 +119,7 @@ int MultiDiskWriter::calculateLength(const DiskWriterEntry* entry, long long int
return length;
}
-int MultiDiskWriter::readData(char* data, int len, long long int position) {
- long long int offset = position;
+int MultiDiskWriter::readData(char* data, int len, long long int offset) {
long long int fileOffset = offset;
bool reading = false;
int rem = len;
@@ -138,7 +137,7 @@ int MultiDiskWriter::readData(char* data, int len, long long int position) {
}
}
if(!reading) {
- throw new DlAbortEx("offset out of range");
+ throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, offset);
}
return totalReadLength;
}
@@ -149,7 +148,7 @@ void MultiDiskWriter::hashUpdate(const DiskWriterEntry* entry, long long int off
char buf[BUFSIZE];
for(int i = 0; i < length/BUFSIZE; i++) {
if(BUFSIZE != entry->diskWriter->readData(buf, BUFSIZE, offset)) {
- throw "error";
+ throw string("error");
}
sha1DigestUpdate(ctx, buf, BUFSIZE);
offset += BUFSIZE;
@@ -157,7 +156,7 @@ void MultiDiskWriter::hashUpdate(const DiskWriterEntry* entry, long long int off
int r = length%BUFSIZE;
if(r > 0) {
if(r != entry->diskWriter->readData(buf, r, offset)) {
- throw "error";
+ throw string("error");
}
sha1DigestUpdate(ctx, buf, r);
}
@@ -184,13 +183,13 @@ string MultiDiskWriter::sha1Sum(long long int offset, long long int length) {
}
}
if(!reading) {
- throw new DlAbortEx("offset out of range");
+ throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, offset);
}
unsigned char hashValue[20];
sha1DigestFinal(ctx, hashValue);
return Util::toHex(hashValue, 20);
} catch(string ex) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_FILE_SHA1SUM, "", strerror(errno));
}
#else
return "";
diff --git a/src/PreAllocationDiskWriter.cc b/src/PreAllocationDiskWriter.cc
index 7bd7dc8f..c5dddea9 100644
--- a/src/PreAllocationDiskWriter.cc
+++ b/src/PreAllocationDiskWriter.cc
@@ -21,6 +21,7 @@
/* copyright --> */
#include "PreAllocationDiskWriter.h"
#include "DlAbortEx.h"
+#include "message.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -43,13 +44,13 @@ void PreAllocationDiskWriter::initAndOpenFile(const string& filename) {
int r = totalLength%bufSize;
for(long long int i = 0; i < x; i++) {
if(write(fd, buf, bufSize) < 0) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_FILE_WRITE, filename.c_str(), strerror(errno));
}
}
if(r > 0) {
seek(totalLength-r);
if(write(fd, buf, r) < 0) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_FILE_WRITE, filename.c_str(), strerror(errno));
}
}
}
diff --git a/src/SegmentMan.cc b/src/SegmentMan.cc
index 1432acbc..e76e40ad 100644
--- a/src/SegmentMan.cc
+++ b/src/SegmentMan.cc
@@ -122,8 +122,14 @@ void SegmentMan::load() {
string segFilename = getSegmentFilePath();
logger->info(MSG_LOADING_SEGMENT_FILE, segFilename.c_str());
FILE* segFile = openSegFile(segFilename, "r+");
- read(segFile);
- fclose(segFile);
+ try {
+ read(segFile);
+ fclose(segFile);
+ } catch(string ex) {
+ fclose(segFile);
+ throw new DlAbortEx(EX_SEGMENT_FILE_READ,
+ segFilename.c_str(), strerror(errno));
+ }
logger->info(MSG_LOADED_SEGMENT_FILE);
for(Segments::iterator itr = segments.begin(); itr != segments.end();
itr++) {
@@ -138,22 +144,29 @@ void SegmentMan::save() const {
string segFilename = getSegmentFilePath();
logger->info(MSG_SAVING_SEGMENT_FILE, segFilename.c_str());
FILE* segFile = openSegFile(segFilename, "w");
- if(fwrite(&totalSize, sizeof(totalSize), 1, segFile) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- for(Segments::const_iterator itr = segments.begin(); itr != segments.end(); itr++) {
- if(fwrite(&*itr, sizeof(Segment), 1, segFile) < 1) {
- throw new DlAbortEx(strerror(errno));
+ try {
+ if(fwrite(&totalSize, sizeof(totalSize), 1, segFile) < 1) {
+ throw string("writeError");
+ }
+ for(Segments::const_iterator itr = segments.begin(); itr != segments.end(); itr++) {
+ if(fwrite(&*itr, sizeof(Segment), 1, segFile) < 1) {
+ throw string("writeError");
+ }
}
+ fclose(segFile);
+ logger->info(MSG_SAVED_SEGMENT_FILE);
+ } catch(string ex) {
+ fclose(segFile);
+ throw new DlAbortEx(EX_SEGMENT_FILE_WRITE,
+ segFilename.c_str(), strerror(errno));
}
- fclose(segFile);
- logger->info(MSG_SAVED_SEGMENT_FILE);
}
FILE* SegmentMan::openSegFile(const string& segFilename, const string& mode) const {
FILE* segFile = fopen(segFilename.c_str(), mode.c_str());
if(segFile == NULL) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_SEGMENT_FILE_OPEN,
+ segFilename.c_str(), strerror(errno));
}
return segFile;
}
@@ -161,13 +174,13 @@ FILE* SegmentMan::openSegFile(const string& segFilename, const string& mode) con
void SegmentMan::read(FILE* file) {
assert(file != NULL);
if(fread(&totalSize, sizeof(totalSize), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
+ throw string("readError");
}
while(1) {
Segment seg;
if(fread(&seg, sizeof(Segment), 1, file) < 1) {
if(ferror(file)) {
- throw new DlAbortEx(strerror(errno));
+ throw string("readError");
} else if(feof(file)) {
break;
}
diff --git a/src/SocketCore.cc b/src/SocketCore.cc
index 3adcc94c..9002dc1b 100644
--- a/src/SocketCore.cc
+++ b/src/SocketCore.cc
@@ -20,6 +20,9 @@
*/
/* copyright --> */
#include "SocketCore.h"
+#include "DlRetryEx.h"
+#include "DlAbortEx.h"
+#include "message.h"
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
@@ -29,10 +32,7 @@
#include <arpa/inet.h>
#include <sys/time.h>
#include <netdb.h>
-#include "DlRetryEx.h"
-#include "DlAbortEx.h"
#include <errno.h>
-#include "message.h"
SocketCore::SocketCore():sockfd(-1) {
init();
@@ -71,13 +71,13 @@ void SocketCore::beginListen(int port) {
//sockfd = socket(AF_UNSPEC, SOCK_STREAM, PF_UNSPEC);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_SOCKET_OPEN, strerror(errno));
}
socklen_t sockopt = 1;
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(socklen_t)) < 0) {
close(sockfd);
sockfd = -1;
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_SOCKET_SET_OPT, strerror(errno));
}
struct sockaddr_in sockaddr;
@@ -87,11 +87,11 @@ void SocketCore::beginListen(int port) {
sockaddr.sin_port = htons(port);
if(bind(sockfd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == -1) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_SOCKET_BIND, strerror(errno));
}
if(listen(sockfd, 1) == -1) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_SOCKET_LISTEN, strerror(errno));
}
}
@@ -101,7 +101,7 @@ SocketCore* SocketCore::acceptConnection() const {
memset((char*)&sockaddr, 0, sizeof(sockaddr));
int fd;
if((fd = accept(sockfd, (struct sockaddr*)&sockaddr, &len)) == -1) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_SOCKET_ACCEPT, strerror(errno));
}
SocketCore* s = new SocketCore(fd);
@@ -113,7 +113,7 @@ void SocketCore::getAddrInfo(pair<string, int>& addrinfo) const {
memset((char*)&listenaddr, 0, sizeof(listenaddr));
socklen_t len = sizeof(listenaddr);
if(getsockname(sockfd, (struct sockaddr*)&listenaddr, &len) == -1) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_SOCKET_GET_NAME, strerror(errno));
}
addrinfo.first = inet_ntoa(listenaddr.sin_addr);
addrinfo.second = ntohs(listenaddr.sin_port);
@@ -124,7 +124,7 @@ void SocketCore::getPeerInfo(pair<string, int>& peerinfo) const {
memset(&peerin, 0, sizeof(peerin));
int len = sizeof(peerin);
if(getpeername(sockfd, (struct sockaddr*)&peerin, (socklen_t*)&len) < 0) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_SOCKET_GET_PEER, strerror(errno));
}
peerinfo.first = inet_ntoa(peerin.sin_addr);
peerinfo.second = ntohs(peerin.sin_port);
@@ -134,13 +134,13 @@ void SocketCore::establishConnection(const string& host, int port) {
closeConnection();
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_SOCKET_OPEN, strerror(errno));
}
socklen_t sockopt = 1;
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(socklen_t)) < 0) {
close(sockfd);
sockfd = -1;
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_SOCKET_SET_OPT, strerror(errno));
}
struct sockaddr_in sockaddr;
@@ -159,7 +159,8 @@ void SocketCore::establishConnection(const string& host, int port) {
struct addrinfo* res;
int ec;
if((ec = getaddrinfo(host.c_str(), NULL, &ai, &res)) != 0) {
- throw new DlAbortEx(gai_strerror(ec));
+ throw new DlAbortEx(EX_RESOLVE_HOSTNAME,
+ host.c_str(), gai_strerror(ec));
}
sockaddr.sin_addr = ((struct sockaddr_in*)res->ai_addr)->sin_addr;
freeaddrinfo(res);
@@ -168,7 +169,7 @@ void SocketCore::establishConnection(const string& host, int port) {
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags|O_NONBLOCK);
if(connect(sockfd, (struct sockaddr*)&sockaddr, (socklen_t)sizeof(sockaddr)) == -1 && errno != EINPROGRESS) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_SOCKET_CONNECT, host.c_str(), strerror(errno));
}
}
@@ -227,7 +228,7 @@ bool SocketCore::isWritable(int timeout) const {
if(errno == EINPROGRESS) {
return false;
} else {
- throw new DlRetryEx(strerror(errno));
+ throw new DlRetryEx(EX_SOCKET_CHECK_WRITABLE, strerror(errno));
}
}
}
@@ -256,7 +257,7 @@ bool SocketCore::isReadable(int timeout) const {
if(errno == EINPROGRESS) {
return false;
} else {
- throw new DlRetryEx(strerror(errno));
+ throw new DlRetryEx(EX_SOCKET_CHECK_READABLE, strerror(errno));
}
}
}
@@ -283,7 +284,7 @@ void SocketCore::writeData(const char* data, int len, int timeout) {
#else // HAVE_LIBGNUTLS
errorMsg = strerror(errno);
#endif
- throw new DlRetryEx(errorMsg);
+ throw new DlRetryEx(EX_SOCKET_SEND, errorMsg);
}
}
@@ -309,7 +310,7 @@ void SocketCore::readData(char* data, int& len, int timeout) {
#else // HAVE_LIBGNUTLS
errorMsg = strerror(errno);
#endif
- throw new DlRetryEx(errorMsg);
+ throw new DlRetryEx(EX_SOCKET_RECV, errorMsg);
}
len = ret;
}
@@ -336,7 +337,7 @@ void SocketCore::peekData(char* data, int& len, int timeout) {
#else // HAVE_LIBGNUTLS
errorMsg = strerror(errno);
#endif
- throw new DlRetryEx(errorMsg);
+ throw new DlRetryEx(EX_SOCKET_PEEK, errorMsg);
}
len = ret;
}
@@ -377,7 +378,7 @@ int SocketCore::gnutlsRecv(char* data, int len) {
if(plen < len) {
int ret = gnutls_record_recv(sslSession, data+plen, len-plen);
if(ret < 0) {
- throw new DlRetryEx(gnutls_strerror(ret));
+ throw new DlRetryEx(EX_SOCKET_RECV, gnutls_strerror(ret));
}
return plen+ret;
} else {
@@ -393,7 +394,7 @@ int SocketCore::gnutlsPeek(char* data, int len) {
memcpy(data, peekBuf, peekBufLength);
int ret = gnutls_record_recv(sslSession, data+peekBufLength, len-peekBufLength);
if(ret < 0) {
- throw new DlRetryEx(gnutls_strerror(ret));
+ throw new DlRetryEx(EX_SOCKET_PEEK, gnutls_strerror(ret));
}
addPeekData(data+peekBufLength, ret);
return peekBufLength;
diff --git a/src/TorrentMan.cc b/src/TorrentMan.cc
index 1713a86d..48c1b863 100644
--- a/src/TorrentMan.cc
+++ b/src/TorrentMan.cc
@@ -376,7 +376,7 @@ void TorrentMan::setup(const string& metaInfoFile, const Strings& targetFilePath
pieces = totalLength/pieceLength+(totalLength%pieceLength ? 1 : 0);
Data* piecesHashData = (Data*)infoDic->get("pieces");
if(piecesHashData->getLen() != pieces*20) {
- throw new DlAbortEx("the number of pieces is wrong.");
+ throw new DlAbortEx("The number of pieces is wrong.");
}
for(int index = 0; index < pieces; index++) {
string hex = Util::toHex((unsigned char*)&piecesHashData->getData()[index*20], 20);
@@ -418,7 +418,7 @@ void TorrentMan::setFileFilter(const Strings& filePaths) {
for(Strings::const_iterator pitr = filePaths.begin();
pitr != filePaths.end(); pitr++) {
if(!diskAdaptor->addDownloadEntry(*pitr)) {
- throw new DlAbortEx("no such file entry <%s>", (*pitr).c_str());
+ throw new DlAbortEx("No such file entry %s", (*pitr).c_str());
}
FileEntry fileEntry = diskAdaptor->getFileEntryFromPath(*pitr);
bitfield->addFilter(fileEntry.offset, fileEntry.length);
@@ -469,7 +469,8 @@ bool TorrentMan::segmentFileExists() const {
FILE* TorrentMan::openSegFile(const string& segFilename, const string& mode) const {
FILE* segFile = fopen(segFilename.c_str(), mode.c_str());
if(segFile == NULL) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_SEGMENT_FILE_OPEN,
+ segFilename.c_str(), strerror(errno));
}
return segFile;
}
@@ -478,8 +479,14 @@ void TorrentMan::load() {
string segFilename = getSegmentFilePath();
logger->info(MSG_LOADING_SEGMENT_FILE, segFilename.c_str());
FILE* segFile = openSegFile(segFilename, "r+");
- read(segFile);
- fclose(segFile);
+ try {
+ read(segFile);
+ fclose(segFile);
+ } catch(string ex) {
+ fclose(segFile);
+ throw new DlAbortEx(EX_SEGMENT_FILE_READ,
+ segFilename.c_str(), strerror(errno));
+ }
logger->info(MSG_LOADED_SEGMENT_FILE);
}
@@ -487,27 +494,27 @@ void TorrentMan::read(FILE* file) {
assert(file != NULL);
unsigned char savedInfoHash[INFO_HASH_LENGTH];
if(fread(savedInfoHash, INFO_HASH_LENGTH, 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
+ throw string("readError");
}
if(Util::toHex(savedInfoHash, INFO_HASH_LENGTH) != Util::toHex(infoHash, INFO_HASH_LENGTH)) {
- throw new DlAbortEx("info hash mismatch");
+ throw new DlAbortEx("Incorrect infoHash.");
}
unsigned char* savedBitfield = new unsigned char[bitfield->getBitfieldLength()];
try {
if(fread(savedBitfield, bitfield->getBitfieldLength(), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
+ throw string("readError");
}
setBitfield(savedBitfield, bitfield->getBitfieldLength());
if(fread(&downloadLength, sizeof(downloadLength), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
+ throw string("readError");
}
if(fread(&uploadLength, sizeof(uploadLength), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
+ throw string("readError");
}
preDownloadLength = downloadLength;
preUploadLength = uploadLength;
delete [] savedBitfield;
- } catch(Exception* ex) {
+ } catch(...) {
delete [] savedBitfield;
throw;
}
@@ -520,20 +527,26 @@ void TorrentMan::save() const {
string segFilename = getSegmentFilePath();
logger->info(MSG_SAVING_SEGMENT_FILE, segFilename.c_str());
FILE* file = openSegFile(segFilename, "w");
- if(fwrite(infoHash, INFO_HASH_LENGTH, 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- if(fwrite(bitfield->getBitfield(), bitfield->getBitfieldLength(), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- if(fwrite(&downloadLength, sizeof(downloadLength), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- if(fwrite(&uploadLength, sizeof(uploadLength), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
+ try {
+ if(fwrite(infoHash, INFO_HASH_LENGTH, 1, file) < 1) {
+ throw string("writeError");
+ }
+ if(fwrite(bitfield->getBitfield(), bitfield->getBitfieldLength(), 1, file) < 1) {
+ throw string("writeError");
+ }
+ if(fwrite(&downloadLength, sizeof(downloadLength), 1, file) < 1) {
+ throw string("writeError");
+ }
+ if(fwrite(&uploadLength, sizeof(uploadLength), 1, file) < 1) {
+ throw string("writeError");
+ }
+ fclose(file);
+ logger->info(MSG_SAVED_SEGMENT_FILE);
+ } catch(string ex) {
+ fclose(file);
+ throw new DlAbortEx(EX_SEGMENT_FILE_WRITE,
+ segFilename.c_str(), strerror(errno));
}
- fclose(file);
- logger->info(MSG_SAVED_SEGMENT_FILE);
}
void TorrentMan::remove() const {
diff --git a/src/TrackerWatcherCommand.cc b/src/TrackerWatcherCommand.cc
index aaeb5258..a005c882 100644
--- a/src/TrackerWatcherCommand.cc
+++ b/src/TrackerWatcherCommand.cc
@@ -32,7 +32,7 @@ TrackerWatcherCommand::TrackerWatcherCommand(int cuid,
TrackerWatcherCommand::~TrackerWatcherCommand() {}
bool TrackerWatcherCommand::execute() {
- if(e->torrentMan->trackers == 0 && e->torrentMan->connections < 30) {
+ if(e->torrentMan->trackers == 0 && e->torrentMan->connections < MIN_PEERS) {
e->torrentMan->req->resetTryCount();
if(e->torrentMan->downloadComplete()) {
diff --git a/src/TrackerWatcherCommand.h b/src/TrackerWatcherCommand.h
index 1151a7b0..14625a9f 100644
--- a/src/TrackerWatcherCommand.h
+++ b/src/TrackerWatcherCommand.h
@@ -25,6 +25,8 @@
#include "Command.h"
#include "TorrentDownloadEngine.h"
+#define MIN_PEERS 15
+
class TrackerWatcherCommand : public Command {
private:
TorrentDownloadEngine* e;
diff --git a/src/Util.cc b/src/Util.cc
index d1405a61..f7d74029 100644
--- a/src/Util.cc
+++ b/src/Util.cc
@@ -22,6 +22,7 @@
#include "Util.h"
#include "DlAbortEx.h"
#include "File.h"
+#include "message.h"
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -210,32 +211,32 @@ void Util::rangedFileCopy(const string& dest, const string& src, long long int s
int srcFd = -1;
try {
if((destFd = open(dest.c_str(), O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR)) == -1) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_FILE_OPEN, dest.c_str(), strerror(errno));
}
if((srcFd = open(src.c_str(), O_RDONLY, S_IRUSR|S_IWUSR)) == -1) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_FILE_OPEN, src.c_str(), strerror(errno));
}
if(lseek(srcFd, srcOffset, SEEK_SET) != srcOffset) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_FILE_SEEK, src.c_str(), strerror(errno));
}
int x = length/bufSize;
int r = length%bufSize;
for(int i = 0; i < x; i++) {
int readLength;
if((readLength = read(srcFd, buf, bufSize)) == -1 || readLength != bufSize) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_FILE_READ, src.c_str(), strerror(errno));
}
if(write(destFd, buf, readLength) == -1) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_FILE_WRITE, dest.c_str(), strerror(errno));
}
}
if(r > 0) {
int readLength;
if((readLength = read(srcFd, buf, r)) == -1 || readLength != r) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_FILE_READ, src.c_str(), strerror(errno));
}
if(write(destFd, buf, r) == -1) {
- throw new DlAbortEx(strerror(errno));
+ throw new DlAbortEx(EX_FILE_WRITE, dest.c_str(), strerror(errno));
}
}
close(srcFd);
diff --git a/src/main.cc b/src/main.cc
index bb3582bd..f87c1068 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -182,9 +182,8 @@ void showUsage() {
" METHOD is either 'get' or 'tunnel'.\n"
" Default: tunnel") << endl;
cout << _(" --http-auth-scheme=SCHEME Set HTTP authentication scheme. Currently, basic\n"
- " is the only supported scheme. You MUST specify\n"
- " this option in order to use HTTP authentication\n"
- " as well as --http-user and --http-passwd.") << endl;
+ " is the only supported scheme.\n"
+ " Default: basic") << endl;
cout << _(" --referer=REFERER Set Referer. This affects to all URLs.") << endl;
cout << _(" --ftp-user=USER Set FTP user. This affects to all URLs.\n"
" Default: anonymous") << endl;
diff --git a/src/message.h b/src/message.h
index a7123d96..7da7e483 100644
--- a/src/message.h
+++ b/src/message.h
@@ -40,10 +40,10 @@
#define MSG_SEND_PEER_MESSAGE_WITH_BITFIELD "CUID#%d - To: %s:%d %s %s"
#define MSG_SEND_PEER_MESSAGE_WITH_INDEX_BEGIN_LENGTH "CUID#%d - To: %s:%d %s index=%d, begin=%d, length=%d"
#define MSG_RECEIVE_PEER_MESSAGE "CUID#%d - From: %s:%d %s"
-#define MSG_GOT_NEW_PIECE "CUID#%d - we got new piece. index=%d"
-#define MSG_GOT_WRONG_PIECE "CUID#%d - we got wrong piece. index=%d"
+#define MSG_GOT_NEW_PIECE _("CUID#%d - we got new piece. index=%d")
+#define MSG_GOT_WRONG_PIECE _("CUID#%d - we got wrong piece. index=%d")
-#define MSG_TRACKER_WARNING_MESSAGE "Tracker returned warning message: %s"
+#define MSG_TRACKER_WARNING_MESSAGE _("Tracker returned warning message: %s")
#define MSG_SEGMENT_FILE_EXISTS _("The segment file %s exists.")
#define MSG_SEGMENT_FILE_DOES_NOT_EXIST _("The segment file %s does not exist.")
@@ -54,7 +54,7 @@
#define EX_TIME_OUT _("Timeout.")
#define EX_INVALID_CHUNK_SIZE _("Invalid chunk size.")
-#define EX_TOO_LARGE_CHUNK _("Too large chunk. size = %d")
+#define EX_TOO_LARGE_CHUNK _("Too large chunk. size=%d")
#define EX_INVALID_HEADER _("Invalid header.")
#define EX_INVALID_RESPONSE _("Invalid response.")
#define EX_NO_HEADER _("No header found.")
@@ -62,16 +62,41 @@
#define EX_PROXY_CONNECTION_FAILED _("Proxy connection failed.")
#define EX_CONNECTION_FAILED _("Connection failed.")
#define EX_FILENAME_MISMATCH _("The requested filename and the previously registered one are not same. %s != %s")
-#define EX_BAD_STATUS _("The response status is not successful. status = %d")
-#define EX_TOO_LARGE_FILE _("Too large file size. size = %lld")
+#define EX_BAD_STATUS _("The response status is not successful. status=%d")
+#define EX_TOO_LARGE_FILE _("Too large file size. size=%lld")
#define EX_TRANSFER_ENCODING_NOT_SUPPORTED _("Transfer encoding %s is not supported.")
#define EX_SSL_INIT_FAILURE _("SSL initialization failed.")
#define EX_SIZE_MISMATCH _("Size mismatch %lld != %lld")
-#define EX_AUTH_FAILED "Authorization failed."
+#define EX_AUTH_FAILED _("Authorization failed.")
#define EX_GOT_EOF _("Got EOF from the server.")
-#define EX_EOF_FROM_PEER "Got EOF from peer."
-#define EX_MULFORMED_META_INFO "Mulformed meta info."
+#define EX_EOF_FROM_PEER _("Got EOF from peer.")
+#define EX_MULFORMED_META_INFO _("Mulformed meta info.")
-#define EX_FILE_OPEN "Error occurred while opening <%s>, cause: %s"
+#define EX_FILE_OPEN _("Failed to open the file %s, cause: %s")
+#define EX_FILE_WRITE _("Failed to write into the file %s, cause: %s")
+#define EX_FILE_READ _("Failed to read from the file %s, cause: %s")
+#define EX_FILE_SHA1SUM _("Failed to calculate SHA1 digest of or a part of the file %s, cause: %s")
+#define EX_FILE_SEEK _("Failed to seek the file %s, cause: %s")
+#define EX_FILE_OFFSET_OUT_OF_RANGE _("The offset is out of range, offset=%lld")
+#define EX_NOT_DIRECTORY _("%s is not a directory.")
+#define EX_MAKE_DIR _("Failed to make the directory %s, cause: %s")
+#define EX_SEGMENT_FILE_OPEN _("Failed to open the segment file %s, cause: %s")
+#define EX_SEGMENT_FILE_WRITE _("Failed to write into the segment file %s, cause: %s")
+#define EX_SEGMENT_FILE_READ _("Failed to read from the segment file %s, cause: %s")
+
+#define EX_SOCKET_OPEN _("Failed to open a socket, cause: %s")
+#define EX_SOCKET_SET_OPT _("Failed to set a socket option, cause: %s")
+#define EX_SOCKET_BIND _("Failed to bind a socket, cause: %s")
+#define EX_SOCKET_LISTEN _("Failed to listen to a socket, cause: %s")
+#define EX_SOCKET_ACCEPT _("Failed to accept a peer connection, cause: %s")
+#define EX_SOCKET_GET_NAME _("Failed to get the name of socket, cause: %s")
+#define EX_SOCKET_GET_PEER _("Failed to get the name of connected peer, cause: %s")
+#define EX_RESOLVE_HOSTNAME _("Failed to resolve the hostname %s, cause: %s")
+#define EX_SOCKET_CONNECT _("Failed to connect to the host %s, cause: %s")
+#define EX_SOCKET_CHECK_WRITABLE _("Failed to check whether the socket is writable, cause: %s")
+#define EX_SOCKET_CHECK_READABLE _("Failed to check whether the socket is readable, cause: %s")
+#define EX_SOCKET_SEND _("Failed to send data, cause: %s")
+#define EX_SOCKET_RECV _("Failed to receive data, cause: %s")
+#define EX_SOCKET_PEEK _("Failed to peek data, cause: %s")
#endif // _D_MESSAGE_H_