Loading...
Searching...
No Matches
Ftp.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2026 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
31
33
34#include <SFML/System/Time.hpp>
35
36#include <filesystem>
37#include <string>
38#include <vector>
39
40
41namespace sf
42{
43class IpAddress;
44
51class SFML_NETWORK_API [[deprecated("Use sf::Sftp if possible")]] Ftp
52{
53public:
58 enum class TransferMode
59 {
60 Binary,
61 Ascii,
62 Ebcdic
63 };
64
70 {
71 public:
76 enum class Status
77 {
78 // 1xx: the requested action is being initiated,
79 // expect another reply before proceeding with a new command
80 RestartMarkerReply = 110,
81 ServiceReadySoon = 120,
82 DataConnectionAlreadyOpened = 125,
83 OpeningDataConnection = 150,
84
85 // 2xx: the requested action has been successfully completed
86 Ok = 200,
87 PointlessCommand = 202,
88 SystemStatus = 211,
89 DirectoryStatus = 212,
90 FileStatus = 213,
91 HelpMessage = 214,
92 SystemType = 215,
93 ServiceReady = 220,
94 ClosingConnection = 221,
95 DataConnectionOpened = 225,
96 ClosingDataConnection = 226,
97 EnteringPassiveMode = 227,
98 LoggedIn = 230,
99 FileActionOk = 250,
100 DirectoryOk = 257,
101
102 // 3xx: the command has been accepted, but the requested action
103 // is dormant, pending receipt of further information
104 NeedPassword = 331,
105 NeedAccountToLogIn = 332,
106 NeedInformation = 350,
107
108 // 4xx: the command was not accepted and the requested action did not take place,
109 // but the error condition is temporary and the action may be requested again
110 ServiceUnavailable = 421,
111 DataConnectionUnavailable = 425,
112 TransferAborted = 426,
113 FileActionAborted = 450,
114 LocalError = 451,
115 InsufficientStorageSpace = 452,
116
117 // 5xx: the command was not accepted and
118 // the requested action did not take place
119 CommandUnknown = 500,
120 ParametersUnknown = 501,
121 CommandNotImplemented = 502,
122 BadCommandSequence = 503,
123 ParameterNotImplemented = 504,
124 NotLoggedIn = 530,
125 NeedAccountToStore = 532,
126 FileUnavailable = 550,
127 PageTypeUnknown = 551,
128 NotEnoughMemory = 552,
129 FilenameNotAllowed = 553,
130
131 // 10xx: SFML custom codes
132 InvalidResponse = 1000,
133 ConnectionFailed = 1001,
134 ConnectionClosed = 1002,
135 InvalidFile = 1003
136 };
137
148 explicit Response(Status code = Status::InvalidResponse, std::string message = "");
149
159 [[nodiscard]] bool isOk() const;
160
167 [[nodiscard]] Status getStatus() const;
168
175 [[nodiscard]] const std::string& getMessage() const;
176
177 private:
179 // Member data
181 Status m_status;
182 std::string m_message;
183 };
184
190 {
191 public:
198 DirectoryResponse(const Response& response);
199
206 [[nodiscard]] const std::filesystem::path& getDirectory() const;
207
208 private:
210 // Member data
212 std::filesystem::path m_directory;
213 };
214
215
221 {
222 public:
230 ListingResponse(const Response& response, const std::string& data);
231
238 [[nodiscard]] const std::vector<std::string>& getListing() const;
239
240 private:
242 // Member data
244 std::vector<std::string> m_listing;
245 };
246
251 Ftp() = default;
252
261
266 Ftp(const Ftp&) = delete;
267
272 Ftp& operator=(const Ftp&) = delete;
273
295 [[nodiscard]] Response connect(IpAddress server, unsigned short port = 21, Time timeout = Time::Zero);
296
305 [[nodiscard]] Response disconnect();
306
316 [[nodiscard]] Response login();
317
330 [[nodiscard]] Response login(const std::string& name, const std::string& password);
331
341 [[nodiscard]] Response keepAlive();
342
355
371 [[nodiscard]] ListingResponse getDirectoryListing(const std::string& directory = "");
372
385 [[nodiscard]] Response changeDirectory(const std::string& directory);
386
395 [[nodiscard]] Response parentDirectory();
396
410 [[nodiscard]] Response createDirectory(const std::string& name);
411
427 [[nodiscard]] Response deleteDirectory(const std::string& name);
428
443 [[nodiscard]] Response renameFile(const std::filesystem::path& file, const std::filesystem::path& newName);
444
460 [[nodiscard]] Response deleteFile(const std::filesystem::path& name);
461
482 [[nodiscard]] Response download(const std::filesystem::path& remoteFile,
483 const std::filesystem::path& localPath,
485
507 [[nodiscard]] Response upload(const std::filesystem::path& localFile,
508 const std::filesystem::path& remotePath,
510 bool append = false);
511
528 [[nodiscard]] Response sendCommand(const std::string& command, const std::string& parameter = "");
529
530private:
540 Response getResponse();
541
547 class DataChannel;
548
549 friend class DataChannel;
550
552 // Member data
554 TcpSocket m_commandSocket;
555 std::string m_receiveBuffer;
556};
557
558} // namespace sf
559
560
#define SFML_NETWORK_API
Specialization of FTP response returning a directory.
Definition Ftp.hpp:190
DirectoryResponse(const Response &response)
Default constructor.
const std::filesystem::path & getDirectory() const
Get the directory returned in the response.
Specialization of FTP response returning a file name listing.
Definition Ftp.hpp:221
const std::vector< std::string > & getListing() const
Return the array of directory/file names.
ListingResponse(const Response &response, const std::string &data)
Default constructor.
FTP response.
Definition Ftp.hpp:70
bool isOk() const
Check if the status code means a success.
Status getStatus() const
Get the status code of the response.
Response(Status code=Status::InvalidResponse, std::string message="")
Default constructor.
const std::string & getMessage() const
Get the full message contained in the response.
Status
Status codes possibly returned by a FTP response.
Definition Ftp.hpp:77
@ InvalidResponse
Not part of the FTP standard, generated by SFML when a received response cannot be parsed.
Definition Ftp.hpp:132
TransferMode
Enumeration of transfer modes.
Definition Ftp.hpp:59
@ Binary
Binary mode (file is transferred as a sequence of bytes).
Definition Ftp.hpp:60
Response deleteFile(const std::filesystem::path &name)
Remove an existing file.
Response createDirectory(const std::string &name)
Create a new directory.
Response deleteDirectory(const std::string &name)
Remove an existing directory.
~Ftp()
Destructor.
Response sendCommand(const std::string &command, const std::string &parameter="")
Send a command to the FTP server.
Response connect(IpAddress server, unsigned short port=21, Time timeout=Time::Zero)
Connect to the specified FTP server.
Response login()
Log in using an anonymous account.
DirectoryResponse getWorkingDirectory()
Get the current working directory.
Response changeDirectory(const std::string &directory)
Change the current working directory.
friend class DataChannel
Definition Ftp.hpp:549
ListingResponse getDirectoryListing(const std::string &directory="")
Get the contents of the given directory.
Response download(const std::filesystem::path &remoteFile, const std::filesystem::path &localPath, TransferMode mode=TransferMode::Binary)
Download a file from the server.
Response renameFile(const std::filesystem::path &file, const std::filesystem::path &newName)
Rename an existing file.
Response login(const std::string &name, const std::string &password)
Log in using a username and a password.
Response keepAlive()
Send a null command to keep the connection alive.
Ftp(const Ftp &)=delete
Deleted copy constructor.
Ftp()=default
Default constructor.
Response disconnect()
Close the connection with the server.
Response parentDirectory()
Go to the parent directory of the current one.
Response upload(const std::filesystem::path &localFile, const std::filesystem::path &remotePath, TransferMode mode=TransferMode::Binary, bool append=false)
Upload a file to the server.
Ftp & operator=(const Ftp &)=delete
Deleted copy assignment.
Encapsulate an IPv4 network address.
Definition IpAddress.hpp:51
Specialized socket using the TCP protocol.
Definition TcpSocket.hpp:58
Represents a time value.
Definition Time.hpp:42
static const Time Zero
Predefined "zero" time value.
Definition Time.hpp:110