1
0
mirror of https://github.com/DNSCrypt/dnscrypt-proxy.git synced 2024-12-29 00:30:30 +01:00

Refactor writing download to cache

This commit is contained in:
William Elwood 2019-10-31 09:03:18 +00:00 committed by Frank Denis
parent 082a4a5e01
commit b29c70551e

View File

@ -72,6 +72,25 @@ func (source *Source) fetchFromCache() (bin, sig []byte, delayTillNextUpdate tim
return return
} }
func (source *Source) writeToCache(bin, sig []byte) (err error) {
f := source.cacheFile
defer func() {
if err != nil {
if absPath, err2 := filepath.Abs(f); err2 == nil {
f = absPath
}
dlog.Warnf("%s: %s", f, err)
}
}()
if err = safefile.WriteFile(f, bin, 0644); err != nil {
return
}
if err = safefile.WriteFile(f+".minisig", sig, 0644); err != nil {
return
}
return
}
func fetchFromURL(xTransport *XTransport, u *url.URL) (bin []byte, err error) { func fetchFromURL(xTransport *XTransport, u *url.URL) (bin []byte, err error) {
var resp *http.Response var resp *http.Response
if resp, _, err = xTransport.Get(u, "", DefaultTimeout); err == nil { if resp, _, err = xTransport.Get(u, "", DefaultTimeout); err == nil {
@ -109,24 +128,11 @@ func (source *Source) fetchWithCache(xTransport *XTransport, urlStr string) (bin
if sig, err = fetchFromURL(xTransport, sigURL); err != nil { if sig, err = fetchFromURL(xTransport, sigURL); err != nil {
return return
} }
if err = AtomicFileWrite(source.cacheFile, bin); err != nil { source.writeToCache(bin, sig) // ignore error: not fatal
if absPath, err2 := filepath.Abs(source.cacheFile); err2 == nil {
dlog.Warnf("%s: %s", absPath, err)
}
}
if err = AtomicFileWrite(source.cacheFile+".minisig", sig); err != nil {
if absPath, err2 := filepath.Abs(source.cacheFile + ".minisig"); err2 == nil {
dlog.Warnf("%s: %s", absPath, err)
}
}
delayTillNextUpdate = source.prefetchDelay delayTillNextUpdate = source.prefetchDelay
return return
} }
func AtomicFileWrite(file string, data []byte) error {
return safefile.WriteFile(file, data, 0644)
}
type URLToPrefetch struct { type URLToPrefetch struct {
url string url string
when time.Time when time.Time