Skip to content

Fail early if output engine file path is unwritable in trtexec #4449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions samples/common/sampleOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,10 @@ void BuildOptions::parse(Arguments& arguments)

if (getAndDelOption(arguments, "--saveEngine", engine))
{
if (!canWriteFile(engine))
{
throw std::invalid_argument(std::string("Cannot write engine file to path: ") + engine);
}
save = true;
}
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 @@ -105,6 +105,36 @@ void loadFromFile(std::string const& fileName, char* dst, size_t size)
}
}

// Check if the file at the given path can be written to.
bool canWriteFile(const std::string& path)
{
// Verify that the target directory exists
namespace fs = std::filesystem;
fs::path p(path);
fs::path dir = p.has_parent_path() ? p.parent_path() : fs::current_path();
if (!fs::exists(dir) || !fs::is_directory(dir))
{
return false;
}

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

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

return ok;
}

std::vector<std::string> splitToStringVec(std::string const& s, char separator, int64_t maxSplit)
{
std::vector<std::string> splitted;
Expand Down
3 changes: 3 additions & 0 deletions samples/common/sampleUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define TRT_SAMPLE_UTILS_H

#include <fstream>
#include <filesystem>
#include <iostream>
#include <memory>
#include <numeric>
Expand Down Expand Up @@ -75,6 +76,8 @@ void dumpInt4Buffer(void const* buffer, std::string const& separator, std::ostre

void loadFromFile(std::string const& fileName, char* dst, size_t size);

bool canWriteFile(const std::string& path);

std::vector<std::string> splitToStringVec(std::string const& option, char separator, int64_t maxSplit = -1);

bool broadcastIOFormats(std::vector<IOFormat> const& formats, size_t nbBindings, bool isInput = true);
Expand Down