From: David Härdeman Date: Sat, 8 Nov 2025 16:48:00 +0000 (+0100) Subject: statefiles: write straight to file in statefiles_write_state4() X-Git-Url: http://git.openwrt.org/?a=commitdiff_plain;h=940ecbb3916162cf734ffe95e87363199a0c119b;p=project%2Fodhcpd.git statefiles: write straight to file in statefiles_write_state4() Instead of writing to a buffer, then to a file, just write straight to file. This also makes the order of the steps in the function more logical (i.e. matches the order in which things are actually written to file). Signed-off-by: David Härdeman Link: https://github.com/openwrt/odhcpd/pull/302 Signed-off-by: Álvaro Fernández Rojas --- diff --git a/src/statefiles.c b/src/statefiles.c index c392e34..2c41032 100644 --- a/src/statefiles.c +++ b/src/statefiles.c @@ -213,26 +213,25 @@ static void statefiles_write_state4(struct write_ctxt *ctxt, struct dhcpv4_lease struct in_addr addr = { .s_addr = lease->addr }; char ipbuf[INET6_ADDRSTRLEN]; - odhcpd_hexlify(hexhwaddr, lease->hwaddr, sizeof(lease->hwaddr)); inet_ntop(AF_INET, &addr, ipbuf, sizeof(ipbuf)); - /* # "ipv4" "32" "/32" */ - ctxt->buf_idx = snprintf(ctxt->buf, ctxt->buf_len, - "# %s %s ipv4 %s%s %" PRId64 " %x 32 %s/32\n", - ctxt->iface->ifname, hexhwaddr, - (lease->flags & OAF_BROKEN_HOSTNAME) ? "broken\\x20" : "", - (lease->hostname ? lease->hostname : "-"), - (lease->valid_until > ctxt->now ? - (int64_t)(lease->valid_until - ctxt->now + ctxt->wall_time) : - (INFINITE_VALID(lease->valid_until) ? -1 : 0)), - ntohl(lease->addr), ipbuf); - if (statefiles_write_host4(ctxt, lease)) { md5_hash(ipbuf, strlen(ipbuf), &ctxt->md5); md5_hash(lease->hostname, strlen(lease->hostname), &ctxt->md5); } - fwrite(ctxt->buf, 1, ctxt->buf_idx, ctxt->fp); + odhcpd_hexlify(hexhwaddr, lease->hwaddr, sizeof(lease->hwaddr)); + + /* # "ipv4" "32" "/32" */ + fprintf(ctxt->fp, + "# %s %s ipv4 %s%s %" PRId64 " %x 32 %s/32\n", + ctxt->iface->ifname, hexhwaddr, + (lease->flags & OAF_BROKEN_HOSTNAME) ? "broken\\x20" : "", + (lease->hostname ? lease->hostname : "-"), + (lease->valid_until > ctxt->now ? + (int64_t)(lease->valid_until - ctxt->now + ctxt->wall_time) : + (INFINITE_VALID(lease->valid_until) ? -1 : 0)), + ntohl(lease->addr), ipbuf); } static bool statefiles_write_state(time_t now)