FileZillaApi.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. // FileZilla - a Windows ftp client
  2. // Copyright (C) 2002-2004 - Tim Kosse <[email protected]>
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the License, or (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program; if not, write to the Free Software
  13. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. // FileZillaApi.cpp: Implementierung der Klasse CFileZillaApi.
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. #include "FileZillaApi.h"
  19. #include "mainthread.h"
  20. #ifndef MPEXT_NO_CACHE
  21. #include "directorycache.h"
  22. #endif
  23. #ifdef _DEBUG
  24. #undef THIS_FILE
  25. static char THIS_FILE[]=__FILE__;
  26. #define new DEBUG_NEW
  27. #endif
  28. //////////////////////////////////////////////////////////////////////
  29. // Konstruktion/Destruktion
  30. //////////////////////////////////////////////////////////////////////
  31. CFileZillaApi::CFileZillaApi()
  32. {
  33. m_hOwnerWnd=0;
  34. #ifndef MPEXT
  35. //Create Message IDs
  36. m_nReplyMessageID=RegisterWindowMessage( _T("FileZillaApiReplyMessage{8EF2E328-685E-4815-A9B9-823512F8381D}") );
  37. #else
  38. m_nReplyMessageID=0;
  39. #endif
  40. m_nInternalMessageID=0;
  41. m_pMainThread=0;
  42. m_bInitialized=FALSE;
  43. }
  44. CFileZillaApi::~CFileZillaApi()
  45. {
  46. Destroy();
  47. }
  48. #ifndef MPEXT
  49. int CFileZillaApi::Init(HWND hOwnerWnd, int nReplyMessageID /*=0*/)
  50. #else
  51. int CFileZillaApi::Init(CApiLog * pParent, CFileZillaTools * pTools)
  52. #endif
  53. {
  54. //Check parameters
  55. //-> No check needed, if hOwnerWnd is NULL, use blocking mode and don't send status messages
  56. //Check if call allowed
  57. if (m_bInitialized)
  58. return FZ_REPLY_ALREADYINIZIALIZED;
  59. //Initialize variables
  60. #ifndef MPEXT
  61. if (nReplyMessageID)
  62. m_nReplyMessageID=nReplyMessageID;
  63. m_hOwnerWnd=hOwnerWnd;
  64. #endif
  65. m_nInternalMessageID=RegisterWindowMessage( _T("FileZillaInternalApiMessage{F958620E-040C-4b33-A091-7E04E10AA660}") );
  66. if (!m_nInternalMessageID)
  67. return FZ_REPLY_NOTINITIALIZED;
  68. //Create thread object
  69. m_pMainThread = CMainThread::Create(THREAD_PRIORITY_BELOW_NORMAL, CREATE_SUSPENDED);
  70. //Initialize Thread variables
  71. m_pMainThread->m_nInternalMessageID=m_nInternalMessageID;
  72. m_pMainThread->m_nReplyMessageID=m_nReplyMessageID;
  73. m_pMainThread->m_hOwnerWnd=m_hOwnerWnd;
  74. m_pMainThread->m_hOwnerWnd=m_hOwnerWnd;
  75. m_pMainThread->m_pTools=pTools;
  76. #ifndef MPEXT
  77. m_pMainThread->InitLog(m_hOwnerWnd, m_nReplyMessageID);
  78. #else
  79. m_pMainThread->InitLog(pParent);
  80. #endif
  81. //Resume Thread
  82. m_pMainThread->ResumeThread();
  83. //Initialization OK
  84. m_bInitialized=TRUE;
  85. return FZ_REPLY_OK;
  86. }
  87. unsigned int CFileZillaApi::GetMessageID()
  88. {
  89. return m_nReplyMessageID;
  90. }
  91. int CFileZillaApi::IsConnected()
  92. {
  93. if (!m_bInitialized)
  94. return FZ_REPLY_NOTINITIALIZED;
  95. return m_pMainThread->IsConnected()?FZ_REPLY_OK:FZ_REPLY_NOTCONNECTED;
  96. }
  97. int CFileZillaApi::IsBusy()
  98. {
  99. if (!m_bInitialized)
  100. return FZ_REPLY_NOTINITIALIZED;
  101. return m_pMainThread->IsBusy()?FZ_REPLY_BUSY:FZ_REPLY_IDLE;
  102. }
  103. int CFileZillaApi::Connect(const t_server &server)
  104. {
  105. //Check parameters
  106. if (server.host==_MPT("") || server.port<1 || server.port>65535)
  107. return FZ_REPLY_INVALIDPARAM;
  108. #ifndef MPEXT_NO_GSS
  109. BOOL bUseGSS = FALSE;
  110. if (COptions::GetOptionVal(OPTION_USEGSS))
  111. {
  112. USES_CONVERSION;
  113. CString GssServers = COptions::GetOption(OPTION_GSSSERVERS);
  114. hostent *fullname = gethostbyname(T2CA(server.host));
  115. CString host;
  116. if (fullname)
  117. host = fullname->h_name;
  118. else
  119. host = server.host;
  120. host.MakeLower();
  121. int i;
  122. while ((i=GssServers.Find( _T(";") ))!=-1)
  123. {
  124. if ((_MPT(".")+GssServers.Left(i))==host.Right(GssServers.Left(i).GetLength()+1) || GssServers.Left(i)==host)
  125. {
  126. bUseGSS = TRUE;
  127. break;
  128. }
  129. GssServers = GssServers.Mid(i+1);
  130. }
  131. }
  132. if (!bUseGSS && server.user == _MPT(""))
  133. return FZ_REPLY_INVALIDPARAM;
  134. #endif
  135. if (!(server.nServerType&FZ_SERVERTYPE_HIGHMASK))
  136. return FZ_REPLY_INVALIDPARAM;
  137. //Check if call allowed
  138. if (!m_bInitialized)
  139. return FZ_REPLY_NOTINITIALIZED;
  140. if (m_pMainThread->IsBusy())
  141. return FZ_REPLY_BUSY;
  142. t_command command;
  143. command.id=FZ_COMMAND_CONNECT;
  144. command.server=server;
  145. m_pMainThread->Command(command);
  146. if (m_hOwnerWnd)
  147. return FZ_REPLY_WOULDBLOCK;
  148. else
  149. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  150. }
  151. int CFileZillaApi::List(int nListMode /*=FZ_LIST_USECACHE*/)
  152. {
  153. //Check if call allowed
  154. if (!m_bInitialized)
  155. return FZ_REPLY_NOTINITIALIZED;
  156. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  157. return FZ_REPLY_NOTCONNECTED;
  158. if (nListMode&FZ_LIST_REALCHANGE)
  159. return FZ_REPLY_INVALIDPARAM;
  160. #ifndef MPEXT_NO_CACHE
  161. if (nListMode&FZ_LIST_FORCECACHE)
  162. nListMode|=FZ_LIST_USECACHE;
  163. //Check if current dir is cached
  164. CServerPath path;
  165. if (nListMode&FZ_LIST_USECACHE)
  166. {
  167. if (!m_pMainThread->GetWorkingDirPath(path) || path.IsEmpty())
  168. m_pMainThread->GetCurrentPath(path);
  169. if (!path.IsEmpty())
  170. {
  171. t_server server;
  172. BOOL res=m_pMainThread->GetCurrentServer(server);
  173. if (res)
  174. {
  175. t_directory *directory=new t_directory;
  176. CDirectoryCache cache;
  177. res=cache.Lookup(path,server,*directory);
  178. if (res)
  179. {
  180. BOOL bExact=TRUE;
  181. if (nListMode & FZ_LIST_EXACT)
  182. for (int i=0;i<directory->num;i++)
  183. if (directory->direntry[i].bUnsure || (directory->direntry[i].size==-1 && !directory->direntry[i].dir))
  184. {
  185. bExact=FALSE;
  186. break;
  187. }
  188. if (bExact)
  189. {
  190. m_pMainThread->SetWorkingDir(directory);
  191. delete directory;
  192. return FZ_REPLY_OK;
  193. }
  194. }
  195. delete directory;
  196. }
  197. }
  198. }
  199. #else
  200. CServerPath path;
  201. // seems to be incorrectly skipped when cache is not required
  202. if (!m_pMainThread->GetWorkingDirPath(path) || path.IsEmpty())
  203. m_pMainThread->GetCurrentPath(path);
  204. #endif
  205. if (m_pMainThread->IsBusy())
  206. return FZ_REPLY_BUSY;
  207. #ifndef MPEXT_NO_CACHE
  208. if (nListMode&FZ_LIST_FORCECACHE)
  209. return FZ_REPLY_ERROR;
  210. #endif
  211. t_command command;
  212. command.id=FZ_COMMAND_LIST;
  213. command.path = path;
  214. command.param4=nListMode;
  215. m_pMainThread->Command(command);
  216. if (m_hOwnerWnd)
  217. return FZ_REPLY_WOULDBLOCK;
  218. else
  219. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  220. }
  221. int CFileZillaApi::Cancel()
  222. {
  223. //Check if call allowed
  224. if (!m_bInitialized)
  225. return FZ_REPLY_NOTINITIALIZED;
  226. if (IsBusy()!=FZ_REPLY_BUSY)
  227. return FZ_REPLY_NOTBUSY;
  228. m_pMainThread->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_CANCEL, 0);
  229. return FZ_REPLY_WOULDBLOCK;
  230. }
  231. void CFileZillaApi::Destroy()
  232. {
  233. if (!m_bInitialized)
  234. return;
  235. ASSERT(m_pMainThread);
  236. HANDLE tmp=m_pMainThread->m_hThread;
  237. m_pMainThread->Quit();
  238. //Wait for the main thread to quit
  239. WaitForSingleObject(tmp, 10000);
  240. #ifndef MPEXT
  241. PostMessage(m_hOwnerWnd, m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_QUITCOMPLETE, 0), 0);
  242. #endif
  243. m_pMainThread=0;
  244. m_bInitialized=FALSE;
  245. }
  246. int CFileZillaApi::Disconnect()
  247. {
  248. //Check if call allowed
  249. if (!m_bInitialized)
  250. return FZ_REPLY_NOTINITIALIZED;
  251. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  252. return FZ_REPLY_NOTCONNECTED;
  253. if (IsBusy()==FZ_REPLY_BUSY)
  254. return FZ_REPLY_BUSY;
  255. m_pMainThread->PostThreadMessage(m_nInternalMessageID,FZAPI_THREADMSG_DISCONNECT,0);
  256. return FZ_REPLY_WOULDBLOCK;
  257. }
  258. int CFileZillaApi::Command(t_command *pCommand)
  259. {
  260. //Check if call allowed
  261. if (!m_bInitialized)
  262. return FZ_REPLY_NOTINITIALIZED;
  263. //Dispatch command to command specific functions
  264. switch(pCommand->id)
  265. {
  266. case FZ_COMMAND_LIST:
  267. if (pCommand->param1!=_MPT(""))
  268. return List(pCommand->path,pCommand->param1,pCommand->param4);
  269. else if (!pCommand->path.IsEmpty())
  270. return List(pCommand->path,pCommand->param4);
  271. else
  272. return List(pCommand->param4);
  273. break;
  274. case FZ_COMMAND_CONNECT:
  275. return Connect(pCommand->server);
  276. break;
  277. case FZ_COMMAND_DISCONNECT:
  278. return Disconnect();
  279. break;
  280. case FZ_COMMAND_FILETRANSFER:
  281. return FileTransfer(pCommand->transferfile);
  282. break;
  283. case FZ_COMMAND_DELETE:
  284. return Delete(pCommand->param1, pCommand->path);
  285. break;
  286. case FZ_COMMAND_REMOVEDIR:
  287. return RemoveDir(pCommand->param1, pCommand->path);
  288. break;
  289. case FZ_COMMAND_MAKEDIR:
  290. return MakeDir(pCommand->path);
  291. break;
  292. case FZ_COMMAND_RENAME:
  293. return Rename(pCommand->param1, pCommand->param2, pCommand->path, pCommand->newPath);
  294. break;
  295. case FZ_COMMAND_CUSTOMCOMMAND:
  296. return CustomCommand(pCommand->param1);
  297. break;
  298. case FZ_COMMAND_CHMOD:
  299. return Chmod(pCommand->param4, pCommand->param1, pCommand->path);
  300. break;
  301. }
  302. return FZ_REPLY_INVALIDPARAM;
  303. }
  304. int CFileZillaApi::List(const CServerPath& path, int nListMode /*=FZ_LIST_USECACHE*/)
  305. {
  306. //Check if call allowed
  307. if (!m_bInitialized)
  308. return FZ_REPLY_NOTINITIALIZED;
  309. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  310. return FZ_REPLY_NOTCONNECTED;
  311. #ifndef MPEXT_NO_CACHE
  312. if ( (nListMode&(FZ_LIST_FORCECACHE|FZ_LIST_REALCHANGE))==(FZ_LIST_FORCECACHE|FZ_LIST_REALCHANGE) )
  313. return FZ_REPLY_INVALIDPARAM;
  314. if (nListMode&FZ_LIST_FORCECACHE)
  315. nListMode|=FZ_LIST_USECACHE;
  316. #endif
  317. if (path.IsEmpty())
  318. return FZ_REPLY_INVALIDPARAM;
  319. #ifndef MPEXT_NO_CACHE
  320. //Check if current dir is cached
  321. if (nListMode&FZ_LIST_USECACHE && !(nListMode&FZ_LIST_REALCHANGE))
  322. {
  323. t_server server;
  324. BOOL res=m_pMainThread->GetCurrentServer(server);
  325. if (res)
  326. {
  327. t_directory *directory=new t_directory;
  328. CDirectoryCache cache;
  329. res=cache.Lookup(path,server,*directory);
  330. if (res)
  331. {
  332. BOOL bExact=TRUE;
  333. if (nListMode & FZ_LIST_EXACT)
  334. for (int i=0;i<directory->num;i++)
  335. if (directory->direntry[i].bUnsure || (directory->direntry[i].size==-1 && !directory->direntry[i].dir))
  336. {
  337. bExact=FALSE;
  338. break;
  339. }
  340. if (bExact)
  341. {
  342. m_pMainThread->SetWorkingDir(directory);
  343. delete directory;
  344. return FZ_REPLY_OK;
  345. }
  346. }
  347. delete directory;
  348. }
  349. }
  350. #endif
  351. if (m_pMainThread->IsBusy())
  352. return FZ_REPLY_BUSY;
  353. #ifndef MPEXT_NO_CACHE
  354. if (nListMode&FZ_LIST_FORCECACHE)
  355. return FZ_REPLY_ERROR;
  356. #endif
  357. t_command command;
  358. command.id=FZ_COMMAND_LIST;
  359. command.path=path;
  360. command.param4=nListMode;
  361. m_pMainThread->Command(command);
  362. if (m_hOwnerWnd)
  363. return FZ_REPLY_WOULDBLOCK;
  364. else
  365. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  366. }
  367. int CFileZillaApi::List(const CServerPath& parent, CString dirname, int nListMode /*=FZ_LIST_USECACHE*/)
  368. {
  369. //Check if call allowed
  370. if (!m_bInitialized)
  371. return FZ_REPLY_NOTINITIALIZED;
  372. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  373. return FZ_REPLY_NOTCONNECTED;
  374. #ifndef MPEXT_NO_CACHE
  375. if ( (nListMode&(FZ_LIST_FORCECACHE|FZ_LIST_REALCHANGE))==(FZ_LIST_FORCECACHE|FZ_LIST_REALCHANGE) )
  376. return FZ_REPLY_INVALIDPARAM;
  377. if (nListMode&FZ_LIST_FORCECACHE)
  378. nListMode|=FZ_LIST_USECACHE;
  379. #endif
  380. if (dirname==_MPT("") || parent.IsEmpty())
  381. return FZ_REPLY_INVALIDPARAM;
  382. #ifndef MPEXT_NO_CACHE
  383. //Check if current dir is cached
  384. if (nListMode&FZ_LIST_USECACHE && !(nListMode&FZ_LIST_REALCHANGE))
  385. {
  386. t_server server;
  387. BOOL res=m_pMainThread->GetCurrentServer(server);
  388. if (res)
  389. {
  390. t_directory *directory=new t_directory;
  391. CDirectoryCache cache;
  392. res=cache.Lookup(parent,dirname,server,*directory);
  393. if (res)
  394. {
  395. BOOL bExact=TRUE;
  396. if (nListMode & FZ_LIST_EXACT)
  397. for (int i=0;i<directory->num;i++)
  398. if (directory->direntry[i].bUnsure || (directory->direntry[i].size==-1 && !directory->direntry[i].dir))
  399. {
  400. bExact=FALSE;
  401. break;
  402. }
  403. if (bExact)
  404. {
  405. m_pMainThread->SetWorkingDir(directory);
  406. delete directory;
  407. return FZ_REPLY_OK;
  408. }
  409. }
  410. delete directory;
  411. }
  412. }
  413. #endif
  414. if (m_pMainThread->IsBusy())
  415. return FZ_REPLY_BUSY;
  416. #ifndef MPEXT_NO_CACHE
  417. if (nListMode&FZ_LIST_FORCECACHE)
  418. return FZ_REPLY_ERROR;
  419. #endif
  420. t_command command;
  421. command.id=FZ_COMMAND_LIST;
  422. command.path=parent;
  423. command.param1=dirname;
  424. command.param4=nListMode;
  425. m_pMainThread->Command(command);
  426. if (m_hOwnerWnd)
  427. return FZ_REPLY_WOULDBLOCK;
  428. else
  429. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  430. }
  431. #ifdef MPEXT
  432. int CFileZillaApi::ListFile(const CServerPath& path, const CString& fileName)
  433. {
  434. //Check if call allowed
  435. if (!m_bInitialized)
  436. return FZ_REPLY_NOTINITIALIZED;
  437. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  438. return FZ_REPLY_NOTCONNECTED;
  439. if (fileName.IsEmpty())
  440. return FZ_REPLY_INVALIDPARAM;
  441. if (m_pMainThread->IsBusy())
  442. return FZ_REPLY_BUSY;
  443. t_command command;
  444. command.id=FZ_COMMAND_LISTFILE;
  445. command.path=path;
  446. command.param1=fileName;
  447. m_pMainThread->Command(command);
  448. if (m_hOwnerWnd)
  449. return FZ_REPLY_WOULDBLOCK;
  450. else
  451. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  452. }
  453. #endif
  454. int CFileZillaApi::FileTransfer(const t_transferfile &TransferFile)
  455. {
  456. //Check if call allowed
  457. if (!m_bInitialized)
  458. return FZ_REPLY_NOTINITIALIZED;
  459. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  460. return FZ_REPLY_NOTCONNECTED;
  461. if (TransferFile.remotefile==_MPT("") || TransferFile.localfile==_MPT("") || TransferFile.remotepath.IsEmpty())
  462. return FZ_REPLY_INVALIDPARAM;
  463. if (IsBusy()==FZ_REPLY_BUSY)
  464. return FZ_REPLY_BUSY;
  465. t_command command;
  466. command.id=FZ_COMMAND_FILETRANSFER;
  467. command.transferfile=TransferFile;
  468. m_pMainThread->Command(command);
  469. if (m_hOwnerWnd)
  470. return FZ_REPLY_WOULDBLOCK;
  471. else
  472. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  473. }
  474. int CFileZillaApi::GetCurrentServer(t_server &server)
  475. {
  476. //Check if call allowed
  477. if (!m_bInitialized)
  478. return FZ_REPLY_NOTINITIALIZED;
  479. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  480. return FZ_REPLY_NOTCONNECTED;
  481. if (m_pMainThread->GetCurrentServer(server))
  482. return FZ_REPLY_OK;
  483. else
  484. return FZ_REPLY_NOTCONNECTED;
  485. }
  486. #ifdef MPEXT
  487. int CFileZillaApi::SetCurrentPath(CServerPath path)
  488. {
  489. //Check if call allowed
  490. if (!m_bInitialized)
  491. return FZ_REPLY_NOTINITIALIZED;
  492. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  493. return FZ_REPLY_NOTCONNECTED;
  494. m_pMainThread->SetCurrentPath(path);
  495. return FZ_REPLY_OK;
  496. }
  497. int CFileZillaApi::GetCurrentPath(CServerPath & path)
  498. {
  499. //Check if call allowed
  500. if (!m_bInitialized)
  501. return FZ_REPLY_NOTINITIALIZED;
  502. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  503. return FZ_REPLY_NOTCONNECTED;
  504. return (m_pMainThread->GetCurrentPath(path) ? FZ_REPLY_OK : FZ_REPLY_NOTCONNECTED);
  505. }
  506. bool CFileZillaApi::UsingMlsd()
  507. {
  508. //Check if call allowed
  509. if (!m_bInitialized)
  510. return false;
  511. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  512. return false;
  513. return m_pMainThread->UsingMlsd();
  514. }
  515. std::string CFileZillaApi::GetTlsVersionStr()
  516. {
  517. //Check if call allowed
  518. if (!m_bInitialized)
  519. return std::string();
  520. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  521. return std::string();
  522. return m_pMainThread->GetTlsVersionStr();
  523. }
  524. std::string CFileZillaApi::GetCipherName()
  525. {
  526. //Check if call allowed
  527. if (!m_bInitialized)
  528. return std::string();
  529. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  530. return std::string();
  531. return m_pMainThread->GetCipherName();
  532. }
  533. #endif
  534. int CFileZillaApi::CustomCommand(CString CustomCommand)
  535. {
  536. //Check if call allowed
  537. if (!m_bInitialized)
  538. return FZ_REPLY_NOTINITIALIZED;
  539. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  540. return FZ_REPLY_NOTCONNECTED;
  541. if (IsBusy()==FZ_REPLY_BUSY)
  542. return FZ_REPLY_BUSY;
  543. t_server server;
  544. int res=GetCurrentServer(server);
  545. if (res!=FZ_REPLY_OK)
  546. return res;
  547. #ifndef MPEXT_NO_SFTP
  548. if (server.nServerType&FZ_SERVERTYPE_SUB_FTP_SFTP)
  549. return FZ_REPLY_NOTSUPPORTED;
  550. #endif
  551. if (CustomCommand==_MPT(""))
  552. return FZ_REPLY_INVALIDPARAM;
  553. t_command command;
  554. command.id=FZ_COMMAND_CUSTOMCOMMAND;
  555. command.param1=CustomCommand;
  556. m_pMainThread->Command(command);
  557. if (m_hOwnerWnd)
  558. return FZ_REPLY_WOULDBLOCK;
  559. else
  560. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  561. }
  562. int CFileZillaApi::Delete(CString FileName, const CServerPath &path /*=CServerPath()*/)
  563. {
  564. //Check if call allowed
  565. if (!m_bInitialized)
  566. return FZ_REPLY_NOTINITIALIZED;
  567. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  568. return FZ_REPLY_NOTCONNECTED;
  569. if (IsBusy()==FZ_REPLY_BUSY)
  570. return FZ_REPLY_BUSY;
  571. if (FileName=="")
  572. return FZ_REPLY_INVALIDPARAM;
  573. CServerPath path2=path;
  574. if (path2.IsEmpty())
  575. {
  576. m_pMainThread->GetCurrentPath(path2);
  577. if (path2.IsEmpty())
  578. return FZ_REPLY_INVALIDPARAM;
  579. }
  580. t_command command;
  581. command.id=FZ_COMMAND_DELETE;
  582. command.param1=FileName;
  583. command.path=path2;
  584. m_pMainThread->Command(command);
  585. if (m_hOwnerWnd)
  586. return FZ_REPLY_WOULDBLOCK;
  587. else
  588. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  589. }
  590. int CFileZillaApi::RemoveDir(CString DirName, const CServerPath &path /*=CServerPath()*/)
  591. {
  592. //Check if call allowed
  593. if (!m_bInitialized)
  594. return FZ_REPLY_NOTINITIALIZED;
  595. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  596. return FZ_REPLY_NOTCONNECTED;
  597. if (IsBusy()==FZ_REPLY_BUSY)
  598. return FZ_REPLY_BUSY;
  599. if (DirName==_MPT(""))
  600. return FZ_REPLY_INVALIDPARAM;
  601. CServerPath path2=path;
  602. if (path2.IsEmpty())
  603. {
  604. m_pMainThread->GetCurrentPath(path2);
  605. if (path2.IsEmpty())
  606. return FZ_REPLY_INVALIDPARAM;
  607. }
  608. t_command command;
  609. command.id=FZ_COMMAND_REMOVEDIR;
  610. command.param1=DirName;
  611. command.path=path2;
  612. m_pMainThread->Command(command);
  613. if (m_hOwnerWnd)
  614. return FZ_REPLY_WOULDBLOCK;
  615. else
  616. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  617. return FZ_REPLY_ERROR;
  618. }
  619. int CFileZillaApi::MakeDir(const CServerPath &path)
  620. {
  621. //Check if call allowed
  622. if (!m_bInitialized)
  623. return FZ_REPLY_NOTINITIALIZED;
  624. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  625. return FZ_REPLY_NOTCONNECTED;
  626. if (IsBusy()==FZ_REPLY_BUSY)
  627. return FZ_REPLY_BUSY;
  628. if (path.IsEmpty() || !path.HasParent())
  629. return FZ_REPLY_INVALIDPARAM;
  630. t_command command;
  631. command.id=FZ_COMMAND_MAKEDIR;
  632. command.path=path;
  633. m_pMainThread->Command(command);
  634. if (m_hOwnerWnd)
  635. return FZ_REPLY_WOULDBLOCK;
  636. else
  637. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  638. return FZ_REPLY_ERROR;
  639. }
  640. int CFileZillaApi::Rename(CString oldName, CString newName, const CServerPath &path /*=CServerPath()*/, const CServerPath &newPath /*=CServerPath()*/)
  641. {
  642. //Check if call allowed
  643. if (!m_bInitialized)
  644. return FZ_REPLY_NOTINITIALIZED;
  645. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  646. return FZ_REPLY_NOTCONNECTED;
  647. if (IsBusy()==FZ_REPLY_BUSY)
  648. return FZ_REPLY_BUSY;
  649. if (oldName==_MPT("") || newName==_MPT(""))
  650. return FZ_REPLY_INVALIDPARAM;
  651. CServerPath path2 = path;
  652. if (path2.IsEmpty())
  653. {
  654. m_pMainThread->GetCurrentPath(path2);
  655. if (path2.IsEmpty())
  656. return FZ_REPLY_INVALIDPARAM;
  657. }
  658. t_command command;
  659. command.id = FZ_COMMAND_RENAME;
  660. command.param1 = oldName;
  661. command.param2 = newName;
  662. command.path = path2;
  663. command.newPath = newPath;
  664. m_pMainThread->Command(command);
  665. if (m_hOwnerWnd)
  666. return FZ_REPLY_WOULDBLOCK;
  667. else
  668. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  669. return FZ_REPLY_ERROR;
  670. }
  671. int CFileZillaApi::SetAsyncRequestResult(int nAction, CAsyncRequestData *pData)
  672. {
  673. if (!this || !pData)
  674. return FZ_REPLY_CRITICALERROR | FZ_REPLY_INVALIDPARAM;
  675. if (IsBadWritePtr(pData, sizeof(CAsyncRequestData)))
  676. return FZ_REPLY_CRITICALERROR;
  677. if (!m_bInitialized)
  678. {
  679. delete pData;
  680. return FZ_REPLY_NOTINITIALIZED;
  681. }
  682. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  683. {
  684. delete pData;
  685. return FZ_REPLY_NOTCONNECTED;
  686. }
  687. switch(pData->nRequestType)
  688. {
  689. case FZ_ASYNCREQUEST_OVERWRITE:
  690. break;
  691. #ifndef MPEXT_NO_SSL
  692. case FZ_ASYNCREQUEST_VERIFYCERT:
  693. if (!((CVerifyCertRequestData *)pData)->pCertData)
  694. {
  695. delete pData;
  696. return FZ_REPLY_INVALIDPARAM;
  697. }
  698. break;
  699. #endif
  700. case FZ_ASYNCREQUEST_NEEDPASS:
  701. break;
  702. #ifndef MPEXT_NO_GSS
  703. case FZ_ASYNCREQUEST_GSS_AUTHFAILED:
  704. case FZ_ASYNCREQUEST_GSS_NEEDUSER:
  705. case FZ_ASYNCREQUEST_GSS_NEEDPASS:
  706. #ifdef MPEXT
  707. break;
  708. #endif
  709. #endif
  710. #ifndef MPEXT_NO_SFTP
  711. case FZ_ASYNCREQUEST_NEWHOSTKEY:
  712. case FZ_ASYNCREQUEST_CHANGEDHOSTKEY:
  713. break;
  714. #endif
  715. default:
  716. delete pData;
  717. return FZ_REPLY_INVALIDPARAM;
  718. }
  719. pData->nRequestResult = nAction;
  720. if (!m_pMainThread)
  721. {
  722. delete pData;
  723. return FZ_REPLY_NOTINITIALIZED;
  724. }
  725. m_pMainThread->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_ASYNCREQUESTREPLY, (LPARAM)pData);
  726. return FZ_REPLY_OK;
  727. }
  728. #ifndef MPEXT
  729. int CFileZillaApi::SetOption(int nOption, int value)
  730. {
  731. if (!m_bInitialized)
  732. return FZ_REPLY_NOTINITIALIZED;
  733. switch (nOption)
  734. {
  735. case FZAPI_OPTION_SHOWHIDDEN:
  736. m_pMainThread->SetOption(nOption, value);
  737. break;
  738. default:
  739. return FZ_REPLY_INVALIDPARAM;
  740. }
  741. return FZ_REPLY_OK;
  742. }
  743. int CFileZillaApi::GetOption(int nOption, int &value)
  744. {
  745. if (!m_bInitialized)
  746. return FZ_REPLY_NOTINITIALIZED;
  747. switch (nOption)
  748. {
  749. case FZAPI_OPTION_SHOWHIDDEN:
  750. value = m_pMainThread->GetOption(nOption);
  751. break;
  752. default:
  753. return FZ_REPLY_INVALIDPARAM;
  754. }
  755. return FZ_REPLY_OK;
  756. }
  757. #endif
  758. int CFileZillaApi::Chmod(int nValue, CString FileName, const CServerPath &path /*=CServerPath()*/ )
  759. {
  760. //Check if call allowed
  761. if (!m_bInitialized)
  762. return FZ_REPLY_NOTINITIALIZED;
  763. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  764. return FZ_REPLY_NOTCONNECTED;
  765. if (IsBusy()==FZ_REPLY_BUSY)
  766. return FZ_REPLY_BUSY;
  767. if (FileName==_MPT(""))
  768. return FZ_REPLY_INVALIDPARAM;
  769. t_command command;
  770. command.id=FZ_COMMAND_CHMOD;
  771. command.param1=FileName;
  772. command.param4=nValue;
  773. command.path=path;
  774. m_pMainThread->Command(command);
  775. if (m_hOwnerWnd)
  776. return FZ_REPLY_WOULDBLOCK;
  777. else
  778. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  779. }
  780. int CFileZillaApi::SetDebugLevel(int nDebugLevel)
  781. {
  782. //Check if call allowed
  783. if (!m_bInitialized)
  784. return FZ_REPLY_NOTINITIALIZED;
  785. if (!m_pMainThread->SetDebugLevel(nDebugLevel))
  786. return FZ_REPLY_ERROR;
  787. return FZ_REPLY_OK;
  788. }
  789. #ifndef MPEXT_NO_CACHE
  790. BOOL CFileZillaApi::DumpDirectoryCache(LPCTSTR pFileName)
  791. {
  792. CDirectoryCache cache;
  793. return cache.Dump(pFileName);
  794. }
  795. #endif
  796. //CAsyncRequestData derived classes
  797. CAsyncRequestData::CAsyncRequestData()
  798. {
  799. nRequestType = 0;
  800. nRequestID = 0;
  801. nRequestResult = 0;
  802. }
  803. CAsyncRequestData::~CAsyncRequestData()
  804. {
  805. }
  806. COverwriteRequestData::COverwriteRequestData()
  807. {
  808. size1 = 0;
  809. size2 = 0;
  810. nRequestType=FZ_ASYNCREQUEST_OVERWRITE;
  811. localtime=0;
  812. remotetime.hasdate = false;
  813. pTransferFile=0;
  814. }
  815. COverwriteRequestData::~COverwriteRequestData()
  816. {
  817. delete pTransferFile;
  818. delete localtime;
  819. }
  820. #ifndef MPEXT_NO_SSL
  821. CVerifyCertRequestData::CVerifyCertRequestData()
  822. {
  823. nRequestType=FZ_ASYNCREQUEST_VERIFYCERT;
  824. pCertData=0;
  825. }
  826. CVerifyCertRequestData::~CVerifyCertRequestData()
  827. {
  828. delete pCertData;
  829. }
  830. #endif
  831. CNeedPassRequestData::CNeedPassRequestData()
  832. {
  833. nRequestType=FZ_ASYNCREQUEST_NEEDPASS;
  834. nOldOpState=0;
  835. }
  836. CNeedPassRequestData::~CNeedPassRequestData()
  837. {
  838. }
  839. #ifndef MPEXT_NO_GSS
  840. CGssNeedPassRequestData::CGssNeedPassRequestData()
  841. {
  842. nRequestType=FZ_ASYNCREQUEST_GSS_NEEDPASS;
  843. }
  844. CGssNeedPassRequestData::~CGssNeedPassRequestData()
  845. {
  846. }
  847. CGssNeedUserRequestData::CGssNeedUserRequestData()
  848. {
  849. nRequestType = FZ_ASYNCREQUEST_GSS_NEEDUSER;
  850. }
  851. CGssNeedUserRequestData::~CGssNeedUserRequestData()
  852. {
  853. }
  854. #endif
  855. #ifndef MPEXT_NO_SFTP
  856. CNewHostKeyRequestData::CNewHostKeyRequestData()
  857. {
  858. nRequestType=FZ_ASYNCREQUEST_NEWHOSTKEY;
  859. }
  860. CNewHostKeyRequestData::~CNewHostKeyRequestData()
  861. {
  862. }
  863. CChangedHostKeyRequestData::CChangedHostKeyRequestData()
  864. {
  865. nRequestType=FZ_ASYNCREQUEST_CHANGEDHOSTKEY;
  866. }
  867. CChangedHostKeyRequestData::~CChangedHostKeyRequestData()
  868. {
  869. }
  870. #endif
  871. BOOL CFileZillaApi::IsValid() const
  872. {
  873. if (!this)
  874. return FALSE;
  875. if (IsBadWritePtr((VOID *)this, sizeof(CFileZillaApi)) )
  876. return FALSE;
  877. if (IsBadWritePtr((VOID *)&m_bInitialized, sizeof(BOOL)) ||
  878. IsBadWritePtr((VOID *)&m_hOwnerWnd, sizeof(HWND)) ||
  879. IsBadWritePtr((VOID *)&m_nInternalMessageID, sizeof(unsigned int)) ||
  880. IsBadWritePtr((VOID *)&m_nReplyMessageID, sizeof(unsigned int)) ||
  881. IsBadWritePtr(m_pMainThread, sizeof(CMainThread)) )
  882. return FALSE;
  883. if (!m_pMainThread->IsValid())
  884. return FALSE;
  885. return TRUE;
  886. }