2016-01-17 22:20:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
2016-08-26 06:11:24 +00:00
|
|
|
* Copyright (C) 2016 Furrtek
|
2016-01-17 22:20:02 +00:00
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* 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 2, 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; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __FILE_H__
|
|
|
|
#define __FILE_H__
|
|
|
|
|
|
|
|
#include "ff.h"
|
|
|
|
|
2016-05-16 21:01:44 +00:00
|
|
|
#include "optional.hpp"
|
|
|
|
|
2016-01-17 22:20:02 +00:00
|
|
|
#include <cstddef>
|
2016-05-11 17:58:57 +00:00
|
|
|
#include <cstdint>
|
2016-01-17 22:20:02 +00:00
|
|
|
#include <string>
|
2016-02-19 05:34:03 +00:00
|
|
|
#include <array>
|
2016-04-20 16:56:35 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <iterator>
|
2016-08-26 06:11:24 +00:00
|
|
|
#include <vector>
|
2016-01-17 22:20:02 +00:00
|
|
|
|
2016-04-20 16:56:35 +00:00
|
|
|
namespace std {
|
|
|
|
namespace filesystem {
|
|
|
|
|
2016-05-13 01:19:28 +00:00
|
|
|
struct filesystem_error {
|
2016-11-27 00:50:44 +00:00
|
|
|
constexpr filesystem_error() = default;
|
2016-05-16 21:01:44 +00:00
|
|
|
|
|
|
|
constexpr filesystem_error(
|
|
|
|
FRESULT fatfs_error
|
|
|
|
) : err { fatfs_error }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr filesystem_error(
|
|
|
|
unsigned int other_error
|
|
|
|
) : err { other_error }
|
|
|
|
{
|
|
|
|
}
|
2016-05-13 01:19:28 +00:00
|
|
|
|
2016-06-21 17:54:13 +00:00
|
|
|
uint32_t code() const {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-05-13 01:19:28 +00:00
|
|
|
std::string what() const;
|
2016-05-16 21:01:44 +00:00
|
|
|
|
|
|
|
private:
|
2016-11-27 00:50:44 +00:00
|
|
|
uint32_t err { FR_OK };
|
2016-05-13 01:19:28 +00:00
|
|
|
};
|
|
|
|
|
2016-09-08 19:57:34 +00:00
|
|
|
struct path {
|
|
|
|
using string_type = std::u16string;
|
|
|
|
using value_type = string_type::value_type;
|
|
|
|
|
|
|
|
static constexpr value_type preferred_separator = u'/';
|
|
|
|
|
|
|
|
path(
|
|
|
|
) : _s { }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
path(
|
|
|
|
const path& p
|
|
|
|
) : _s { p._s }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
path(
|
|
|
|
path&& p
|
|
|
|
) : _s { std::move(p._s) }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Source>
|
|
|
|
path(
|
|
|
|
const Source& source
|
|
|
|
) : path { std::begin(source), std::end(source) }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class InputIt>
|
|
|
|
path(
|
|
|
|
InputIt first,
|
|
|
|
InputIt last
|
|
|
|
) : _s { first, last }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
path(
|
|
|
|
const char16_t* const s
|
|
|
|
) : _s { s }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
path(
|
|
|
|
const TCHAR* const s
|
|
|
|
) : _s { reinterpret_cast<const std::filesystem::path::value_type*>(s) }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
path& operator=(const path& p) {
|
|
|
|
_s = p._s;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
path& operator=(path&& p) {
|
|
|
|
_s = std::move(p._s);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-05-01 05:42:28 +00:00
|
|
|
path parent_path() const;
|
2016-09-08 19:57:34 +00:00
|
|
|
path extension() const;
|
|
|
|
path filename() const;
|
|
|
|
path stem() const;
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return _s.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
const value_type* c_str() const {
|
|
|
|
return native().c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
const string_type& native() const {
|
|
|
|
return _s;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string string() const;
|
|
|
|
|
|
|
|
path& operator+=(const path& p) {
|
|
|
|
_s += p._s;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
path& operator+=(const string_type& str) {
|
|
|
|
_s += str;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-05-01 05:42:28 +00:00
|
|
|
path& operator/=(const path& p) {
|
|
|
|
if (_s.back() != preferred_separator)
|
|
|
|
_s + preferred_separator;
|
|
|
|
_s += p._s;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-09-08 19:57:34 +00:00
|
|
|
path& replace_extension(const path& replacement = path());
|
|
|
|
|
|
|
|
private:
|
|
|
|
string_type _s;
|
2016-05-13 01:19:28 +00:00
|
|
|
};
|
|
|
|
|
2023-05-01 05:42:28 +00:00
|
|
|
bool operator==(const path& lhs, const path& rhs);
|
2016-10-01 17:44:11 +00:00
|
|
|
bool operator<(const path& lhs, const path& rhs);
|
2016-09-08 19:57:34 +00:00
|
|
|
bool operator>(const path& lhs, const path& rhs);
|
2023-05-01 16:25:32 +00:00
|
|
|
path operator+(const path& lhs, const path& rhs);
|
2023-05-01 05:42:28 +00:00
|
|
|
path operator/(const path& lhs, const path& rhs);
|
2016-09-08 19:57:34 +00:00
|
|
|
|
2016-04-20 16:56:35 +00:00
|
|
|
using file_status = BYTE;
|
|
|
|
|
2016-08-22 01:06:39 +00:00
|
|
|
static_assert(sizeof(path::value_type) == 2, "sizeof(std::filesystem::path::value_type) != 2");
|
|
|
|
static_assert(sizeof(path::value_type) == sizeof(TCHAR), "FatFs TCHAR size != std::filesystem::path::value_type");
|
|
|
|
|
2016-05-11 17:58:57 +00:00
|
|
|
struct space_info {
|
|
|
|
static_assert(sizeof(std::uintmax_t) >= 8, "std::uintmax_t too small (<uint64_t)");
|
|
|
|
|
|
|
|
std::uintmax_t capacity;
|
|
|
|
std::uintmax_t free;
|
|
|
|
std::uintmax_t available;
|
|
|
|
};
|
|
|
|
|
2016-04-20 16:56:35 +00:00
|
|
|
struct directory_entry : public FILINFO {
|
|
|
|
file_status status() const {
|
|
|
|
return fattrib;
|
|
|
|
}
|
|
|
|
|
2016-10-01 17:44:11 +00:00
|
|
|
std::uintmax_t size() const {
|
|
|
|
return fsize;
|
|
|
|
};
|
|
|
|
|
2016-09-08 19:57:34 +00:00
|
|
|
const std::filesystem::path path() const noexcept { return { fname }; };
|
2016-04-20 16:56:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class directory_iterator {
|
|
|
|
struct Impl {
|
|
|
|
DIR dir;
|
|
|
|
directory_entry filinfo;
|
|
|
|
|
|
|
|
~Impl() {
|
|
|
|
f_closedir(&dir);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-27 00:50:44 +00:00
|
|
|
std::shared_ptr<Impl> impl { };
|
|
|
|
const path pattern { };
|
2016-04-20 16:56:35 +00:00
|
|
|
|
|
|
|
friend bool operator!=(const directory_iterator& lhs, const directory_iterator& rhs);
|
|
|
|
|
|
|
|
public:
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using value_type = directory_entry;
|
|
|
|
using pointer = const directory_entry*;
|
|
|
|
using reference = const directory_entry&;
|
|
|
|
using iterator_category = std::input_iterator_tag;
|
|
|
|
|
|
|
|
directory_iterator() noexcept { };
|
2016-09-08 03:46:45 +00:00
|
|
|
directory_iterator(std::filesystem::path path, std::filesystem::path wild);
|
|
|
|
|
2016-04-20 16:56:35 +00:00
|
|
|
~directory_iterator() { }
|
|
|
|
|
|
|
|
directory_iterator& operator++();
|
|
|
|
|
|
|
|
reference operator*() const {
|
|
|
|
// TODO: Exception or assert if impl == nullptr.
|
|
|
|
return impl->filinfo;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline const directory_iterator& begin(const directory_iterator& iter) noexcept { return iter; };
|
|
|
|
inline directory_iterator end(const directory_iterator&) noexcept { return { }; };
|
|
|
|
|
|
|
|
inline bool operator!=(const directory_iterator& lhs, const directory_iterator& rhs) { return lhs.impl != rhs.impl; };
|
|
|
|
|
2016-10-01 17:44:11 +00:00
|
|
|
bool is_directory(const file_status s);
|
2016-04-20 16:56:35 +00:00
|
|
|
bool is_regular_file(const file_status s);
|
|
|
|
|
2016-05-11 17:58:57 +00:00
|
|
|
space_info space(const path& p);
|
|
|
|
|
2016-04-20 16:56:35 +00:00
|
|
|
} /* namespace filesystem */
|
|
|
|
} /* namespace std */
|
|
|
|
|
2018-01-09 21:12:19 +00:00
|
|
|
struct FATTimestamp {
|
|
|
|
uint16_t FAT_date;
|
|
|
|
uint16_t FAT_time;
|
|
|
|
};
|
|
|
|
|
2023-05-01 16:25:32 +00:00
|
|
|
bool file_exists(const std::filesystem::path& file_path);
|
2022-02-01 18:37:38 +00:00
|
|
|
uint32_t delete_file(const std::filesystem::path& file_path);
|
|
|
|
uint32_t rename_file(const std::filesystem::path& file_path, const std::filesystem::path& new_name);
|
2018-01-09 21:12:19 +00:00
|
|
|
FATTimestamp file_created_date(const std::filesystem::path& file_path);
|
2017-09-19 19:14:56 +00:00
|
|
|
uint32_t make_new_directory(const std::filesystem::path& dir_path);
|
2016-08-22 01:06:39 +00:00
|
|
|
|
2017-12-07 00:58:25 +00:00
|
|
|
std::vector<std::filesystem::path> scan_root_files(const std::filesystem::path& directory, const std::filesystem::path& extension);
|
|
|
|
std::vector<std::filesystem::path> scan_root_directories(const std::filesystem::path& directory);
|
2023-05-01 16:25:32 +00:00
|
|
|
|
|
|
|
/* Gets an auto incrementing filename. */
|
2017-12-07 00:58:25 +00:00
|
|
|
std::filesystem::path next_filename_stem_matching_pattern(std::filesystem::path filename_stem_pattern);
|
|
|
|
|
2016-12-06 17:34:45 +00:00
|
|
|
/* Values added to FatFs FRESULT enum, values outside the FRESULT data type */
|
|
|
|
static_assert(sizeof(FIL::err) == 1, "FatFs FIL::err size not expected.");
|
|
|
|
|
|
|
|
/* Dangerous to expose these, as FatFs native error values are byte-sized. However,
|
2017-06-23 07:40:22 +00:00
|
|
|
* my filesystem_error implementation is fine with it. */
|
2016-12-06 17:34:45 +00:00
|
|
|
#define FR_DISK_FULL (0x100)
|
|
|
|
#define FR_EOF (0x101)
|
|
|
|
#define FR_BAD_SEEK (0x102)
|
|
|
|
#define FR_UNEXPECTED (0x103)
|
|
|
|
|
2016-05-13 04:44:37 +00:00
|
|
|
class File {
|
|
|
|
public:
|
2016-08-22 05:15:19 +00:00
|
|
|
using Size = uint64_t;
|
|
|
|
using Offset = uint64_t;
|
2018-01-09 21:12:19 +00:00
|
|
|
using Timestamp = uint32_t;
|
2016-05-16 21:01:44 +00:00
|
|
|
using Error = std::filesystem::filesystem_error;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct Result {
|
|
|
|
enum class Type {
|
|
|
|
Success,
|
|
|
|
Error,
|
|
|
|
} type;
|
|
|
|
union {
|
|
|
|
T value_;
|
|
|
|
Error error_;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool is_ok() const {
|
|
|
|
return type == Type::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_error() const {
|
|
|
|
return type == Type::Error;
|
|
|
|
}
|
|
|
|
|
2016-07-25 02:12:09 +00:00
|
|
|
const T& value() const {
|
2016-05-16 21:01:44 +00:00
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error error() const {
|
|
|
|
return error_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result() = delete;
|
|
|
|
|
|
|
|
constexpr Result(
|
|
|
|
T value
|
|
|
|
) : type { Type::Success },
|
|
|
|
value_ { value }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Result(
|
|
|
|
Error error
|
|
|
|
) : type { Type::Error },
|
|
|
|
error_ { error }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Result() {
|
|
|
|
if( type == Type::Success ) {
|
|
|
|
value_.~T();
|
|
|
|
}
|
|
|
|
}
|
2016-05-13 04:44:37 +00:00
|
|
|
};
|
|
|
|
|
2016-05-16 21:01:44 +00:00
|
|
|
File() { };
|
2016-05-13 04:44:37 +00:00
|
|
|
~File();
|
|
|
|
|
2016-05-16 21:01:44 +00:00
|
|
|
/* Prevent copies */
|
|
|
|
File(const File&) = delete;
|
|
|
|
File& operator=(const File&) = delete;
|
2016-05-13 04:55:39 +00:00
|
|
|
|
2016-05-16 21:01:44 +00:00
|
|
|
// TODO: Return Result<>.
|
2016-08-22 01:06:39 +00:00
|
|
|
Optional<Error> open(const std::filesystem::path& filename);
|
|
|
|
Optional<Error> append(const std::filesystem::path& filename);
|
|
|
|
Optional<Error> create(const std::filesystem::path& filename);
|
2016-05-13 04:55:39 +00:00
|
|
|
|
2016-08-22 05:15:19 +00:00
|
|
|
Result<Size> read(void* const data, const Size bytes_to_read);
|
|
|
|
Result<Size> write(const void* const data, const Size bytes_to_write);
|
2018-01-09 21:12:19 +00:00
|
|
|
|
2016-08-22 05:15:19 +00:00
|
|
|
Result<Offset> seek(const uint64_t Offset);
|
2018-01-09 21:12:19 +00:00
|
|
|
Timestamp created_date();
|
2017-03-23 21:28:21 +00:00
|
|
|
Size size();
|
2016-05-13 04:44:37 +00:00
|
|
|
|
|
|
|
template<size_t N>
|
2016-08-22 05:15:19 +00:00
|
|
|
Result<Size> write(const std::array<uint8_t, N>& data) {
|
2016-05-13 04:44:37 +00:00
|
|
|
return write(data.data(), N);
|
|
|
|
}
|
|
|
|
|
2016-07-25 03:39:21 +00:00
|
|
|
Optional<Error> write_line(const std::string& s);
|
2016-05-13 04:44:37 +00:00
|
|
|
|
2016-05-16 21:01:44 +00:00
|
|
|
// TODO: Return Result<>.
|
|
|
|
Optional<Error> sync();
|
2016-05-13 04:44:37 +00:00
|
|
|
|
|
|
|
private:
|
2016-11-27 00:50:44 +00:00
|
|
|
FIL f { };
|
2016-05-13 04:44:37 +00:00
|
|
|
|
2016-08-22 01:06:39 +00:00
|
|
|
Optional<Error> open_fatfs(const std::filesystem::path& filename, BYTE mode);
|
2016-05-16 21:01:44 +00:00
|
|
|
};
|
2016-05-13 04:44:37 +00:00
|
|
|
|
2016-01-17 22:20:02 +00:00
|
|
|
#endif/*__FILE_H__*/
|