2018-10-06 20:39:24 -07:00
|
|
|
#pragma once
|
2015-06-26 12:30:03 -07:00
|
|
|
|
2019-08-30 00:52:22 -07:00
|
|
|
#include <cstdint>
|
2017-06-07 11:05:21 -07:00
|
|
|
#include <functional>
|
2019-08-30 00:52:22 -07:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "nod/IDiscIO.hpp"
|
|
|
|
#include "nod/Util.hpp"
|
|
|
|
|
|
|
|
#include <logvisor/logvisor.hpp>
|
2015-06-26 12:30:03 -07:00
|
|
|
|
2018-12-07 21:21:47 -08:00
|
|
|
namespace nod {
|
2015-06-26 12:30:03 -07:00
|
|
|
|
2018-12-07 21:21:47 -08:00
|
|
|
class IFileIO {
|
2015-06-26 12:30:03 -07:00
|
|
|
public:
|
2018-12-07 21:21:47 -08:00
|
|
|
virtual ~IFileIO() = default;
|
|
|
|
virtual bool exists() = 0;
|
|
|
|
virtual uint64_t size() = 0;
|
2015-06-26 12:30:03 -07:00
|
|
|
|
2018-12-07 21:21:47 -08:00
|
|
|
struct IWriteStream : nod::IWriteStream {
|
|
|
|
uint64_t copyFromDisc(IPartReadStream& discio, uint64_t length) {
|
|
|
|
uint64_t read = 0;
|
|
|
|
uint8_t buf[0x7c00];
|
|
|
|
while (length) {
|
|
|
|
uint64_t thisSz = nod::min(uint64_t(0x7c00), length);
|
|
|
|
uint64_t readSz = discio.read(buf, thisSz);
|
|
|
|
if (thisSz != readSz) {
|
2020-04-11 15:45:06 -07:00
|
|
|
LogModule.report(logvisor::Error, FMT_STRING("unable to read enough from disc"));
|
2018-12-07 21:21:47 -08:00
|
|
|
return read;
|
2017-05-21 20:12:48 -07:00
|
|
|
}
|
2018-12-07 21:21:47 -08:00
|
|
|
if (write(buf, readSz) != readSz) {
|
2020-04-11 15:45:06 -07:00
|
|
|
LogModule.report(logvisor::Error, FMT_STRING("unable to write in file"));
|
2018-12-07 21:21:47 -08:00
|
|
|
return read;
|
2017-05-21 20:12:48 -07:00
|
|
|
}
|
2018-12-07 21:21:47 -08:00
|
|
|
length -= thisSz;
|
|
|
|
read += thisSz;
|
|
|
|
}
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
uint64_t copyFromDisc(IPartReadStream& discio, uint64_t length, const std::function<void(float)>& prog) {
|
|
|
|
uint64_t read = 0;
|
|
|
|
uint8_t buf[0x7c00];
|
|
|
|
uint64_t total = length;
|
|
|
|
while (length) {
|
|
|
|
uint64_t thisSz = nod::min(uint64_t(0x7c00), length);
|
|
|
|
uint64_t readSz = discio.read(buf, thisSz);
|
|
|
|
if (thisSz != readSz) {
|
2020-04-11 15:45:06 -07:00
|
|
|
LogModule.report(logvisor::Error, FMT_STRING("unable to read enough from disc"));
|
2018-12-07 21:21:47 -08:00
|
|
|
return read;
|
|
|
|
}
|
|
|
|
if (write(buf, readSz) != readSz) {
|
2020-04-11 15:45:06 -07:00
|
|
|
LogModule.report(logvisor::Error, FMT_STRING("unable to write in file"));
|
2018-12-07 21:21:47 -08:00
|
|
|
return read;
|
|
|
|
}
|
|
|
|
length -= thisSz;
|
|
|
|
read += thisSz;
|
|
|
|
prog(read / float(total));
|
|
|
|
}
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
virtual std::unique_ptr<IWriteStream> beginWriteStream() const = 0;
|
|
|
|
virtual std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const = 0;
|
2015-06-26 12:30:03 -07:00
|
|
|
|
2018-12-07 21:21:47 -08:00
|
|
|
struct IReadStream : nod::IReadStream {
|
|
|
|
virtual uint64_t copyToDisc(struct IPartWriteStream& discio, uint64_t length) = 0;
|
|
|
|
};
|
|
|
|
virtual std::unique_ptr<IReadStream> beginReadStream() const = 0;
|
|
|
|
virtual std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const = 0;
|
2015-06-26 12:30:03 -07:00
|
|
|
};
|
|
|
|
|
2021-06-22 12:16:56 -07:00
|
|
|
std::unique_ptr<IFileIO> NewFileIO(std::string_view path, int64_t maxWriteSize = -1);
|
2015-06-26 12:30:03 -07:00
|
|
|
|
2018-12-07 21:21:47 -08:00
|
|
|
} // namespace nod
|