FileZillaApi.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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)
  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. #ifndef MPEXT
  75. m_pMainThread->InitLog(m_hOwnerWnd, m_nReplyMessageID);
  76. #else
  77. m_pMainThread->InitLog(pParent);
  78. #endif
  79. //Resume Thread
  80. m_pMainThread->ResumeThread();
  81. //Initialization OK
  82. m_bInitialized=TRUE;
  83. return FZ_REPLY_OK;
  84. }
  85. unsigned int CFileZillaApi::GetMessageID()
  86. {
  87. return m_nReplyMessageID;
  88. }
  89. int CFileZillaApi::IsConnected()
  90. {
  91. if (!m_bInitialized)
  92. return FZ_REPLY_NOTINITIALIZED;
  93. return m_pMainThread->IsConnected()?FZ_REPLY_OK:FZ_REPLY_NOTCONNECTED;
  94. }
  95. int CFileZillaApi::IsBusy()
  96. {
  97. if (!m_bInitialized)
  98. return FZ_REPLY_NOTINITIALIZED;
  99. return m_pMainThread->IsBusy()?FZ_REPLY_BUSY:FZ_REPLY_IDLE;
  100. }
  101. int CFileZillaApi::Connect(const t_server &server)
  102. {
  103. //Check parameters
  104. if (server.host=="" || server.port<1 || server.port>65535)
  105. return FZ_REPLY_INVALIDPARAM;
  106. #ifndef MPEXT_NO_GSS
  107. BOOL bUseGSS = FALSE;
  108. if (COptions::GetOptionVal(OPTION_USEGSS))
  109. {
  110. USES_CONVERSION;
  111. CString GssServers = COptions::GetOption(OPTION_GSSSERVERS);
  112. hostent *fullname = gethostbyname(T2CA(server.host));
  113. CString host;
  114. if (fullname)
  115. host = fullname->h_name;
  116. else
  117. host = server.host;
  118. host.MakeLower();
  119. int i;
  120. while ((i=GssServers.Find( _T(";") ))!=-1)
  121. {
  122. if (("."+GssServers.Left(i))==host.Right(GssServers.Left(i).GetLength()+1) || GssServers.Left(i)==host)
  123. {
  124. bUseGSS = TRUE;
  125. break;
  126. }
  127. GssServers = GssServers.Mid(i+1);
  128. }
  129. }
  130. if (!bUseGSS && server.user == "")
  131. return FZ_REPLY_INVALIDPARAM;
  132. #endif
  133. if (!(server.nServerType&FZ_SERVERTYPE_HIGHMASK))
  134. return FZ_REPLY_INVALIDPARAM;
  135. //Check if call allowed
  136. if (!m_bInitialized)
  137. return FZ_REPLY_NOTINITIALIZED;
  138. if (m_pMainThread->IsBusy())
  139. return FZ_REPLY_BUSY;
  140. t_command command;
  141. command.id=FZ_COMMAND_CONNECT;
  142. command.server=server;
  143. m_pMainThread->Command(command);
  144. if (m_hOwnerWnd)
  145. return FZ_REPLY_WOULDBLOCK;
  146. else
  147. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  148. }
  149. int CFileZillaApi::List(int nListMode /*=FZ_LIST_USECACHE*/)
  150. {
  151. //Check if call allowed
  152. if (!m_bInitialized)
  153. return FZ_REPLY_NOTINITIALIZED;
  154. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  155. return FZ_REPLY_NOTCONNECTED;
  156. if (nListMode&FZ_LIST_REALCHANGE)
  157. return FZ_REPLY_INVALIDPARAM;
  158. #ifndef MPEXT_NO_CACHE
  159. if (nListMode&FZ_LIST_FORCECACHE)
  160. nListMode|=FZ_LIST_USECACHE;
  161. //Check if current dir is cached
  162. CServerPath path;
  163. if (nListMode&FZ_LIST_USECACHE)
  164. {
  165. if (!m_pMainThread->GetWorkingDirPath(path) || path.IsEmpty())
  166. m_pMainThread->GetCurrentPath(path);
  167. if (!path.IsEmpty())
  168. {
  169. t_server server;
  170. BOOL res=m_pMainThread->GetCurrentServer(server);
  171. if (res)
  172. {
  173. t_directory *directory=new t_directory;
  174. CDirectoryCache cache;
  175. res=cache.Lookup(path,server,*directory);
  176. if (res)
  177. {
  178. BOOL bExact=TRUE;
  179. if (nListMode & FZ_LIST_EXACT)
  180. for (int i=0;i<directory->num;i++)
  181. if (directory->direntry[i].bUnsure || (directory->direntry[i].size==-1 && !directory->direntry[i].dir))
  182. {
  183. bExact=FALSE;
  184. break;
  185. }
  186. if (bExact)
  187. {
  188. m_pMainThread->SetWorkingDir(directory);
  189. delete directory;
  190. return FZ_REPLY_OK;
  191. }
  192. }
  193. delete directory;
  194. }
  195. }
  196. }
  197. #else
  198. CServerPath path;
  199. // seems to be incorrectly skipped when cache is not required
  200. if (!m_pMainThread->GetWorkingDirPath(path) || path.IsEmpty())
  201. m_pMainThread->GetCurrentPath(path);
  202. #endif
  203. if (m_pMainThread->IsBusy())
  204. return FZ_REPLY_BUSY;
  205. #ifndef MPEXT_NO_CACHE
  206. if (nListMode&FZ_LIST_FORCECACHE)
  207. return FZ_REPLY_ERROR;
  208. #endif
  209. t_command command;
  210. command.id=FZ_COMMAND_LIST;
  211. command.path = path;
  212. command.param4=nListMode;
  213. m_pMainThread->Command(command);
  214. if (m_hOwnerWnd)
  215. return FZ_REPLY_WOULDBLOCK;
  216. else
  217. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  218. }
  219. int CFileZillaApi::Cancel()
  220. {
  221. //Check if call allowed
  222. if (!m_bInitialized)
  223. return FZ_REPLY_NOTINITIALIZED;
  224. if (IsBusy()!=FZ_REPLY_BUSY)
  225. return FZ_REPLY_NOTBUSY;
  226. m_pMainThread->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_CANCEL, 0);
  227. return FZ_REPLY_WOULDBLOCK;
  228. }
  229. void CFileZillaApi::Destroy()
  230. {
  231. if (!m_bInitialized)
  232. return;
  233. ASSERT(m_pMainThread);
  234. HANDLE tmp=m_pMainThread->m_hThread;
  235. m_pMainThread->Quit();
  236. //Wait for the main thread to quit
  237. WaitForSingleObject(tmp, 10000);
  238. #ifndef MPEXT
  239. PostMessage(m_hOwnerWnd, m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_QUITCOMPLETE, 0), 0);
  240. #endif
  241. m_pMainThread=0;
  242. m_bInitialized=FALSE;
  243. }
  244. int CFileZillaApi::Disconnect()
  245. {
  246. //Check if call allowed
  247. if (!m_bInitialized)
  248. return FZ_REPLY_NOTINITIALIZED;
  249. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  250. return FZ_REPLY_NOTCONNECTED;
  251. if (IsBusy()==FZ_REPLY_BUSY)
  252. return FZ_REPLY_BUSY;
  253. m_pMainThread->PostThreadMessage(m_nInternalMessageID,FZAPI_THREADMSG_DISCONNECT,0);
  254. return FZ_REPLY_WOULDBLOCK;
  255. }
  256. int CFileZillaApi::Command(t_command *pCommand)
  257. {
  258. //Check if call allowed
  259. if (!m_bInitialized)
  260. return FZ_REPLY_NOTINITIALIZED;
  261. //Dispatch command to command specific functions
  262. switch(pCommand->id)
  263. {
  264. case FZ_COMMAND_LIST:
  265. if (pCommand->param1!="")
  266. return List(pCommand->path,pCommand->param1,pCommand->param4);
  267. else if (!pCommand->path.IsEmpty())
  268. return List(pCommand->path,pCommand->param4);
  269. else
  270. return List(pCommand->param4);
  271. break;
  272. case FZ_COMMAND_CONNECT:
  273. return Connect(pCommand->server);
  274. break;
  275. case FZ_COMMAND_DISCONNECT:
  276. return Disconnect();
  277. break;
  278. case FZ_COMMAND_FILETRANSFER:
  279. return FileTransfer(pCommand->transferfile);
  280. break;
  281. case FZ_COMMAND_DELETE:
  282. return Delete(pCommand->param1, pCommand->path);
  283. break;
  284. case FZ_COMMAND_REMOVEDIR:
  285. return RemoveDir(pCommand->param1, pCommand->path);
  286. break;
  287. case FZ_COMMAND_MAKEDIR:
  288. return MakeDir(pCommand->path);
  289. break;
  290. case FZ_COMMAND_RENAME:
  291. return Rename(pCommand->param1, pCommand->param2, pCommand->path, pCommand->newPath);
  292. break;
  293. case FZ_COMMAND_CUSTOMCOMMAND:
  294. return CustomCommand(pCommand->param1);
  295. break;
  296. case FZ_COMMAND_CHMOD:
  297. return Chmod(pCommand->param4, pCommand->param1, pCommand->path);
  298. break;
  299. }
  300. return FZ_REPLY_INVALIDPARAM;
  301. }
  302. int CFileZillaApi::List(const CServerPath& path, int nListMode /*=FZ_LIST_USECACHE*/)
  303. {
  304. //Check if call allowed
  305. if (!m_bInitialized)
  306. return FZ_REPLY_NOTINITIALIZED;
  307. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  308. return FZ_REPLY_NOTCONNECTED;
  309. #ifndef MPEXT_NO_CACHE
  310. if ( (nListMode&(FZ_LIST_FORCECACHE|FZ_LIST_REALCHANGE))==(FZ_LIST_FORCECACHE|FZ_LIST_REALCHANGE) )
  311. return FZ_REPLY_INVALIDPARAM;
  312. if (nListMode&FZ_LIST_FORCECACHE)
  313. nListMode|=FZ_LIST_USECACHE;
  314. #endif
  315. if (path.IsEmpty())
  316. return FZ_REPLY_INVALIDPARAM;
  317. #ifndef MPEXT_NO_CACHE
  318. //Check if current dir is cached
  319. if (nListMode&FZ_LIST_USECACHE && !(nListMode&FZ_LIST_REALCHANGE))
  320. {
  321. t_server server;
  322. BOOL res=m_pMainThread->GetCurrentServer(server);
  323. if (res)
  324. {
  325. t_directory *directory=new t_directory;
  326. CDirectoryCache cache;
  327. res=cache.Lookup(path,server,*directory);
  328. if (res)
  329. {
  330. BOOL bExact=TRUE;
  331. if (nListMode & FZ_LIST_EXACT)
  332. for (int i=0;i<directory->num;i++)
  333. if (directory->direntry[i].bUnsure || (directory->direntry[i].size==-1 && !directory->direntry[i].dir))
  334. {
  335. bExact=FALSE;
  336. break;
  337. }
  338. if (bExact)
  339. {
  340. m_pMainThread->SetWorkingDir(directory);
  341. delete directory;
  342. return FZ_REPLY_OK;
  343. }
  344. }
  345. delete directory;
  346. }
  347. }
  348. #endif
  349. if (m_pMainThread->IsBusy())
  350. return FZ_REPLY_BUSY;
  351. #ifndef MPEXT_NO_CACHE
  352. if (nListMode&FZ_LIST_FORCECACHE)
  353. return FZ_REPLY_ERROR;
  354. #endif
  355. t_command command;
  356. command.id=FZ_COMMAND_LIST;
  357. command.path=path;
  358. command.param4=nListMode;
  359. m_pMainThread->Command(command);
  360. if (m_hOwnerWnd)
  361. return FZ_REPLY_WOULDBLOCK;
  362. else
  363. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  364. }
  365. int CFileZillaApi::List(const CServerPath& parent, CString dirname, int nListMode /*=FZ_LIST_USECACHE*/)
  366. {
  367. //Check if call allowed
  368. if (!m_bInitialized)
  369. return FZ_REPLY_NOTINITIALIZED;
  370. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  371. return FZ_REPLY_NOTCONNECTED;
  372. #ifndef MPEXT_NO_CACHE
  373. if ( (nListMode&(FZ_LIST_FORCECACHE|FZ_LIST_REALCHANGE))==(FZ_LIST_FORCECACHE|FZ_LIST_REALCHANGE) )
  374. return FZ_REPLY_INVALIDPARAM;
  375. if (nListMode&FZ_LIST_FORCECACHE)
  376. nListMode|=FZ_LIST_USECACHE;
  377. #endif
  378. if (dirname=="" || parent.IsEmpty())
  379. return FZ_REPLY_INVALIDPARAM;
  380. #ifndef MPEXT_NO_CACHE
  381. //Check if current dir is cached
  382. if (nListMode&FZ_LIST_USECACHE && !(nListMode&FZ_LIST_REALCHANGE))
  383. {
  384. t_server server;
  385. BOOL res=m_pMainThread->GetCurrentServer(server);
  386. if (res)
  387. {
  388. t_directory *directory=new t_directory;
  389. CDirectoryCache cache;
  390. res=cache.Lookup(parent,dirname,server,*directory);
  391. if (res)
  392. {
  393. BOOL bExact=TRUE;
  394. if (nListMode & FZ_LIST_EXACT)
  395. for (int i=0;i<directory->num;i++)
  396. if (directory->direntry[i].bUnsure || (directory->direntry[i].size==-1 && !directory->direntry[i].dir))
  397. {
  398. bExact=FALSE;
  399. break;
  400. }
  401. if (bExact)
  402. {
  403. m_pMainThread->SetWorkingDir(directory);
  404. delete directory;
  405. return FZ_REPLY_OK;
  406. }
  407. }
  408. delete directory;
  409. }
  410. }
  411. #endif
  412. if (m_pMainThread->IsBusy())
  413. return FZ_REPLY_BUSY;
  414. #ifndef MPEXT_NO_CACHE
  415. if (nListMode&FZ_LIST_FORCECACHE)
  416. return FZ_REPLY_ERROR;
  417. #endif
  418. t_command command;
  419. command.id=FZ_COMMAND_LIST;
  420. command.path=parent;
  421. command.param1=dirname;
  422. command.param4=nListMode;
  423. m_pMainThread->Command(command);
  424. if (m_hOwnerWnd)
  425. return FZ_REPLY_WOULDBLOCK;
  426. else
  427. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  428. }
  429. int CFileZillaApi::FileTransfer(const t_transferfile &TransferFile)
  430. {
  431. //Check if call allowed
  432. if (!m_bInitialized)
  433. return FZ_REPLY_NOTINITIALIZED;
  434. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  435. return FZ_REPLY_NOTCONNECTED;
  436. if (TransferFile.remotefile=="" || TransferFile.localfile=="" || TransferFile.remotepath.IsEmpty())
  437. return FZ_REPLY_INVALIDPARAM;
  438. if (IsBusy()==FZ_REPLY_BUSY)
  439. return FZ_REPLY_BUSY;
  440. t_command command;
  441. command.id=FZ_COMMAND_FILETRANSFER;
  442. command.transferfile=TransferFile;
  443. m_pMainThread->Command(command);
  444. if (m_hOwnerWnd)
  445. return FZ_REPLY_WOULDBLOCK;
  446. else
  447. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  448. }
  449. int CFileZillaApi::GetCurrentServer(t_server &server)
  450. {
  451. //Check if call allowed
  452. if (!m_bInitialized)
  453. return FZ_REPLY_NOTINITIALIZED;
  454. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  455. return FZ_REPLY_NOTCONNECTED;
  456. if (m_pMainThread->GetCurrentServer(server))
  457. return FZ_REPLY_OK;
  458. else
  459. return FZ_REPLY_NOTCONNECTED;
  460. }
  461. #ifdef MPEXT
  462. int CFileZillaApi::SetCurrentPath(CServerPath path)
  463. {
  464. //Check if call allowed
  465. if (!m_bInitialized)
  466. return FZ_REPLY_NOTINITIALIZED;
  467. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  468. return FZ_REPLY_NOTCONNECTED;
  469. m_pMainThread->SetCurrentPath(path);
  470. return FZ_REPLY_OK;
  471. }
  472. int CFileZillaApi::GetCurrentPath(CServerPath & path)
  473. {
  474. //Check if call allowed
  475. if (!m_bInitialized)
  476. return FZ_REPLY_NOTINITIALIZED;
  477. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  478. return FZ_REPLY_NOTCONNECTED;
  479. return (m_pMainThread->GetCurrentPath(path) ? FZ_REPLY_OK : FZ_REPLY_NOTCONNECTED);
  480. }
  481. #endif
  482. int CFileZillaApi::CustomCommand(CString CustomCommand)
  483. {
  484. //Check if call allowed
  485. if (!m_bInitialized)
  486. return FZ_REPLY_NOTINITIALIZED;
  487. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  488. return FZ_REPLY_NOTCONNECTED;
  489. if (IsBusy()==FZ_REPLY_BUSY)
  490. return FZ_REPLY_BUSY;
  491. t_server server;
  492. int res=GetCurrentServer(server);
  493. if (res!=FZ_REPLY_OK)
  494. return res;
  495. #ifndef MPEXT_NO_SFTP
  496. if (server.nServerType&FZ_SERVERTYPE_SUB_FTP_SFTP)
  497. return FZ_REPLY_NOTSUPPORTED;
  498. #endif
  499. if (CustomCommand=="")
  500. return FZ_REPLY_INVALIDPARAM;
  501. t_command command;
  502. command.id=FZ_COMMAND_CUSTOMCOMMAND;
  503. command.param1=CustomCommand;
  504. m_pMainThread->Command(command);
  505. if (m_hOwnerWnd)
  506. return FZ_REPLY_WOULDBLOCK;
  507. else
  508. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  509. }
  510. int CFileZillaApi::Delete(CString FileName, const CServerPath &path /*=CServerPath()*/)
  511. {
  512. //Check if call allowed
  513. if (!m_bInitialized)
  514. return FZ_REPLY_NOTINITIALIZED;
  515. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  516. return FZ_REPLY_NOTCONNECTED;
  517. if (IsBusy()==FZ_REPLY_BUSY)
  518. return FZ_REPLY_BUSY;
  519. if (FileName=="")
  520. return FZ_REPLY_INVALIDPARAM;
  521. CServerPath path2=path;
  522. if (path2.IsEmpty())
  523. {
  524. m_pMainThread->GetCurrentPath(path2);
  525. if (path2.IsEmpty())
  526. return FZ_REPLY_INVALIDPARAM;
  527. }
  528. t_command command;
  529. command.id=FZ_COMMAND_DELETE;
  530. command.param1=FileName;
  531. command.path=path2;
  532. m_pMainThread->Command(command);
  533. if (m_hOwnerWnd)
  534. return FZ_REPLY_WOULDBLOCK;
  535. else
  536. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  537. }
  538. int CFileZillaApi::RemoveDir(CString DirName, const CServerPath &path /*=CServerPath()*/)
  539. {
  540. //Check if call allowed
  541. if (!m_bInitialized)
  542. return FZ_REPLY_NOTINITIALIZED;
  543. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  544. return FZ_REPLY_NOTCONNECTED;
  545. if (IsBusy()==FZ_REPLY_BUSY)
  546. return FZ_REPLY_BUSY;
  547. if (DirName=="")
  548. return FZ_REPLY_INVALIDPARAM;
  549. CServerPath path2=path;
  550. if (path2.IsEmpty())
  551. {
  552. m_pMainThread->GetCurrentPath(path2);
  553. if (path2.IsEmpty())
  554. return FZ_REPLY_INVALIDPARAM;
  555. }
  556. t_command command;
  557. command.id=FZ_COMMAND_REMOVEDIR;
  558. command.param1=DirName;
  559. command.path=path2;
  560. m_pMainThread->Command(command);
  561. if (m_hOwnerWnd)
  562. return FZ_REPLY_WOULDBLOCK;
  563. else
  564. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  565. return FZ_REPLY_ERROR;
  566. }
  567. int CFileZillaApi::MakeDir(const CServerPath &path)
  568. {
  569. //Check if call allowed
  570. if (!m_bInitialized)
  571. return FZ_REPLY_NOTINITIALIZED;
  572. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  573. return FZ_REPLY_NOTCONNECTED;
  574. if (IsBusy()==FZ_REPLY_BUSY)
  575. return FZ_REPLY_BUSY;
  576. if (path.IsEmpty() || !path.HasParent())
  577. return FZ_REPLY_INVALIDPARAM;
  578. t_command command;
  579. command.id=FZ_COMMAND_MAKEDIR;
  580. command.path=path;
  581. m_pMainThread->Command(command);
  582. if (m_hOwnerWnd)
  583. return FZ_REPLY_WOULDBLOCK;
  584. else
  585. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  586. return FZ_REPLY_ERROR;
  587. }
  588. int CFileZillaApi::Rename(CString oldName, CString newName, const CServerPath &path /*=CServerPath()*/, const CServerPath &newPath /*=CServerPath()*/)
  589. {
  590. //Check if call allowed
  591. if (!m_bInitialized)
  592. return FZ_REPLY_NOTINITIALIZED;
  593. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  594. return FZ_REPLY_NOTCONNECTED;
  595. if (IsBusy()==FZ_REPLY_BUSY)
  596. return FZ_REPLY_BUSY;
  597. if (oldName=="" || newName=="")
  598. return FZ_REPLY_INVALIDPARAM;
  599. CServerPath path2 = path;
  600. if (path2.IsEmpty())
  601. {
  602. m_pMainThread->GetCurrentPath(path2);
  603. if (path2.IsEmpty())
  604. return FZ_REPLY_INVALIDPARAM;
  605. }
  606. t_command command;
  607. command.id = FZ_COMMAND_RENAME;
  608. command.param1 = oldName;
  609. command.param2 = newName;
  610. command.path = path2;
  611. command.newPath = newPath;
  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::SetAsyncRequestResult(int nAction, CAsyncRequestData *pData)
  620. {
  621. if (!this || !pData)
  622. return FZ_REPLY_CRITICALERROR | FZ_REPLY_INVALIDPARAM;
  623. if (IsBadWritePtr(pData, sizeof(CAsyncRequestData)))
  624. return FZ_REPLY_CRITICALERROR;
  625. if (!m_bInitialized)
  626. {
  627. delete pData;
  628. return FZ_REPLY_NOTINITIALIZED;
  629. }
  630. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  631. {
  632. delete pData;
  633. return FZ_REPLY_NOTCONNECTED;
  634. }
  635. switch(pData->nRequestType)
  636. {
  637. case FZ_ASYNCREQUEST_OVERWRITE:
  638. break;
  639. #ifndef MPEXT_NO_SSL
  640. case FZ_ASYNCREQUEST_VERIFYCERT:
  641. if (!((CVerifyCertRequestData *)pData)->pCertData)
  642. {
  643. delete pData;
  644. return FZ_REPLY_INVALIDPARAM;
  645. }
  646. break;
  647. #endif
  648. #ifndef MPEXT_NO_GSS
  649. case FZ_ASYNCREQUEST_GSS_AUTHFAILED:
  650. case FZ_ASYNCREQUEST_GSS_NEEDUSER:
  651. case FZ_ASYNCREQUEST_GSS_NEEDPASS:
  652. #ifdef MPEXT
  653. break;
  654. #endif
  655. #endif
  656. #ifndef MPEXT_NO_SFTP
  657. case FZ_ASYNCREQUEST_NEWHOSTKEY:
  658. case FZ_ASYNCREQUEST_CHANGEDHOSTKEY:
  659. break;
  660. #endif
  661. default:
  662. delete pData;
  663. return FZ_REPLY_INVALIDPARAM;
  664. }
  665. pData->nRequestResult = nAction;
  666. if (!m_pMainThread)
  667. {
  668. delete pData;
  669. return FZ_REPLY_NOTINITIALIZED;
  670. }
  671. m_pMainThread->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_ASYNCREQUESTREPLY, (LPARAM)pData);
  672. return FZ_REPLY_OK;
  673. }
  674. #ifndef MPEXT
  675. int CFileZillaApi::SetOption(int nOption, int value)
  676. {
  677. if (!m_bInitialized)
  678. return FZ_REPLY_NOTINITIALIZED;
  679. switch (nOption)
  680. {
  681. case FZAPI_OPTION_SHOWHIDDEN:
  682. m_pMainThread->SetOption(nOption, value);
  683. break;
  684. default:
  685. return FZ_REPLY_INVALIDPARAM;
  686. }
  687. return FZ_REPLY_OK;
  688. }
  689. int CFileZillaApi::GetOption(int nOption, int &value)
  690. {
  691. if (!m_bInitialized)
  692. return FZ_REPLY_NOTINITIALIZED;
  693. switch (nOption)
  694. {
  695. case FZAPI_OPTION_SHOWHIDDEN:
  696. value = m_pMainThread->GetOption(nOption);
  697. break;
  698. default:
  699. return FZ_REPLY_INVALIDPARAM;
  700. }
  701. return FZ_REPLY_OK;
  702. }
  703. #endif
  704. int CFileZillaApi::Chmod(int nValue, CString FileName, const CServerPath &path /*=CServerPath()*/ )
  705. {
  706. //Check if call allowed
  707. if (!m_bInitialized)
  708. return FZ_REPLY_NOTINITIALIZED;
  709. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  710. return FZ_REPLY_NOTCONNECTED;
  711. if (IsBusy()==FZ_REPLY_BUSY)
  712. return FZ_REPLY_BUSY;
  713. if (FileName=="")
  714. return FZ_REPLY_INVALIDPARAM;
  715. t_command command;
  716. command.id=FZ_COMMAND_CHMOD;
  717. command.param1=FileName;
  718. command.param4=nValue;
  719. command.path=path;
  720. m_pMainThread->Command(command);
  721. if (m_hOwnerWnd)
  722. return FZ_REPLY_WOULDBLOCK;
  723. else
  724. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  725. }
  726. int CFileZillaApi::SetDebugLevel(int nDebugLevel)
  727. {
  728. //Check if call allowed
  729. if (!m_bInitialized)
  730. return FZ_REPLY_NOTINITIALIZED;
  731. if (!m_pMainThread->SetDebugLevel(nDebugLevel))
  732. return FZ_REPLY_ERROR;
  733. return FZ_REPLY_OK;
  734. }
  735. #ifndef MPEXT_NO_CACHE
  736. BOOL CFileZillaApi::DumpDirectoryCache(LPCTSTR pFileName)
  737. {
  738. CDirectoryCache cache;
  739. return cache.Dump(pFileName);
  740. }
  741. #endif
  742. //CAsyncRequestData derived classes
  743. CAsyncRequestData::CAsyncRequestData()
  744. {
  745. }
  746. CAsyncRequestData::~CAsyncRequestData()
  747. {
  748. }
  749. COverwriteRequestData::COverwriteRequestData()
  750. {
  751. nRequestType=FZ_ASYNCREQUEST_OVERWRITE;
  752. time1=0;
  753. time2=0;
  754. pTransferFile=0;
  755. }
  756. COverwriteRequestData::~COverwriteRequestData()
  757. {
  758. delete pTransferFile;
  759. delete time1;
  760. delete time2;
  761. }
  762. #ifndef MPEXT_NO_SSL
  763. CVerifyCertRequestData::CVerifyCertRequestData()
  764. {
  765. nRequestType=FZ_ASYNCREQUEST_VERIFYCERT;
  766. pCertData=0;
  767. }
  768. CVerifyCertRequestData::~CVerifyCertRequestData()
  769. {
  770. delete pCertData;
  771. }
  772. #endif
  773. #ifndef MPEXT_NO_GSS
  774. CGssNeedPassRequestData::CGssNeedPassRequestData()
  775. {
  776. nRequestType=FZ_ASYNCREQUEST_GSS_NEEDPASS;
  777. }
  778. CGssNeedPassRequestData::~CGssNeedPassRequestData()
  779. {
  780. }
  781. CGssNeedUserRequestData::CGssNeedUserRequestData()
  782. {
  783. nRequestType = FZ_ASYNCREQUEST_GSS_NEEDUSER;
  784. }
  785. CGssNeedUserRequestData::~CGssNeedUserRequestData()
  786. {
  787. }
  788. #endif
  789. #ifndef MPEXT_NO_SFTP
  790. CNewHostKeyRequestData::CNewHostKeyRequestData()
  791. {
  792. nRequestType=FZ_ASYNCREQUEST_NEWHOSTKEY;
  793. }
  794. CNewHostKeyRequestData::~CNewHostKeyRequestData()
  795. {
  796. }
  797. CChangedHostKeyRequestData::CChangedHostKeyRequestData()
  798. {
  799. nRequestType=FZ_ASYNCREQUEST_CHANGEDHOSTKEY;
  800. }
  801. CChangedHostKeyRequestData::~CChangedHostKeyRequestData()
  802. {
  803. }
  804. #endif
  805. BOOL CFileZillaApi::IsValid() const
  806. {
  807. if (!this)
  808. return FALSE;
  809. if (IsBadWritePtr((VOID *)this, sizeof(CFileZillaApi)) )
  810. return FALSE;
  811. if (IsBadWritePtr((VOID *)&m_bInitialized, sizeof(BOOL)) ||
  812. IsBadWritePtr((VOID *)&m_hOwnerWnd, sizeof(HWND)) ||
  813. IsBadWritePtr((VOID *)&m_nInternalMessageID, sizeof(unsigned int)) ||
  814. IsBadWritePtr((VOID *)&m_nReplyMessageID, sizeof(unsigned int)) ||
  815. IsBadWritePtr(m_pMainThread, sizeof(CMainThread)) )
  816. return FALSE;
  817. if (!m_pMainThread->IsValid())
  818. return FALSE;
  819. return TRUE;
  820. }