Commit 38bb1d2a authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Fix defect when fallocate() failed with EOPNOTSUPP

Closes #24
parent cb4c52d8
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -158,7 +158,13 @@ AC_SEARCH_LIBS([tar_open], [tar],
AC_CHECK_HEADER_STDBOOL

# Checks for library functions.
AC_CHECK_FUNC([fallocate],[fallocate],[fallocate])
AS_IF([test "x${PROTOC}" == "x"],
          [AC_MSG_ERROR([ProtoBuf compiler "protoc" not found.])])

AC_CHECK_FUNC([fallocate], 
              [AC_DEFINE([HAVE_FALLOCATE], 
                         [1], [Define if file preallocation is available])])

################################################################################
### write makefiles
+12 −5
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <climits>
#include "config.h"

#include "utils.hpp"
#include "logger.hpp"
@@ -71,15 +72,21 @@ do_sendfile(int in_fd, int out_fd) {
    }

    // preallocate output file
#ifdef HAVE_FALLOCATE
    if(::fallocate(out_fd, 0, 0, sz) == -1) {
        if(errno != EOPNOTSUPP) {
            return static_cast<ssize_t>(-1);
        }
#endif // HAVE_FALLOCATE

        // filesystem doesn't support fallocate(), fallback to truncate()
        if(errno == EOPNOTSUPP) {
        if(::ftruncate(out_fd, sz) != 0) {
            return static_cast<ssize_t>(-1);
        }

#ifdef HAVE_FALLOCATE
    }
        return static_cast<ssize_t>(-1);
    }
#endif // HAVE_FALLOCATE

    // copy data
	off_t offset = 0;
+16 −11
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include "config.h"

#include "utils.hpp"
#include "logger.hpp"
@@ -58,18 +59,24 @@ create_file(const bfs::path& filename,
    }

    // preallocate output file
#ifdef HAVE_FALLOCATE
    if(::fallocate(out_fd, 0, 0, size) == -1) {
        // filesystem doesn't support fallocate(), fallback to truncate()
        if(errno == EOPNOTSUPP) {
            if(::ftruncate(out_fd, size) != 0) {
        if(errno != EOPNOTSUPP) {
            ec = std::make_error_code(static_cast<std::errc>(errno));
            return std::make_tuple(ec, nullptr);
        }
        }
#endif // HAVE_FALLOCATE

        // filesystem doesn't support fallocate(), fallback to truncate()
        if(::ftruncate(out_fd, size) != 0) {
            ec = std::make_error_code(static_cast<std::errc>(errno));
            return std::make_tuple(ec, nullptr);
        }

#ifdef HAVE_FALLOCATE
    }
#endif // HAVE_FALLOCATE

retry_close:
    if(close(out_fd) == -1) {
        if(errno == EINTR) {
@@ -213,10 +220,8 @@ local_path_to_remote_resource_transferor::transfer(
        LOGGER_DEBUG("};");
        LOGGER_FLUSH();

        auto rpc = 
            m_network_endpoint->post<rpc::remote_transfer>(endp, args);

        auto resp = rpc.get();
        auto resp = 
            m_network_endpoint->post<rpc::remote_transfer>(endp, args).get();

        if(static_cast<task_status>(resp.at(0).status()) ==
            task_status::finished_with_error) {
+16 −9
Original line number Diff line number Diff line
@@ -63,19 +63,26 @@ copy_memory_region(const std::shared_ptr<norns::io::task_info>& task_info,
        return std::make_error_code(static_cast<std::errc>(errno));
    }

    // preallocate output file
#ifdef HAVE_FALLOCATE
    if(::fallocate(out_fd, 0, 0, size) == -1) {
        if(errno != EOPNOTSUPP) {
            LOGGER_ERROR("fallocate() error");
            rv = errno;
            goto cleanup_on_error;
        }
#endif // HAVE_FALLOCATE

        // filesystem doesn't support fallocate(), fallback to truncate()
        if(errno == EOPNOTSUPP) {
        if(::ftruncate(out_fd, size) != 0) {
            LOGGER_ERROR("ftruncate() error on {}", dst);
            rv = errno;
            goto cleanup_on_error;
        }

#ifdef HAVE_FALLOCATE
    }
        LOGGER_ERROR("fallocate() error");
        rv = errno;
        goto cleanup_on_error;
    }
#endif // HAVE_FALLOCATE

    dst_addr = ::mmap(NULL, size, PROT_WRITE, MAP_SHARED, out_fd, 0);

+13 −7
Original line number Diff line number Diff line
@@ -52,18 +52,24 @@ create_file(const bfs::path& filename,
    }

    // preallocate output file
#ifdef HAVE_FALLOCATE
    if(::fallocate(out_fd, 0, 0, size) == -1) {
        // filesystem doesn't support fallocate(), fallback to truncate()
        if(errno == EOPNOTSUPP) {
            if(::ftruncate(out_fd, size) != 0) {
        if(errno != EOPNOTSUPP) {
            ec = std::make_error_code(static_cast<std::errc>(errno));
            return std::make_tuple(ec, nullptr);
        }
        }
#endif // HAVE_FALLOCATE

        // filesystem doesn't support fallocate(), fallback to truncate()
        if(::ftruncate(out_fd, size) != 0) {
            ec = std::make_error_code(static_cast<std::errc>(errno));
            return std::make_tuple(ec, nullptr);
        }

#ifdef HAVE_FALLOCATE
    }
#endif // HAVE_FALLOCATE

retry_close:
    if(close(out_fd) == -1) {
        if(errno == EINTR) {