MCPcopy Create free account
hub / github.com/ElementsProject/lightning / append_to_file

Function append_to_file

lightningd/configs.c:341–381  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

339}
340
341static size_t append_to_file(struct lightningd *ld,
342 const char *fname,
343 const char *str,
344 bool must_exist)
345{
346 int fd;
347 const char *buffer;
348
349 fd = open(fname, O_RDWR|O_APPEND);
350 if (fd < 0) {
351 if (errno != ENOENT || must_exist)
352 fatal("Could not write to config %s: %s",
353 fname, strerror(errno));
354 fd = open(fname, O_RDWR|O_APPEND|O_CREAT, 0644);
355 if (fd < 0)
356 fatal("Could not create config file %s: %s",
357 fname, strerror(errno));
358 }
359
360 /* Note: always nul terminates */
361 buffer = grab_fd_str(tmpctx, fd);
362 if (!buffer)
363 fatal("Error reading %s: %s", fname, strerror(errno));
364
365 /* If there's a last character and it's not \n, add one */
366 if (tal_bytelen(buffer) > 1
367 && buffer[tal_bytelen(buffer)-2] != '\n')
368 str = tal_strcat(tmpctx, "\n", str);
369
370 /* Always append a \n ourselves */
371 str = tal_strcat(tmpctx, str, "\n");
372 if (write(fd, str, strlen(str)) != strlen(str))
373 fatal("Could not write to config file %s: %s",
374 fname, strerror(errno));
375 if (fsync(fd) != 0)
376 fatal("Syncing %s: %s", fname, strerror(errno));
377 close(fd);
378
379 /* 1-based counter of where new stuff appeared */
380 return strcount(buffer, "\n") + 1;
381}
382
383static const char *grab_and_check(const tal_t *ctx,
384 const char *fname,

Callers 3

create_setconfig_includeFunction · 0.85
configvar_saveFunction · 0.85
configvar_save_multiFunction · 0.85

Calls 4

grab_fd_strFunction · 0.85
tal_bytelenFunction · 0.85
strcountFunction · 0.85
fatalFunction · 0.70

Tested by

no test coverage detected