|
| 1 | +#include "FTPServer.h" |
| 2 | +#include <stdint.h> |
| 3 | +#include <fstream> |
| 4 | +#include <dirent.h> |
| 5 | + |
| 6 | + |
| 7 | +/** |
| 8 | + * Called at the start of a STOR request. The file name is the name of the file the client would like to |
| 9 | + * save. |
| 10 | + */ |
| 11 | +void FTPFileCallbacks::onStoreStart(std::string fileName) { |
| 12 | + printf(">> FTPFileCallbacks::onStoreStart: fileName=%s\n", fileName.c_str()); |
| 13 | + m_storeFile.open(fileName, std::ios::binary); // Open the file for writing. |
| 14 | + if (m_storeFile.fail()) { |
| 15 | + throw FTPServer::FileException(); |
| 16 | + } |
| 17 | + printf("<< FTPFileCallbacks::onStoreStart\n"); |
| 18 | +} // FTPFileCallbacks#onStoreStart |
| 19 | + |
| 20 | + |
| 21 | +/** |
| 22 | + * Called when the client presents a new chunk of data to be saved. |
| 23 | + */ |
| 24 | +size_t FTPFileCallbacks::onStoreData(uint8_t* data, size_t size) { |
| 25 | + printf(">> FTPFileCallbacks::onStoreData: size=%ld\n", size); |
| 26 | + m_storeFile.write((char *)data, size); // Store data received. |
| 27 | + printf("<< FTPFileCallbacks::onStoreData: size=%ld\n", size); |
| 28 | + return size; |
| 29 | +} // FTPFileCallbacks#onStoreData |
| 30 | + |
| 31 | + |
| 32 | +/** |
| 33 | + * Called at the end of a STOR request. This indicates that the client has completed its transmission of the |
| 34 | + * file. |
| 35 | + */ |
| 36 | +void FTPFileCallbacks::onStoreEnd() { |
| 37 | + printf(">> FTPFileCallbacks::onStoreEnd\n"); |
| 38 | + m_storeFile.close(); // Close the open file. |
| 39 | + printf("<< FTPFileCallbacks::onStoreEnd\n"); |
| 40 | +} // FTPFileCallbacks#onStoreEnd |
| 41 | + |
| 42 | + |
| 43 | +/** |
| 44 | + * Called when the client requests retrieval of a file. |
| 45 | + */ |
| 46 | +void FTPFileCallbacks::onRetrieveStart(std::string fileName) { |
| 47 | + printf(">> FTPFileCallbacks::onRetrieveStart: fileName=%s\n", fileName.c_str()); |
| 48 | + m_byteCount = 0; |
| 49 | + m_retrieveFile.open(fileName, std::ios::binary); |
| 50 | + if (m_retrieveFile.fail()) { |
| 51 | + printf("<< FTPFileCallbacks::onRetrieveStart: ***FileException***\n"); |
| 52 | + throw FTPServer::FileException(); |
| 53 | + } |
| 54 | + printf("<< FTPFileCallbacks::onRetrieveStart\n"); |
| 55 | +} // FTPFileCallbacks#onRetrieveStart |
| 56 | + |
| 57 | + |
| 58 | +/** |
| 59 | + * Called when the client is ready to receive the next piece of the file. To indicate that there |
| 60 | + * is no more data to send, return a size of 0. |
| 61 | + * @param data The data buffer that we can fill to return data back to the client. |
| 62 | + * @param size The maximum size of the data buffer that we can populate. |
| 63 | + * @return The size of data being returned. Return 0 to indicate that there is no more data to return. |
| 64 | + */ |
| 65 | +size_t FTPFileCallbacks::onRetrieveData(uint8_t* data, size_t size) { |
| 66 | + printf(">> FTPFileCallbacks::onRetrieveData\n"); |
| 67 | + m_retrieveFile.read((char *)data, size); |
| 68 | + size_t readSize = m_retrieveFile.gcount(); |
| 69 | + m_byteCount += readSize; |
| 70 | + printf("<< FTPFileCallbacks::onRetrieveData: sizeRead=%ld\n", readSize); |
| 71 | + return m_retrieveFile.gcount(); // Return the number of bytes read. |
| 72 | +} // FTPFileCallbacks#onRetrieveData |
| 73 | + |
| 74 | + |
| 75 | +/** |
| 76 | + * Called when the retrieval has been completed. |
| 77 | + */ |
| 78 | +void FTPFileCallbacks::onRetrieveEnd() { |
| 79 | + printf(">> FTPFileCallbacks::onRetrieveEnd\n"); |
| 80 | + m_retrieveFile.close(); |
| 81 | + printf("<< FTPFileCallbacks::onRetrieveEnd: bytesTransmitted=%d\n", m_byteCount); |
| 82 | +} // FTPFileCallbacks#onRetrieveEnd |
| 83 | + |
| 84 | + |
| 85 | +/** |
| 86 | + * Return a list of files in the file system. |
| 87 | + * @return a list of files in the file system. |
| 88 | + */ |
| 89 | +std::string FTPFileCallbacks::onDir() { |
| 90 | + |
| 91 | + DIR* dir = opendir(FTPServer::getCurrentDirectory().c_str()); |
| 92 | + std::stringstream ss; |
| 93 | + while(1) { |
| 94 | + struct dirent* pDirentry = readdir(dir); |
| 95 | + if (pDirentry == nullptr) { |
| 96 | + break; |
| 97 | + } |
| 98 | + ss << pDirentry->d_name << "\r\n"; |
| 99 | + } |
| 100 | + closedir(dir); |
| 101 | + return ss.str(); |
| 102 | +} // FTPFileCallbacks#onDir |
| 103 | + |
| 104 | + |
| 105 | +/// ---- END OF FTPFileCallbacks |
| 106 | + |
| 107 | + |
| 108 | +void FTPCallbacks::onStoreStart(std::string fileName) { |
| 109 | + printf(">> FTPCallbacks::onStoreStart: fileName=%s\n", fileName.c_str()); |
| 110 | + printf("<< FTPCallbacks::onStoreStart\n"); |
| 111 | +} // FTPCallbacks#onStoreStart |
| 112 | + |
| 113 | + |
| 114 | +size_t FTPCallbacks::onStoreData(uint8_t* data, size_t size) { |
| 115 | + printf(">> FTPCallbacks::onStoreData: size=%ld\n", size); |
| 116 | + printf("<< FTPCallbacks::onStoreData\n"); |
| 117 | + return 0; |
| 118 | +} // FTPCallbacks#onStoreData |
| 119 | + |
| 120 | + |
| 121 | +void FTPCallbacks::onStoreEnd() { |
| 122 | + printf(">> FTPCallbacks::onStoreEnd\n"); |
| 123 | + printf("<< FTPCallbacks::onStoreEnd\n"); |
| 124 | +} // FTPCallbacks#onStoreEnd |
| 125 | + |
| 126 | + |
| 127 | +void FTPCallbacks::onRetrieveStart(std::string fileName) { |
| 128 | + printf(">> FTPCallbacks::onRetrieveStart\n"); |
| 129 | + printf("<< FTPCallbacks::onRetrieveStart\n"); |
| 130 | +} // FTPCallbacks#onRetrieveStart |
| 131 | + |
| 132 | + |
| 133 | +size_t FTPCallbacks::onRetrieveData(uint8_t *data, size_t size) { |
| 134 | + printf(">> FTPCallbacks::onRetrieveData\n"); |
| 135 | + printf("<< FTPCallbacks::onRetrieveData: 0\n"); |
| 136 | + return 0; |
| 137 | +} // FTPCallbacks#onRetrieveData |
| 138 | + |
| 139 | + |
| 140 | +void FTPCallbacks::onRetrieveEnd() { |
| 141 | +printf(">> FTPCallbacks::onRetrieveEnd\n"); |
| 142 | +printf("<< FTPCallbacks::onRetrieveEnd\n"); |
| 143 | +} // FTPCallbacks#onRetrieveEnd |
| 144 | + |
| 145 | + |
| 146 | +std::string FTPCallbacks::onDir() { |
| 147 | + printf(">> FTPCallbacks::onDir\n"); |
| 148 | + printf("<< FTPCallbacks::onDir\n"); |
| 149 | + return ""; |
| 150 | +} // FTPCallbacks#onDir |
| 151 | + |
| 152 | +FTPCallbacks::~FTPCallbacks() { |
| 153 | + |
| 154 | +} // FTPCallbacks#~FTPCallbacks |
0 commit comments