| 1209 | } |
| 1210 | |
| 1211 | static struct command_result *nodes_refresh(struct command *cmd, |
| 1212 | struct table_desc *td, |
| 1213 | struct db_query *dbq) |
| 1214 | { |
| 1215 | struct sql *sql = sql_of(cmd->plugin); |
| 1216 | struct out_req *req; |
| 1217 | size_t msglen; |
| 1218 | u16 type, flags; |
| 1219 | |
| 1220 | if (sql->gosstore_fd == -1) { |
| 1221 | sql->gosstore_fd = open("gossip_store", O_RDONLY); |
| 1222 | if (sql->gosstore_fd == -1) |
| 1223 | plugin_err(cmd->plugin, "Could not open gossip_store: %s", |
| 1224 | strerror(errno)); |
| 1225 | } |
| 1226 | |
| 1227 | /* First time, set off to end and load from scratch */ |
| 1228 | if (sql->gosstore_nodes_off == 0) { |
| 1229 | sql->gosstore_nodes_off = find_gossip_store_end(sql->gosstore_fd, 1); |
| 1230 | return default_refresh(cmd, td, dbq); |
| 1231 | } |
| 1232 | |
| 1233 | /* OK, try catching up! */ |
| 1234 | while (gossip_store_readhdr(sql->gosstore_fd, sql->gosstore_nodes_off, |
| 1235 | &msglen, NULL, &flags, &type)) { |
| 1236 | struct node_id id; |
| 1237 | size_t off = sql->gosstore_nodes_off; |
| 1238 | |
| 1239 | sql->gosstore_nodes_off += sizeof(struct gossip_hdr) + msglen; |
| 1240 | |
| 1241 | if (flags & GOSSIP_STORE_DELETED_BIT) |
| 1242 | continue; |
| 1243 | |
| 1244 | if (type == WIRE_GOSSIP_STORE_ENDED) { |
| 1245 | /* Force a reopen */ |
| 1246 | sql->gosstore_nodes_off = sql->gosstore_channels_off = 0; |
| 1247 | close(sql->gosstore_fd); |
| 1248 | sql->gosstore_fd = -1; |
| 1249 | return nodes_refresh(cmd, td, dbq); |
| 1250 | } |
| 1251 | |
| 1252 | if (type == WIRE_NODE_ANNOUNCEMENT) { |
| 1253 | /* This can fail if entry not fully written yet. */ |
| 1254 | if (!extract_node_id(sql->gosstore_fd, off, type, &id)) { |
| 1255 | sql->gosstore_nodes_off = off; |
| 1256 | break; |
| 1257 | } |
| 1258 | |
| 1259 | /* FIXME: sqlite 3.24.0 (2018-06-04) added UPSERT, but |
| 1260 | * we don't require it. */ |
| 1261 | delete_node_from_db(cmd, &id); |
| 1262 | req = jsonrpc_request_start(cmd, "listnodes", |
| 1263 | listnodes_one_done, |
| 1264 | forward_error, |
| 1265 | dbq); |
| 1266 | json_add_node_id(req->js, "id", &id); |
| 1267 | return send_outreq(req); |
| 1268 | } |
no test coverage detected