generator: add helper function for writing unit files

It doesn't save too much, but it's a common pattern so I think it's worth
to factor this out.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2017-12-15 16:47:11 +01:00
parent e09fc88440
commit fb883e759d
5 changed files with 73 additions and 66 deletions
+34
View File
@@ -19,6 +19,7 @@
***/
#include <errno.h>
#include <stdio_ext.h>
#include <unistd.h>
#include "alloc-util.h"
@@ -39,6 +40,39 @@
#include "unit-name.h"
#include "util.h"
int generator_open_unit_file(
const char *dest,
const char *source,
const char *name,
FILE **file) {
const char *unit;
FILE *f;
unit = strjoina(dest, "/", name);
f = fopen(unit, "wxe");
if (!f) {
if (source && errno == EEXIST)
return log_error_errno(errno,
"Failed to create unit file %s, as it already exists. Duplicate entry in %s?",
unit, source);
else
return log_error_errno(errno,
"Failed to create unit file %s: %m",
unit);
}
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
fprintf(f,
"# Automatically generated by %s\n\n",
program_invocation_short_name);
*file = f;
return 0;
}
int generator_add_symlink(const char *root, const char *dst, const char *dep_type, const char *src) {
/* Adds a symlink from <dst>.<dep_type>.d/ to ../<src> */
+6
View File
@@ -22,6 +22,12 @@
#include <stdio.h>
int generator_open_unit_file(
const char *dest,
const char *source,
const char *name,
FILE **file);
int generator_add_symlink(const char *root, const char *dst, const char *dep_type, const char *src);
int generator_write_fsck_deps(