Skip to content

Commit

Permalink
Fix file size exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
abellgithub committed Feb 19, 2021
1 parent e5768ca commit 29dbe69
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 23 deletions.
3 changes: 3 additions & 0 deletions io/LasHeader.cpp
Expand Up @@ -398,8 +398,11 @@ ILeStream& operator>>(ILeStream& in, LasHeader& h)
throw LasHeader::error("Invalid point count. Number of points exceeds file size.");
if (h.m_vlrOffset > h.m_fileSize)
throw LasHeader::error("Invalid VLR offset - exceeds file size.");
// There was a bug in PDAL where it didn't write the VLR offset :(
/**
if (h.m_eVlrOffset > h.m_fileSize)
throw LasHeader::error("Invalid extended VLR offset - exceeds file size.");
**/

// Read regular VLRs.
in.seek(h.m_vlrOffset);
Expand Down
2 changes: 1 addition & 1 deletion io/LasReader.cpp
Expand Up @@ -246,7 +246,7 @@ void LasReader::initializeLocal(PointTableRef table, MetadataNode& m)
throwError(err.what());
}

m_p->header.initialize(log(), FileUtils::fileSize(m_filename));
m_p->header.initialize(log(), Utils::fileSize(m_filename));
createStream();
std::istream *stream(m_streamIf->m_istream);

Expand Down
6 changes: 3 additions & 3 deletions kernels/InfoKernel.cpp
Expand Up @@ -253,9 +253,9 @@ MetadataNode InfoKernel::run(const std::string& filename)
root.add("reader", m_reader->getName());
root.add("now", t.str());

if (pdal::FileUtils::fileExists(filename) &&
(!pdal::FileUtils::isDirectory(filename))) // allow for s3 uris and directories
root.add("file_size", pdal::FileUtils::fileSize(filename));
uintmax_t size = Utils::fileSize(filename);
if (size)
root.add("file_size", size);

return root;
}
Expand Down
14 changes: 14 additions & 0 deletions pdal/PDALUtils.cpp
Expand Up @@ -241,6 +241,20 @@ class ArbiterInStream : public std::ifstream

} // unnamed namespace

uintmax_t fileSize(const std::string& path)
{
uintmax_t size = 0;
if (isRemote(path))
{
std::unique_ptr<std::size_t> pSize = arbiter::Arbiter().tryGetSize(path);
if (pSize)
size = *pSize;
}
else
size = FileUtils::fileSize(path);
return size;
}

/**
Create a file (may be on a supported remote filesystem).
Expand Down
1 change: 1 addition & 0 deletions pdal/PDALUtils.hpp
Expand Up @@ -264,6 +264,7 @@ inline void writeProgress(int fd, const std::string& type,
std::string dllDir();
std::string PDAL_DLL toJSON(const MetadataNode& m);
void PDAL_DLL toJSON(const MetadataNode& m, std::ostream& o);
uintmax_t PDAL_DLL fileSize(const std::string& path);
std::istream PDAL_DLL *openFile(const std::string& path, bool asBinary = true);
std::ostream PDAL_DLL *createFile(const std::string& path,
bool asBinary = true);
Expand Down
3 changes: 2 additions & 1 deletion pdal/util/FileUtils.cpp
Expand Up @@ -255,7 +255,8 @@ bool fileExists(const std::string& name)

uintmax_t fileSize(const std::string& file)
{
return pdalboost::filesystem::file_size(toNative(file));
pdalboost::system::error_code ec;
return pdalboost::filesystem::file_size(toNative(file), ec);
}


Expand Down
45 changes: 27 additions & 18 deletions vendor/arbiter/arbiter.cpp
Expand Up @@ -99,6 +99,27 @@ namespace

return merge(in, config);
}

inline bool iequals(const std::string& s, const std::string& s2)
{
if (s.length() != s2.length())
return false;
for (size_t i = 0; i < s.length(); ++i)
if (std::toupper(s[i]) != std::toupper(s2[i]))
return false;
return true;
}

inline bool findEntry(const StringMap& map, const std::string& key, std::string& val)
{
for (auto& p : map)
if (iequals(p.first, key))
{
val = p.second;
return true;
}
return false;
}
}

Arbiter::Arbiter() : Arbiter(json().dump()) { }
Expand Down Expand Up @@ -1366,11 +1387,9 @@ std::unique_ptr<std::size_t> Http::tryGetSize(std::string path) const
auto http(m_pool.acquire());
Response res(http.head(typedPath(path)));

if (res.ok() && res.headers().count("Content-Length"))
{
const std::string& str(res.headers().at("Content-Length"));
size.reset(new std::size_t(std::stoul(str)));
}
std::string val;
if (res.ok() && findEntry(res.headers(), "Content-Length", val))
size.reset(new std::size_t(std::stoul(val)));

return size;
}
Expand Down Expand Up @@ -2572,19 +2591,9 @@ std::unique_ptr<std::size_t> Google::tryGetSize(const std::string path) const
const auto res(
https.internalHead(resource.endpoint(), headers, altMediaQuery));

if (res.ok())
{
if (res.headers().count("Content-Length"))
{
const auto& s(res.headers().at("Content-Length"));
return makeUnique<std::size_t>(std::stoull(s));
}
else if (res.headers().count("content-length"))
{
const auto& s(res.headers().at("content-length"));
return makeUnique<std::size_t>(std::stoull(s));
}
}
std::string val;
if (res.ok() && findEntry(res.headers(), "Content-Length", val))
return makeUnique<std::size_t>(std::stoull(val));

return std::unique_ptr<std::size_t>();
}
Expand Down

0 comments on commit 29dbe69

Please sign in to comment.