Skip to content

Commit

Permalink
Merge pull request #2325 from fizyr-forks/allocate-disk-space-before-…
Browse files Browse the repository at this point in the history
…mmap

Allocate disk space with posix_fallocate before mmapping.
  • Loading branch information
taketwo authored May 30, 2018
2 parents 48e4edf + 2b0ed8a commit 3ceaf43
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 63 deletions.
73 changes: 20 additions & 53 deletions io/include/pcl/io/impl/pcd_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,25 +206,14 @@ pcl::PCDWriter::writeBinary (const std::string &file_name,
CloseHandle (fm);

#else
// Stretch the file size to the size of the data
off_t result = pcl_lseek (fd, getpagesize () + data_size - 1, SEEK_SET);

if (result < 0)
// Allocate disk space for the entire file to prevent bus errors.
if (::posix_fallocate (fd, 0, data_idx + data_size) != 0)
{
pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
PCL_ERROR ("[pcl::PCDWriter::writeBinary] lseek errno: %d strerror: %s\n", errno, strerror (errno));
PCL_ERROR ("[pcl::PCDWriter::writeBinary] posix_fallocate errno: %d strerror: %s\n", errno, strerror (errno));

throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during lseek ()!");
return (-1);
}
// Write a bogus entry so that the new file size comes in effect
result = static_cast<int> (::write (fd, "", 1));
if (result != 1)
{
pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during write ()!");
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during posix_fallocate ()!");
return (-1);
}

Expand Down Expand Up @@ -400,36 +389,24 @@ pcl::PCDWriter::writeBinaryCompressed (const std::string &file_name,
return (-1);
}

#if !_WIN32
// Stretch the file size to the size of the data
off_t result = pcl_lseek (fd, getpagesize () + data_size - 1, SEEK_SET);
if (result < 0)
{
pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
PCL_ERROR ("[pcl::PCDWriter::writeBinary] lseek errno: %d strerror: %s\n", errno, strerror (errno));

throw pcl::IOException ("[pcl::PCDWriter::writeBinaryCompressed] Error during lseek ()!");
return (-1);
}
// Write a bogus entry so that the new file size comes in effect
result = static_cast<int> (::write (fd, "", 1));
if (result != 1)
{
pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
throw pcl::IOException ("[pcl::PCDWriter::writeBinaryCompressed] Error during write ()!");
return (-1);
}
#endif

// Prepare the map
#if _WIN32
HANDLE fm = CreateFileMapping (h_native_file, NULL, PAGE_READWRITE, 0, compressed_final_size, NULL);
char *map = static_cast<char*>(MapViewOfFile (fm, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, compressed_final_size));
CloseHandle (fm);

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

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

char *map = static_cast<char*> (mmap (0, compressed_final_size, PROT_WRITE, MAP_SHARED, fd, 0));
if (map == reinterpret_cast<char*> (-1)) //MAP_FAILED)
{
Expand Down Expand Up @@ -719,24 +696,14 @@ pcl::PCDWriter::writeBinary (const std::string &file_name,
CloseHandle (fm);

#else
// Stretch the file size to the size of the data
off_t result = pcl_lseek (fd, getpagesize () + data_size - 1, SEEK_SET);
if (result < 0)
{
pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
PCL_ERROR ("[pcl::PCDWriter::writeBinary] lseek errno: %d strerror: %s\n", errno, strerror (errno));

throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during lseek ()!");
return (-1);
}
// Write a bogus entry so that the new file size comes in effect
result = static_cast<int> (::write (fd, "", 1));
if (result != 1)
// Allocate disk space for the entire file to prevent bus errors.
if (::posix_fallocate (fd, 0, data_idx + data_size) != 0)
{
pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during write ()!");
PCL_ERROR ("[pcl::PCDWriter::writeBinary] posix_fallocate errno: %d strerror: %s\n", errno, strerror (errno));

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

Expand Down
14 changes: 4 additions & 10 deletions io/src/lzf_image_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,12 @@ pcl::io::LZFImageWriter::saveImageBlob (const char* data,
int fd = pcl_open (filename.c_str (), O_RDWR | O_CREAT | O_TRUNC, static_cast<mode_t> (0600));
if (fd < 0)
return (false);
// Stretch the file size to the size of the data
off_t result = pcl_lseek (fd, data_size - 1, SEEK_SET);
if (result < 0)
{
pcl_close (fd);
return (false);
}
// Write a bogus entry so that the new file size comes in effect
result = static_cast<int> (::write (fd, "", 1));
if (result != 1)

// Allocate disk space for the entire file to prevent bus errors.
if (::posix_fallocate (fd, 0, data_size) != 0)
{
pcl_close (fd);
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during posix_fallocate ()!");
return (false);
}

Expand Down

0 comments on commit 3ceaf43

Please sign in to comment.