Skip to content

Commit

Permalink
throw an error if we try to open a file with ~ in the file name becau…
Browse files Browse the repository at this point in the history
…se we do not do shell expansion. Throw an error for globs with ~ in the file name on windows #2024
  • Loading branch information
hobu committed Jul 31, 2018
1 parent 40bcc10 commit 97dcfc2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pdal/util/FileUtils.cpp
Expand Up @@ -79,6 +79,11 @@ namespace FileUtils

std::istream *openFile(std::string const& filename, bool asBinary)
{
std::string::size_type found_tilde(std::string::npos);
found_tilde = filename.find('~');
if (found_tilde != std::string::npos)
throw pdal::pdal_error("PDAL does not support shell expansion");

std::ifstream *ifs = nullptr;

std::string name(filename);
Expand Down Expand Up @@ -371,7 +376,15 @@ std::string extension(const std::string& filename)
std::vector<std::string> glob(std::string path)
{
std::vector<std::string> filenames;


#ifdef WIN32

std::string::size_type found_tilde(std::string::npos);
found_tilde = path.find('~');
if (found_tilde != std::string::npos)
throw pdal::pdal_error("PDAL does not support shell expansion");

WIN32_FIND_DATA ffd;
HANDLE handle = FindFirstFile(path.c_str(), &ffd);

Expand Down
6 changes: 6 additions & 0 deletions test/unit/FileUtilsTest.cpp
Expand Up @@ -75,6 +75,8 @@ TEST(FileUtilsTest, test_file_ops)
// delete test
FileUtils::deleteFile(tmp2);
EXPECT_TRUE(FileUtils::fileExists(tmp2)==false);

EXPECT_THROW(FileUtils::openFile("~foo1.glob"), pdal::pdal_error);
}

TEST(FileUtilsTest, test_readFileIntoString)
Expand Down Expand Up @@ -242,6 +244,10 @@ TEST(FileUtilsTest, glob)
EXPECT_EQ(FileUtils::glob(TP("*.glob")).size(), 0u);
EXPECT_EQ(FileUtils::glob(TP("foo1.glob")).size(), 0u);

#ifdef _WIN32
EXPECT_THROW(FileUtils::glob(TP("~foo1.glob")), pdal::pdal_error);
#endif

FileUtils::deleteFile("temp.glob");
FileUtils::closeFile(FileUtils::createFile("temp.glob"));
EXPECT_EQ(FileUtils::glob("temp.glob").size(), 1u);
Expand Down

0 comments on commit 97dcfc2

Please sign in to comment.