FileZillaApi.cpp 25 KB

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