Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
Smjert committed Nov 1, 2023
1 parent fc86c2e commit cc126a0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 23 deletions.
82 changes: 66 additions & 16 deletions osquery/filesystem/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
#if WIN32
#include <osquery/utils/conversions/windows/strings.h>
#endif
#include <osquery/utils/system/system.h>

#include <osquery/utils/expected/expected.h>
#include <osquery/utils/json/json.h>
#include <osquery/utils/system/system.h>

namespace fs = boost::filesystem;
namespace errc = boost::system::errc;
Expand Down Expand Up @@ -158,39 +158,89 @@ Status readWithSize(OpenReadableFile& file_handle,
// }

} while (total_bytes < size);

/* NOTE: If the read happens successfully but in the end we get less than what we asked,
we cannot assume that this is an error necessarily, since the file could be one with no size,
so knowing how much data to read before hand is impossible. */
content.resize(total_bytes);


return Status::success();
}

Expected<std::string, Error> readFile(const fs::path& path, bool log) {
ReadResult readFile(const fs::path& path, bool log) {
OpenReadableFile handle(path, false);

if (handle.fd == nullptr || !handle.fd->isValid()) {
return Status::failure("Cannot open file for reading: " +
handle.fd->getFilePath().string());
return ReadResult::failure("Cannot open file for reading: " +
handle.fd->getFilePath().string());
}

std::uint64_t file_size = handle.fd->size();

// Apply the max byte-read.
auto read_max = FLAGS_read_max;
if (file_size > read_max) {
auto s =
Status::failure("Cannot read " + path.string() +
" size exceeds limit: " + std::to_string(file_size) +
" > " + std::to_string(read_max));
auto error_message = "Cannot read " + path.string() +
" size exceeds limit: " + std::to_string(file_size) +
" > " + std::to_string(read_max);
if (log) {
LOG(WARNING) << s.getMessage();
LOG(WARNING) << error_message;
}
return s;
return ReadResult::failure(error_message);
}

readWithSize()
std::string content;

return Status::success();


auto status = readWithSize(handle, content, file_size);

if (!status.ok()) {
return ReadResult::failure(status.getMessage());
}

return content;
}

Status readFile(const fs::path& path, std::string& content, bool log) {
return readFile(
path, ([&content](std::string_view buffer) { content += buffer; }), log);
ReadResult readFile(const fs::path& path,
std::size_t block_size,
std::function<void(std::string_view buffer)> callback,
bool log) {
OpenReadableFile handle(path, false);
if (handle.fd == nullptr || !handle.fd->isValid()) {
return ReadResult::failure("Cannot open file for reading: " +
handle.fd->getFilePath().string());
}

std::uint64_t file_size = handle.fd->size();

// Apply the max byte-read.
auto read_max = FLAGS_read_max;
if (file_size > read_max) {
auto error_message = "Cannot read " + path.string() +
" size exceeds limit: " + std::to_string(file_size) +
" > " + std::to_string(read_max);
if (log) {
LOG(WARNING) << error_message;
}
return ReadResult::failure(error_message);
}

std::string content;
std::size_t total_bytes = 0;

do {
auto status = readWithSize(handle, content, file_size);

if (!status.ok()) {
return ReadResult::failure(status.getMessage());
}

total_bytes += content.size();


} while ()
}

Status isWritable(const fs::path& path, bool effective) {
Expand Down
17 changes: 11 additions & 6 deletions osquery/filesystem/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ enum GlobLimits : size_t {
GLOB_NO_CANON = 0x4,
};

enum class ReadError {
GenericError,
};

inline GlobLimits operator|(GlobLimits a, GlobLimits b) {
return static_cast<GlobLimits>(static_cast<size_t>(a) |
static_cast<size_t>(b));
Expand All @@ -45,6 +49,7 @@ const std::string kSQLGlobRecursive{kSQLGlobWildcard + kSQLGlobWildcard};
/// Calls the setlocale() API, only used on Windows
void initializeFilesystemAPILocale();

using ReadResult = Expected<std::string, ReadError>;
/**
* @brief Read a file from disk.
*
Expand All @@ -59,14 +64,14 @@ void initializeFilesystemAPILocale();
*
* @return an instance of Status, indicating success or failure.
*/
Status readFile(const boost::filesystem::path& path,
std::string& content,
bool log = true);
ReadResult readFile(const boost::filesystem::path& path,
std::string& content,
bool log = true);

/// Internal representation for predicate-based chunk reading.
Status readFile(const boost::filesystem::path& path,
std::function<void(std::string_view buffer)> predicate,
bool log = true);
ReadResult readFile(const boost::filesystem::path& path,
std::function<void(std::string_view buffer)> predicate,
bool log = true);

/**
* @brief Write text to disk.
Expand Down
2 changes: 1 addition & 1 deletion osquery/utils/status/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Status {
* success or failure of an operation. On successful operations, the idiom
* is for the message to be "OK"
*/
std::string getMessage() const {
const std::string& getMessage() const {
return message_;
}

Expand Down

0 comments on commit cc126a0

Please sign in to comment.