1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Zbigniew Jędrzejewski-Szmek
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "fileio-label.h"
27 int drop_in_file(const char *dir, const char *unit, unsigned level,
28 const char *name, char **_p, char **_q) {
30 _cleanup_free_ char *b = NULL;
33 char prefix[DECIMAL_STR_MAX(unsigned)];
40 sprintf(prefix, "%u", level);
42 b = xescape(name, "/.");
46 if (!filename_is_safe(b))
49 p = strjoin(dir, "/", unit, ".d", NULL);
53 q = strjoin(p, "/", prefix, "-", b, ".conf", NULL);
64 int write_drop_in(const char *dir, const char *unit, unsigned level,
65 const char *name, const char *data) {
67 _cleanup_free_ char *p = NULL, *q = NULL;
75 r = drop_in_file(dir, unit, level, name, &p, &q);
80 return write_string_file_atomic_label(q, data);
83 int write_drop_in_format(const char *dir, const char *unit, unsigned level,
84 const char *name, const char *format, ...) {
85 _cleanup_free_ char *p = NULL;
95 r = vasprintf(&p, format, ap);
101 return write_drop_in(dir, unit, level, name, p);