Use io::JoinPath to build paths and avoid \ when constructing file names.

JoinPath is being made to deal with different OS path separators. Defaulting to
`/` doesn't work in all cases. As an example, on Windows, when a `\` is in a
path, `/` is no longer considered a path separator which is why we want to avoid
it in filenames.

PiperOrigin-RevId: 296086151
Change-Id: Ib2dfef55e9e779ff5138f960dc462fce8a14833b
This commit is contained in:
Brian Atkinson 2020-02-19 16:37:06 -08:00 committed by TensorFlower Gardener
parent 0dd277c674
commit 9c7537daae

View File

@ -43,7 +43,8 @@ std::string MakeUniqueFilename(string name) {
// Remove illegal characters from `name`. // Remove illegal characters from `name`.
for (int i = 0; i < name.size(); ++i) { for (int i = 0; i < name.size(); ++i) {
char ch = name[i]; char ch = name[i];
if (ch == '/' || ch == '[' || ch == ']' || ch == '*' || ch == '?') { if (ch == '/' || ch == '[' || ch == ']' || ch == '*' || ch == '?' ||
ch == '\\') {
name[i] = '_'; name[i] = '_';
} }
} }
@ -123,10 +124,7 @@ Status CreateFileForDumping(llvm::StringRef name,
<< "' directory for dumping: " << status; << "' directory for dumping: " << status;
return Status(error::Code::UNAVAILABLE, "(unavailable)"); return Status(error::Code::UNAVAILABLE, "(unavailable)");
} }
*filepath = llvm::Twine(dir) *filepath = io::JoinPath(dir, MakeUniqueFilename(std::string(name)));
.concat("/")
.concat(MakeUniqueFilename(std::string(name)))
.str();
// Try to open the file and generate a raw_ostream. // Try to open the file and generate a raw_ostream.
std::unique_ptr<WritableFile> file; std::unique_ptr<WritableFile> file;