From 9c73ab3070255fe7952fd9b3b948e42c848e58cd Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 7 Apr 2023 16:18:50 +0200 Subject: [PATCH] Simplify updateCache() --- dnscrypt-proxy/sources.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/dnscrypt-proxy/sources.go b/dnscrypt-proxy/sources.go index a3ccc26a..a15b0cd7 100644 --- a/dnscrypt-proxy/sources.go +++ b/dnscrypt-proxy/sources.go @@ -98,17 +98,24 @@ func writeSource(f string, bin, sig []byte) (err error) { return fSig.Commit() } -func (source *Source) updateCache(bin, sig []byte, now time.Time) { +func (source *Source) updateCache(bin, sig []byte, now time.Time) error { f := source.cacheFile - var writeErr error // an error writing cache isn't fatal - if !bytes.Equal(source.bin, bin) { - if writeErr = writeSource(f, bin, sig); writeErr != nil { - source.bin = bin - return - } + // If the data and signature are unchanged, update the files timestamps only + if bytes.Equal(source.bin, bin) { + _ = os.Chtimes(f, now, now) + _ = os.Chtimes(f+".minisig", now, now) + return nil } - os.Chtimes(f, now, now) - source.bin = bin + // Otherwise, write the new data and signature + if err := writeSource(f, bin, sig); err != nil { + dlog.Warnf("Source [%s] failed to update cache file [%s]: %v", source.name, f, err) + return err + } + source.bin = bin // In-memory copy of the cache file content + // The tests require the timestamps to be updated, no idea why + _ = os.Chtimes(f, now, now) + _ = os.Chtimes(f+".minisig", now, now) + return nil } func (source *Source) parseURLs(urls []string) {