From 7e7051d38742c639f9dd0c0aa4e12fa9ab301101 Mon Sep 17 00:00:00 2001 From: Seebs Date: Tue, 12 May 2020 15:40:38 -0500 Subject: [PATCH] don't try to truncate files when invoked read-only --- fragment.go | 8 +++++++- holder.go | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/fragment.go b/fragment.go index f70e9e71c..9aeceb79a 100644 --- a/fragment.go +++ b/fragment.go @@ -223,6 +223,9 @@ func (f *fragment) Open() error { // get no data. It tries to write the current storage to the provided file, // which is assumed to be the file they didn't get any data from. func (f *fragment) emptyStorage(file *os.File) (bool, error) { + if f.holder.ReadOnly { + return false, errors.New("can't flush/create storage for read-only holder") + } // No data. We'll mark this for no mapping, clear any existing // mapped containers, and set the Source to nil. We also have no // ops. @@ -276,7 +279,10 @@ func (f *fragment) importStorage(data []byte, file *os.File, newGen generation, } f.holder.Logger.Printf("warning: unmarshal storage, file=%s, err=%v", file.Name(), err) trunc, ok := cause.(roaring.FileShouldBeTruncatedError) - if ok { + if ok && !f.holder.ReadOnly { + // if the holder is ReadOnly, we silently ignore the "advisory" + // error. This may be a bad idea. + // generation code looks for a FileShouldBeTruncatedError return false, trunc } diff --git a/holder.go b/holder.go index d06920cd3..90df16860 100644 --- a/holder.go +++ b/holder.go @@ -101,6 +101,11 @@ type Holder struct { // needs to be queued and completed after all indexes // have opened. opening bool + + // ReadOnly indicates that this holder's contents should not produce + // disk writes under any circumstances. It must be set before Open + // is called, and changing it is not supported. + ReadOnly bool } func (h *Holder) StartTransaction(ctx context.Context, id string, timeout time.Duration, exclusive bool) (*Transaction, error) {