Skip to content

feat: support skip padding_file by bt #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: support skip padding_file by bt
  • Loading branch information
zeromake committed Dec 16, 2024
commit 25ed62def985af74fceb0fc571d7c4ff007458df
2 changes: 1 addition & 1 deletion compat/a2io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const std::wstring toNamespacedPath(const wchar_t* path)
}
originPath = L"\\\\?\\" + originPath;
}
wprintf(L"toNamespacedPath: %s\n", originPath.c_str());
// wprintf(L"toNamespacedPath: %s\n", originPath.c_str());
return std::move(originPath);
}

Expand Down
4 changes: 0 additions & 4 deletions compat/gai_strerror.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
extern "C" {
#endif /* __cplusplus */

#ifdef _WIN32
# undef SIZE_MAX
#endif // _WIN32

#ifndef EAI_SYSTEM
# define EAI_SYSTEM -11 /* System error returned in `errno'. */
#endif
Expand Down
4 changes: 0 additions & 4 deletions compat/getaddrinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
extern "C" {
#endif /* __cplusplus */

#ifdef _WIN32
# undef SIZE_MAX
#endif // _WIN32

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif // HAVE_CONFIG_H
Expand Down
2 changes: 1 addition & 1 deletion src/core/Context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ Context::Context(bool standalone, int argc, char** argv, const KeyVals& options)

if (standalone && !op->getAsBool(PREF_ENABLE_RPC) && requestGroups.empty() &&
!uriListParser) {
global::cout()->printf("%s\n", MSG_NO_FILES_TO_DOWNLOAD);
A2_LOG_NOTICE(MSG_NO_FILES_TO_DOWNLOAD);
}
else {
if (!requestGroups.empty()) {
Expand Down
9 changes: 6 additions & 3 deletions src/core/DownloadContext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ void DownloadContext::setFileFilter(SegList<int> sgl)
using namespace std::placeholders;

if (!sgl.hasNext() || fileEntries_.size() == 1) {
std::for_each(fileEntries_.begin(), fileEntries_.end(),
std::bind(&FileEntry::setRequested, _1, true));
std::for_each(fileEntries_.begin(), fileEntries_.end(), [](auto& e) {
if (!e->isPaddingFile()) e->setRequested(true);
});
return;
}
assert(sgl.peek() >= 1);
Expand All @@ -148,7 +149,9 @@ void DownloadContext::setFileFilter(SegList<int> sgl)
for (; i < len && sgl.hasNext(); ++i) {
size_t idx = sgl.peek() - 1;
if (i == idx) {
fileEntries_[i]->setRequested(true);
if (!fileEntries_[i]->isPaddingFile()) {
fileEntries_[i]->setRequested(true);
}
sgl.next();
}
else if (i < idx) {
Expand Down
1 change: 1 addition & 0 deletions src/core/MultiUrlRequestInfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ static const DWORD mainThread = GetCurrentThreadId();

static void handler(int signal)
{
A2_LOG_NOTICE(fmt("handler signal %d %d", signal, global::globalHaltRequested));
if (
#ifdef SIGHUP
signal == SIGHUP ||
Expand Down
1 change: 1 addition & 0 deletions src/network/HttpServerBodyCommand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ void HttpServerBodyCommand::addHttpServerResponseCommand(bool delayed)
auto resp = aria2::make_unique<HttpServerResponseCommand>(
getCuid(), httpServer_, e_, socket_);
if (delayed) {
e_->deleteSocketForWriteCheck(socket_, resp.get());
e_->addCommand(aria2::make_unique<DelayedCommand>(getCuid(), e_, 1_s,
std::move(resp), true));
return;
Expand Down
4 changes: 3 additions & 1 deletion src/protocol/bt/BtDependency.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ namespace {
void copyValues(const std::shared_ptr<FileEntry>& d,
const std::shared_ptr<FileEntry>& s)
{
d->setRequested(true);
if (!d->isPaddingFile()) {
d->setRequested(true);
}
d->setPath(s->getPath());
d->addUris(std::begin(s->getRemainingUris()),
std::end(s->getRemainingUris()));
Expand Down
4 changes: 4 additions & 0 deletions src/protocol/bt/bittorrent_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ void extractFileEntries(const std::shared_ptr<DownloadContext>& ctx,
fileEntry->setOriginalName(utf8Path);
fileEntry->setSuffixPath(suffixPath);
fileEntry->setMaxConnectionPerServer(maxConn);
if (utf8Path.find("_____padding_file_") != std::string::npos) {
fileEntry->setPaddingFile(true);
fileEntry->setRequested(false);
}
fileEntries.push_back(fileEntry);
offset += fileEntry->getLength();
}
Expand Down
1 change: 1 addition & 0 deletions src/rpc/RpcMethodImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ void createFileEntry(List* files, InputIterator first, InputIterator last,
{
size_t index = 1;
for (; first != last; ++first, ++index) {
if ((*first)->isPaddingFile()) continue;
auto entry = Dict::g();
entry->put(KEY_INDEX, util::uitos(index));
entry->put(KEY_PATH, (*first)->getPath());
Expand Down
7 changes: 7 additions & 0 deletions src/storage/FileEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class FileEntry {

bool requested_;
bool uniqueProtocol_;
bool paddingFile_ = false;

void storePool(const std::shared_ptr<Request>& request);

Expand Down Expand Up @@ -132,6 +133,12 @@ class FileEntry {
bool isRequested() const { return requested_; }

void setRequested(bool flag) { requested_ = flag; }

bool isPaddingFile() const { return paddingFile_; }

void setPaddingFile(bool flag) {
paddingFile_ = flag;
}

const std::deque<std::string>& getRemainingUris() const { return uris_; }

Expand Down
41 changes: 29 additions & 12 deletions src/storage/MultiDiskAdaptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ void MultiDiskAdaptor::resetDiskWriterEntries()
A2_LOG_DEBUG(fmt("%s needs DiskWriter", fileEntry->getPath().c_str()));
dwent->needsDiskWriter(true);
}
if (fileEntry->isPaddingFile()) {
dwent->setSkip(true);
}
}
// Check shared piece backward
lastOffset = std::numeric_limits<int64_t>::max();
Expand All @@ -180,6 +183,9 @@ void MultiDiskAdaptor::resetDiskWriterEntries()
}
DefaultDiskWriterFactory dwFactory;
for (auto& dwent : diskWriterEntries_) {
if (dwent->getSkip()) {
continue;
}
if (dwent->needsFileAllocation() || dwent->needsDiskWriter() ||
dwent->fileExists()) {
A2_LOG_DEBUG(fmt("Creating DiskWriter for filename=%s",
Expand Down Expand Up @@ -344,13 +350,14 @@ void MultiDiskAdaptor::writeData(const unsigned char* data, size_t len,
int64_t fileOffset = offset - (*first)->getFileEntry()->getOffset();
for (auto i = first, eoi = diskWriterEntries_.cend(); i != eoi; ++i) {
ssize_t writeLength = calculateLength((*i).get(), fileOffset, rem);
openIfNot((*i).get(), &DiskWriterEntry::openFile);
if (!(*i)->isOpen()) {
throwOnDiskWriterNotOpened((*i).get(), offset + (len - rem));
if (!(*i)->getSkip()) {
openIfNot((*i).get(), &DiskWriterEntry::openFile);
if (!(*i)->isOpen()) {
throwOnDiskWriterNotOpened((*i).get(), offset + (len - rem));
}
(*i)->getDiskWriter()->writeData(data + (len - rem), writeLength,
fileOffset);
}

(*i)->getDiskWriter()->writeData(data + (len - rem), writeLength,
fileOffset);
rem -= writeLength;
fileOffset = 0;
if (rem == 0) {
Expand Down Expand Up @@ -380,22 +387,32 @@ ssize_t MultiDiskAdaptor::readData(unsigned char* data, size_t len,
int64_t fileOffset = offset - (*first)->getFileEntry()->getOffset();
for (auto i = first, eoi = diskWriterEntries_.cend(); i != eoi; ++i) {
ssize_t readLength = calculateLength((*i).get(), fileOffset, rem);
openIfNot((*i).get(), &DiskWriterEntry::openFile);
if (!(*i)->isOpen()) {
throwOnDiskWriterNotOpened((*i).get(), offset + (len - rem));
bool skip = (*i)->getSkip();
if (!skip) {
openIfNot((*i).get(), &DiskWriterEntry::openFile);
if (!(*i)->isOpen()) {
throwOnDiskWriterNotOpened((*i).get(), offset + (len - rem));
}
}

while (readLength > 0) {
auto nread = (*i)->getDiskWriter()->readData(data + (len - rem),
readLength, fileOffset);
ssize_t nread = 0;
if (!skip) {
nread = (*i)->getDiskWriter()->readData(data + (len - rem), readLength,
fileOffset);
} else {
// Skip padding reading fill is zero
nread = readLength;
memset(data + (len - rem), 0, readLength);
}

if (nread == 0) {
return totalReadLength;
}

totalReadLength += nread;

if (dropCache) {
if (dropCache && !skip) {
(*i)->getDiskWriter()->dropCache(nread, fileOffset);
}

Expand Down
5 changes: 5 additions & 0 deletions src/storage/MultiDiskAdaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class DiskWriterEntry {
bool open_;
bool needsFileAllocation_;
bool needsDiskWriter_;
bool skip_ = false;

public:
DiskWriterEntry(const std::shared_ptr<FileEntry>& fileEntry);
Expand Down Expand Up @@ -88,6 +89,10 @@ class DiskWriterEntry {
bool needsDiskWriter() const { return needsDiskWriter_; }

void needsDiskWriter(bool f) { needsDiskWriter_ = f; }

bool getSkip() const { return skip_; }

void setSkip(bool f) { skip_ = f; }
};

typedef std::vector<std::unique_ptr<DiskWriterEntry>> DiskWriterEntries;
Expand Down