2016-01-17 17:20:02 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
2016-08-26 02:11:24 -04:00
|
|
|
* Copyright (C) 2016 Furrtek
|
2016-01-17 17:20:02 -05: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "file.hpp"
|
|
|
|
|
2016-04-20 12:57:17 -04:00
|
|
|
#include <algorithm>
|
2016-09-08 01:20:51 -04:00
|
|
|
#include <locale>
|
|
|
|
#include <codecvt>
|
2016-04-20 12:57:17 -04:00
|
|
|
|
2016-08-21 21:06:39 -04:00
|
|
|
Optional<File::Error> File::open_fatfs(const std::filesystem::path& filename, BYTE mode) {
|
|
|
|
auto result = f_open(&f, reinterpret_cast<const TCHAR*>(filename.c_str()), mode);
|
2016-05-16 17:01:44 -04:00
|
|
|
if( result == FR_OK ) {
|
|
|
|
if( mode & FA_OPEN_ALWAYS ) {
|
|
|
|
const auto result = f_lseek(&f, f_size(&f));
|
|
|
|
if( result != FR_OK ) {
|
2016-04-30 18:09:34 -04:00
|
|
|
f_close(&f);
|
|
|
|
}
|
2016-01-17 17:20:02 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:01:44 -04:00
|
|
|
|
|
|
|
if( result == FR_OK ) {
|
|
|
|
return { };
|
|
|
|
} else {
|
|
|
|
return { result };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 21:06:39 -04:00
|
|
|
Optional<File::Error> File::open(const std::filesystem::path& filename) {
|
2016-05-16 17:01:44 -04:00
|
|
|
return open_fatfs(filename, FA_READ);
|
|
|
|
}
|
|
|
|
|
2016-08-21 21:06:39 -04:00
|
|
|
Optional<File::Error> File::append(const std::filesystem::path& filename) {
|
2016-05-16 17:01:44 -04:00
|
|
|
return open_fatfs(filename, FA_WRITE | FA_OPEN_ALWAYS);
|
|
|
|
}
|
|
|
|
|
2016-08-21 21:06:39 -04:00
|
|
|
Optional<File::Error> File::create(const std::filesystem::path& filename) {
|
2016-05-16 17:01:44 -04:00
|
|
|
return open_fatfs(filename, FA_WRITE | FA_CREATE_ALWAYS);
|
2016-01-17 17:20:02 -05:00
|
|
|
}
|
|
|
|
|
2016-04-30 18:09:34 -04:00
|
|
|
File::~File() {
|
2016-01-17 17:20:02 -05:00
|
|
|
f_close(&f);
|
|
|
|
}
|
|
|
|
|
2016-08-22 01:15:19 -04:00
|
|
|
File::Result<File::Size> File::read(void* const data, const Size bytes_to_read) {
|
2016-01-17 17:20:02 -05:00
|
|
|
UINT bytes_read = 0;
|
2016-05-16 17:01:44 -04:00
|
|
|
const auto result = f_read(&f, data, bytes_to_read, &bytes_read);
|
|
|
|
if( result == FR_OK ) {
|
|
|
|
return { static_cast<size_t>(bytes_read) };
|
|
|
|
} else {
|
|
|
|
return { static_cast<Error>(result) };
|
2016-05-13 00:55:01 -04:00
|
|
|
}
|
2016-01-17 17:20:02 -05:00
|
|
|
}
|
|
|
|
|
2016-08-22 01:15:19 -04:00
|
|
|
File::Result<File::Size> File::write(const void* const data, const Size bytes_to_write) {
|
2016-01-17 17:20:02 -05:00
|
|
|
UINT bytes_written = 0;
|
2016-05-16 17:01:44 -04:00
|
|
|
const auto result = f_write(&f, data, bytes_to_write, &bytes_written);
|
|
|
|
if( result == FR_OK ) {
|
2016-07-24 22:12:37 -04:00
|
|
|
if( bytes_to_write == bytes_written ) {
|
2016-08-22 01:15:19 -04:00
|
|
|
return { static_cast<File::Size>(bytes_written) };
|
2016-07-24 22:12:37 -04:00
|
|
|
} else {
|
|
|
|
return Error { FR_DISK_FULL };
|
|
|
|
}
|
2016-05-16 17:01:44 -04:00
|
|
|
} else {
|
|
|
|
return { static_cast<Error>(result) };
|
2016-05-13 00:55:01 -04:00
|
|
|
}
|
2016-01-17 17:20:02 -05:00
|
|
|
}
|
|
|
|
|
2016-08-22 01:15:19 -04:00
|
|
|
File::Result<File::Offset> File::seek(const Offset new_position) {
|
2016-05-16 17:01:44 -04:00
|
|
|
/* NOTE: Returns *old* position, not new position */
|
2016-05-01 14:29:44 -04:00
|
|
|
const auto old_position = f_tell(&f);
|
2016-05-16 17:01:44 -04:00
|
|
|
const auto result = f_lseek(&f, new_position);
|
|
|
|
if( result != FR_OK ) {
|
|
|
|
return { static_cast<Error>(result) };
|
2016-05-01 14:29:44 -04:00
|
|
|
}
|
|
|
|
if( f_tell(&f) != new_position ) {
|
2016-05-16 17:01:44 -04:00
|
|
|
return { static_cast<Error>(FR_BAD_SEEK) };
|
2016-05-01 14:29:44 -04:00
|
|
|
}
|
2016-08-22 01:15:19 -04:00
|
|
|
return { static_cast<File::Offset>(old_position) };
|
2016-05-01 14:29:44 -04:00
|
|
|
}
|
|
|
|
|
2016-07-24 23:39:21 -04:00
|
|
|
Optional<File::Error> File::write_line(const std::string& s) {
|
|
|
|
const auto result_s = write(s.c_str(), s.size());
|
|
|
|
if( result_s.is_error() ) {
|
|
|
|
return { result_s.error() };
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto result_crlf = write("\r\n", 2);
|
|
|
|
if( result_crlf.is_error() ) {
|
|
|
|
return { result_crlf.error() };
|
2016-05-13 00:55:01 -04:00
|
|
|
}
|
2016-07-24 23:39:21 -04:00
|
|
|
|
|
|
|
return { };
|
2016-01-17 17:20:02 -05:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:01:44 -04:00
|
|
|
Optional<File::Error> File::sync() {
|
|
|
|
const auto result = f_sync(&f);
|
|
|
|
if( result == FR_OK ) {
|
|
|
|
return { };
|
|
|
|
} else {
|
|
|
|
return { result };
|
2016-05-13 00:54:13 -04:00
|
|
|
}
|
2016-01-17 17:20:02 -05:00
|
|
|
}
|
2016-04-20 12:56:35 -04:00
|
|
|
|
2016-08-21 21:06:39 -04:00
|
|
|
static std::filesystem::path find_last_file_matching_pattern(const std::filesystem::path& pattern) {
|
|
|
|
std::filesystem::path last_match;
|
2016-09-07 23:46:45 -04:00
|
|
|
for(const auto& entry : std::filesystem::directory_iterator(u"", pattern)) {
|
2016-04-20 12:57:17 -04:00
|
|
|
if( std::filesystem::is_regular_file(entry.status()) ) {
|
2016-10-01 13:47:21 -04:00
|
|
|
const auto& match = entry.path();
|
2016-04-20 12:57:17 -04:00
|
|
|
if( match > last_match ) {
|
|
|
|
last_match = match;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return last_match;
|
|
|
|
}
|
|
|
|
|
2016-09-08 15:57:34 -04:00
|
|
|
static std::filesystem::path increment_filename_stem_ordinal(std::filesystem::path path) {
|
|
|
|
auto t = path.replace_extension().native();
|
|
|
|
auto it = t.rbegin();
|
2016-04-20 12:57:17 -04:00
|
|
|
|
|
|
|
// Increment decimal number before the extension.
|
2016-09-08 15:57:34 -04:00
|
|
|
for(; it != t.rend(); ++it) {
|
2016-04-20 12:57:17 -04:00
|
|
|
const auto c = *it;
|
|
|
|
if( c < '0' ) {
|
|
|
|
return { };
|
|
|
|
} else if( c < '9' ) {
|
|
|
|
*it += 1;
|
|
|
|
break;
|
|
|
|
} else if( c == '9' ) {
|
|
|
|
*it = '0';
|
|
|
|
} else {
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 15:57:34 -04:00
|
|
|
return t;
|
2016-04-20 12:57:17 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 15:57:34 -04:00
|
|
|
std::filesystem::path next_filename_stem_matching_pattern(std::filesystem::path filename_pattern) {
|
|
|
|
const auto next_filename = find_last_file_matching_pattern(filename_pattern.replace_extension(u".*"));
|
|
|
|
if( next_filename.empty() ) {
|
|
|
|
auto pattern_s = filename_pattern.replace_extension().native();
|
|
|
|
std::replace(std::begin(pattern_s), std::end(pattern_s), '?', '0');
|
|
|
|
return pattern_s;
|
2016-04-20 12:57:17 -04:00
|
|
|
} else {
|
2016-09-08 15:57:34 -04:00
|
|
|
return increment_filename_stem_ordinal(next_filename);
|
2016-04-20 12:57:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 18:31:16 -05:00
|
|
|
std::vector<std::filesystem::path> scan_root_files(const std::filesystem::path& directory,
|
|
|
|
const std::filesystem::path& extension) {
|
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
std::vector<std::filesystem::path> file_list { };
|
2017-02-01 18:31:16 -05:00
|
|
|
|
|
|
|
for(const auto& entry : std::filesystem::directory_iterator(directory, extension)) {
|
|
|
|
if( std::filesystem::is_regular_file(entry.status()) ) {
|
|
|
|
file_list.push_back(entry.path());
|
2016-08-26 02:11:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_list;
|
|
|
|
}
|
|
|
|
|
2016-04-20 12:56:35 -04:00
|
|
|
namespace std {
|
|
|
|
namespace filesystem {
|
|
|
|
|
2016-05-12 21:19:28 -04:00
|
|
|
std::string filesystem_error::what() const {
|
|
|
|
switch(err) {
|
2016-05-16 17:01:44 -04:00
|
|
|
case FR_OK: return "ok";
|
2016-05-12 21:19:28 -04:00
|
|
|
case FR_DISK_ERR: return "disk error";
|
|
|
|
case FR_INT_ERR: return "insanity detected";
|
|
|
|
case FR_NOT_READY: return "not ready";
|
|
|
|
case FR_NO_FILE: return "no file";
|
|
|
|
case FR_NO_PATH: return "no path";
|
|
|
|
case FR_INVALID_NAME: return "invalid name";
|
|
|
|
case FR_DENIED: return "denied";
|
|
|
|
case FR_EXIST: return "exists";
|
|
|
|
case FR_INVALID_OBJECT: return "invalid object";
|
|
|
|
case FR_WRITE_PROTECTED: return "write protected";
|
|
|
|
case FR_INVALID_DRIVE: return "invalid drive";
|
|
|
|
case FR_NOT_ENABLED: return "not enabled";
|
|
|
|
case FR_NO_FILESYSTEM: return "no filesystem";
|
|
|
|
case FR_MKFS_ABORTED: return "mkfs aborted";
|
|
|
|
case FR_TIMEOUT: return "timeout";
|
|
|
|
case FR_LOCKED: return "locked";
|
|
|
|
case FR_NOT_ENOUGH_CORE: return "not enough core";
|
|
|
|
case FR_TOO_MANY_OPEN_FILES: return "too many open files";
|
|
|
|
case FR_INVALID_PARAMETER: return "invalid parameter";
|
2016-05-13 00:55:01 -04:00
|
|
|
case FR_EOF: return "end of file";
|
|
|
|
case FR_DISK_FULL: return "disk full";
|
|
|
|
case FR_BAD_SEEK: return "bad seek";
|
2016-05-16 17:01:44 -04:00
|
|
|
case FR_UNEXPECTED: return "unexpected";
|
2016-05-12 21:19:28 -04:00
|
|
|
default: return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 15:57:34 -04:00
|
|
|
path path::extension() const {
|
|
|
|
const auto t = filename().native();
|
|
|
|
const auto index = t.find_last_of(u'.');
|
|
|
|
if( index == t.npos ) {
|
|
|
|
return { };
|
|
|
|
} else {
|
|
|
|
return t.substr(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
path path::filename() const {
|
|
|
|
const auto index = _s.find_last_of(preferred_separator);
|
|
|
|
if( index == _s.npos ) {
|
|
|
|
return _s;
|
|
|
|
} else {
|
|
|
|
return _s.substr(index + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
path path::stem() const {
|
|
|
|
const auto t = filename().native();
|
|
|
|
const auto index = t.find_last_of(u'.');
|
|
|
|
if( index == t.npos ) {
|
|
|
|
return t;
|
|
|
|
} else {
|
|
|
|
return t.substr(0, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string path::string() const {
|
2016-09-08 01:20:51 -04:00
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<path::value_type>, path::value_type> conv;
|
2016-09-08 15:57:34 -04:00
|
|
|
return conv.to_bytes(native());
|
|
|
|
}
|
|
|
|
|
|
|
|
path& path::replace_extension(const path& replacement) {
|
|
|
|
const auto t = extension().native();
|
|
|
|
_s.erase(_s.size() - t.size());
|
|
|
|
if( !replacement._s.empty() ) {
|
|
|
|
if( replacement._s.front() != u'.' ) {
|
|
|
|
_s += u'.';
|
|
|
|
}
|
|
|
|
_s += replacement._s;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-10-01 13:44:11 -04:00
|
|
|
bool operator<(const path& lhs, const path& rhs) {
|
|
|
|
return lhs.native() < rhs.native();
|
|
|
|
}
|
|
|
|
|
2016-09-08 15:57:34 -04:00
|
|
|
bool operator>(const path& lhs, const path& rhs) {
|
|
|
|
return lhs.native() > rhs.native();
|
2016-09-08 01:20:51 -04:00
|
|
|
}
|
|
|
|
|
2016-04-20 12:56:35 -04:00
|
|
|
directory_iterator::directory_iterator(
|
2016-09-07 23:46:45 -04:00
|
|
|
std::filesystem::path path,
|
|
|
|
std::filesystem::path wild
|
|
|
|
) : pattern { wild }
|
|
|
|
{
|
2016-04-20 12:56:35 -04:00
|
|
|
impl = std::make_shared<Impl>();
|
2016-09-07 23:46:45 -04:00
|
|
|
const auto result = f_findfirst(&impl->dir, &impl->filinfo, reinterpret_cast<const TCHAR*>(path.c_str()), reinterpret_cast<const TCHAR*>(pattern.c_str()));
|
2016-04-20 12:56:35 -04:00
|
|
|
if( result != FR_OK ) {
|
|
|
|
impl.reset();
|
|
|
|
// TODO: Throw exception if/when I enable exceptions...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
directory_iterator& directory_iterator::operator++() {
|
|
|
|
const auto result = f_findnext(&impl->dir, &impl->filinfo);
|
|
|
|
if( (result != FR_OK) || (impl->filinfo.fname[0] == 0) ) {
|
|
|
|
impl.reset();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-10-01 13:44:11 -04:00
|
|
|
bool is_directory(const file_status s) {
|
|
|
|
return (s & AM_DIR);
|
|
|
|
}
|
|
|
|
|
2016-04-20 12:56:35 -04:00
|
|
|
bool is_regular_file(const file_status s) {
|
|
|
|
return !(s & AM_DIR);
|
|
|
|
}
|
|
|
|
|
2016-05-11 13:58:57 -04:00
|
|
|
space_info space(const path& p) {
|
|
|
|
DWORD free_clusters { 0 };
|
|
|
|
FATFS* fs;
|
2016-08-21 21:06:39 -04:00
|
|
|
if( f_getfree(reinterpret_cast<const TCHAR*>(p.c_str()), &free_clusters, &fs) == FR_OK ) {
|
2016-05-11 13:58:57 -04:00
|
|
|
#if _MAX_SS != _MIN_SS
|
|
|
|
static_assert(false, "FatFs not configured for fixed sector size");
|
|
|
|
#else
|
2016-05-13 21:10:23 -04:00
|
|
|
const std::uintmax_t cluster_bytes = fs->csize * _MIN_SS;
|
2016-05-11 13:58:57 -04:00
|
|
|
return {
|
2016-05-13 21:10:23 -04:00
|
|
|
(fs->n_fatent - 2) * cluster_bytes,
|
|
|
|
free_clusters * cluster_bytes,
|
|
|
|
free_clusters * cluster_bytes,
|
2016-05-11 13:58:57 -04:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
return { 0, 0, 0 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 12:56:35 -04:00
|
|
|
} /* namespace filesystem */
|
|
|
|
} /* namespace std */
|