Commit ebcab3b9 authored by Ramon Nou's avatar Ramon Nou Committed by Marc Vef
Browse files

Increase size via truncate implemented.

parent ae6a9b0b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Additional tests to increase code coverage ([!141](https://storage.bsc.es/gitlab/hpc/gekkofs/-/merge_requests/141)).
- GKFS_ENABLE_UNUSED_FUNCTIONS added to disable code to increase code coverage.
- Updated Parallax version to new API (parallax option needs kv_format.parallax in the path, and the database in a device with O_DIRECT)
- Support for increasing file size via `truncate()` added ([!159](https://storage.bsc.es/gitlab/hpc/gekkofs/-/merge_requests/159)

### Changed

+25 −4
Original line number Diff line number Diff line
@@ -143,6 +143,7 @@ gkfs_open(const std::string& path, mode_t mode, int flags) {
        errno = ENOTSUP;
        return -1;
    }

    // metadata object filled during create or stat
    gkfs::metadata::Metadata md{};
    if(flags & O_CREAT) {
@@ -578,10 +579,28 @@ gkfs_truncate(const std::string& path, off_t length) {

    auto size = md->size();
    if(static_cast<unsigned long>(length) > size) {
        LOG(DEBUG, "Length is greater then file size: {} > {}", length, size);
        LOG(DEBUG, "Length is greater then file size: '{}' > '{}'", length,
            size);
        auto output_fd = gkfs_open(path, md->mode(), O_WRONLY);
        if(output_fd == -1) {
            errno = EINVAL;
            return -1;
        }
        gkfs_lseek(output_fd, 0, SEEK_END);
        ssize_t n = static_cast<unsigned long>(length) - size;
        // Zeroes the buffer. All make_* are value initialized
        auto buf = std::make_unique<char[]>(n);
        if(!buf) {
            errno = ENOMEM;
            return -1;
        }
        if(gkfs_write(output_fd, buf.get(), (size_t) n) != n) {
            errno = EINVAL;
            return -1;
        }
        CTX->file_map()->remove(output_fd);
        return 0;
    }
    return gkfs_truncate(path, size, length);
}

@@ -677,8 +696,10 @@ ssize_t
gkfs_write(int fd, const void* buf, size_t count) {
    auto gkfs_fd = CTX->file_map()->get(fd);
    auto pos = gkfs_fd->pos(); // retrieve the current offset
    if(gkfs_fd->get_flag(gkfs::filemap::OpenFile_flags::append))
    if(gkfs_fd->get_flag(gkfs::filemap::OpenFile_flags::append)) {
        gkfs_lseek(gkfs_fd, 0, SEEK_END);
        pos = gkfs_fd->pos(); // Pos should be updated with append
    }
    auto ret = gkfs_pwrite(gkfs_fd, reinterpret_cast<const char*>(buf), count,
                           pos);
    // Update offset in file descriptor in the file map
+6 −2
Original line number Diff line number Diff line
@@ -150,7 +150,11 @@ def test_fail_truncate(gkfs_daemon, gkfs_client):

    # Truncate to a size greater than the file size
    ret = gkfs_client.truncate(truncfile, buf_length + 1)
    assert ret.retval == -1
    assert ret.errno == errno.EINVAL
    assert ret.retval == 0

    
    # Size should increase
    ret = gkfs_client.stat(truncfile)
    assert ret.statbuf.st_size == buf_length+1