From 09ea81f995f00b9565037b0fafe121eccfe4cf57 Mon Sep 17 00:00:00 2001 From: Markus Vieth Date: Fri, 14 Jan 2022 16:15:05 +0100 Subject: [PATCH] Clean up test_io and improve raw_allocate --- io/include/pcl/io/impl/pcd_io.hpp | 24 +++++++++++++++--------- io/include/pcl/io/low_level_io.h | 16 ++++++++++++---- test/io/test_io.cpp | 17 ++++++++--------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/io/include/pcl/io/impl/pcd_io.hpp b/io/include/pcl/io/impl/pcd_io.hpp index bc4535b2d33..b1499ffebb3 100644 --- a/io/include/pcl/io/impl/pcd_io.hpp +++ b/io/include/pcl/io/impl/pcd_io.hpp @@ -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); } @@ -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); } @@ -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); } diff --git a/io/include/pcl/io/low_level_io.h b/io/include/pcl/io/low_level_io.h index e59d1a632ed..9fd4e9d26bf 100644 --- a/io/include/pcl/io/low_level_io.h +++ b/io/include/pcl/io/low_level_io.h @@ -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. diff --git a/test/io/test_io.cpp b/test/io/test_io.cpp index 5e46e8561e2..f910d4c6f89 100644 --- a/test/io/test_io.cpp +++ b/test/io/test_io.cpp @@ -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); @@ -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 ("test_pcl_io_binary.pcd", cloud, true); @@ -595,7 +596,7 @@ TEST (PCL, IO) pcl::Indices indices (cloud.width * cloud.height / 2); for (int i = 0; i < static_cast (indices.size ()); ++i) indices[i] = i; - // Save as ASCII + // Save as binary try { w.write ("test_pcl_io_binary.pcd", cloud, indices, true); @@ -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); @@ -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); @@ -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"); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1274,7 +1273,7 @@ TEST (PCL, Locale) { PCL_WARN ("Failed to set locale, skipping test.\n"); } - int res = writer.writeASCII ("test_pcl_io_ascii.pcd", cloud); + int res = writer.writeASCII ("test_pcl_io_ascii_locale.pcd", cloud); EXPECT_EQ (res, 0); PCDReader reader; @@ -1290,7 +1289,7 @@ TEST (PCL, Locale) { PCL_WARN ("Failed to set locale, skipping test.\n"); } - reader.read ("test_pcl_io_ascii.pcd", cloud2); + reader.read ("test_pcl_io_ascii_locale.pcd", cloud2); std::locale::global (std::locale::classic ()); EXPECT_EQ (cloud2.width, cloud.width); @@ -1312,7 +1311,7 @@ TEST (PCL, Locale) { } - remove ("test_pcl_io_ascii.pcd"); + remove ("test_pcl_io_ascii_locale.pcd"); #endif }