ftpiiu_plugin/include/fs.h

166 lines
4.0 KiB
C
Raw Normal View History

2020-04-05 21:16:16 +02:00
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <dirent.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <memory>
2020-04-24 22:51:43 +02:00
#include <string_view>
2020-04-05 21:16:16 +02:00
namespace fs
{
2020-04-06 07:36:03 +02:00
/// \brief Print size in human-readable format (KiB, MiB, etc)
/// \param size_ Size to print
2020-04-05 21:16:16 +02:00
std::string printSize (std::uint64_t size_);
2020-04-06 07:36:03 +02:00
/// \brief File I/O object
2020-04-05 21:16:16 +02:00
class File
{
public:
~File ();
File ();
File (File const &that_) = delete;
2020-04-06 07:36:03 +02:00
/// \brief Move constructor
/// \param that_ Object to move from
2020-04-05 21:16:16 +02:00
File (File &&that_);
File &operator= (File const &that_) = delete;
2020-04-06 07:36:03 +02:00
/// \brief Move assignment
/// \param that_ Object to move from
2020-04-05 21:16:16 +02:00
File &operator= (File &&that_);
2020-04-06 07:36:03 +02:00
/// \brief bool cast operator
explicit operator bool () const;
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
/// \brief std::FILE* cast operator
operator std::FILE * () const;
/// \brief Set buffer size
/// \param size_ Buffer size
2020-04-05 21:16:16 +02:00
void setBufferSize (std::size_t size_);
2020-04-06 07:36:03 +02:00
/// \brief Open file
/// \param path_ Path to open
/// \param mode_ Access mode (\sa std::fopen)
2020-04-05 21:16:16 +02:00
bool open (char const *path_, char const *mode_ = "rb");
2020-04-06 07:36:03 +02:00
/// \brief Close file
2020-04-05 21:16:16 +02:00
void close ();
2020-04-06 07:36:03 +02:00
/// \brief Seek to file position
/// \param pos_ File position
/// \param origin_ Reference position (\sa std::fseek)
2020-04-05 21:16:16 +02:00
ssize_t seek (std::size_t pos_, int origin_);
2020-04-06 07:36:03 +02:00
/// \brief Read data
/// \param data_ Output buffer
/// \param size_ Size to read
/// \note Can return partial reads
2020-04-05 21:16:16 +02:00
ssize_t read (void *data_, std::size_t size_);
2020-04-06 07:36:03 +02:00
2020-04-24 22:51:43 +02:00
/// \brief Read line
std::string_view readLine ();
2020-04-06 07:36:03 +02:00
/// \brief Read data
/// \param data_ Output buffer
/// \param size_ Size to read
/// \note Fails on partial reads and errors
2020-04-05 21:16:16 +02:00
bool readAll (void *data_, std::size_t size_);
2020-04-06 07:36:03 +02:00
/// \brief Write data
/// \param data_ Input data
/// \param size_ Size to write
/// \note Can return partial writes
2020-04-05 21:16:16 +02:00
ssize_t write (void const *data_, std::size_t size_);
2020-04-06 07:36:03 +02:00
/// \brief Write data
/// \param data_ Input data
/// \param size_ Size to write
/// \note Fails on partials writes and errors
2020-04-05 21:16:16 +02:00
bool writeAll (void const *data_, std::size_t size_);
private:
2020-04-06 07:36:03 +02:00
/// \brief Underlying std::FILE*
2020-04-05 21:16:16 +02:00
std::unique_ptr<std::FILE, int (*) (std::FILE *)> m_fp{nullptr, nullptr};
2020-04-06 07:36:03 +02:00
/// \brief Buffer
2020-04-05 21:16:16 +02:00
std::unique_ptr<char[]> m_buffer;
2020-04-06 07:36:03 +02:00
/// \brief Buffer size
2020-04-05 21:16:16 +02:00
std::size_t m_bufferSize = 0;
2020-04-24 22:51:43 +02:00
/// \brief Line buffer
char *m_lineBuffer = nullptr;
/// \brief Line buffer size
std::size_t m_lineBufferSize = 0;
2020-04-05 21:16:16 +02:00
};
2020-04-06 07:36:03 +02:00
/// Directory object
2020-04-05 21:16:16 +02:00
class Dir
{
public:
~Dir ();
Dir ();
Dir (Dir const &that_) = delete;
2020-04-06 07:36:03 +02:00
/// \brief Move constructor
/// \param that_ Object to move from
2020-04-05 21:16:16 +02:00
Dir (Dir &&that_);
Dir &operator= (Dir const &that_) = delete;
2020-04-06 07:36:03 +02:00
/// \brief Move assignment
/// \param that_ Object to move from
2020-04-05 21:16:16 +02:00
Dir &operator= (Dir &&that_);
2020-04-06 07:36:03 +02:00
/// \brief bool cast operator
explicit operator bool () const;
/// \brief DIR* cast operator
2020-04-05 21:16:16 +02:00
operator DIR * () const;
2020-04-06 07:36:03 +02:00
/// \brief Open directory
/// \param path_ Path to open
2020-04-05 21:16:16 +02:00
bool open (char const *const path_);
2020-04-06 07:36:03 +02:00
/// \brief Close directory
2020-04-05 21:16:16 +02:00
void close ();
2020-04-06 07:36:03 +02:00
/// \brief Read a directory entry
/// \note Returns nullptr on end-of-directory or error; check errno
2020-04-05 21:16:16 +02:00
struct dirent *read ();
private:
2020-04-06 07:36:03 +02:00
/// \brief Underlying DIR*
2020-04-05 21:16:16 +02:00
std::unique_ptr<DIR, int (*) (DIR *)> m_dp{nullptr, nullptr};
};
}