Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appending to an existing file doesn't work #222

Open
kestrel-one opened this issue Oct 10, 2024 · 2 comments
Open

Appending to an existing file doesn't work #222

kestrel-one opened this issue Oct 10, 2024 · 2 comments

Comments

@kestrel-one
Copy link

I'd like to append to an existing LZ4 file in a single thread. I only ever have one writer at a time.

Writing to an existing file (O_APPEND mode) appears to be possible, but I'm unable to read anything after the "last block" that was created by the first writer. Simple example below:

package main

import (
	"fmt"
	"io"
	"os"

	"github.com/pierrec/lz4/v4"
)

func main() {
	// Open a file, truncate it, and write to it
	write1File, _ := os.OpenFile("test", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
	write1 := lz4.NewWriter(write1File)
	write1.Write([]byte{1})
	write1.Close()
	write1File.Close()

	// Open the same file and append to it
	write2File, _ := os.OpenFile("test", os.O_APPEND|os.O_WRONLY, 0666)
	write2 := lz4.NewWriter(write2File)
	write2.Write([]byte{2})
	write2.Close()
	write2File.Close()

	// Now try to read from the file
	file, _ := os.Open("test")
	reader := lz4.NewReader(file)
	contents, _ := io.ReadAll(reader)

	fmt.Println(contents) // []byte{1}
}
@pierrec
Copy link
Owner

pierrec commented Dec 12, 2024

This is expected behaviour: you are basically appending multiple lz4 files and a reader will only see the first one.
There is a PR that supports this use case by disabling the header being written for the second file (when opened in append mode), but it need some work.

@pierrec pierrec closed this as completed Dec 12, 2024
@pierrec pierrec reopened this Dec 13, 2024
@pierrec
Copy link
Owner

pierrec commented Dec 13, 2024

Reopening this as a good night sleep makes you reconsider things :)
The specs do mention frame concatenation and the fact that the reference implementation does support it, so I will make sure we do as well. Thanks for pointing that out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants