📄 ftpclient.cpp
字号:
{
switch( representation.Format().AsEnum() )
{
case CTypeFormat::tfNonPrint: strCmd += _T(" N"); break;
case CTypeFormat::tfTelnetFormat: strCmd += _T(" T"); break;
case CTypeFormat::tfCarriageControl: strCmd += _T(" C"); break;
default:
ASSERT( false );
}
}
CReply Reply;
if( !SendCommand(strCmd, Reply) )
return FTP_ERROR;
return SimpleErrorCheck(Reply);
}
/// Executes the ftp command CWD (CHANGE WORKING DIRECTORY)
/// This command allows the user to work with a different directory or dataset
/// for file storage or retrieval without altering his login or accounting
/// information. Transfer parameters are similarly unchanged.
/// @param[in] strDirectory Pathname specifying a directory or other system
/// dependent file group designator.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::ChangeWorkingDirectory(const tstring& strDirectory) const
{
ASSERT( !strDirectory.empty() );
CReply Reply;
if( !SendCommand(_T("CWD ")+strDirectory, Reply) )
return FTP_ERROR;
return SimpleErrorCheck(Reply);
}
/// Executes the ftp command MKD (MAKE DIRECTORY)
/// This command causes the directory specified in the pathname to be created
/// as a directory (if the pathname is absolute) or as a subdirectory of the
/// current working directory (if the pathname is relative).
/// @pararm[in] strDirectory Pathname specifying a directory to be created.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::MakeDirectory(const tstring& strDirectory) const
{
ASSERT( !strDirectory.empty() );
CReply Reply;
if( !SendCommand(_T("MKD ")+strDirectory, Reply) )
return FTP_ERROR;
return SimpleErrorCheck(Reply);
}
/// Executes the ftp command SITE (SITE PARAMETERS)
/// This command is used by the server to provide services specific to his
/// system that are essential to file transfer but not sufficiently universal
/// to be included as commands in the protocol. The nature of these services
/// and the specification of their syntax can be stated in a reply to the HELP
/// SITE command.
/// @param[in] strCmd Command to be executed.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::SiteParameters(const tstring& strCmd) const
{
ASSERT( !strCmd.empty() );
CReply Reply;
if( !SendCommand(_T("SITE ")+strCmd, Reply) )
return FTP_ERROR;
return SimpleErrorCheck(Reply);
}
/// Executes the ftp command HELP
/// This command shall cause the server to send helpful information regarding
/// its implementation status over the control connection to the user.
/// The command may take an argument (e.g., any command name) and return more
/// specific information as a response. The reply is type 211 or 214.
/// It is suggested that HELP be allowed before entering a USER command. The
/// server may use this reply to specify site-dependent parameters, e.g., in
/// response to HELP SITE.
/// @param[in] strTopic Topic of the requested help.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::Help(const tstring& strTopic) const
{
CReply Reply;
if( !SendCommand(strTopic.empty()?_T("HELP"):_T("HELP ")+strTopic, Reply) )
return FTP_ERROR;
return SimpleErrorCheck(Reply);
}
/// Executes the ftp command DELE (DELETE)
/// This command causes the file specified in the pathname to be deleted at the
/// server site. If an extra level of protection is desired (such as the query,
/// "Do you really wish to delete?"), it should be provided by the user-FTP process.
/// @param[in] strFile Pathname of the file to delete.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::Delete(const tstring& strFile) const
{
ASSERT( !strFile.empty() );
CReply Reply;
if( !SendCommand(_T("DELE ")+strFile, Reply) )
return FTP_ERROR;
return SimpleErrorCheck(Reply);
}
/// Executes the ftp command RMD (REMOVE DIRECTORY)
/// This command causes the directory specified in the pathname to be removed
/// as a directory (if the pathname is absolute) or as a subdirectory of the
/// current working directory (if the pathname is relative).
/// @param[in] strDirectory Pathname of the directory to delete.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::RemoveDirectory(const tstring& strDirectory) const
{
ASSERT( !strDirectory.empty() );
CReply Reply;
if( !SendCommand(_T("RMD ")+strDirectory, Reply) )
return FTP_ERROR;
return SimpleErrorCheck(Reply);
}
/// Executes the ftp command STRU (FILE STRUCTURE)
/// see documentation of nsFTP::CStructure
/// The default structure is File.
/// @param[in] crStructure see Documentation of nsFTP::CStructure
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::FileStructure(const CStructure& crStructure) const
{
tstring strStructure;
switch( crStructure.AsEnum() )
{
case CStructure::scFile: strStructure=_T("F"); break;
case CStructure::scRecord: strStructure=_T("R"); break;
case CStructure::scPage: strStructure=_T("P"); break;
default:
ASSERT( false );
return FTP_ERROR;
}
CReply Reply;
if( !SendCommand(_T("STRU ")+strStructure, Reply) )
return FTP_ERROR;
return SimpleErrorCheck(Reply);
}
/// Executes the ftp command MODE (TRANSFER MODE)
/// see documentation of nsFTP::CTransferMode
/// The default transfer mode is Stream.
/// @param[in] crTransferMode see Documentation of nsFTP::CTransferMode
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::TransferMode(const CTransferMode& crTransferMode) const
{
tstring strMode;
switch( crTransferMode.AsEnum() )
{
case CTransferMode::tmStream: strMode=_T("S"); break;
case CTransferMode::tmBlock: strMode=_T("B"); break;
case CTransferMode::tmCompressed: strMode=_T("C"); break;
default:
ASSERT( false );
return FTP_ERROR;
}
CReply Reply;
if( !SendCommand(_T("MODE ")+strMode, Reply) )
return FTP_ERROR;
return SimpleErrorCheck(Reply);
}
/// Executes the ftp command STAT (STATUS)
/// This command shall cause a status response to be sent over the control
/// connection in the form of a reply. The command may be sent during a file
/// transfer (along with the Telnet IP and Synch signals--see the Section on
/// FTP Commands) in which case the server will respond with the status of the
/// operation in progress, or it may be sent between file transfers. In the
/// latter case, the command may have an argument field.
/// @param[in] strPath If the argument is a pathname, the command is analogous
/// to the "list" command except that data shall be transferred
/// over the control connection. If a partial pathname is
/// given, the server may respond with a list of file names or
/// attributes associated with that specification. If no argument
/// is given, the server should return general status information
/// about the server FTP process. This should include current
/// values of all transfer parameters and the status of connections.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::Status(const tstring& strPath) const
{
CReply Reply;
if( !SendCommand(strPath.empty()?_T("STAT"):_T("STAT ")+strPath, Reply) )
return FTP_ERROR;
return SimpleErrorCheck(Reply);
}
/// Executes the ftp command ALLO (ALLOCATE)
/// This command may be required by some servers to reserve sufficient storage
/// to accommodate the new file to be transferred.
/// @param[in] iReserveBytes The argument shall be a decimal integer representing
/// the number of bytes (using the logical byte size) of
/// storage to be reserved for the file. For files sent
/// with record or page structure a maximum record or page
/// size (in logical bytes) might also be necessary; this
/// is indicated by a decimal integer in a second argument
/// field of the command.
/// @pararm[in] piMaxPageOrRecordSize This second argument is optional. This command
/// shall be followed by a STORe or APPEnd command.
/// The ALLO command should be treated as a NOOP (no operation)
/// by those servers which do not require that the maximum
/// size of the file be declared beforehand, and those servers
/// interested in only the maximum record or page size should
/// accept a dummy value in the first argument and ignore it.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::Allocate(int iReserveBytes, const int* piMaxPageOrRecordSize/*=NULL*/) const
{
tstring strCmd;
if( piMaxPageOrRecordSize==NULL )
strCmd = CMakeString() << _T("ALLO ") << iReserveBytes;
else
strCmd = CMakeString() << _T("ALLO ") << iReserveBytes << _T(" R ") << *piMaxPageOrRecordSize;
CReply Reply;
if( !SendCommand(strCmd, Reply) )
return FTP_ERROR;
return SimpleErrorCheck(Reply);
}
/// Executes the ftp command SMNT ()
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::StructureMount(const tstring& strPath) const
{
CReply Reply;
if( !SendCommand(_T("SMNT ")+strPath, Reply) )
return FTP_ERROR;
return SimpleErrorCheck(Reply);
}
/// Executes the ftp command (STRUCTURE MOUNT)
/// This command allows the user to mount a different file system data structure
/// without altering his login or accounting information. Transfer parameters
/// are similarly unchanged. The argument is a pathname specifying a directory
/// or other system dependent file group designator.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::Reinitialize() const
{
CReply Reply;
if( !SendCommand(_T("REIN"), Reply) )
return FTP_ERROR;
if( Reply.Code().IsPositiveCompletionReply() )
return FTP_OK;
else if( Reply.Code().IsPositivePreliminaryReply() )
{
if( !GetResponse(Reply) || !Reply.Code().IsPositiveCompletionReply() )
return FTP_ERROR;
}
else if( Reply.Code().IsNegativeReply() )
return FTP_NOTOK;
ASSERT( Reply.Code().IsPositiveIntermediateReply() );
return FTP_ERROR;
}
/// Executes the ftp command REST (RESTART)
/// This command does not cause file transfer but skips over the file to the
/// specified data checkpoint. This command shall be immediately followed
/// by the appropriate FTP service command which shall cause file transfer
/// to resume.
/// @param[in] dwPosition Represents the server marker at which file transfer
/// is to be restarted.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::Restart(DWORD dwPosition) const
{
tstring strCmd;
strCmd = CMakeString() << _T("REST ") << dwPosition;
CReply Reply;
if( !SendCommand(strCmd, Reply) )
return FTP_ERROR;
if( Reply.Code().IsPositiveIntermediateReply() )
return FTP_OK;
else if( Reply.Code().IsNegativeReply() )
return FTP_NOTOK;
ASSERT( Reply.Code().IsPositiveReply() );
return FTP_ERROR;
}
/// Executes the ftp command SIZE
/// Return size of file.
/// SIZE is not specified in RFC 959.
/// @param[in] Pathname of a file.
/// @param[out] Size of the file specified in pathname.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::FileSize(const tstring& strPath, long& lSize) const
{
CReply Reply;
if( !SendCommand(_T("SIZE ")+strPath, Reply) )
return FTP_ERROR;
lSize = CCnv::TStringToLong(Reply.Value().substr(4).c_str());
return SimpleErrorCheck(Reply);
}
/// Executes the ftp command MDTM
/// Show last modification time of file.
/// MDTM is not specified in RFC 959.
/// @param[in] strPath Pathname of a file.
/// @param[out] strModificationTime Modification time of the file specified in pathname.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::FileModificationTime(const tstring& strPath, tstring& strModificationTime) const
{
strModificationTime.erase();
CReply Reply;
if( !SendCommand(_T("MDTM ")+strPath, Reply) )
return FTP_ERROR;
if( Reply.Value().length()>=18 )
{
tstring strTemp(Reply.Value().substr(4));
size_t iPos=strTemp.find(_T('.'));
if( iPos>-1 )
strTemp = strTemp.substr(0, iPos);
if( strTemp.length()==14 )
strModificationTime=strTemp;
}
if( strModificationTime.empty() )
return FTP_ERROR;
return SimpleErrorCheck(Reply);
}
/// Show last modification time of file.
/// @param[in] strPath Pathname of a file.
/// @param[out] tmModificationTime Modification time of the file specified in pathname.
/// @return see return values of CFTPClient::SimpleErrorCheck
int CFTPClient::FileModificationTime(const tstring& strPath, tm& tmModificationTime) const
{
tstring strTemp;
const int iRet = FileModificationTime(strPath, strTemp);
memset(&tmModificationTime, 0, sizeof(tmModificationTime));
if( iRet==FTP_OK )
{
tmModificationTime.tm_year = CCnv::TStringToLong(strTemp.substr(0, 4).c_str());
tmModificationTime.tm_mon = CCnv::TStringToLong(strTemp.substr(4, 2).c_str());
tmModificationTime.tm_mday = CCnv::TStringToLong(strTemp.substr(6, 2).c_str());
tmModificationTime.tm_hour = CCnv::TStringToLong(strTemp.substr(8, 2).c_str());
tmModificationTime.tm_min = CCnv::TStringToLong(strTemp.substr(10, 2).c_str());
tmModificationTime.tm_sec = CCnv::TStringToLong(strTemp.su
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -