Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions samples/common/sampleOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,11 @@ void BuildOptions::parse(Arguments& arguments)
if (getAndDelOption(arguments, "--saveEngine", engine))
{
save = true;
// Validate that the engine file path is writable before doing expensive work
if (!canWriteFile(engine))
{
throw std::invalid_argument(std::string("Cannot write engine file to path: ") + engine);
}
}
if (load && save)
{
Expand Down
30 changes: 30 additions & 0 deletions samples/common/sampleUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,4 +775,34 @@ std::vector<std::string> sanitizeArgv(int32_t argc, char** argv)
return sanitizedArgs;
}

bool canWriteFile(std::string const& path)
{
namespace fs = std::filesystem;
fs::path p(path);
fs::path dir = p.has_parent_path() ? p.parent_path() : fs::current_path();

// Check if the directory exists and is a directory
if (!fs::exists(dir) || !fs::is_directory(dir))
{
return false;
}

// Try to create a temporary file to test write permissions
fs::path const tempFilePath = dir / ".writetest.tmp";
std::ofstream test(tempFilePath.string(), std::ios::out | std::ios::trunc);
if (!test.is_open())
{
return false;
}
test << "test";
bool const ok = test.good();
test.close();

// Clean up the temporary file
std::error_code ec;
fs::remove(tempFilePath, ec);

return ok;
}

} // namespace sample
6 changes: 6 additions & 0 deletions samples/common/sampleUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef TRT_SAMPLE_UTILS_H
#define TRT_SAMPLE_UTILS_H

#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
Expand Down Expand Up @@ -175,6 +176,11 @@ std::string sanitizeRemoteAutoTuningConfig(const std::string& config);
//! @return Vector of sanitized argument strings
std::vector<std::string> sanitizeArgv(int32_t argc, char** argv);

//! Check if a file path is writable by testing write access to the parent directory
//! @param path The file path to check
//! @return true if the path is writable, false otherwise
bool canWriteFile(std::string const& path);

} // namespace sample

#endif // TRT_SAMPLE_UTILS_H