Unverified Commit 7e2e2a5f authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

bugfix: implement segmented read

According to the read manpage a read could return less bytes then
requested due to an interrupt or an error.

We now try to repeat the read operation until all the existent data have
been read correctly.
parent 43d53700
Loading
Loading
Loading
Loading
+30 −9
Original line number Diff line number Diff line
@@ -95,15 +95,36 @@ void ChunkStorage::read_chunk(const std::string& file_path, unsigned int chunk_i
        log->error("Failed to open chunk file for read. File: {}, Error: {}", chunk_path, std::strerror(errno));
        throw std::system_error(errno, std::system_category(), "Failed to open chunk file for read");
    }
    size_t tot_read = 0;
    ssize_t read = 0;

    do {
        read = pread64(fd,
                       buff + tot_read,
                       size - tot_read,
                       offset + tot_read);
        if(read == 0) {
            break;
        }

    auto read = pread64(fd, buff, size, offset);
        if (read < 0) {
            log->error("Failed to read chunk file. File: {}, size: {}, offset: {}, Error: {}",
                    chunk_path, size, offset, std::strerror(errno));
            throw std::system_error(errno, std::system_category(), "Failed to read chunk file");
        }

    ABT_eventual_set(eventual, &read, sizeof(size_t));
#ifndef NDEBUG
        if(tot_read + read < size) {
            log->warn("Read less bytes than requested: {}/{}. Total read was {}", read, size - tot_read, size);
        }
#endif
        assert(read > 0);
        tot_read += read;


    } while (tot_read != size);

    ABT_eventual_set(eventual, &tot_read, sizeof(size_t));

    auto err = close(fd);
    if (err < 0) {