2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-02-17 13:35:04 +00:00
|
|
|
*
|
|
|
|
* 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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-09-21 15:31:24 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2006-02-17 13:35:04 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "File.h"
|
2008-09-30 15:16:37 +00:00
|
|
|
|
2008-03-15 04:19:46 +00:00
|
|
|
#include <stdlib.h>
|
2008-09-07 14:38:26 +00:00
|
|
|
#include <sys/types.h>
|
2011-08-19 13:35:15 +00:00
|
|
|
#ifdef HAVE_UTIME_H
|
|
|
|
# include <utime.h>
|
|
|
|
#endif // HAVE_UTIME_H
|
2009-06-20 16:47:54 +00:00
|
|
|
#include <unistd.h>
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2009-10-18 12:31:07 +00:00
|
|
|
#include <vector>
|
2008-09-30 15:16:37 +00:00
|
|
|
#include <cstring>
|
2008-10-07 13:40:12 +00:00
|
|
|
#include <cstdio>
|
2008-09-30 15:16:37 +00:00
|
|
|
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-09-30 15:16:37 +00:00
|
|
|
#include "A2STR.h"
|
2009-06-20 16:47:54 +00:00
|
|
|
#include "array_fun.h"
|
2011-01-09 09:27:08 +00:00
|
|
|
#include "Logger.h"
|
|
|
|
#include "LogFactory.h"
|
|
|
|
#include "fmt.h"
|
2008-09-30 15:16:37 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
File::File(const std::string& name) : name_(name) {}
|
|
|
|
|
|
|
|
File::File(const File& c) : name_(c.name_) {}
|
2006-02-17 13:35:04 +00:00
|
|
|
|
|
|
|
File::~File() {}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
File& File::operator=(const File& c)
|
|
|
|
{
|
|
|
|
if(this != &c) {
|
|
|
|
name_ = c.name_;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-07-06 04:38:54 +00:00
|
|
|
int File::fillStat(a2_struct_stat& fstat) {
|
2011-08-04 12:43:02 +00:00
|
|
|
return a2stat(utf8ToWChar(name_).c_str(), &fstat);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool File::exists() {
|
2008-07-06 04:38:54 +00:00
|
|
|
a2_struct_stat fstat;
|
2006-02-17 13:35:04 +00:00
|
|
|
return fillStat(fstat) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool File::isFile() {
|
2008-07-06 04:38:54 +00:00
|
|
|
a2_struct_stat fstat;
|
2006-02-17 13:35:04 +00:00
|
|
|
if(fillStat(fstat) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return S_ISREG(fstat.st_mode) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool File::isDir() {
|
2008-07-06 04:38:54 +00:00
|
|
|
a2_struct_stat fstat;
|
2006-02-17 13:35:04 +00:00
|
|
|
if(fillStat(fstat) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return S_ISDIR(fstat.st_mode) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool File::remove() {
|
|
|
|
if(isFile()) {
|
2011-08-04 12:43:02 +00:00
|
|
|
return a2unlink(utf8ToWChar(name_).c_str()) == 0;
|
2006-02-17 13:35:04 +00:00
|
|
|
} else if(isDir()) {
|
2011-08-04 12:43:02 +00:00
|
|
|
return a2rmdir(utf8ToWChar(name_).c_str()) == 0;
|
2006-02-17 13:35:04 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2012-06-25 14:35:24 +00:00
|
|
|
int64_t File::size() {
|
2008-07-06 04:38:54 +00:00
|
|
|
a2_struct_stat fstat;
|
2006-03-21 14:12:51 +00:00
|
|
|
if(fillStat(fstat) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return fstat.st_size;
|
|
|
|
}
|
2006-11-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To divide TorrentMan into 6 classes: BtContext, BtRuntime,
PeerStorage, PieceStorage, BtAnnounce and BtProgressInfoFile
* src/TrackerWatcherComand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.cc: Use pieceStorage, btRuntime
* src/PeerAbstractCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerAbstractCommand.cc: Use btRuntime.
* src/BtContextAwareCommand.h: New class.
* src/FileEntry.h: Added accessor methods for following
variables.
(path): Made private.
(length): Made private.
(offset): Made private.
(extracted): Made private.
(requested): Made private.
(FileEntries): New definition.
(FileEntryHandle): New definition.
* src/FileEntry.cc: New file.
* src/HaveEraseCommand.h: Made subclass of
BtContextAwareCommand.
* src/HaveEraseCommand.cc: Use btRuntime, pieceStorage.
* src/PeerChokeCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerChokeCommand.cc: Use btRuntime, peerStorage,
pieceStorage.
* src/PieceStorage.h: New file.
* src/PeerInteractionCommand.h: Use btContext.
* src/PeerInteractionCommand.cc: Use pieceStorage, peerStorage,
btRuntime.
* src/DefaultBtProgressInfoFile.h: New file.
* src/DefaultBtProgressInfoFile.cc: New file.
* src/File.cc
(Util.h): New include.
(mkdirs): New function.
* src/MultiDiskAdaptor.h
(mkdir): New function.
* src/PeerListProcessor.h
(Peers): Removed.
* src/PeerInteraction.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(btAnnounce): New variable.
(getTorrentMan): Removed.
(getBtContext): New function.
* src/PeerInteraction.cc: Use btContext, peerStorage,
pieceStorage,
btAnnounce.
* src/HandshakeMessage.h
(TorrentMan.h): Removed.
* src/HandshakeMessage.cc: Use btContext.
* src/DefaultBtAnnounce.cc: New file.
* src/MultiDiskWriter.cc: Use the accessor methods of FileEntry.
* src/DefaultPieceStorage.cc: New file.
* src/DefaultBtContext.h: New file.
* src/TorrentRequestInfo.cc: Use btContext, pieceStorage.
Use the accessor methods of FileEntry.
* src/CookieBox.cc: Updated to use Util::slice().
* src/PieceMessage.cc: Use btContext, pieceStorage.
* src/common.h (SharedHandle.h): New include.
* src/PeerMessage.cc (PeerMessage): Added btContext,
peerStorage,
pieceStorage.
* src/TorrentAutoSaveCommand.h: Made subclass of
BtContextAwareCommand.
* src/DiskAdaptor.h
(topDir): Removed.
(getFileEntryFromPath): Changed the return type to
FileEntryHandle.
(setTopDir): Removed.
(getTopDir): Removed.
* src/BtContext.h: New file.
* src/DefaultPeerStorage.h: New file.
* src/PieceMessage.h (TorrentMan.h): Removed.
* src/RequestMessage.h (TorrentMan.h): Removed.
* src/TorrentDownloadEngine.h
(uploadLength): New variable.
(btContext): New variable.
(btRuntime): New variable.
(pieceStorage): New variable.
(peerStorage): New variable.
(btAnnounce): New variable.
(btProgressInfoFile): New variable.
(torrentMan): Removed.
(setBtContext): New function.
* src/TorrentDownloadEngine.cc: Use BtContext, BtRuntime,
pieceStorage,
peerStorage, btAnnounce, btProgressInfoFile.
* src/Piece.h
(toString): New function.
(Pieces): New type definition.
* src/Peer.h
(active): New variable.
(Peer): Added active.
(activate): Set active to true.
(deactivate): Set active to false.
(isActive): New function.
(Peers): New type definition.
* src/DirectDiskAdaptor.cc
(getFilePath): Use the accessor methods of FileEntry.
* src/TorrentConsoleDownloadEngine.h
(afterEachIteration): New function.
* src/TorrentConsoleDownloadEngine.cc
(haltRequested): New variable.
(sendStatistics): Use pieceStorage, btRuntime.
(afterEachIteration): New function.
* src/AnnounceList: AnnounceTier->AnnounceTierHandle.
* src/Directry.h
(Directory): New function.
(DirectoryHandle): New type definition.
* src/BtProgressInfoFile.h: New file.
* src/RequestMessage.cc: Use pieceStorage.
* src/BtRuntime.h: New file.
* src/DefaultBtContext.cc: New file.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/MultiDiskAdaptor.cc
(mkdir): New function.
(openFile): Call mkdir().
(initAndOpenFile): Call mkdir().
* src/CancelMessage.h
(TorrentMan.h): Removed.
* src/RejectMessage.h
(TorrentMan.h): Removed.
* src/DownloadEngineFactory.cc
(DefaultPieceStorage.h): New include.
(DefaultPeerStorage.h): New include.
(DefaultBtAnnounce.h): New include.
(DefaultBtProgressInfoFile.h): New include.
(newTorrentConsoleEngine): Rewritten.
* src/ShareRatioSeedCriteria.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(btRuntime): New variable.
(evaluate): Use btContext, btRuntime, peerStorage.
* src/AnnounceTier.h: New file.
* src/BtAnnounce.h: New file.
* src/BtRegistry.h: New file.
* src/PeerInitiateConnectionCommand.h: Added btContext.
* src/PeerConnection.h (TorrentMan.h): Removed.
* src/PeerMessageFactory.cc: Use btContext, pieceStorage.
* src/Util.h
(slice): Added an argument.
* src/Util.cc
(slice): Added an argument to control whether trim is made or
not.
* src/PeerStorage.h: New file.
* src/BtRegistry.cc: New file.
* src/TrackerUpdateCommand.h: Made subclass of
BtContextAwareCommand.
* src/CopyDiskAdaptor.cc: Use the accessor methods of FileEntry.
* src/MultiDiskWriter.h: FileEntry -> FileEntryHandle
* src/PeerListenCommand.cc: Use btRuntime, peerStorage,
btContext.
* src/TorrentRequestInfo.h
(e): Removed.
(showFileEntry): Added an argument.
(getDownloadEngine): Return 0.
* src/DefaultBtAnnounce.h: New file.
* src/TorrentAutoSaveCommand.cc: Use btRuntime,
btProgressInfoFile.
* src/TrackerWatcherComand.cc: Use btRuntime, btAnnounce,
* src/PeerMessageFactory.h
(btContext): New variable.
(pieceStorage): New variable.
* src/TrackerUpdateCommand.cc: Use btRuntime, peerStorage,
btContext,
btAnnounce.
* src/DiskAdaptor.cc
(DiskAdaptor): Removed topDir.
(~DiskAdaptor): Removed topDir.
* src/PeerListenCommand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.h: Made subclass of
BtContextAwareCommand.
* src/File.h (mkdirs): New function.
* src/DefaultPeerStorage): New file.
* src/DownloadEngineFactory.h
(newTorrentConsoleEngine): Use btContext.
* src/BtContextAwareCommand.cc: New file.
* src/PeerInitiateConnectionCommand.cc: Use btRuntime,
peerStorage.
* src/PeerMessage.h
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(setBtContext): New function.
* src/Directry.cc
(Directory): New function.
(createDir): Do nothing if name.size() == 0.
* src/AnnounceList.h
(AnnounceTier): Removed.
(AnnounceTiers): Removed.
* src/DefaultPieceStorage.h: New file.
* src/Piece.cc (toString): New function.
To fix typo:
* src/main.cc (showVersion): Fixed typo.
To fix compile warning:
* src/DelegatingPeerListProcessor.cc
(canHandle): Added "return false".
2006-11-05 15:04:17 +00:00
|
|
|
|
|
|
|
bool File::mkdirs() {
|
|
|
|
if(isDir()) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-01-09 09:27:08 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
std::string path = name_;
|
|
|
|
for(std::string::iterator i = path.begin(), eoi = path.end(); i != eoi; ++i) {
|
|
|
|
if(*i == '\\') {
|
|
|
|
*i = '/';
|
|
|
|
}
|
|
|
|
}
|
2012-02-19 12:19:11 +00:00
|
|
|
std::string::iterator dbegin;
|
|
|
|
if(util::startsWith(path, "//")) {
|
|
|
|
// UNC path
|
|
|
|
std::string::size_type hostEnd = path.find('/', 2);
|
|
|
|
if(hostEnd == std::string::npos) {
|
|
|
|
// UNC path with only hostname considered as an error.
|
|
|
|
return false;
|
|
|
|
} else if(hostEnd == 2) {
|
|
|
|
// If path starts with "///", it is not considered as UNC.
|
|
|
|
dbegin = path.begin();
|
|
|
|
} else {
|
|
|
|
std::string::iterator i = path.begin()+hostEnd;
|
|
|
|
std::string::iterator eoi = path.end();
|
|
|
|
// //host/mount/dir/...
|
|
|
|
// | |
|
|
|
|
// i (at this point)
|
|
|
|
// |
|
|
|
|
// dbegin (will be)
|
|
|
|
// Skip to after first directory part. This is because
|
|
|
|
// //host/dir appears to be non-directory and mkdir it fails.
|
|
|
|
for(; i != eoi && *i == '/'; ++i);
|
|
|
|
for(; i != eoi && *i != '/'; ++i);
|
|
|
|
dbegin = i;
|
|
|
|
A2_LOG_DEBUG(fmt("UNC Prefix %s",
|
|
|
|
std::string(path.begin(), dbegin).c_str()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dbegin = path.begin();
|
|
|
|
}
|
2011-01-09 09:27:08 +00:00
|
|
|
std::string::iterator begin = path.begin();
|
|
|
|
std::string::iterator end = path.end();
|
2012-02-19 12:19:11 +00:00
|
|
|
for(std::string::iterator i = dbegin; i != end;) {
|
2011-01-09 09:27:08 +00:00
|
|
|
#else // !__MINGW32__
|
|
|
|
std::string::iterator begin = name_.begin();
|
|
|
|
std::string::iterator end = name_.end();
|
|
|
|
for(std::string::iterator i = begin; i != end;) {
|
2012-02-19 12:19:11 +00:00
|
|
|
#endif // !__MINGW32__
|
2011-01-09 09:27:08 +00:00
|
|
|
std::string::iterator j = std::find(i, end, '/');
|
2010-10-11 15:07:26 +00:00
|
|
|
if(std::distance(i, j) == 0) {
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2010-11-26 12:34:02 +00:00
|
|
|
i = j;
|
2011-01-09 09:27:08 +00:00
|
|
|
if(i != end) {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
if(*(j-1) == ':') {
|
|
|
|
// This is a drive letter, e.g. C:, so skip it.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif // __MINGW32__
|
2011-11-04 16:11:04 +00:00
|
|
|
std::string dir(begin, j);
|
2011-08-08 12:46:10 +00:00
|
|
|
A2_LOG_DEBUG(fmt("Making directory %s", dir.c_str()));
|
2010-10-11 15:07:26 +00:00
|
|
|
if(File(dir).isDir()) {
|
2011-08-08 12:46:10 +00:00
|
|
|
A2_LOG_DEBUG(fmt("%s exists and is a directory.", dir.c_str()));
|
2006-11-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To divide TorrentMan into 6 classes: BtContext, BtRuntime,
PeerStorage, PieceStorage, BtAnnounce and BtProgressInfoFile
* src/TrackerWatcherComand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.cc: Use pieceStorage, btRuntime
* src/PeerAbstractCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerAbstractCommand.cc: Use btRuntime.
* src/BtContextAwareCommand.h: New class.
* src/FileEntry.h: Added accessor methods for following
variables.
(path): Made private.
(length): Made private.
(offset): Made private.
(extracted): Made private.
(requested): Made private.
(FileEntries): New definition.
(FileEntryHandle): New definition.
* src/FileEntry.cc: New file.
* src/HaveEraseCommand.h: Made subclass of
BtContextAwareCommand.
* src/HaveEraseCommand.cc: Use btRuntime, pieceStorage.
* src/PeerChokeCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerChokeCommand.cc: Use btRuntime, peerStorage,
pieceStorage.
* src/PieceStorage.h: New file.
* src/PeerInteractionCommand.h: Use btContext.
* src/PeerInteractionCommand.cc: Use pieceStorage, peerStorage,
btRuntime.
* src/DefaultBtProgressInfoFile.h: New file.
* src/DefaultBtProgressInfoFile.cc: New file.
* src/File.cc
(Util.h): New include.
(mkdirs): New function.
* src/MultiDiskAdaptor.h
(mkdir): New function.
* src/PeerListProcessor.h
(Peers): Removed.
* src/PeerInteraction.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(btAnnounce): New variable.
(getTorrentMan): Removed.
(getBtContext): New function.
* src/PeerInteraction.cc: Use btContext, peerStorage,
pieceStorage,
btAnnounce.
* src/HandshakeMessage.h
(TorrentMan.h): Removed.
* src/HandshakeMessage.cc: Use btContext.
* src/DefaultBtAnnounce.cc: New file.
* src/MultiDiskWriter.cc: Use the accessor methods of FileEntry.
* src/DefaultPieceStorage.cc: New file.
* src/DefaultBtContext.h: New file.
* src/TorrentRequestInfo.cc: Use btContext, pieceStorage.
Use the accessor methods of FileEntry.
* src/CookieBox.cc: Updated to use Util::slice().
* src/PieceMessage.cc: Use btContext, pieceStorage.
* src/common.h (SharedHandle.h): New include.
* src/PeerMessage.cc (PeerMessage): Added btContext,
peerStorage,
pieceStorage.
* src/TorrentAutoSaveCommand.h: Made subclass of
BtContextAwareCommand.
* src/DiskAdaptor.h
(topDir): Removed.
(getFileEntryFromPath): Changed the return type to
FileEntryHandle.
(setTopDir): Removed.
(getTopDir): Removed.
* src/BtContext.h: New file.
* src/DefaultPeerStorage.h: New file.
* src/PieceMessage.h (TorrentMan.h): Removed.
* src/RequestMessage.h (TorrentMan.h): Removed.
* src/TorrentDownloadEngine.h
(uploadLength): New variable.
(btContext): New variable.
(btRuntime): New variable.
(pieceStorage): New variable.
(peerStorage): New variable.
(btAnnounce): New variable.
(btProgressInfoFile): New variable.
(torrentMan): Removed.
(setBtContext): New function.
* src/TorrentDownloadEngine.cc: Use BtContext, BtRuntime,
pieceStorage,
peerStorage, btAnnounce, btProgressInfoFile.
* src/Piece.h
(toString): New function.
(Pieces): New type definition.
* src/Peer.h
(active): New variable.
(Peer): Added active.
(activate): Set active to true.
(deactivate): Set active to false.
(isActive): New function.
(Peers): New type definition.
* src/DirectDiskAdaptor.cc
(getFilePath): Use the accessor methods of FileEntry.
* src/TorrentConsoleDownloadEngine.h
(afterEachIteration): New function.
* src/TorrentConsoleDownloadEngine.cc
(haltRequested): New variable.
(sendStatistics): Use pieceStorage, btRuntime.
(afterEachIteration): New function.
* src/AnnounceList: AnnounceTier->AnnounceTierHandle.
* src/Directry.h
(Directory): New function.
(DirectoryHandle): New type definition.
* src/BtProgressInfoFile.h: New file.
* src/RequestMessage.cc: Use pieceStorage.
* src/BtRuntime.h: New file.
* src/DefaultBtContext.cc: New file.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/MultiDiskAdaptor.cc
(mkdir): New function.
(openFile): Call mkdir().
(initAndOpenFile): Call mkdir().
* src/CancelMessage.h
(TorrentMan.h): Removed.
* src/RejectMessage.h
(TorrentMan.h): Removed.
* src/DownloadEngineFactory.cc
(DefaultPieceStorage.h): New include.
(DefaultPeerStorage.h): New include.
(DefaultBtAnnounce.h): New include.
(DefaultBtProgressInfoFile.h): New include.
(newTorrentConsoleEngine): Rewritten.
* src/ShareRatioSeedCriteria.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(btRuntime): New variable.
(evaluate): Use btContext, btRuntime, peerStorage.
* src/AnnounceTier.h: New file.
* src/BtAnnounce.h: New file.
* src/BtRegistry.h: New file.
* src/PeerInitiateConnectionCommand.h: Added btContext.
* src/PeerConnection.h (TorrentMan.h): Removed.
* src/PeerMessageFactory.cc: Use btContext, pieceStorage.
* src/Util.h
(slice): Added an argument.
* src/Util.cc
(slice): Added an argument to control whether trim is made or
not.
* src/PeerStorage.h: New file.
* src/BtRegistry.cc: New file.
* src/TrackerUpdateCommand.h: Made subclass of
BtContextAwareCommand.
* src/CopyDiskAdaptor.cc: Use the accessor methods of FileEntry.
* src/MultiDiskWriter.h: FileEntry -> FileEntryHandle
* src/PeerListenCommand.cc: Use btRuntime, peerStorage,
btContext.
* src/TorrentRequestInfo.h
(e): Removed.
(showFileEntry): Added an argument.
(getDownloadEngine): Return 0.
* src/DefaultBtAnnounce.h: New file.
* src/TorrentAutoSaveCommand.cc: Use btRuntime,
btProgressInfoFile.
* src/TrackerWatcherComand.cc: Use btRuntime, btAnnounce,
* src/PeerMessageFactory.h
(btContext): New variable.
(pieceStorage): New variable.
* src/TrackerUpdateCommand.cc: Use btRuntime, peerStorage,
btContext,
btAnnounce.
* src/DiskAdaptor.cc
(DiskAdaptor): Removed topDir.
(~DiskAdaptor): Removed topDir.
* src/PeerListenCommand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.h: Made subclass of
BtContextAwareCommand.
* src/File.h (mkdirs): New function.
* src/DefaultPeerStorage): New file.
* src/DownloadEngineFactory.h
(newTorrentConsoleEngine): Use btContext.
* src/BtContextAwareCommand.cc: New file.
* src/PeerInitiateConnectionCommand.cc: Use btRuntime,
peerStorage.
* src/PeerMessage.h
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(setBtContext): New function.
* src/Directry.cc
(Directory): New function.
(createDir): Do nothing if name.size() == 0.
* src/AnnounceList.h
(AnnounceTier): Removed.
(AnnounceTiers): Removed.
* src/DefaultPieceStorage.h: New file.
* src/Piece.cc (toString): New function.
To fix typo:
* src/main.cc (showVersion): Fixed typo.
To fix compile warning:
* src/DelegatingPeerListProcessor.cc
(canHandle): Added "return false".
2006-11-05 15:04:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-08-04 12:43:02 +00:00
|
|
|
if(a2mkdir(utf8ToWChar(dir).c_str(), DIR_OPEN_MODE) == -1) {
|
2011-08-08 12:46:10 +00:00
|
|
|
A2_LOG_DEBUG(fmt("Failed to create %s", dir.c_str()));
|
2006-11-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To divide TorrentMan into 6 classes: BtContext, BtRuntime,
PeerStorage, PieceStorage, BtAnnounce and BtProgressInfoFile
* src/TrackerWatcherComand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.cc: Use pieceStorage, btRuntime
* src/PeerAbstractCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerAbstractCommand.cc: Use btRuntime.
* src/BtContextAwareCommand.h: New class.
* src/FileEntry.h: Added accessor methods for following
variables.
(path): Made private.
(length): Made private.
(offset): Made private.
(extracted): Made private.
(requested): Made private.
(FileEntries): New definition.
(FileEntryHandle): New definition.
* src/FileEntry.cc: New file.
* src/HaveEraseCommand.h: Made subclass of
BtContextAwareCommand.
* src/HaveEraseCommand.cc: Use btRuntime, pieceStorage.
* src/PeerChokeCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerChokeCommand.cc: Use btRuntime, peerStorage,
pieceStorage.
* src/PieceStorage.h: New file.
* src/PeerInteractionCommand.h: Use btContext.
* src/PeerInteractionCommand.cc: Use pieceStorage, peerStorage,
btRuntime.
* src/DefaultBtProgressInfoFile.h: New file.
* src/DefaultBtProgressInfoFile.cc: New file.
* src/File.cc
(Util.h): New include.
(mkdirs): New function.
* src/MultiDiskAdaptor.h
(mkdir): New function.
* src/PeerListProcessor.h
(Peers): Removed.
* src/PeerInteraction.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(btAnnounce): New variable.
(getTorrentMan): Removed.
(getBtContext): New function.
* src/PeerInteraction.cc: Use btContext, peerStorage,
pieceStorage,
btAnnounce.
* src/HandshakeMessage.h
(TorrentMan.h): Removed.
* src/HandshakeMessage.cc: Use btContext.
* src/DefaultBtAnnounce.cc: New file.
* src/MultiDiskWriter.cc: Use the accessor methods of FileEntry.
* src/DefaultPieceStorage.cc: New file.
* src/DefaultBtContext.h: New file.
* src/TorrentRequestInfo.cc: Use btContext, pieceStorage.
Use the accessor methods of FileEntry.
* src/CookieBox.cc: Updated to use Util::slice().
* src/PieceMessage.cc: Use btContext, pieceStorage.
* src/common.h (SharedHandle.h): New include.
* src/PeerMessage.cc (PeerMessage): Added btContext,
peerStorage,
pieceStorage.
* src/TorrentAutoSaveCommand.h: Made subclass of
BtContextAwareCommand.
* src/DiskAdaptor.h
(topDir): Removed.
(getFileEntryFromPath): Changed the return type to
FileEntryHandle.
(setTopDir): Removed.
(getTopDir): Removed.
* src/BtContext.h: New file.
* src/DefaultPeerStorage.h: New file.
* src/PieceMessage.h (TorrentMan.h): Removed.
* src/RequestMessage.h (TorrentMan.h): Removed.
* src/TorrentDownloadEngine.h
(uploadLength): New variable.
(btContext): New variable.
(btRuntime): New variable.
(pieceStorage): New variable.
(peerStorage): New variable.
(btAnnounce): New variable.
(btProgressInfoFile): New variable.
(torrentMan): Removed.
(setBtContext): New function.
* src/TorrentDownloadEngine.cc: Use BtContext, BtRuntime,
pieceStorage,
peerStorage, btAnnounce, btProgressInfoFile.
* src/Piece.h
(toString): New function.
(Pieces): New type definition.
* src/Peer.h
(active): New variable.
(Peer): Added active.
(activate): Set active to true.
(deactivate): Set active to false.
(isActive): New function.
(Peers): New type definition.
* src/DirectDiskAdaptor.cc
(getFilePath): Use the accessor methods of FileEntry.
* src/TorrentConsoleDownloadEngine.h
(afterEachIteration): New function.
* src/TorrentConsoleDownloadEngine.cc
(haltRequested): New variable.
(sendStatistics): Use pieceStorage, btRuntime.
(afterEachIteration): New function.
* src/AnnounceList: AnnounceTier->AnnounceTierHandle.
* src/Directry.h
(Directory): New function.
(DirectoryHandle): New type definition.
* src/BtProgressInfoFile.h: New file.
* src/RequestMessage.cc: Use pieceStorage.
* src/BtRuntime.h: New file.
* src/DefaultBtContext.cc: New file.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/MultiDiskAdaptor.cc
(mkdir): New function.
(openFile): Call mkdir().
(initAndOpenFile): Call mkdir().
* src/CancelMessage.h
(TorrentMan.h): Removed.
* src/RejectMessage.h
(TorrentMan.h): Removed.
* src/DownloadEngineFactory.cc
(DefaultPieceStorage.h): New include.
(DefaultPeerStorage.h): New include.
(DefaultBtAnnounce.h): New include.
(DefaultBtProgressInfoFile.h): New include.
(newTorrentConsoleEngine): Rewritten.
* src/ShareRatioSeedCriteria.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(btRuntime): New variable.
(evaluate): Use btContext, btRuntime, peerStorage.
* src/AnnounceTier.h: New file.
* src/BtAnnounce.h: New file.
* src/BtRegistry.h: New file.
* src/PeerInitiateConnectionCommand.h: Added btContext.
* src/PeerConnection.h (TorrentMan.h): Removed.
* src/PeerMessageFactory.cc: Use btContext, pieceStorage.
* src/Util.h
(slice): Added an argument.
* src/Util.cc
(slice): Added an argument to control whether trim is made or
not.
* src/PeerStorage.h: New file.
* src/BtRegistry.cc: New file.
* src/TrackerUpdateCommand.h: Made subclass of
BtContextAwareCommand.
* src/CopyDiskAdaptor.cc: Use the accessor methods of FileEntry.
* src/MultiDiskWriter.h: FileEntry -> FileEntryHandle
* src/PeerListenCommand.cc: Use btRuntime, peerStorage,
btContext.
* src/TorrentRequestInfo.h
(e): Removed.
(showFileEntry): Added an argument.
(getDownloadEngine): Return 0.
* src/DefaultBtAnnounce.h: New file.
* src/TorrentAutoSaveCommand.cc: Use btRuntime,
btProgressInfoFile.
* src/TrackerWatcherComand.cc: Use btRuntime, btAnnounce,
* src/PeerMessageFactory.h
(btContext): New variable.
(pieceStorage): New variable.
* src/TrackerUpdateCommand.cc: Use btRuntime, peerStorage,
btContext,
btAnnounce.
* src/DiskAdaptor.cc
(DiskAdaptor): Removed topDir.
(~DiskAdaptor): Removed topDir.
* src/PeerListenCommand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.h: Made subclass of
BtContextAwareCommand.
* src/File.h (mkdirs): New function.
* src/DefaultPeerStorage): New file.
* src/DownloadEngineFactory.h
(newTorrentConsoleEngine): Use btContext.
* src/BtContextAwareCommand.cc: New file.
* src/PeerInitiateConnectionCommand.cc: Use btRuntime,
peerStorage.
* src/PeerMessage.h
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(setBtContext): New function.
* src/Directry.cc
(Directory): New function.
(createDir): Do nothing if name.size() == 0.
* src/AnnounceList.h
(AnnounceTier): Removed.
(AnnounceTiers): Removed.
* src/DefaultPieceStorage.h: New file.
* src/Piece.cc (toString): New function.
To fix typo:
* src/main.cc (showVersion): Fixed typo.
To fix compile warning:
* src/DelegatingPeerListProcessor.cc
(canHandle): Added "return false".
2006-11-05 15:04:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-26 12:16:57 +00:00
|
|
|
|
|
|
|
mode_t File::mode()
|
|
|
|
{
|
2008-07-06 04:38:54 +00:00
|
|
|
a2_struct_stat fstat;
|
2007-03-26 12:16:57 +00:00
|
|
|
if(fillStat(fstat) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return fstat.st_mode;
|
|
|
|
}
|
2007-06-30 09:52:39 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string File::getBasename() const
|
2007-06-30 09:52:39 +00:00
|
|
|
{
|
2011-08-18 08:27:41 +00:00
|
|
|
std::string::size_type lastSlashIndex =
|
|
|
|
name_.find_last_of(getPathSeparators());
|
2008-03-16 09:04:21 +00:00
|
|
|
if(lastSlashIndex == std::string::npos) {
|
2010-06-21 13:51:56 +00:00
|
|
|
return name_;
|
2008-03-16 09:04:21 +00:00
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
return name_.substr(lastSlashIndex+1);
|
2008-03-16 09:04:21 +00:00
|
|
|
}
|
2007-06-30 09:52:39 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string File::getDirname() const
|
2007-06-30 09:52:39 +00:00
|
|
|
{
|
2011-08-18 08:27:41 +00:00
|
|
|
std::string::size_type lastSlashIndex =
|
|
|
|
name_.find_last_of(getPathSeparators());
|
2008-03-16 09:04:21 +00:00
|
|
|
if(lastSlashIndex == std::string::npos) {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(name_.empty()) {
|
2008-05-13 14:15:23 +00:00
|
|
|
return A2STR::NIL;
|
2008-03-16 09:04:21 +00:00
|
|
|
} else {
|
2012-09-24 14:01:08 +00:00
|
|
|
return ".";
|
2008-03-16 09:04:21 +00:00
|
|
|
}
|
|
|
|
} else if(lastSlashIndex == 0) {
|
2012-09-24 14:01:08 +00:00
|
|
|
return "/";
|
2008-03-16 09:04:21 +00:00
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
return name_.substr(0, lastSlashIndex);
|
2008-03-16 09:04:21 +00:00
|
|
|
}
|
2007-06-30 09:52:39 +00:00
|
|
|
}
|
2007-07-23 13:04:48 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
bool File::isDir(const std::string& filename)
|
2007-07-23 13:04:48 +00:00
|
|
|
{
|
|
|
|
return File(filename).isDir();
|
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
bool File::renameTo(const std::string& dest)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
/* MinGW's rename() doesn't delete an existing destination */
|
2011-08-04 12:43:02 +00:00
|
|
|
if (_waccess(utf8ToWChar(dest).c_str(), 0) == 0) {
|
|
|
|
if (a2unlink(utf8ToWChar(dest).c_str()) != 0) {
|
2007-10-11 16:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // __MINGW32__
|
2011-08-04 12:43:02 +00:00
|
|
|
if(a2rename(utf8ToWChar(name_).c_str(), utf8ToWChar(dest).c_str()) == 0) {
|
2010-06-21 13:51:56 +00:00
|
|
|
name_ = dest;
|
2007-10-11 16:58:24 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-09-07 14:38:26 +00:00
|
|
|
bool File::utime(const Time& actime, const Time& modtime) const
|
|
|
|
{
|
2011-08-19 13:35:15 +00:00
|
|
|
#if defined HAVE_UTIMES && !(defined __MINGW32__)
|
|
|
|
struct timeval times[2] = {
|
|
|
|
{ actime.getTime(), 0 },
|
|
|
|
{ modtime.getTime(), 0 }
|
|
|
|
};
|
|
|
|
return utimes(name_.c_str(), times) == 0;
|
|
|
|
#else // !HAVE_UTIMES
|
2011-08-04 12:43:02 +00:00
|
|
|
a2utimbuf ub;
|
2008-09-07 14:38:26 +00:00
|
|
|
ub.actime = actime.getTime();
|
|
|
|
ub.modtime = modtime.getTime();
|
2011-08-04 12:43:02 +00:00
|
|
|
return a2utime(utf8ToWChar(name_).c_str(), &ub) == 0;
|
2011-08-19 13:35:15 +00:00
|
|
|
#endif // !HAVE_UTIMES
|
2008-09-07 14:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Time File::getModifiedTime()
|
|
|
|
{
|
|
|
|
a2_struct_stat fstat;
|
|
|
|
if(fillStat(fstat) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return Time(fstat.st_mtime);
|
|
|
|
}
|
|
|
|
|
2009-06-20 16:47:54 +00:00
|
|
|
std::string File::getCurrentDir()
|
|
|
|
{
|
2011-08-04 12:43:02 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
const size_t buflen = 2048;
|
|
|
|
wchar_t buf[buflen];
|
|
|
|
if(_wgetcwd(buf, buflen)) {
|
|
|
|
return wCharToUtf8(buf);
|
|
|
|
} else {
|
2012-09-24 14:01:08 +00:00
|
|
|
return ".";
|
2011-08-04 12:43:02 +00:00
|
|
|
}
|
|
|
|
#else // !__MINGW32__
|
2009-09-19 09:02:58 +00:00
|
|
|
const size_t buflen = 2048;
|
|
|
|
char buf[buflen];
|
|
|
|
if(getcwd(buf, buflen)) {
|
|
|
|
return std::string(buf);
|
|
|
|
} else {
|
2012-09-24 14:01:08 +00:00
|
|
|
return ".";
|
2009-06-20 16:47:54 +00:00
|
|
|
}
|
2011-08-04 12:43:02 +00:00
|
|
|
#endif // !__MINGW32__
|
2009-06-20 16:47:54 +00:00
|
|
|
}
|
|
|
|
|
2012-09-24 14:04:42 +00:00
|
|
|
const char* File::getPathSeparators()
|
2011-08-18 08:27:41 +00:00
|
|
|
{
|
|
|
|
#ifdef __MINGW32__
|
2012-09-24 14:04:42 +00:00
|
|
|
return "/\\";
|
2011-08-18 08:27:41 +00:00
|
|
|
#else // !__MINGW32__
|
2012-09-24 14:04:42 +00:00
|
|
|
return "/";
|
2011-08-18 08:27:41 +00:00
|
|
|
#endif // !__MINGW32__
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|