local function download(url, fname)
local kluautil = require('kluautil')
- local file, rcode, errmsg
- file, errmsg = io.open(fname, 'w')
+ local fname_tmp = os.tmpname()
+ local file, ok, errmsg
+ file, errmsg = io.open(fname_tmp, 'w')
if not file then
- error(string.format("unable to open file %s: %s", fname, errmsg))
+ return false, string.format("unable to open file %s: %s", fname_tmp, errmsg)
end
log_info(ffi.C.LOG_GRP_PREFILL, "downloading root zone to file %s ...", fname)
- rcode, errmsg = kluautil.kr_https_fetch(url, file, rz_ca_file)
- if rcode == nil then
- error(string.format("fetch of `%s` failed: %s", url, errmsg))
- end
-
+ ok, errmsg = kluautil.kr_https_fetch(url, file, rz_ca_file)
file:close()
+ if not ok then
+ os.remove(fname_tmp) -- try to clean up but ignore errors
+ return false, string.format("fetch of `%s` failed: %s", url, errmsg)
+ end
+ ok, errmsg = os.rename(fname_tmp, fname)
+ if ok == nil then
+ os.remove(fname_tmp) -- try to clean up but ignore errors
+ return false, string.format("unable to move to file %s: %s", fname, errmsg);
+ end
+ return true
end
local function import(fname)
log_info(ffi.C.LOG_GRP_PREFILL, "root zone file valid for %s, reusing data from disk",
display_delay(file_ttl))
else
- local ok, errmsg = pcall(download, rz_url, rz_local_fname)
+ local ok, errmsg = download(rz_url, rz_local_fname)
if not ok then
rz_cur_interval = rz_https_fail_interval
- math.random(rz_interval_randomizer_limit)
.. "will retry root zone download in %s",
errmsg, display_delay(rz_cur_interval))
restart_timer(rz_cur_interval)
- os.remove(rz_local_fname)
return
end
file_ttl = rz_default_interval