FileZillaApi.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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==_MPT("") || 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 ((_MPT(".")+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 == _MPT(""))
  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!=_MPT(""))
  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==_MPT("") || 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. #ifdef MPEXT
  430. int CFileZillaApi::ListFile(const CServerPath& path, const CString& fileName)
  431. {
  432. //Check if call allowed
  433. if (!m_bInitialized)
  434. return FZ_REPLY_NOTINITIALIZED;
  435. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  436. return FZ_REPLY_NOTCONNECTED;
  437. if (fileName.IsEmpty())
  438. return FZ_REPLY_INVALIDPARAM;
  439. if (m_pMainThread->IsBusy())
  440. return FZ_REPLY_BUSY;
  441. t_command command;
  442. command.id=FZ_COMMAND_LISTFILE;
  443. command.path=path;
  444. command.param1=fileName;
  445. m_pMainThread->Command(command);
  446. if (m_hOwnerWnd)
  447. return FZ_REPLY_WOULDBLOCK;
  448. else
  449. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  450. }
  451. #endif
  452. int CFileZillaApi::FileTransfer(const t_transferfile &TransferFile)
  453. {
  454. //Check if call allowed
  455. if (!m_bInitialized)
  456. return FZ_REPLY_NOTINITIALIZED;
  457. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  458. return FZ_REPLY_NOTCONNECTED;
  459. if (TransferFile.remotefile==_MPT("") || TransferFile.localfile==_MPT("") || TransferFile.remotepath.IsEmpty())
  460. return FZ_REPLY_INVALIDPARAM;
  461. if (IsBusy()==FZ_REPLY_BUSY)
  462. return FZ_REPLY_BUSY;
  463. t_command command;
  464. command.id=FZ_COMMAND_FILETRANSFER;
  465. command.transferfile=TransferFile;
  466. m_pMainThread->Command(command);
  467. if (m_hOwnerWnd)
  468. return FZ_REPLY_WOULDBLOCK;
  469. else
  470. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  471. }
  472. int CFileZillaApi::GetCurrentServer(t_server &server)
  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. if (m_pMainThread->GetCurrentServer(server))
  480. return FZ_REPLY_OK;
  481. else
  482. return FZ_REPLY_NOTCONNECTED;
  483. }
  484. #ifdef MPEXT
  485. int CFileZillaApi::SetCurrentPath(CServerPath path)
  486. {
  487. //Check if call allowed
  488. if (!m_bInitialized)
  489. return FZ_REPLY_NOTINITIALIZED;
  490. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  491. return FZ_REPLY_NOTCONNECTED;
  492. m_pMainThread->SetCurrentPath(path);
  493. return FZ_REPLY_OK;
  494. }
  495. int CFileZillaApi::GetCurrentPath(CServerPath & path)
  496. {
  497. //Check if call allowed
  498. if (!m_bInitialized)
  499. return FZ_REPLY_NOTINITIALIZED;
  500. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  501. return FZ_REPLY_NOTCONNECTED;
  502. return (m_pMainThread->GetCurrentPath(path) ? FZ_REPLY_OK : FZ_REPLY_NOTCONNECTED);
  503. }
  504. #endif
  505. int CFileZillaApi::CustomCommand(CString CustomCommand)
  506. {
  507. //Check if call allowed
  508. if (!m_bInitialized)
  509. return FZ_REPLY_NOTINITIALIZED;
  510. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  511. return FZ_REPLY_NOTCONNECTED;
  512. if (IsBusy()==FZ_REPLY_BUSY)
  513. return FZ_REPLY_BUSY;
  514. t_server server;
  515. int res=GetCurrentServer(server);
  516. if (res!=FZ_REPLY_OK)
  517. return res;
  518. #ifndef MPEXT_NO_SFTP
  519. if (server.nServerType&FZ_SERVERTYPE_SUB_FTP_SFTP)
  520. return FZ_REPLY_NOTSUPPORTED;
  521. #endif
  522. if (CustomCommand==_MPT(""))
  523. return FZ_REPLY_INVALIDPARAM;
  524. t_command command;
  525. command.id=FZ_COMMAND_CUSTOMCOMMAND;
  526. command.param1=CustomCommand;
  527. m_pMainThread->Command(command);
  528. if (m_hOwnerWnd)
  529. return FZ_REPLY_WOULDBLOCK;
  530. else
  531. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  532. }
  533. int CFileZillaApi::Delete(CString FileName, const CServerPath &path /*=CServerPath()*/)
  534. {
  535. //Check if call allowed
  536. if (!m_bInitialized)
  537. return FZ_REPLY_NOTINITIALIZED;
  538. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  539. return FZ_REPLY_NOTCONNECTED;
  540. if (IsBusy()==FZ_REPLY_BUSY)
  541. return FZ_REPLY_BUSY;
  542. if (FileName=="")
  543. return FZ_REPLY_INVALIDPARAM;
  544. CServerPath path2=path;
  545. if (path2.IsEmpty())
  546. {
  547. m_pMainThread->GetCurrentPath(path2);
  548. if (path2.IsEmpty())
  549. return FZ_REPLY_INVALIDPARAM;
  550. }
  551. t_command command;
  552. command.id=FZ_COMMAND_DELETE;
  553. command.param1=FileName;
  554. command.path=path2;
  555. m_pMainThread->Command(command);
  556. if (m_hOwnerWnd)
  557. return FZ_REPLY_WOULDBLOCK;
  558. else
  559. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  560. }
  561. int CFileZillaApi::RemoveDir(CString DirName, const CServerPath &path /*=CServerPath()*/)
  562. {
  563. //Check if call allowed
  564. if (!m_bInitialized)
  565. return FZ_REPLY_NOTINITIALIZED;
  566. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  567. return FZ_REPLY_NOTCONNECTED;
  568. if (IsBusy()==FZ_REPLY_BUSY)
  569. return FZ_REPLY_BUSY;
  570. if (DirName==_MPT(""))
  571. return FZ_REPLY_INVALIDPARAM;
  572. CServerPath path2=path;
  573. if (path2.IsEmpty())
  574. {
  575. m_pMainThread->GetCurrentPath(path2);
  576. if (path2.IsEmpty())
  577. return FZ_REPLY_INVALIDPARAM;
  578. }
  579. t_command command;
  580. command.id=FZ_COMMAND_REMOVEDIR;
  581. command.param1=DirName;
  582. command.path=path2;
  583. m_pMainThread->Command(command);
  584. if (m_hOwnerWnd)
  585. return FZ_REPLY_WOULDBLOCK;
  586. else
  587. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  588. return FZ_REPLY_ERROR;
  589. }
  590. int CFileZillaApi::MakeDir(const CServerPath &path)
  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 (path.IsEmpty() || !path.HasParent())
  600. return FZ_REPLY_INVALIDPARAM;
  601. t_command command;
  602. command.id=FZ_COMMAND_MAKEDIR;
  603. command.path=path;
  604. m_pMainThread->Command(command);
  605. if (m_hOwnerWnd)
  606. return FZ_REPLY_WOULDBLOCK;
  607. else
  608. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  609. return FZ_REPLY_ERROR;
  610. }
  611. int CFileZillaApi::Rename(CString oldName, CString newName, const CServerPath &path /*=CServerPath()*/, const CServerPath &newPath /*=CServerPath()*/)
  612. {
  613. //Check if call allowed
  614. if (!m_bInitialized)
  615. return FZ_REPLY_NOTINITIALIZED;
  616. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  617. return FZ_REPLY_NOTCONNECTED;
  618. if (IsBusy()==FZ_REPLY_BUSY)
  619. return FZ_REPLY_BUSY;
  620. if (oldName==_MPT("") || newName==_MPT(""))
  621. return FZ_REPLY_INVALIDPARAM;
  622. CServerPath path2 = path;
  623. if (path2.IsEmpty())
  624. {
  625. m_pMainThread->GetCurrentPath(path2);
  626. if (path2.IsEmpty())
  627. return FZ_REPLY_INVALIDPARAM;
  628. }
  629. t_command command;
  630. command.id = FZ_COMMAND_RENAME;
  631. command.param1 = oldName;
  632. command.param2 = newName;
  633. command.path = path2;
  634. command.newPath = newPath;
  635. m_pMainThread->Command(command);
  636. if (m_hOwnerWnd)
  637. return FZ_REPLY_WOULDBLOCK;
  638. else
  639. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  640. return FZ_REPLY_ERROR;
  641. }
  642. int CFileZillaApi::SetAsyncRequestResult(int nAction, CAsyncRequestData *pData)
  643. {
  644. if (!this || !pData)
  645. return FZ_REPLY_CRITICALERROR | FZ_REPLY_INVALIDPARAM;
  646. if (IsBadWritePtr(pData, sizeof(CAsyncRequestData)))
  647. return FZ_REPLY_CRITICALERROR;
  648. if (!m_bInitialized)
  649. {
  650. delete pData;
  651. return FZ_REPLY_NOTINITIALIZED;
  652. }
  653. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  654. {
  655. delete pData;
  656. return FZ_REPLY_NOTCONNECTED;
  657. }
  658. switch(pData->nRequestType)
  659. {
  660. case FZ_ASYNCREQUEST_OVERWRITE:
  661. break;
  662. #ifndef MPEXT_NO_SSL
  663. case FZ_ASYNCREQUEST_VERIFYCERT:
  664. if (!((CVerifyCertRequestData *)pData)->pCertData)
  665. {
  666. delete pData;
  667. return FZ_REPLY_INVALIDPARAM;
  668. }
  669. break;
  670. #endif
  671. case FZ_ASYNCREQUEST_NEEDPASS:
  672. break;
  673. #ifndef MPEXT_NO_GSS
  674. case FZ_ASYNCREQUEST_GSS_AUTHFAILED:
  675. case FZ_ASYNCREQUEST_GSS_NEEDUSER:
  676. case FZ_ASYNCREQUEST_GSS_NEEDPASS:
  677. #ifdef MPEXT
  678. break;
  679. #endif
  680. #endif
  681. #ifndef MPEXT_NO_SFTP
  682. case FZ_ASYNCREQUEST_NEWHOSTKEY:
  683. case FZ_ASYNCREQUEST_CHANGEDHOSTKEY:
  684. break;
  685. #endif
  686. default:
  687. delete pData;
  688. return FZ_REPLY_INVALIDPARAM;
  689. }
  690. pData->nRequestResult = nAction;
  691. if (!m_pMainThread)
  692. {
  693. delete pData;
  694. return FZ_REPLY_NOTINITIALIZED;
  695. }
  696. m_pMainThread->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_ASYNCREQUESTREPLY, (LPARAM)pData);
  697. return FZ_REPLY_OK;
  698. }
  699. #ifndef MPEXT
  700. int CFileZillaApi::SetOption(int nOption, int value)
  701. {
  702. if (!m_bInitialized)
  703. return FZ_REPLY_NOTINITIALIZED;
  704. switch (nOption)
  705. {
  706. case FZAPI_OPTION_SHOWHIDDEN:
  707. m_pMainThread->SetOption(nOption, value);
  708. break;
  709. default:
  710. return FZ_REPLY_INVALIDPARAM;
  711. }
  712. return FZ_REPLY_OK;
  713. }
  714. int CFileZillaApi::GetOption(int nOption, int &value)
  715. {
  716. if (!m_bInitialized)
  717. return FZ_REPLY_NOTINITIALIZED;
  718. switch (nOption)
  719. {
  720. case FZAPI_OPTION_SHOWHIDDEN:
  721. value = m_pMainThread->GetOption(nOption);
  722. break;
  723. default:
  724. return FZ_REPLY_INVALIDPARAM;
  725. }
  726. return FZ_REPLY_OK;
  727. }
  728. #endif
  729. int CFileZillaApi::Chmod(int nValue, CString FileName, const CServerPath &path /*=CServerPath()*/ )
  730. {
  731. //Check if call allowed
  732. if (!m_bInitialized)
  733. return FZ_REPLY_NOTINITIALIZED;
  734. if (IsConnected()==FZ_REPLY_NOTCONNECTED)
  735. return FZ_REPLY_NOTCONNECTED;
  736. if (IsBusy()==FZ_REPLY_BUSY)
  737. return FZ_REPLY_BUSY;
  738. if (FileName==_MPT(""))
  739. return FZ_REPLY_INVALIDPARAM;
  740. t_command command;
  741. command.id=FZ_COMMAND_CHMOD;
  742. command.param1=FileName;
  743. command.param4=nValue;
  744. command.path=path;
  745. m_pMainThread->Command(command);
  746. if (m_hOwnerWnd)
  747. return FZ_REPLY_WOULDBLOCK;
  748. else
  749. return m_pMainThread->LastOperationSuccessful()?FZ_REPLY_OK:FZ_REPLY_ERROR;
  750. }
  751. int CFileZillaApi::SetDebugLevel(int nDebugLevel)
  752. {
  753. //Check if call allowed
  754. if (!m_bInitialized)
  755. return FZ_REPLY_NOTINITIALIZED;
  756. if (!m_pMainThread->SetDebugLevel(nDebugLevel))
  757. return FZ_REPLY_ERROR;
  758. return FZ_REPLY_OK;
  759. }
  760. #ifndef MPEXT_NO_CACHE
  761. BOOL CFileZillaApi::DumpDirectoryCache(LPCTSTR pFileName)
  762. {
  763. CDirectoryCache cache;
  764. return cache.Dump(pFileName);
  765. }
  766. #endif
  767. //CAsyncRequestData derived classes
  768. CAsyncRequestData::CAsyncRequestData()
  769. {
  770. nRequestType = 0;
  771. nRequestID = 0;
  772. nRequestResult = 0;
  773. }
  774. CAsyncRequestData::~CAsyncRequestData()
  775. {
  776. }
  777. COverwriteRequestData::COverwriteRequestData()
  778. {
  779. size1 = 0;
  780. size2 = 0;
  781. nRequestType=FZ_ASYNCREQUEST_OVERWRITE;
  782. time1=0;
  783. time2=0;
  784. pTransferFile=0;
  785. }
  786. COverwriteRequestData::~COverwriteRequestData()
  787. {
  788. delete pTransferFile;
  789. delete time1;
  790. delete time2;
  791. }
  792. #ifndef MPEXT_NO_SSL
  793. CVerifyCertRequestData::CVerifyCertRequestData()
  794. {
  795. nRequestType=FZ_ASYNCREQUEST_VERIFYCERT;
  796. pCertData=0;
  797. }
  798. CVerifyCertRequestData::~CVerifyCertRequestData()
  799. {
  800. delete pCertData;
  801. }
  802. #endif
  803. CNeedPassRequestData::CNeedPassRequestData()
  804. {
  805. nRequestType=FZ_ASYNCREQUEST_NEEDPASS;
  806. nOldOpState=0;
  807. }
  808. CNeedPassRequestData::~CNeedPassRequestData()
  809. {
  810. }
  811. #ifndef MPEXT_NO_GSS
  812. CGssNeedPassRequestData::CGssNeedPassRequestData()
  813. {
  814. nRequestType=FZ_ASYNCREQUEST_GSS_NEEDPASS;
  815. }
  816. CGssNeedPassRequestData::~CGssNeedPassRequestData()
  817. {
  818. }
  819. CGssNeedUserRequestData::CGssNeedUserRequestData()
  820. {
  821. nRequestType = FZ_ASYNCREQUEST_GSS_NEEDUSER;
  822. }
  823. CGssNeedUserRequestData::~CGssNeedUserRequestData()
  824. {
  825. }
  826. #endif
  827. #ifndef MPEXT_NO_SFTP
  828. CNewHostKeyRequestData::CNewHostKeyRequestData()
  829. {
  830. nRequestType=FZ_ASYNCREQUEST_NEWHOSTKEY;
  831. }
  832. CNewHostKeyRequestData::~CNewHostKeyRequestData()
  833. {
  834. }
  835. CChangedHostKeyRequestData::CChangedHostKeyRequestData()
  836. {
  837. nRequestType=FZ_ASYNCREQUEST_CHANGEDHOSTKEY;
  838. }
  839. CChangedHostKeyRequestData::~CChangedHostKeyRequestData()
  840. {
  841. }
  842. #endif
  843. BOOL CFileZillaApi::IsValid() const
  844. {
  845. if (!this)
  846. return FALSE;
  847. if (IsBadWritePtr((VOID *)this, sizeof(CFileZillaApi)) )
  848. return FALSE;
  849. if (IsBadWritePtr((VOID *)&m_bInitialized, sizeof(BOOL)) ||
  850. IsBadWritePtr((VOID *)&m_hOwnerWnd, sizeof(HWND)) ||
  851. IsBadWritePtr((VOID *)&m_nInternalMessageID, sizeof(unsigned int)) ||
  852. IsBadWritePtr((VOID *)&m_nReplyMessageID, sizeof(unsigned int)) ||
  853. IsBadWritePtr(m_pMainThread, sizeof(CMainThread)) )
  854. return FALSE;
  855. if (!m_pMainThread->IsValid())
  856. return FALSE;
  857. return TRUE;
  858. }