core: rename (and modernize) bus_unit_check_load_state() → bus_unit_validate_load_state()

Let's use a switch() statement, cover more cases with pretty messages.
Also let's rename it to "validate", as that's more specific that
"check", as it implies checking for a "valid"/"good" state, which is
what this function does.
This commit is contained in:
Lennart Poettering 2018-06-01 17:30:43 +02:00
parent c602fd0f19
commit e49da001c4
4 changed files with 23 additions and 15 deletions

View file

@ -610,7 +610,7 @@ static int method_set_unit_properties(sd_bus_message *message, void *userdata, s
if (r < 0)
return r;
r = bus_unit_check_load_state(u, error);
r = bus_unit_validate_load_state(u, error);
if (r < 0)
return r;
@ -634,7 +634,7 @@ static int method_ref_unit(sd_bus_message *message, void *userdata, sd_bus_error
if (r < 0)
return r;
r = bus_unit_check_load_state(u, error);
r = bus_unit_validate_load_state(u, error);
if (r < 0)
return r;
@ -658,7 +658,7 @@ static int method_unref_unit(sd_bus_message *message, void *userdata, sd_bus_err
if (r < 0)
return r;
r = bus_unit_check_load_state(u, error);
r = bus_unit_validate_load_state(u, error);
if (r < 0)
return r;

View file

@ -1712,22 +1712,30 @@ int bus_unit_set_properties(
return n;
}
int bus_unit_check_load_state(Unit *u, sd_bus_error *error) {
int bus_unit_validate_load_state(Unit *u, sd_bus_error *error) {
assert(u);
if (u->load_state == UNIT_LOADED)
/* Generates a pretty error if a unit isn't properly loaded. */
switch (u->load_state) {
case UNIT_LOADED:
return 0;
/* Give a better description of the unit error when
* possible. Note that in the case of UNIT_MASKED, load_error
* is not set. */
if (u->load_state == UNIT_MASKED)
return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit %s is masked.", u->id);
if (u->load_state == UNIT_NOT_FOUND)
case UNIT_NOT_FOUND:
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not found.", u->id);
return sd_bus_error_set_errnof(error, u->load_error, "Unit %s is not loaded properly: %m.", u->id);
case UNIT_ERROR: /* Only show .load_error in UNIT_ERROR state */
return sd_bus_error_set_errnof(error, u->load_error, "Unit %s failed to loaded properly: %m.", u->id);
case UNIT_MASKED:
return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit %s is masked.", u->id);
case UNIT_STUB:
case UNIT_MERGED:
default:
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unexpected load state of unit %s", u->id);
}
}
static int bus_unit_track_handler(sd_bus_track *t, void *userdata) {

View file

@ -31,7 +31,7 @@ int bus_unit_method_ref(sd_bus_message *message, void *userdata, sd_bus_error *e
int bus_unit_method_unref(sd_bus_message *message, void *userdata, sd_bus_error *error);
int bus_unit_queue_job(sd_bus_message *message, Unit *u, JobType type, JobMode mode, bool reload_if_possible, sd_bus_error *error);
int bus_unit_check_load_state(Unit *u, sd_bus_error *error);
int bus_unit_validate_load_state(Unit *u, sd_bus_error *error);
int bus_unit_track_add_name(Unit *u, const char *name);
int bus_unit_track_add_sender(Unit *u, sd_bus_message *m);

View file

@ -910,7 +910,7 @@ int transaction_add_job_and_dependencies(
return sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->id);
if (type != JOB_STOP) {
r = bus_unit_check_load_state(unit, e);
r = bus_unit_validate_load_state(unit, e);
if (r < 0)
return r;
}