Skip to content

Commit

Permalink
Make the paths in feedback messages canonical
Browse files Browse the repository at this point in the history
It is easier to debug and figure out the right files since the canonical paths are unambiguous, compared to the previous implementation's relative paths.
  • Loading branch information
dr8co committed Nov 1, 2023
1 parent c92f7c0 commit 9cb5254
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/encryption/encryptDecrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,18 @@ inline void checkOutputFile(const fs::path &inFile, fs::path &outFile, const Ope
}

// If the output file exists, ask for confirmation for overwriting
if (auto file = outFile.string();fs::exists(outFile)) {
std::cout << file << " already exists. \nDo you want to overwrite it? (y/n): ";
if (fs::exists(outFile)) {
std::cout << fs::canonical(outFile) << " already exists. \nDo you want to overwrite it? (y/n): ";
if (!validateYesNo())
throw std::runtime_error("Operation aborted.");
}

// Determine if the output file can be written if it exists
if (auto file = outFile.string(); fs::exists(outFile) && !(isWritable(file) && isReadable(file)))
if (auto file = fs::canonical(outFile).string(); fs::exists(outFile) && !(isWritable(file) && isReadable(file)))
throw std::runtime_error(std::format("{} is not writable/readable.", file));

// Check if there is enough space on the disk to save the output file.
const auto availableSpace = getAvailableSpace(outFile.string());
const auto availableSpace = getAvailableSpace(outFile);
const auto fileSize = fs::file_size(inFile);
if (std::cmp_less(availableSpace, fileSize)) {
std::cerr << "Not enough space on disk to save " << outFile.string() << std::endl;
Expand Down Expand Up @@ -225,7 +225,7 @@ void fileEncryptionDecryption(const std::string &inputFileName, const std::strin

// If we reach here, the operation was successful
auto pre = mode == OperationMode::Encryption ? "En" : "De";
std::cout << std::format("{}cryption completed successfully. \n{}crypted file saved at '{}'", pre, pre,
std::cout << std::format("{}cryption completed successfully. \n{}crypted file saved as '{}'", pre, pre,
outputFileName) << std::endl;

// Preserve file permissions
Expand Down Expand Up @@ -330,11 +330,11 @@ void encryptDecrypt() {
}
}

std::cout << pre << "crypting " << inputPath << " with " << algoDescription.find(cipher)->second
<< "..." << std::endl;
std::cout << pre << "crypting " << fs::canonical(inputPath) << " with "
<< algoDescription.find(cipher)->second << "..." << std::endl;

fileEncryptionDecryption(inputPath.string(), outputPath.string(), password,
static_cast<int>(cipher), static_cast<OperationMode>(choice));
fileEncryptionDecryption(fs::canonical(inputPath).string(), fs::canonical(outputPath).string(),
password, static_cast<int>(cipher), static_cast<OperationMode>(choice));
std::cout << std::endl;

} catch (const std::exception &ex) {
Expand Down
2 changes: 1 addition & 1 deletion src/secureAllocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace privacy {
template<typename T>
/// \brief Custom allocator for STL containers, which locks and zeroizes memory.
/// \details Adapted from https://en.cppreference.com/w/cpp/named_req/Allocator
struct Allocator {
class Allocator {
public:

[[maybe_unused]] typedef T value_type;
Expand Down
9 changes: 3 additions & 6 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
#include <charconv>
#include <readline/readline.h>
#include <unistd.h>
#include <filesystem>
#include <utility>
#include <termios.h>
#include <optional>

namespace fs = std::filesystem;

/// \brief Performs Base64 decoding of a string into binary data.
/// \param encodedData Base64 encoded string.
/// \return a vector of the decoded binary data.
Expand Down Expand Up @@ -159,8 +156,8 @@ bool isReadable(const std::string &filename) {
/// \note This function is meant to be used to detect possible errors
/// early enough before file operations, and to warn the user to
/// check their filesystem storage space when it seems insufficient.
std::uintmax_t getAvailableSpace(const std::string &path) noexcept {
fs::path filePath(path);
std::uintmax_t getAvailableSpace(const fs::path &path) noexcept {
fs::path filePath{path};

std::error_code ec; // For ignoring errors to avoid throwing

Expand All @@ -169,7 +166,7 @@ std::uintmax_t getAvailableSpace(const std::string &path) noexcept {
filePath = filePath.parent_path();
if (ec) ec.clear();

const auto space = fs::space(filePath, ec);
const auto space = fs::space(fs::canonical(filePath, ec), ec);

// Return 0 in case of an error
return std::cmp_less(space.available, 0) || std::cmp_equal(space.available, UINTMAX_MAX) ? 0 : space.available;
Expand Down
5 changes: 4 additions & 1 deletion src/utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
#include <cstdint>
#include <optional>
#include <iostream>
#include <filesystem>
#include <openssl/buffer.h>
#include <openssl/evp.h>

namespace fs = std::filesystem;

template<typename T>
// Describes a type that can be formatted to the output stream
concept PrintableToStream = requires(std::ostream &os, const T &t) {
Expand Down Expand Up @@ -101,7 +104,7 @@ bool isWritable(const std::string &filename);

bool isReadable(const std::string &filename);

std::uintmax_t getAvailableSpace(const std::string &path) noexcept;
std::uintmax_t getAvailableSpace(const fs::path &path) noexcept;

bool copyFilePermissions(const std::string &srcFile, const std::string &destFile) noexcept;

Expand Down

0 comments on commit 9cb5254

Please sign in to comment.