Skip to content

Commit d00a52c

Browse files
committed
pid1: split out helper func for two similar code paths
1 parent 7842c5f commit d00a52c

File tree

1 file changed

+40
-46
lines changed

1 file changed

+40
-46
lines changed

src/core/dbus-timer.c

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,42 @@ const sd_bus_vtable bus_timer_vtable[] = {
137137
SD_BUS_VTABLE_END
138138
};
139139

140+
static int timer_add_one_calendar_spec(
141+
Timer *t,
142+
const char *name,
143+
TimerBase base,
144+
UnitWriteFlags flags,
145+
const char *str,
146+
sd_bus_error *error) {
147+
148+
_cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
149+
int r;
150+
151+
r = calendar_spec_from_string(str, &c);
152+
if (r == -EINVAL)
153+
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid calendar spec");
154+
if (r < 0)
155+
return r;
156+
157+
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
158+
unit_write_settingf(UNIT(t), flags|UNIT_ESCAPE_SPECIFIERS, name,
159+
"%s=%s", timer_base_to_string(base), str);
160+
161+
TimerValue *v = new(TimerValue, 1);
162+
if (!v)
163+
return -ENOMEM;
164+
165+
*v = (TimerValue) {
166+
.base = base,
167+
.calendar_spec = c,
168+
};
169+
170+
LIST_PREPEND(value, t->values, v);
171+
}
172+
173+
return 1;
174+
};
175+
140176
static int bus_timer_set_transient_property(
141177
Timer *t,
142178
const char *name,
@@ -239,36 +275,17 @@ static int bus_timer_set_transient_property(
239275
return r;
240276

241277
while ((r = sd_bus_message_read(message, "(ss)", &base_name, &str)) > 0) {
242-
_cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
243278
TimerBase b;
244279

245280
b = timer_base_from_string(base_name);
246281
if (b != TIMER_CALENDAR)
247-
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid timer base: %s", base_name);
282+
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
283+
"Invalid timer base: %s", base_name);
248284

249-
r = calendar_spec_from_string(str, &c);
250-
if (r == -EINVAL)
251-
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid calendar spec: %s", str);
285+
r = timer_add_one_calendar_spec(t, name, b, flags, str, error);
252286
if (r < 0)
253287
return r;
254288

255-
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
256-
TimerValue *v;
257-
258-
unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", base_name, str);
259-
260-
v = new(TimerValue, 1);
261-
if (!v)
262-
return -ENOMEM;
263-
264-
*v = (TimerValue) {
265-
.base = b,
266-
.calendar_spec = TAKE_PTR(c),
267-
};
268-
269-
LIST_PREPEND(value, t->values, v);
270-
}
271-
272289
empty = false;
273290
}
274291
if (r < 0)
@@ -328,8 +345,6 @@ static int bus_timer_set_transient_property(
328345

329346
} else if (streq(name, "OnCalendar")) {
330347

331-
TimerValue *v;
332-
_cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
333348
const char *str;
334349

335350
log_notice("Client is using obsolete %s= transient property, please use TimersCalendar= instead.", name);
@@ -338,28 +353,7 @@ static int bus_timer_set_transient_property(
338353
if (r < 0)
339354
return r;
340355

341-
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
342-
r = calendar_spec_from_string(str, &c);
343-
if (r == -EINVAL)
344-
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid calendar spec");
345-
if (r < 0)
346-
return r;
347-
348-
unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", name, str);
349-
350-
v = new(TimerValue, 1);
351-
if (!v)
352-
return -ENOMEM;
353-
354-
*v = (TimerValue) {
355-
.base = TIMER_CALENDAR,
356-
.calendar_spec = TAKE_PTR(c),
357-
};
358-
359-
LIST_PREPEND(value, t->values, v);
360-
}
361-
362-
return 1;
356+
return timer_add_one_calendar_spec(t, name, TIMER_CALENDAR, flags, str, error);
363357
}
364358

365359
return 0;

0 commit comments

Comments
 (0)