mirror of https://github.com/aria2/aria2
Better auto-renaming
parent
1ab2b99692
commit
e6e2de96ca
|
@ -778,8 +778,16 @@ void RequestGroup::tryAutoFileRenaming()
|
||||||
fmt("File renaming failed: %s", getFirstFilePath().c_str()),
|
fmt("File renaming failed: %s", getFirstFilePath().c_str()),
|
||||||
error_code::FILE_RENAMING_FAILED);
|
error_code::FILE_RENAMING_FAILED);
|
||||||
}
|
}
|
||||||
|
auto fn = filepath;
|
||||||
|
std::string ext;
|
||||||
|
auto idx = fn.find_last_of(".");
|
||||||
|
auto slash = fn.find_last_of("\\/");
|
||||||
|
if (idx != std::string::npos && (slash == std::string::npos || slash < idx)) {
|
||||||
|
ext = fn.substr(idx);
|
||||||
|
fn = fn.substr(0, idx);
|
||||||
|
}
|
||||||
for (int i = 1; i < 10000; ++i) {
|
for (int i = 1; i < 10000; ++i) {
|
||||||
auto newfilename = fmt("%s.%d", filepath.c_str(), i);
|
auto newfilename = fmt("%s.%d%s", fn.c_str(), i, ext.c_str());
|
||||||
File newfile(newfilename);
|
File newfile(newfilename);
|
||||||
File ctrlfile(newfile.getPath() + DefaultBtProgressInfoFile::getSuffix());
|
File ctrlfile(newfile.getPath() + DefaultBtProgressInfoFile::getSuffix());
|
||||||
if (!newfile.exists() || (newfile.exists() && ctrlfile.exists())) {
|
if (!newfile.exists() || (newfile.exists() && ctrlfile.exists())) {
|
||||||
|
|
|
@ -199,8 +199,6 @@ private:
|
||||||
|
|
||||||
void initializePostDownloadHandler();
|
void initializePostDownloadHandler();
|
||||||
|
|
||||||
void tryAutoFileRenaming();
|
|
||||||
|
|
||||||
// Returns the result code of this RequestGroup. If the download
|
// Returns the result code of this RequestGroup. If the download
|
||||||
// finished, then returns error_code::FINISHED. If the
|
// finished, then returns error_code::FINISHED. If the
|
||||||
// download didn't finish and error result is available in
|
// download didn't finish and error result is available in
|
||||||
|
@ -219,6 +217,8 @@ public:
|
||||||
|
|
||||||
bool isCheckIntegrityReady();
|
bool isCheckIntegrityReady();
|
||||||
|
|
||||||
|
void tryAutoFileRenaming();
|
||||||
|
|
||||||
const std::shared_ptr<SegmentMan>& getSegmentMan() const
|
const std::shared_ptr<SegmentMan>& getSegmentMan() const
|
||||||
{
|
{
|
||||||
return segmentMan_;
|
return segmentMan_;
|
||||||
|
|
|
@ -39,6 +39,35 @@ void RequestGroupTest::testGetFirstFilePath()
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), group.getFirstFilePath());
|
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), group.getFirstFilePath());
|
||||||
|
|
||||||
|
// test file renaming
|
||||||
|
option_->put(PREF_AUTO_FILE_RENAMING, "false");
|
||||||
|
try {
|
||||||
|
group.tryAutoFileRenaming();
|
||||||
|
}
|
||||||
|
catch (const Exception& ex) {
|
||||||
|
CPPUNIT_ASSERT_EQUAL(error_code::FILE_ALREADY_EXISTS, ex.getErrorCode());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
option_->put(PREF_AUTO_FILE_RENAMING, "true");
|
||||||
|
group.tryAutoFileRenaming();
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile.1"), group.getFirstFilePath());
|
||||||
|
|
||||||
|
ctx->getFirstFileEntry()->setPath("/tmp/myfile.txt");
|
||||||
|
group.tryAutoFileRenaming();
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile.1.txt"), group.getFirstFilePath());
|
||||||
|
|
||||||
|
ctx->getFirstFileEntry()->setPath("/tmp.txt/myfile");
|
||||||
|
group.tryAutoFileRenaming();
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("/tmp.txt/myfile.1"), group.getFirstFilePath());
|
||||||
|
|
||||||
|
ctx->getFirstFileEntry()->setPath("/tmp.txt/myfile.txt");
|
||||||
|
group.tryAutoFileRenaming();
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("/tmp.txt/myfile.1.txt"), group.getFirstFilePath());
|
||||||
|
|
||||||
|
// test in-memory
|
||||||
|
ctx->getFirstFileEntry()->setPath("/tmp/myfile");
|
||||||
|
|
||||||
group.markInMemoryDownload();
|
group.markInMemoryDownload();
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("[MEMORY]myfile"), group.getFirstFilePath());
|
CPPUNIT_ASSERT_EQUAL(std::string("[MEMORY]myfile"), group.getFirstFilePath());
|
||||||
|
|
Loading…
Reference in New Issue