FileZillaApi.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. bool CFileZillaApi::UsingUtf8()
  516. {
  517. //Check if call allowed
  518. if (!m_bInitialized)
  519. return false;
  520. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  521. return false;
  522. return m_pMainThread->UsingUtf8();
  523. }
  524. std::string CFileZillaApi::GetTlsVersionStr()
  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->GetTlsVersionStr();
  532. }
  533. std::string CFileZillaApi::GetCipherName()
  534. {
  535. //Check if call allowed
  536. if (!m_bInitialized)
  537. return std::string();
  538. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  539. return std::string();
  540. return m_pMainThread->GetCipherName();
  541. }
  542. #endif
  543. int CFileZillaApi::CustomCommand(CString CustomCommand)
  544. {
  545. //Check if call allowed
  546. if (!m_bInitialized)
  547. return FZ_REPLY_NOTINITIALIZED;
  548. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  549. return FZ_REPLY_NOTCONNECTED;
  550. if (IsBusy()==FZ_REPLY_BUSY)
  551. return FZ_REPLY_BUSY;
  552. t_server server;
  553. int res=GetCurrentServer(server);
  554. if (res!=FZ_REPLY_OK)
  555. return res;
  556. #ifndef MPEXT_NO_SFTP
  557. if (server.nServerType&FZ_SERVERTYPE_SUB_FTP_SFTP)
  558. return FZ_REPLY_NOTSUPPORTED;
  559. #endif
  560. if (CustomCommand==_MPT(""))
  561. return FZ_REPLY_INVALIDPARAM;
  562. t_command command;
  563. command.id=FZ_COMMAND_CUSTOMCOMMAND;
  564. command.param1=CustomCommand;
  565. m_pMainThread->Command(command);
  566. if (m_hOwnerWnd)
  567. return FZ_REPLY_WOULDBLOCK;
  568. else
  569. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  570. }
  571. int CFileZillaApi::Delete(CString FileName, const CServerPath &path /*=CServerPath()*/)
  572. {
  573. //Check if call allowed
  574. if (!m_bInitialized)
  575. return FZ_REPLY_NOTINITIALIZED;
  576. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  577. return FZ_REPLY_NOTCONNECTED;
  578. if (IsBusy()==FZ_REPLY_BUSY)
  579. return FZ_REPLY_BUSY;
  580. if (FileName=="")
  581. return FZ_REPLY_INVALIDPARAM;
  582. CServerPath path2=path;
  583. if (path2.IsEmpty())
  584. {
  585. m_pMainThread->GetCurrentPath(path2);
  586. if (path2.IsEmpty())
  587. return FZ_REPLY_INVALIDPARAM;
  588. }
  589. t_command command;
  590. command.id=FZ_COMMAND_DELETE;
  591. command.param1=FileName;
  592. command.path=path2;
  593. m_pMainThread->Command(command);
  594. if (m_hOwnerWnd)
  595. return FZ_REPLY_WOULDBLOCK;
  596. else
  597. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  598. }
  599. int CFileZillaApi::RemoveDir(CString DirName, const CServerPath &path /*=CServerPath()*/)
  600. {
  601. //Check if call allowed
  602. if (!m_bInitialized)
  603. return FZ_REPLY_NOTINITIALIZED;
  604. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  605. return FZ_REPLY_NOTCONNECTED;
  606. if (IsBusy()==FZ_REPLY_BUSY)
  607. return FZ_REPLY_BUSY;
  608. if (DirName==_MPT(""))
  609. return FZ_REPLY_INVALIDPARAM;
  610. CServerPath path2=path;
  611. if (path2.IsEmpty())
  612. {
  613. m_pMainThread->GetCurrentPath(path2);
  614. if (path2.IsEmpty())
  615. return FZ_REPLY_INVALIDPARAM;
  616. }
  617. t_command command;
  618. command.id=FZ_COMMAND_REMOVEDIR;
  619. command.param1=DirName;
  620. command.path=path2;
  621. m_pMainThread->Command(command);
  622. if (m_hOwnerWnd)
  623. return FZ_REPLY_WOULDBLOCK;
  624. else
  625. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  626. return FZ_REPLY_ERROR;
  627. }
  628. int CFileZillaApi::MakeDir(const CServerPath &path)
  629. {
  630. //Check if call allowed
  631. if (!m_bInitialized)
  632. return FZ_REPLY_NOTINITIALIZED;
  633. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  634. return FZ_REPLY_NOTCONNECTED;
  635. if (IsBusy()==FZ_REPLY_BUSY)
  636. return FZ_REPLY_BUSY;
  637. if (path.IsEmpty() || !path.HasParent())
  638. return FZ_REPLY_INVALIDPARAM;
  639. t_command command;
  640. command.id=FZ_COMMAND_MAKEDIR;
  641. command.path=path;
  642. m_pMainThread->Command(command);
  643. if (m_hOwnerWnd)
  644. return FZ_REPLY_WOULDBLOCK;
  645. else
  646. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  647. return FZ_REPLY_ERROR;
  648. }
  649. int CFileZillaApi::Rename(CString oldName, CString newName, const CServerPath &path /*=CServerPath()*/, const CServerPath &newPath /*=CServerPath()*/)
  650. {
  651. //Check if call allowed
  652. if (!m_bInitialized)
  653. return FZ_REPLY_NOTINITIALIZED;
  654. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  655. return FZ_REPLY_NOTCONNECTED;
  656. if (IsBusy()==FZ_REPLY_BUSY)
  657. return FZ_REPLY_BUSY;
  658. if (oldName==_MPT("") || newName==_MPT(""))
  659. return FZ_REPLY_INVALIDPARAM;
  660. CServerPath path2 = path;
  661. if (path2.IsEmpty())
  662. {
  663. m_pMainThread->GetCurrentPath(path2);
  664. if (path2.IsEmpty())
  665. return FZ_REPLY_INVALIDPARAM;
  666. }
  667. t_command command;
  668. command.id = FZ_COMMAND_RENAME;
  669. command.param1 = oldName;
  670. command.param2 = newName;
  671. command.path = path2;
  672. command.newPath = newPath;
  673. m_pMainThread->Command(command);
  674. if (m_hOwnerWnd)
  675. return FZ_REPLY_WOULDBLOCK;
  676. else
  677. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  678. return FZ_REPLY_ERROR;
  679. }
  680. int CFileZillaApi::SetAsyncRequestResult(int nAction, CAsyncRequestData *pData)
  681. {
  682. if (!this || !pData)
  683. return FZ_REPLY_CRITICALERROR | FZ_REPLY_INVALIDPARAM;
  684. if (IsBadWritePtr(pData, sizeof(CAsyncRequestData)))
  685. return FZ_REPLY_CRITICALERROR;
  686. if (!m_bInitialized)
  687. {
  688. delete pData;
  689. return FZ_REPLY_NOTINITIALIZED;
  690. }
  691. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  692. {
  693. delete pData;
  694. return FZ_REPLY_NOTCONNECTED;
  695. }
  696. switch(pData->nRequestType)
  697. {
  698. case FZ_ASYNCREQUEST_OVERWRITE:
  699. break;
  700. #ifndef MPEXT_NO_SSL
  701. case FZ_ASYNCREQUEST_VERIFYCERT:
  702. if (!((CVerifyCertRequestData *)pData)->pCertData)
  703. {
  704. delete pData;
  705. return FZ_REPLY_INVALIDPARAM;
  706. }
  707. break;
  708. #endif
  709. case FZ_ASYNCREQUEST_NEEDPASS:
  710. break;
  711. #ifndef MPEXT_NO_GSS
  712. case FZ_ASYNCREQUEST_GSS_AUTHFAILED:
  713. case FZ_ASYNCREQUEST_GSS_NEEDUSER:
  714. case FZ_ASYNCREQUEST_GSS_NEEDPASS:
  715. #ifdef MPEXT
  716. break;
  717. #endif
  718. #endif
  719. #ifndef MPEXT_NO_SFTP
  720. case FZ_ASYNCREQUEST_NEWHOSTKEY:
  721. case FZ_ASYNCREQUEST_CHANGEDHOSTKEY:
  722. break;
  723. #endif
  724. default:
  725. delete pData;
  726. return FZ_REPLY_INVALIDPARAM;
  727. }
  728. pData->nRequestResult = nAction;
  729. if (!m_pMainThread)
  730. {
  731. delete pData;
  732. return FZ_REPLY_NOTINITIALIZED;
  733. }
  734. m_pMainThread->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_ASYNCREQUESTREPLY, (LPARAM)pData);
  735. return FZ_REPLY_OK;
  736. }
  737. #ifndef MPEXT
  738. int CFileZillaApi::SetOption(int nOption, int value)
  739. {
  740. if (!m_bInitialized)
  741. return FZ_REPLY_NOTINITIALIZED;
  742. switch (nOption)
  743. {
  744. case FZAPI_OPTION_SHOWHIDDEN:
  745. m_pMainThread->SetOption(nOption, value);
  746. break;
  747. default:
  748. return FZ_REPLY_INVALIDPARAM;
  749. }
  750. return FZ_REPLY_OK;
  751. }
  752. int CFileZillaApi::GetOption(int nOption, int &value)
  753. {
  754. if (!m_bInitialized)
  755. return FZ_REPLY_NOTINITIALIZED;
  756. switch (nOption)
  757. {
  758. case FZAPI_OPTION_SHOWHIDDEN:
  759. value = m_pMainThread->GetOption(nOption);
  760. break;
  761. default:
  762. return FZ_REPLY_INVALIDPARAM;
  763. }
  764. return FZ_REPLY_OK;
  765. }
  766. #endif
  767. int CFileZillaApi::Chmod(int nValue, CString FileName, const CServerPath &path /*=CServerPath()*/ )
  768. {
  769. //Check if call allowed
  770. if (!m_bInitialized)
  771. return FZ_REPLY_NOTINITIALIZED;
  772. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  773. return FZ_REPLY_NOTCONNECTED;
  774. if (IsBusy()==FZ_REPLY_BUSY)
  775. return FZ_REPLY_BUSY;
  776. if (FileName==_MPT(""))
  777. return FZ_REPLY_INVALIDPARAM;
  778. t_command command;
  779. command.id=FZ_COMMAND_CHMOD;
  780. command.param1=FileName;
  781. command.param4=nValue;
  782. command.path=path;
  783. m_pMainThread->Command(command);
  784. if (m_hOwnerWnd)
  785. return FZ_REPLY_WOULDBLOCK;
  786. else
  787. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  788. }
  789. int CFileZillaApi::SetDebugLevel(int nDebugLevel)
  790. {
  791. //Check if call allowed
  792. if (!m_bInitialized)
  793. return FZ_REPLY_NOTINITIALIZED;
  794. if (!m_pMainThread->SetDebugLevel(nDebugLevel))
  795. return FZ_REPLY_ERROR;
  796. return FZ_REPLY_OK;
  797. }
  798. #ifndef MPEXT_NO_CACHE
  799. BOOL CFileZillaApi::DumpDirectoryCache(LPCTSTR pFileName)
  800. {
  801. CDirectoryCache cache;
  802. return cache.Dump(pFileName);
  803. }
  804. #endif
  805. //CAsyncRequestData derived classes
  806. CAsyncRequestData::CAsyncRequestData()
  807. {
  808. nRequestType = 0;
  809. nRequestID = 0;
  810. nRequestResult = 0;
  811. }
  812. CAsyncRequestData::~CAsyncRequestData()
  813. {
  814. }
  815. COverwriteRequestData::COverwriteRequestData()
  816. {
  817. size1 = 0;
  818. size2 = 0;
  819. nRequestType=FZ_ASYNCREQUEST_OVERWRITE;
  820. localtime=0;
  821. remotetime.hasdate = false;
  822. pTransferFile=0;
  823. }
  824. COverwriteRequestData::~COverwriteRequestData()
  825. {
  826. delete pTransferFile;
  827. delete localtime;
  828. }
  829. #ifndef MPEXT_NO_SSL
  830. CVerifyCertRequestData::CVerifyCertRequestData()
  831. {
  832. nRequestType=FZ_ASYNCREQUEST_VERIFYCERT;
  833. pCertData=0;
  834. }
  835. CVerifyCertRequestData::~CVerifyCertRequestData()
  836. {
  837. delete pCertData;
  838. }
  839. #endif
  840. CNeedPassRequestData::CNeedPassRequestData()
  841. {
  842. nRequestType=FZ_ASYNCREQUEST_NEEDPASS;
  843. nOldOpState=0;
  844. }
  845. CNeedPassRequestData::~CNeedPassRequestData()
  846. {
  847. }
  848. #ifndef MPEXT_NO_GSS
  849. CGssNeedPassRequestData::CGssNeedPassRequestData()
  850. {
  851. nRequestType=FZ_ASYNCREQUEST_GSS_NEEDPASS;
  852. }
  853. CGssNeedPassRequestData::~CGssNeedPassRequestData()
  854. {
  855. }
  856. CGssNeedUserRequestData::CGssNeedUserRequestData()
  857. {
  858. nRequestType = FZ_ASYNCREQUEST_GSS_NEEDUSER;
  859. }
  860. CGssNeedUserRequestData::~CGssNeedUserRequestData()
  861. {
  862. }
  863. #endif
  864. #ifndef MPEXT_NO_SFTP
  865. CNewHostKeyRequestData::CNewHostKeyRequestData()
  866. {
  867. nRequestType=FZ_ASYNCREQUEST_NEWHOSTKEY;
  868. }
  869. CNewHostKeyRequestData::~CNewHostKeyRequestData()
  870. {
  871. }
  872. CChangedHostKeyRequestData::CChangedHostKeyRequestData()
  873. {
  874. nRequestType=FZ_ASYNCREQUEST_CHANGEDHOSTKEY;
  875. }
  876. CChangedHostKeyRequestData::~CChangedHostKeyRequestData()
  877. {
  878. }
  879. #endif
  880. BOOL CFileZillaApi::IsValid() const
  881. {
  882. if (!this)
  883. return FALSE;
  884. if (IsBadWritePtr((VOID *)this, sizeof(CFileZillaApi)) )
  885. return FALSE;
  886. if (IsBadWritePtr((VOID *)&m_bInitialized, sizeof(BOOL)) ||
  887. IsBadWritePtr((VOID *)&m_hOwnerWnd, sizeof(HWND)) ||
  888. IsBadWritePtr((VOID *)&m_nInternalMessageID, sizeof(unsigned int)) ||
  889. IsBadWritePtr((VOID *)&m_nReplyMessageID, sizeof(unsigned int)) ||
  890. IsBadWritePtr(m_pMainThread, sizeof(CMainThread)) )
  891. return FALSE;
  892. if (!m_pMainThread->IsValid())
  893. return FALSE;
  894. return TRUE;
  895. }