summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2012-12-16 16:00:18 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2012-12-16 17:29:01 +0900
commitb9da9d4ed3ef9fe7b1488506555e3605aeb500ed (patch)
treef2b61ce65623ec222428360fe827b2a62ab9bbae /src
parent04586f50b17317ef257f6d3f81190021c93ad5ea (diff)
Use 64 bits random bytes as GID
This change replaces the current 64 bit sequential GID with 64 bits random bytes GID in an attempt to support persistent GID. Internally, the GID is stored as uint64_t. For human representation and RPC interface, GID is represented as 16 bytes hex string. For console readout, 16 bytes are too long, so it is abbreviated to first 6 bytes. When querying GID in RPC calls, user can speicfy the prefix of GID as long as the prefix is shared by more than 1 GID entries.
Diffstat (limited to 'src')
-rw-r--r--src/BtDependency.cc12
-rw-r--r--src/BtStopDownloadCommand.cc4
-rw-r--r--src/ConsoleStatCalc.cc8
-rw-r--r--src/DownloadResult.h2
-rw-r--r--src/FtpNegotiationCommand.cc2
-rw-r--r--src/GroupId.cc158
-rw-r--r--src/GroupId.h81
-rw-r--r--src/HttpResponseCommand.cc4
-rw-r--r--src/Makefile.am3
-rw-r--r--src/Metalink2RequestGroup.cc2
-rw-r--r--src/RequestGroup.cc29
-rw-r--r--src/RequestGroup.h16
-rw-r--r--src/RequestGroupMan.cc31
-rw-r--r--src/RpcMethodImpl.cc76
-rw-r--r--src/TrackerWatcherCommand.cc5
-rw-r--r--src/WebSocketSessionMan.cc2
-rw-r--r--src/download_helper.cc6
-rw-r--r--src/message.h4
-rw-r--r--src/util.cc2
19 files changed, 346 insertions, 101 deletions
diff --git a/src/BtDependency.cc b/src/BtDependency.cc
index a952feae..8862e9c3 100644
--- a/src/BtDependency.cc
+++ b/src/BtDependency.cc
@@ -155,20 +155,20 @@ bool BtDependency::resolve()
}
} catch(RecoverableException& e) {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
- A2_LOG_INFO(fmt("BtDependency for GID#%" PRId64 " failed. Go without Bt.",
- dependant_->getGID()));
+ A2_LOG_INFO(fmt("BtDependency for GID#%s failed. Go without Bt.",
+ GroupId::toHex(dependant_->getGID()).c_str()));
return true;
}
- A2_LOG_INFO(fmt("Dependency resolved for GID#%" PRId64 "",
- dependant_->getGID()));
+ A2_LOG_INFO(fmt("Dependency resolved for GID#%s",
+ GroupId::toHex(dependant_->getGID()).c_str()));
dependant_->setDownloadContext(context);
return true;
} else if(dependee_->getNumCommand() == 0) {
// dependee_'s download failed.
// cut reference here
dependee_.reset();
- A2_LOG_INFO(fmt("BtDependency for GID#%" PRId64 " failed. Go without Bt.",
- dependant_->getGID()));
+ A2_LOG_INFO(fmt("BtDependency for GID#%s failed. Go without Bt.",
+ GroupId::toHex(dependant_->getGID()).c_str()));
return true;
} else {
return false;
diff --git a/src/BtStopDownloadCommand.cc b/src/BtStopDownloadCommand.cc
index b6e28cb6..3fe63b5a 100644
--- a/src/BtStopDownloadCommand.cc
+++ b/src/BtStopDownloadCommand.cc
@@ -62,9 +62,9 @@ void BtStopDownloadCommand::preProcess()
enableExit();
}
if(checkPoint_.difference(global::wallclock()) >= timeout_) {
- A2_LOG_NOTICE(fmt(_("GID#%" PRId64 " Stop downloading torrent due to"
+ A2_LOG_NOTICE(fmt(_("GID#%s Stop downloading torrent due to"
" --bt-stop-timeout option."),
- requestGroup_->getGID()));
+ GroupId::toHex(requestGroup_->getGID()).c_str()));
requestGroup_->setForceHaltRequested(true);
getDownloadEngine()->setRefreshInterval(0);
enableExit();
diff --git a/src/ConsoleStatCalc.cc b/src/ConsoleStatCalc.cc
index 861fece8..8893fea6 100644
--- a/src/ConsoleStatCalc.cc
+++ b/src/ConsoleStatCalc.cc
@@ -146,7 +146,7 @@ void printProgressCompact(std::ostream& o, const DownloadEngine* e,
i = groups.begin(), eoi = groups.end(); i != eoi && cnt < MAX_ITEM;
++i, ++cnt) {
TransferStat stat = (*i)->calculateStat();
- o << "[#" << (*i)->getGID() << " ";
+ o << "[#" << GroupId::toAbbrevHex((*i)->getGID()) << " ";
printSizeProgress(o, *i, stat, sizeFormatter);
o << "]";
}
@@ -166,7 +166,7 @@ void printProgress
if(rg->getTotalLength() > 0 && stat.downloadSpeed > 0) {
eta = (rg->getTotalLength()-rg->getCompletedLength())/stat.downloadSpeed;
}
- o << "[#" << rg->getGID() << " ";
+ o << "[#" << GroupId::toAbbrevHex(rg->getGID()) << " ";
printSizeProgress(o, rg, stat, sizeFormatter);
o << " CN:"
<< rg->getNumConnection();
@@ -330,7 +330,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
e->getFileAllocationMan()->getPickedEntry();
if(entry) {
o << " [FileAlloc:#"
- << entry->getRequestGroup()->getGID() << " "
+ << GroupId::toAbbrevHex(entry->getRequestGroup()->getGID()) << " "
<< sizeFormatter(entry->getCurrentLength()) << "B/"
<< sizeFormatter(entry->getTotalLength()) << "B(";
if(entry->getTotalLength() > 0) {
@@ -350,7 +350,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
e->getCheckIntegrityMan()->getPickedEntry();
if(entry) {
o << " [Checksum:#"
- << entry->getRequestGroup()->getGID() << " "
+ << GroupId::toAbbrevHex(entry->getRequestGroup()->getGID()) << " "
<< sizeFormatter(entry->getCurrentLength()) << "B/"
<< sizeFormatter(entry->getTotalLength()) << "B(";
if(entry->getTotalLength() > 0) {
diff --git a/src/DownloadResult.h b/src/DownloadResult.h
index fa7e3f51..b6972ad0 100644
--- a/src/DownloadResult.h
+++ b/src/DownloadResult.h
@@ -54,7 +54,7 @@ class MetadataInfo;
struct DownloadResult
{
- a2_gid_t gid;
+ SharedHandle<GroupId> gid;
std::vector<SharedHandle<FileEntry> > fileEntries;
diff --git a/src/FtpNegotiationCommand.cc b/src/FtpNegotiationCommand.cc
index 362f94e7..6da4dc27 100644
--- a/src/FtpNegotiationCommand.cc
+++ b/src/FtpNegotiationCommand.cc
@@ -423,7 +423,7 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength)
sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED;
A2_LOG_NOTICE
(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
- getRequestGroup()->getGID(),
+ GroupId::toHex(getRequestGroup()->getGID()).c_str(),
getRequestGroup()->getFirstFilePath().c_str()));
}
poolConnection();
diff --git a/src/GroupId.cc b/src/GroupId.cc
new file mode 100644
index 00000000..a5c9d4ea
--- /dev/null
+++ b/src/GroupId.cc
@@ -0,0 +1,158 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2012 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "GroupId.h"
+#include "util.h"
+
+namespace aria2 {
+
+std::set<a2_gid_t> GroupId::set_;
+
+SharedHandle<GroupId> GroupId::create()
+{
+ a2_gid_t n;
+ for(;;) {
+ util::generateRandomData(reinterpret_cast<unsigned char*>(&n), sizeof(n));
+ if(n != 0 && set_.count(n) == 0) {
+ break;
+ }
+ }
+ SharedHandle<GroupId> res(new GroupId(n));
+ return res;
+}
+
+SharedHandle<GroupId> GroupId::import(a2_gid_t n)
+{
+ SharedHandle<GroupId> res;
+ if(n == 0 || set_.count(n) != 0) {
+ return res;
+ }
+ res.reset(new GroupId(n));
+ return res;
+}
+
+void GroupId::clear()
+{
+ set_.clear();
+}
+
+int GroupId::expandUnique(a2_gid_t& n, const char* hex)
+{
+ a2_gid_t p = 0;
+ size_t i;
+ for(i = 0; hex[i]; ++i) {
+ unsigned int c = util::hexCharToUInt(hex[i]);
+ if(c == 255) {
+ return ERR_INVALID;
+ }
+ p <<= 4;
+ p |= c;
+ }
+ if(i == 0 || i > sizeof(a2_gid_t)*2) {
+ return ERR_INVALID;
+ }
+ p <<= 64-i*4;
+ a2_gid_t mask = UINT64_MAX-((1LL << (64-i*4))-1);
+ std::set<a2_gid_t>::const_iterator itr = set_.lower_bound(p);
+ if(itr == set_.end()) {
+ return ERR_NOT_FOUND;
+ }
+ if(p == ((*itr)&mask)) {
+ n = *itr;
+ ++itr;
+ if(itr == set_.end() || p != ((*itr)&mask)) {
+ return 0;
+ } else {
+ return ERR_NOT_UNIQUE;
+ }
+ } else {
+ return ERR_NOT_FOUND;
+ }
+}
+
+int GroupId::toNumericId(a2_gid_t& n, const char* hex)
+{
+ a2_gid_t p = 0;
+ size_t i;
+ for(i = 0; hex[i]; ++i) {
+ unsigned int c = util::hexCharToUInt(hex[i]);
+ if(c == 255) {
+ return ERR_INVALID;
+ }
+ p <<= 4;
+ p |= c;
+ }
+ if(p == 0 || i != sizeof(a2_gid_t)*2) {
+ return ERR_INVALID;
+ }
+ n = p;
+ return 0;
+}
+
+std::string GroupId::toHex(a2_gid_t gid)
+{
+ a2_gid_t n = hton64(gid);
+ return util::toHex(reinterpret_cast<unsigned char*>(&n), sizeof(n));
+}
+
+std::string GroupId::toAbbrevHex(a2_gid_t gid)
+{
+ const size_t abbrevSize = 6;
+ std::string h = toHex(gid);
+ assert(h.size() >= abbrevSize);
+ return toHex(gid).erase(abbrevSize);
+}
+
+std::string GroupId::toHex() const
+{
+ return toHex(gid_);
+}
+
+std::string GroupId::toAbbrevHex() const
+{
+ return toAbbrevHex(gid_);
+}
+
+GroupId::GroupId(a2_gid_t gid)
+ : gid_(gid)
+{
+ set_.insert(gid_);
+}
+
+GroupId::~GroupId()
+{
+ set_.erase(gid_);
+}
+
+} // namespace aria2
diff --git a/src/GroupId.h b/src/GroupId.h
new file mode 100644
index 00000000..25e3c4f2
--- /dev/null
+++ b/src/GroupId.h
@@ -0,0 +1,81 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2012 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef D_GROUP_ID_H
+#define D_GROUP_ID_H
+
+#include "common.h"
+
+#include <set>
+#include <string>
+
+#include "SharedHandle.h"
+
+namespace aria2 {
+
+typedef uint64_t a2_gid_t;
+
+class GroupId {
+public:
+ static SharedHandle<GroupId> create();
+ static SharedHandle<GroupId> import(a2_gid_t n);
+ static void clear();
+ enum {
+ ERR_NOT_UNIQUE = -1,
+ ERR_NOT_FOUND = -2,
+ ERR_INVALID = -3
+ };
+ static int expandUnique(a2_gid_t& n, const char* hex);
+ static int toNumericId(a2_gid_t& n, const char* hex);
+ static std::string toHex(a2_gid_t n);
+ static std::string toAbbrevHex(a2_gid_t n);
+
+ ~GroupId();
+ a2_gid_t getNumericId() const
+ {
+ return gid_;
+ }
+ std::string toHex() const;
+ std::string toAbbrevHex() const;
+private:
+ static std::set<a2_gid_t> set_;
+
+ GroupId(a2_gid_t gid);
+
+ a2_gid_t gid_;
+};
+
+} // namespace aria2
+
+#endif // D_GROUP_ID_H
diff --git a/src/HttpResponseCommand.cc b/src/HttpResponseCommand.cc
index e85a0329..c04e5818 100644
--- a/src/HttpResponseCommand.cc
+++ b/src/HttpResponseCommand.cc
@@ -189,7 +189,7 @@ bool HttpResponseCommand::executeInternal()
getDownloadContext()->setChecksumVerified(true);
A2_LOG_NOTICE
(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
- getRequestGroup()->getGID(),
+ GroupId::toHex(getRequestGroup()->getGID()).c_str(),
getRequestGroup()->getFirstFilePath().c_str()));
poolConnection();
getFileEntry()->poolRequest(getRequest());
@@ -453,7 +453,7 @@ bool HttpResponseCommand::handleOtherEncoding
getDownloadContext()->setChecksumVerified(true);
A2_LOG_NOTICE
(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
- getRequestGroup()->getGID(),
+ GroupId::toHex(getRequestGroup()->getGID()).c_str(),
getRequestGroup()->getFirstFilePath().c_str()));
}
poolConnection();
diff --git a/src/Makefile.am b/src/Makefile.am
index 2e9217ef..656af6f5 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -242,7 +242,8 @@ SRCS = SocketCore.cc SocketCore.h\
AnonDiskWriterFactory.h\
XmlRpcRequestParserController.cc XmlRpcRequestParserController.h\
WrDiskCache.cc WrDiskCache.h\
- WrDiskCacheEntry.cc WrDiskCacheEntry.h
+ WrDiskCacheEntry.cc WrDiskCacheEntry.h\
+ GroupId.cc GroupId.h
if MINGW_BUILD
SRCS += WinConsoleFile.cc WinConsoleFile.h
diff --git a/src/Metalink2RequestGroup.cc b/src/Metalink2RequestGroup.cc
index 6f15f85e..3a282618 100644
--- a/src/Metalink2RequestGroup.cc
+++ b/src/Metalink2RequestGroup.cc
@@ -241,7 +241,7 @@ Metalink2RequestGroup::createRequestGroup
}
#endif // ENABLE_BITTORRENT
SharedHandle<Option> option = util::copy(optionTemplate);
- SharedHandle<RequestGroup> rg(new RequestGroup(option));
+ SharedHandle<RequestGroup> rg(new RequestGroup(GroupId::create(), option));
SharedHandle<DownloadContext> dctx;
int numSplit = option->getAsInt(PREF_SPLIT);
int maxConn = option->getAsInt(PREF_MAX_CONNECTION_PER_SERVER);
diff --git a/src/RequestGroup.cc b/src/RequestGroup.cc
index c296c321..1776e548 100644
--- a/src/RequestGroup.cc
+++ b/src/RequestGroup.cc
@@ -120,10 +120,9 @@
namespace aria2 {
-a2_gid_t RequestGroup::gidCounter_ = 0;
-
-RequestGroup::RequestGroup(const SharedHandle<Option>& option)
- : gid_(newGID()),
+RequestGroup::RequestGroup(const SharedHandle<GroupId>& gid,
+ const SharedHandle<Option>& option)
+ : gid_(gid),
state_(STATE_WAITING),
option_(option),
numConcurrentCommand_(option->getAsInt(PREF_SPLIT)),
@@ -244,7 +243,8 @@ SharedHandle<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
{
downloadContext_->setChecksumVerified(true);
A2_LOG_NOTICE(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
- gid_, downloadContext_->getBasePath().c_str()));
+ gid_->toHex().c_str(),
+ downloadContext_->getBasePath().c_str()));
}
} else {
checkEntry.reset(new StreamCheckIntegrityEntry(this));
@@ -346,9 +346,9 @@ void RequestGroup::createInitialCommand
btAnnouncePtr->shuffleAnnounce();
SharedHandle<BtAnnounce> btAnnounce(btAnnouncePtr);
- assert(!btRegistry->get(gid_));
+ assert(!btRegistry->get(gid_->getNumericId()));
btRegistry->put
- (gid_, SharedHandle<BtObject>
+ (gid_->getNumericId(), SharedHandle<BtObject>
(new BtObject
(downloadContext_,
pieceStorage_,
@@ -962,7 +962,7 @@ void RequestGroup::decreaseNumCommand()
{
--numCommand_;
if(!numCommand_ && requestGroupMan_) {
- A2_LOG_DEBUG(fmt("GID#%" PRId64 " - Request queue check", gid_));
+ A2_LOG_DEBUG(fmt("GID#%s - Request queue check", gid_->toHex().c_str()));
requestGroupMan_->requestQueueCheck();
}
}
@@ -1007,7 +1007,7 @@ void RequestGroup::setPauseRequested(bool f)
void RequestGroup::releaseRuntimeResource(DownloadEngine* e)
{
#ifdef ENABLE_BITTORRENT
- e->getBtRegistry()->remove(gid_);
+ e->getBtRegistry()->remove(gid_->getNumericId());
btRuntime_ = 0;
peerStorage_ = 0;
#endif // ENABLE_BITTORRENT
@@ -1152,7 +1152,8 @@ bool RequestGroup::needsFileAllocation() const
SharedHandle<DownloadResult> RequestGroup::createDownloadResult() const
{
- A2_LOG_DEBUG(fmt("GID#%" PRId64 " - Creating DownloadResult.", gid_));
+ A2_LOG_DEBUG(fmt("GID#%s - Creating DownloadResult.",
+ gid_->toHex().c_str()));
TransferStat st = calculateStat();
SharedHandle<DownloadResult> res(new DownloadResult());
res->gid = gid_;
@@ -1298,12 +1299,4 @@ bool RequestGroup::p2pInvolved() const
#endif // !ENABLE_BITTORRENT
}
-a2_gid_t RequestGroup::newGID()
-{
- if(gidCounter_ == INT64_MAX) {
- gidCounter_ = 0;
- }
- return ++gidCounter_;
-}
-
} // namespace aria2
diff --git a/src/RequestGroup.h b/src/RequestGroup.h
index 321d4d90..df35c19e 100644
--- a/src/RequestGroup.h
+++ b/src/RequestGroup.h
@@ -47,6 +47,7 @@
#include "Request.h"
#include "error_code.h"
#include "MetadataInfo.h"
+#include "GroupId.h"
namespace aria2 {
@@ -73,8 +74,6 @@ class BtRuntime;
class PeerStorage;
#endif // ENABLE_BITTORRENT
-typedef int64_t a2_gid_t;
-
class RequestGroup {
public:
enum HaltReason {
@@ -89,9 +88,7 @@ public:
STATE_ACTIVE
};
private:
- static a2_gid_t gidCounter_;
-
- a2_gid_t gid_;
+ SharedHandle<GroupId> gid_;
int state_;
@@ -201,7 +198,8 @@ private:
(const SharedHandle<BtProgressInfoFile>& progressInfoFile);
public:
- RequestGroup(const SharedHandle<Option>& option);
+ RequestGroup(const SharedHandle<GroupId>& gid,
+ const SharedHandle<Option>& option);
~RequestGroup();
@@ -264,7 +262,7 @@ public:
a2_gid_t getGID() const
{
- return gid_;
+ return gid_->getNumericId();
}
TransferStat calculateStat() const;
@@ -550,10 +548,6 @@ public:
{
state_ = state;
}
-
- static void resetGIDCounter() { gidCounter_ = 0; }
-
- static a2_gid_t newGID();
};
} // namespace aria2
diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc
index 86a26b39..30b7111c 100644
--- a/src/RequestGroupMan.cc
+++ b/src/RequestGroupMan.cc
@@ -261,7 +261,8 @@ size_t RequestGroupMan::changeReservedGroupPosition
std::deque<SharedHandle<RequestGroup> >::iterator i =
findByGID(reservedGroups_.begin(), reservedGroups_.end(), gid);
if(i == reservedGroups_.end()) {
- throw DL_ABORT_EX(fmt("GID#%" PRId64 " not found in the waiting queue.", gid));
+ throw DL_ABORT_EX(fmt("GID#%s not found in the waiting queue.",
+ GroupId::toHex(gid).c_str()));
}
SharedHandle<RequestGroup> rg = *i;
const size_t maxPos = reservedGroups_.size()-1;
@@ -401,7 +402,8 @@ public:
group->closeFile();
if(group->isPauseRequested()) {
A2_LOG_NOTICE
- (fmt(_("Download GID#%" PRId64 " paused"), group->getGID()));
+ (fmt(_("Download GID#%s paused"),
+ GroupId::toHex(group->getGID()).c_str()));
group->saveControlFile();
} else if(group->downloadFinished() &&
!group->getDownloadContext()->isChecksumVerificationNeeded()) {
@@ -429,7 +431,8 @@ public:
if(group->getOption()->getAsBool(PREF_BT_REMOVE_UNSELECTED_FILE) &&
!group->inMemoryDownload() &&
dctx->hasAttribute(CTX_ATTR_BT)) {
- A2_LOG_INFO(fmt(MSG_REMOVING_UNSELECTED_FILE, group->getGID()));
+ A2_LOG_INFO(fmt(MSG_REMOVING_UNSELECTED_FILE,
+ GroupId::toHex(group->getGID()).c_str()));
const std::vector<SharedHandle<FileEntry> >& files =
dctx->getFileEntries();
for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
@@ -447,8 +450,8 @@ public:
#endif // ENABLE_BITTORRENT
} else {
A2_LOG_NOTICE
- (fmt(_("Download GID#%" PRId64 " not complete: %s"),
- group->getGID(),
+ (fmt(_("Download GID#%s not complete: %s"),
+ GroupId::toHex(group->getGID()).c_str(),
group->getDownloadContext()->getBasePath().c_str()));
group->saveControlFile();
}
@@ -760,24 +763,20 @@ const char* getStatusStr(DownloadStatus status, bool useColor)
void RequestGroupMan::showDownloadResults(OutputFile& o, bool full) const
{
-#ifdef __MINGW32__
- int pathRowSize = 58;
-#else // !__MINGW32__
- int pathRowSize = 59;
-#endif // !__MINGW32__
+ int pathRowSize = 55;
// Download Results:
// idx|stat|path/length
// ===+====+=======================================================================
o.printf("\n%s"
- "\ngid|stat|avg speed |",
+ "\ngid |stat|avg speed |",
_("Download Results:"));
if(full) {
o.write(" %|path/URI"
- "\n===+====+===========+===+");
+ "\n======+====+===========+===+");
pathRowSize -= 4;
} else {
o.write("path/URI"
- "\n===+====+===========+");
+ "\n======+====+===========+");
}
std::string line(pathRowSize, '=');
o.printf("%s\n", line.c_str());
@@ -837,7 +836,7 @@ void formatDownloadResultCommon
const char* status,
const SharedHandle<DownloadResult>& downloadResult)
{
- o << std::setw(3) << downloadResult->gid << "|"
+ o << std::setw(3) << downloadResult->gid->toAbbrevHex() << "|"
<< std::setw(4) << status << "|"
<< std::setw(11);
if(downloadResult->sessionTime > 0) {
@@ -975,7 +974,7 @@ RequestGroupMan::findDownloadResult(a2_gid_t gid) const
{
for(std::deque<SharedHandle<DownloadResult> >::const_iterator i =
downloadResults_.begin(), eoi = downloadResults_.end(); i != eoi; ++i) {
- if((*i)->gid == gid) {
+ if((*i)->gid->getNumericId() == gid) {
return *i;
}
}
@@ -986,7 +985,7 @@ bool RequestGroupMan::removeDownloadResult(a2_gid_t gid)
{
for(std::deque<SharedHandle<DownloadResult> >::iterator i =
downloadResults_.begin(), eoi = downloadResults_.end(); i != eoi; ++i) {
- if((*i)->gid == gid) {
+ if((*i)->gid->getNumericId() == gid) {
downloadResults_.erase(i);
return true;
}
diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc
index 88940ba4..d93e3c8e 100644
--- a/src/RpcMethodImpl.cc
+++ b/src/RpcMethodImpl.cc
@@ -148,7 +148,7 @@ const std::string KEY_NUM_ACTIVE = "numActive";
namespace {
SharedHandle<ValueBase> createGIDResponse(a2_gid_t gid)
{
- return String::g(util::itos(gid));
+ return String::g(GroupId::toHex(gid));
}
} // namespace
@@ -185,12 +185,19 @@ namespace {
a2_gid_t str2Gid(const String* str)
{
assert(str);
- int64_t gid;
- if(util::parseLLIntNoThrow(gid, str->s())) {
- return gid;
- } else {
- throw DL_ABORT_EX(fmt("Bad GID %s", str->s().c_str()));
- }
+ if(str->s().size() > sizeof(a2_gid_t)*2) {
+ throw DL_ABORT_EX(fmt("Invalid GID %s", str->s().c_str()));
+ }
+ a2_gid_t n;
+ switch(GroupId::expandUnique(n, str->s().c_str())) {
+ case GroupId::ERR_NOT_UNIQUE:
+ throw DL_ABORT_EX(fmt("GID %s is not unique", str->s().c_str()));
+ case GroupId::ERR_NOT_FOUND:
+ throw DL_ABORT_EX(fmt("GID %s is not found", str->s().c_str()));
+ case GroupId::ERR_INVALID:
+ throw DL_ABORT_EX(fmt("Invalid GID %s", str->s().c_str()));
+ }
+ return n;
}
} // namespace
@@ -364,7 +371,7 @@ SharedHandle<ValueBase> AddMetalinkRpcMethod::process
}
for(std::vector<SharedHandle<RequestGroup> >::const_iterator i =
result.begin(), eoi = result.end(); i != eoi; ++i) {
- gids->append(util::itos((*i)->getGID()));
+ gids->append(GroupId::toHex((*i)->getGID()));
}
}
return gids;
@@ -391,12 +398,13 @@ SharedHandle<ValueBase> removeDownload
if(group->isDependencyResolved()) {
e->getRequestGroupMan()->removeReservedGroup(gid);
} else {
- throw DL_ABORT_EX(fmt("GID#%" PRId64 " cannot be removed now", gid));
+ throw DL_ABORT_EX(fmt("GID#%s cannot be removed now",
+ GroupId::toHex(gid).c_str()));
}
}
} else {
- throw DL_ABORT_EX(fmt("Active Download not found for GID#%" PRId64,
- gid));
+ throw DL_ABORT_EX(fmt("Active Download not found for GID#%s",
+ GroupId::toHex(gid).c_str()));
}
return createGIDResponse(gid);
}
@@ -455,7 +463,8 @@ SharedHandle<ValueBase> pauseDownload
return createGIDResponse(gid);
}
}
- throw DL_ABORT_EX(fmt("GID#%" PRId64 " cannot be paused now", gid));
+ throw DL_ABORT_EX(fmt("GID#%s cannot be paused now",
+ GroupId::toHex(gid).c_str()));
}
} // namespace
@@ -519,7 +528,8 @@ SharedHandle<ValueBase> UnpauseRpcMethod::process
if(!group ||
group->getState() != RequestGroup::STATE_WAITING ||
!group->isPauseRequested()) {
- throw DL_ABORT_EX(fmt("GID#%" PRId64 " cannot be unpaused now", gid));
+ throw DL_ABORT_EX(fmt("GID#%s cannot be unpaused now",
+ GroupId::toHex(gid).c_str()));
} else {
group->setPauseRequested(false);
e->getRequestGroupMan()->requestQueueCheck();
@@ -644,7 +654,7 @@ void gatherProgressCommon
{
const SharedHandle<PieceStorage>& ps = group->getPieceStorage();
if(requested_key(keys, KEY_GID)) {
- entryDict->put(KEY_GID, util::itos(group->getGID()));
+ entryDict->put(KEY_GID, GroupId::toHex(group->getGID()).c_str());
}
if(requested_key(keys, KEY_TOTAL_LENGTH)) {
// This is "filtered" total length if --select-file is used.
@@ -690,14 +700,14 @@ void gatherProgressCommon
// The element is GID.
for(std::vector<a2_gid_t>::const_iterator i = group->followedBy().begin(),
eoi = group->followedBy().end(); i != eoi; ++i) {
- list->append(util::itos(*i));
+ list->append(GroupId::toHex(*i));
}
entryDict->put(KEY_FOLLOWED_BY, list);
}
}
if(requested_key(keys, KEY_BELONGS_TO)) {
if(group->belongsTo()) {
- entryDict->put(KEY_BELONGS_TO, util::itos(group->belongsTo()));
+ entryDict->put(KEY_BELONGS_TO, GroupId::toHex(group->belongsTo()));
}
}
if(requested_key(keys, KEY_FILES)) {
@@ -832,7 +842,7 @@ void gatherStoppedDownload
const std::vector<std::string>& keys)
{
if(requested_key(keys, KEY_GID)) {
- entryDict->put(KEY_GID, util::itos(ds->gid));
+ entryDict->put(KEY_GID, ds->gid->toHex());
}
if(requested_key(keys, KEY_ERROR_CODE)) {
entryDict->put(KEY_ERROR_CODE, util::itos(static_cast<int>(ds->result)));
@@ -852,14 +862,14 @@ void gatherStoppedDownload
// The element is GID.
for(std::vector<a2_gid_t>::const_iterator i = ds->followedBy.begin(),
eoi = ds->followedBy.end(); i != eoi; ++i) {
- list->append(util::itos(*i));
+ list->append(GroupId::toHex(*i));
}
entryDict->put(KEY_FOLLOWED_BY, list);
}
}
if(requested_key(keys, KEY_BELONGS_TO)) {
if(ds->belongsTo) {
- entryDict->put(KEY_BELONGS_TO, util::itos(ds->belongsTo));
+ entryDict->put(KEY_BELONGS_TO, GroupId::toHex(ds->belongsTo));
}
}
if(requested_key(keys, KEY_FILES)) {
@@ -922,8 +932,8 @@ SharedHandle<ValueBase> GetFilesRpcMethod::process
SharedHandle<DownloadResult> dr =
e->getRequestGroupMan()->findDownloadResult(gid);
if(!dr) {
- throw DL_ABORT_EX(fmt("No file data is available for GID#%" PRId64 "",
- gid));
+ throw DL_ABORT_EX(fmt("No file data is available for GID#%s",
+ GroupId::toHex(gid).c_str()));
} else {
createFileEntry(files, dr->fileEntries.begin(), dr->fileEntries.end(),
dr->totalLength, dr->pieceLength, dr->bitfield);
@@ -949,7 +959,8 @@ SharedHandle<ValueBase> GetUrisRpcMethod::process
a2_gid_t gid = str2Gid(gidParam);
SharedHandle<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
if(!group) {
- throw DL_ABORT_EX(fmt("No URI data is available for GID#%" PRId64, gid));
+ throw DL_ABORT_EX(fmt("No URI data is available for GID#%s",
+ GroupId::toHex(gid).c_str()));
}
SharedHandle<List> uriList = List::g();
// TODO Current implementation just returns first FileEntry's URIs.
@@ -968,7 +979,8 @@ SharedHandle<ValueBase> GetPeersRpcMethod::process
a2_gid_t gid = str2Gid(gidParam);
SharedHandle<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
if(!group) {
- throw DL_ABORT_EX(fmt("No peer data is available for GID#%" PRId64, gid));
+ throw DL_ABORT_EX(fmt("No peer data is available for GID#%s",
+ GroupId::toHex(gid).c_str()));
}
SharedHandle<List> peers = List::g();
const SharedHandle<BtObject>& btObject =
@@ -998,7 +1010,8 @@ SharedHandle<ValueBase> TellStatusRpcMethod::process
SharedHandle<DownloadResult> ds =
e->getRequestGroupMan()->findDownloadResult(gid);
if(!ds) {
- throw DL_ABORT_EX(fmt("No such download for GID#%" PRId64 "", gid));
+ throw DL_ABORT_EX(fmt("No such download for GID#%s",
+ GroupId::toHex(gid).c_str()));
}
gatherStoppedDownload(entryDict, ds, keys);
} else {
@@ -1090,7 +1103,8 @@ SharedHandle<ValueBase> RemoveDownloadResultRpcMethod::process
a2_gid_t gid = str2Gid(gidParam);
if(!e->getRequestGroupMan()->removeDownloadResult(gid)) {
- throw DL_ABORT_EX(fmt("Could not remove download result of GID#%" PRId64 "", gid));
+ throw DL_ABORT_EX(fmt("Could not remove download result of GID#%s",
+ GroupId::toHex(gid).c_str()));
}
return VLB_OK;
}
@@ -1191,7 +1205,8 @@ SharedHandle<ValueBase> ChangeOptionRpcMethod::process
}
changeOption(group, option, e);
} else {
- throw DL_ABORT_EX(fmt("Cannot change option for GID#%" PRId64, gid));
+ throw DL_ABORT_EX(fmt("Cannot change option for GID#%s",
+ GroupId::toHex(gid).c_str()));
}
return VLB_OK;
}
@@ -1277,7 +1292,8 @@ SharedHandle<ValueBase> GetOptionRpcMethod::process
a2_gid_t gid = str2Gid(gidParam);
SharedHandle<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
if(!group) {
- throw DL_ABORT_EX(fmt("Cannot get option for GID#%" PRId64 "", gid));
+ throw DL_ABORT_EX(fmt("Cannot get option for GID#%s",
+ GroupId::toHex(gid).c_str()));
}
SharedHandle<Dict> result = Dict::g();
SharedHandle<Option> option = group->getOption();
@@ -1344,7 +1360,8 @@ SharedHandle<ValueBase> GetServersRpcMethod::process
a2_gid_t gid = str2Gid(gidParam);
SharedHandle<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
if(!group || group->getState() != RequestGroup::STATE_ACTIVE) {
- throw DL_ABORT_EX(fmt("No active download for GID#%" PRId64, gid));
+ throw DL_ABORT_EX(fmt("No active download for GID#%s",
+ GroupId::toHex(gid).c_str()));
}
const SharedHandle<DownloadContext>& dctx = group->getDownloadContext();
const std::vector<SharedHandle<FileEntry> >& files = dctx->getFileEntries();
@@ -1390,7 +1407,8 @@ SharedHandle<ValueBase> ChangeUriRpcMethod::process
size_t index = indexParam->i()-1;
SharedHandle<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
if(!group) {
- throw DL_ABORT_EX(fmt("Cannot remove URIs from GID#%" PRId64 "", gid));
+ throw DL_ABORT_EX(fmt("Cannot remove URIs from GID#%s",
+ GroupId::toHex(gid).c_str()));
}
const SharedHandle<DownloadContext>& dctx = group->getDownloadContext();
const std::vector<SharedHandle<FileEntry> >& files = dctx->getFileEntries();
diff --git a/src/TrackerWatcherCommand.cc b/src/TrackerWatcherCommand.cc
index 00822a4f..fcd3b6f2 100644
--- a/src/TrackerWatcherCommand.cc
+++ b/src/TrackerWatcherCommand.cc
@@ -221,7 +221,7 @@ TrackerWatcherCommand::createRequestGroup(const std::string& uri)
std::vector<std::string> uris;
uris.push_back(uri);
SharedHandle<Option> option = util::copy(getOption());
- SharedHandle<RequestGroup> rg(new RequestGroup(option));
+ SharedHandle<RequestGroup> rg(new RequestGroup(GroupId::create(), option));
if(backupTrackerIsAvailable(requestGroup_->getDownloadContext())) {
A2_LOG_DEBUG("This is multi-tracker announce.");
} else {
@@ -255,7 +255,8 @@ TrackerWatcherCommand::createRequestGroup(const std::string& uri)
rg->clearPreDownloadHandler();
rg->clearPostDownloadHandler();
dctx->setAcceptMetalink(false);
- A2_LOG_INFO(fmt("Creating tracker request group GID#%" PRId64 "", rg->getGID()));
+ A2_LOG_INFO(fmt("Creating tracker request group GID#%s",
+ GroupId::toHex(rg->getGID()).c_str()));
return rg;
}
diff --git a/src/WebSocketSessionMan.cc b/src/WebSocketSessionMan.cc
index 15f6c935..26771bc4 100644
--- a/src/WebSocketSessionMan.cc
+++ b/src/WebSocketSessionMan.cc
@@ -66,7 +66,7 @@ void WebSocketSessionMan::addNotification
dict->put("jsonrpc", "2.0");
dict->put("method", method);
SharedHandle<Dict> eventSpec = Dict::g();
- eventSpec->put("gid", util::itos(group->getGID()));
+ eventSpec->put("gid", GroupId::toHex((group->getGID())));
SharedHandle<List> params = List::g();
params->append(eventSpec);
dict->put("params", params);
diff --git a/src/download_helper.cc b/src/download_helper.cc
index 092ba544..5a448dcf 100644
--- a/src/download_helper.cc
+++ b/src/download_helper.cc
@@ -112,7 +112,7 @@ SharedHandle<RequestGroup> createRequestGroup
bool useOutOption = false)
{
SharedHandle<Option> option = util::copy(optionTemplate);
- SharedHandle<RequestGroup> rg(new RequestGroup(option));
+ SharedHandle<RequestGroup> rg(new RequestGroup(GroupId::create(), option));
SharedHandle<DownloadContext> dctx
(new DownloadContext
(option->getAsInt(PREF_PIECE_LENGTH),
@@ -168,7 +168,7 @@ createBtRequestGroup(const std::string& metaInfoUri,
bool adjustAnnounceUri = true)
{
SharedHandle<Option> option = util::copy(optionTemplate);
- SharedHandle<RequestGroup> rg(new RequestGroup(option));
+ SharedHandle<RequestGroup> rg(new RequestGroup(GroupId::create(), option));
SharedHandle<DownloadContext> dctx(new DownloadContext());
// may throw exception
bittorrent::loadFromMemory(torrent, dctx, option, auxUris,
@@ -210,7 +210,7 @@ createBtMagnetRequestGroup
const SharedHandle<Option>& optionTemplate)
{
SharedHandle<Option> option = util::copy(optionTemplate);
- SharedHandle<RequestGroup> rg(new RequestGroup(option));
+ SharedHandle<RequestGroup> rg(new RequestGroup(GroupId::create(), option));
SharedHandle<DownloadContext> dctx
(new DownloadContext(METADATA_PIECE_SIZE, 0,
A2STR::NIL));
diff --git a/src/message.h b/src/message.h
index 66681dfa..fd6ee70a 100644
--- a/src/message.h
+++ b/src/message.h
@@ -54,7 +54,7 @@
#define MSG_GOT_NEW_PIECE "CUID#%" PRId64 " - we got new piece. index=%lu"
#define MSG_GOT_WRONG_PIECE "CUID#%" PRId64 " - we got wrong piece. index=%lu"
#define MSG_DOWNLOAD_NOT_COMPLETE "CUID#%" PRId64 " - Download not complete: %s"
-#define MSG_DOWNLOAD_ALREADY_COMPLETED _("GID#%" PRId64 " - Download has already completed: %s")
+#define MSG_DOWNLOAD_ALREADY_COMPLETED _("GID#%s - Download has already completed: %s")
#define MSG_RESOLVING_HOSTNAME "CUID#%" PRId64 " - Resolving hostname %s"
#define MSG_NAME_RESOLUTION_COMPLETE \
"CUID#%" PRId64 " - Name resolution complete: %s -> %s"
@@ -197,7 +197,7 @@
#define MSG_DIR_TRAVERSAL_DETECTED _("Detected directory traversal directive in %s")
#define MSG_HASH_CHECK_NOT_DONE \
"File has already been downloaded but hash check has not been done yet."
-#define MSG_REMOVING_UNSELECTED_FILE _("GID#%" PRId64 " - Removing unselected file.")
+#define MSG_REMOVING_UNSELECTED_FILE _("GID#%s - Removing unselected file.")
#define MSG_FILE_REMOVED _("File %s removed.")
#define MSG_FILE_COULD_NOT_REMOVED _("File %s could not be removed.")
diff --git a/src/util.cc b/src/util.cc
index 0f9083b2..da652a8e 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -1691,7 +1691,7 @@ void executeHook
size_t numFiles,
const std::string& firstFilename)
{
- const std::string gidStr = util::itos(gid);
+ const std::string gidStr = GroupId::toHex(gid);
const std::string numFilesStr = util::uitos(numFiles);
#ifndef __MINGW32__
A2_LOG_INFO(fmt("Executing user command: %s %s %s %s",