Ftp.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /******************************************************************************
  2. Use 'Ftp' to handle FTP connections.
  3. /******************************************************************************/
  4. struct FtpFile : FileInfo
  5. {
  6. Str name;
  7. };
  8. struct Ftp // File Transfer Protocol, Ftp supports only one command/transfer at a time
  9. {
  10. // manage
  11. Bool login (C Str8 &host, C Str8 &user, C Str8 &password, Bool ignore_auth_result=false); // connect to FTP, false on fail, connection will use secure mode if 'host' starts with "ftps://", 'ignore_auth_result'=if ignore authorization results and continue even when they failed
  12. void logout( ); // disconnect from FTP
  13. // get / set
  14. Bool is ( )C {return _socket.is();} // if connected to the FTP
  15. Long progress ( )C {return _progress ;} // get transfer progress (in bytes)
  16. Long total ( )C {return _total ;} // get transfer total (in bytes, -1=unknown)
  17. Bool noop ( ); // send empty message to keep up the connection, false on fail
  18. Long fileSize (C Str &file ); // get file size , -1 on fail
  19. Bool fileTime (C Str &file, DateTime &dt ); // get file modification time , false on fail
  20. Bool rename (C Str &src , C Str &dest); // rename file/directory , false on fail
  21. Bool removeFile(C Str &file ); // remove file , false on fail
  22. Bool removeDir (C Str &dir ); // remove empty directory , false on fail
  23. Bool createDir (C Str &dir ); // create empty directory , false on fail
  24. Bool changeDir (C Str &dir ); // change current directory , false on fail
  25. // io, if following functions will be called in a secondary thread, then you may not perform any operations on the given 'File', until the methods return
  26. Bool download(C Str &src , File &dest, Long offset=0, Bool passive=true, Cipher * src_cipher=null); // download 'src' file from FTP to 'dest' file , 'passive'=transfer mode, false on fail, 'dest' file should be already opened for writing mode (either to disk or memory)
  27. Bool upload( File &src , C Str &dest , Bool passive=true, Cipher *dest_cipher=null); // upload 'src' file to FTP 'dest' location , 'passive'=transfer mode, false on fail, 'src' file should be already opened for reading mode (either from disk or memory), data is uploaded from current position of 'src' file (not from start)
  28. Bool append( File &src , C Str &dest , Bool passive=true ); // append 'src' file to FTP 'dest' location , 'passive'=transfer mode, false on fail, 'src' file should be already opened for reading mode (either from disk or memory), data is uploaded from current position of 'src' file (not from start)
  29. Bool listFiles(C Str &path, MemPtr<FtpFile> files , Bool passive=true ); // retrieve a list of all files in the specified path, 'passive'=transfer mode, false on fail, warning: not all fields in the 'FtpFile' may be valid, this is because hosts don't necessarily provide all the info
  30. Bool listNames(C Str &path, MemPtr<Str > names , Bool passive=true ); // retrieve a list of all names in the specified path, 'passive'=transfer mode, false on fail
  31. void abort( ); // notify that the current transfer operation should be aborted, this can be called in a secondary thread to cancel any active transfer
  32. ~Ftp() {logout();}
  33. Ftp();
  34. private:
  35. Bool _binary , _abort, _timeout, _timeouts;
  36. Int _port;
  37. Long _progress, _total;
  38. Str8 _response, _host, _user, _password;
  39. SecureSocket _socket;
  40. #if EE_PRIVATE
  41. Bool send (SecureSocket &socket, CPtr data, Int size);
  42. Str8 response (Int time);
  43. Str8 command (Str8 cmd);
  44. Bool mode (Bool binary);
  45. Bool connect (SecureSocket &transfer, C Str &ftp_file, CChar8 *cmd, Long offset, Bool passive);
  46. Bool transfer (File &file , C Str &ftp_file, CChar8 *cmd, Long offset, Bool passive, Bool send, Bool binary, Cipher *cipher=null);
  47. Bool reconnect();
  48. #endif
  49. };
  50. /******************************************************************************/