Skip to content
Snippets Groups Projects
Commit 6f45fa69 authored by Nicolas Pope's avatar Nicolas Pope
Browse files

#38 Check file is valid before writing

parent 9e279cef
No related branches found
No related tags found
No related merge requests found
...@@ -101,10 +101,10 @@ struct StreamPacket { ...@@ -101,10 +101,10 @@ struct StreamPacket {
inline int frameNumber() const { return (version >= 4) ? frame_number : streamID; } inline int frameNumber() const { return (version >= 4) ? frame_number : streamID; }
inline size_t frameSetID() const { return (version >= 4) ? streamID : 0; } inline size_t frameSetID() const { return (version >= 4) ? streamID : 0; }
int64_t localTimestamp; // Not message packet / saved int64_t localTimestamp = 0; // Not message packet / saved
mutable unsigned int hint_capability; // Is this a video stream, for example mutable unsigned int hint_capability = 0; // Is this a video stream, for example
size_t hint_source_total; // Number of tracks per frame to expect size_t hint_source_total = 0; // Number of tracks per frame to expect
int retry_count = 0; // Decode retry count int retry_count = 0; // Decode retry count
unsigned int hint_peerid = 0; unsigned int hint_peerid = 0;
operator std::string() const; operator std::string() const;
......
...@@ -205,7 +205,14 @@ Socket ftl::net::internal::create_tcp_socket() { ...@@ -205,7 +205,14 @@ Socket ftl::net::internal::create_tcp_socket() {
std::string ftl::net::internal::get_host(const SocketAddress& addr) { std::string ftl::net::internal::get_host(const SocketAddress& addr) {
char hbuf[1024]; char hbuf[1024];
int err = getnameinfo(reinterpret_cast<const sockaddr*>(&(addr.addr)), addr.len, hbuf, sizeof(hbuf), NULL, 0, NI_NAMEREQD); int err = getnameinfo(
reinterpret_cast<const sockaddr*>(&(addr.addr)),
addr.len,
hbuf,
sizeof(hbuf),
NULL,
0,
NI_NAMEREQD);
if (err == 0) { return std::string(hbuf); } if (err == 0) { return std::string(hbuf); }
else if (err == EAI_NONAME) return ftl::net::internal::get_ip(addr); else if (err == EAI_NONAME) return ftl::net::internal::get_ip(addr);
else else
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <utility> #include <utility>
#include <limits> #include <limits>
#include <algorithm> #include <algorithm>
#include <filesystem>
#include <thread> #include <thread>
#include <chrono> #include <chrono>
#include "filestream.hpp" #include "filestream.hpp"
...@@ -478,6 +479,14 @@ bool File::run() { ...@@ -478,6 +479,14 @@ bool File::run() {
return true; return true;
} }
bool File::_validateFilename() const {
std::filesystem::path file = std::filesystem::u8path(uri_.toFilePath());
if (!std::filesystem::exists(file)) return true;
if (std::string(file.extension().u8string().c_str()) == ".ftl") return true;
// TODO(Nick): Could also check directory path
return false;
}
bool File::begin() { bool File::begin() {
if (active_) return true; if (active_) return true;
if (mode_ == Mode::Read) { if (mode_ == Mode::Read) {
...@@ -498,6 +507,7 @@ bool File::begin() { ...@@ -498,6 +507,7 @@ bool File::begin() {
run(); run();
} else if (mode_ == Mode::Write) { } else if (mode_ == Mode::Write) {
if (!ostream_) ostream_ = new std::ofstream; if (!ostream_) ostream_ = new std::ofstream;
if (!_validateFilename()) return false;
ostream_->open(uri_.toFilePath(), std::ifstream::out | std::ifstream::binary); ostream_->open(uri_.toFilePath(), std::ifstream::out | std::ifstream::binary);
if (!ostream_->good()) { if (!ostream_->good()) {
......
...@@ -122,6 +122,7 @@ class File : public Stream { ...@@ -122,6 +122,7 @@ class File : public Stream {
bool _open(); bool _open();
bool _checkFile(); bool _checkFile();
bool _validateFilename() const;
/* Apply version patches etc... */ /* Apply version patches etc... */
void _patchPackets(ftl::protocol::StreamPacket *spkt, ftl::protocol::DataPacket *pkt); void _patchPackets(ftl::protocol::StreamPacket *spkt, ftl::protocol::DataPacket *pkt);
......
#include "catch.hpp" #include "catch.hpp"
#include <fstream>
#include <filesystem> #include <filesystem>
#include <ftl/protocol/streams.hpp> #include <ftl/protocol/streams.hpp>
#include <ftl/protocol.hpp> #include <ftl/protocol.hpp>
...@@ -124,3 +125,13 @@ TEST_CASE("File write and read", "[stream]") { ...@@ -124,3 +125,13 @@ TEST_CASE("File write and read", "[stream]") {
REQUIRE( channels[2] == Channel::kScreen ); REQUIRE( channels[2] == Channel::kScreen );
} }
} }
TEST_CASE("File write fails for bad filename", "[stream]") {
std::string filename = (std::filesystem::temp_directory_path() / "badfile.exe").string();
std::ofstream out(filename);
out << "something";
out.close();
auto writer = ftl::createStream(filename);
REQUIRE(!writer->begin());
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment