Skip to content

Commit

Permalink
Merge pull request #5137 from mvieth/test_io_fallocate
Browse files Browse the repository at this point in the history
Clean up test_io and improve raw_fallocate
  • Loading branch information
mvieth authored Jan 17, 2022
2 parents 7b4e6a1 + 09ea81f commit c034495
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
24 changes: 15 additions & 9 deletions io/include/pcl/io/impl/pcd_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,15 @@ pcl::PCDWriter::writeBinary (const std::string &file_name,

#else
// Allocate disk space for the entire file to prevent bus errors.
if (io::raw_fallocate (fd, data_idx + data_size) != 0)
const int allocate_res = io::raw_fallocate (fd, data_idx + data_size);
if (allocate_res != 0)
{
io::raw_close (fd);
resetLockingPermissions (file_name, file_lock);
PCL_ERROR ("[pcl::PCDWriter::writeBinary] posix_fallocate errno: %d strerror: %s\n", errno, strerror (errno));
PCL_ERROR ("[pcl::PCDWriter::writeBinary] raw_fallocate(length=%zu) returned %i. errno: %d strerror: %s\n",
data_idx + data_size, allocate_res, errno, strerror (errno));

throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during posix_fallocate ()!");
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during raw_fallocate ()!");
return (-1);
}

Expand Down Expand Up @@ -372,13 +374,15 @@ pcl::PCDWriter::writeBinaryCompressed (const std::string &file_name,

#else
// Allocate disk space for the entire file to prevent bus errors.
if (io::raw_fallocate (fd, compressed_final_size) != 0)
const int allocate_res = io::raw_fallocate (fd, compressed_final_size);
if (allocate_res != 0)
{
io::raw_close (fd);
resetLockingPermissions (file_name, file_lock);
PCL_ERROR ("[pcl::PCDWriter::writeBinaryCompressed] posix_fallocate errno: %d strerror: %s\n", errno, strerror (errno));
PCL_ERROR ("[pcl::PCDWriter::writeBinaryCompressed] raw_fallocate(length=%u) returned %i. errno: %d strerror: %s\n",
compressed_final_size, allocate_res, errno, strerror (errno));

throw pcl::IOException ("[pcl::PCDWriter::writeBinaryCompressed] Error during posix_fallocate ()!");
throw pcl::IOException ("[pcl::PCDWriter::writeBinaryCompressed] Error during raw_fallocate ()!");
return (-1);
}

Expand Down Expand Up @@ -646,13 +650,15 @@ pcl::PCDWriter::writeBinary (const std::string &file_name,

#else
// Allocate disk space for the entire file to prevent bus errors.
if (io::raw_fallocate (fd, data_idx + data_size) != 0)
const int allocate_res = io::raw_fallocate (fd, data_idx + data_size);
if (allocate_res != 0)
{
io::raw_close (fd);
resetLockingPermissions (file_name, file_lock);
PCL_ERROR ("[pcl::PCDWriter::writeBinary] posix_fallocate errno: %d strerror: %s\n", errno, strerror (errno));
PCL_ERROR ("[pcl::PCDWriter::writeBinary] raw_fallocate(length=%zu) returned %i. errno: %d strerror: %s\n",
data_idx + data_size, allocate_res, errno, strerror (errno));

throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during posix_fallocate ()!");
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during raw_fallocate ()!");
return (-1);
}

Expand Down
16 changes: 12 additions & 4 deletions io/include/pcl/io/low_level_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,24 @@ namespace pcl
// Android's libc doesn't have posix_fallocate.
if (::fallocate(fd, 0, 0, length) == 0)
return 0;

// fallocate returns -1 on error and sets errno
// EINVAL should indicate an unsupported filesystem.
// All other errors are passed up.
if (errno != EINVAL)
return -1;
# else
// Conforming POSIX systems have posix_fallocate.
if (::posix_fallocate(fd, 0, length) == 0)
const int res = ::posix_fallocate(fd, 0, length);
if (res == 0)
return 0;
# endif

// posix_fallocate does not set errno
// EINVAL should indicate an unsupported filesystem.
// All other errors are passed up.
if (errno != EINVAL)
return -1;
if (res != EINVAL)
return res;
# endif

// Try to deal with unsupported filesystems by simply seeking + writing.
// This may not really allocate space, but the file size will be set.
Expand Down
17 changes: 8 additions & 9 deletions test/io/test_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ TEST (PCL, IO)
EXPECT_EQ (bool (cloud_blob.is_dense), cloud.is_dense);
EXPECT_EQ (std::size_t (cloud_blob.data.size () * 2), // PointXYZI is 16*2 (XYZ+1, Intensity+3)
cloud_blob.width * cloud_blob.height * sizeof (PointXYZI)); // test for loadPCDFile ()
remove ("test_pcl_io.pcd");

// Convert from blob to data type
fromPCLPointCloud2 (cloud_blob, cloud);
Expand All @@ -523,7 +524,7 @@ TEST (PCL, IO)
EXPECT_FLOAT_EQ (cloud[nr_p - 1].z, last.z); // test for fromPCLPointCloud2 ()
EXPECT_FLOAT_EQ (cloud[nr_p - 1].intensity, last.intensity); // test for fromPCLPointCloud2 ()

// Save as ASCII
// Save as binary
try
{
w.write<PointXYZI> ("test_pcl_io_binary.pcd", cloud, true);
Expand Down Expand Up @@ -595,7 +596,7 @@ TEST (PCL, IO)

pcl::Indices indices (cloud.width * cloud.height / 2);
for (int i = 0; i < static_cast<int> (indices.size ()); ++i) indices[i] = i;
// Save as ASCII
// Save as binary
try
{
w.write<PointXYZI> ("test_pcl_io_binary.pcd", cloud, indices, true);
Expand All @@ -611,6 +612,7 @@ TEST (PCL, IO)
EXPECT_EQ (bool (cloud_blob.is_dense), cloud.is_dense);
EXPECT_EQ (std::size_t (cloud_blob.data.size () * 2), // PointXYZI is 16*2 (XYZ+1, Intensity+3)
cloud_blob.width * cloud_blob.height * sizeof (PointXYZI)); // test for loadPCDFile ()
remove ("test_pcl_io_binary.pcd");

// Convert from blob to data type
fromPCLPointCloud2 (cloud_blob, cloud);
Expand Down Expand Up @@ -643,6 +645,7 @@ TEST (PCL, IO)
EXPECT_TRUE (cloud_blob.is_dense);
EXPECT_EQ (std::size_t (cloud_blob.data.size () * 2), // PointXYZI is 16*2 (XYZ+1, Intensity+3)
cloud_blob.width * cloud_blob.height * sizeof (PointXYZI)); // test for loadPCDFile ()
remove ("test_pcl_io_ascii.pcd");

// Convert from blob to data type
fromPCLPointCloud2 (cloud_blob, cloud);
Expand All @@ -656,10 +659,6 @@ TEST (PCL, IO)
EXPECT_FLOAT_EQ (cloud[0].y, first.y); // test for fromPCLPointCloud2 ()
EXPECT_FLOAT_EQ (cloud[0].z, first.z); // test for fromPCLPointCloud2 ()
EXPECT_FLOAT_EQ (cloud[0].intensity, first.intensity); // test for fromPCLPointCloud2 ()

remove ("test_pcl_io_ascii.pcd");
remove ("test_pcl_io_binary.pcd");
remove ("test_pcl_io.pcd");
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1274,7 +1273,7 @@ TEST (PCL, Locale)
{
PCL_WARN ("Failed to set locale, skipping test.\n");
}
int res = writer.writeASCII<PointXYZ> ("test_pcl_io_ascii.pcd", cloud);
int res = writer.writeASCII<PointXYZ> ("test_pcl_io_ascii_locale.pcd", cloud);
EXPECT_EQ (res, 0);

PCDReader reader;
Expand All @@ -1290,7 +1289,7 @@ TEST (PCL, Locale)
{
PCL_WARN ("Failed to set locale, skipping test.\n");
}
reader.read<PointXYZ> ("test_pcl_io_ascii.pcd", cloud2);
reader.read<PointXYZ> ("test_pcl_io_ascii_locale.pcd", cloud2);
std::locale::global (std::locale::classic ());

EXPECT_EQ (cloud2.width, cloud.width);
Expand All @@ -1312,7 +1311,7 @@ TEST (PCL, Locale)
{
}

remove ("test_pcl_io_ascii.pcd");
remove ("test_pcl_io_ascii_locale.pcd");
#endif
}

Expand Down

0 comments on commit c034495

Please sign in to comment.