@@ -107,7 +107,7 @@ Unit* unit_new(Manager *m, size_t size) {
107107 u -> type = _UNIT_TYPE_INVALID ;
108108 u -> default_dependencies = true;
109109 u -> unit_file_state = _UNIT_FILE_STATE_INVALID ;
110- u -> unit_file_preset = -1 ;
110+ u -> unit_file_preset = _PRESET_ACTION_INVALID ;
111111 u -> on_failure_job_mode = JOB_REPLACE ;
112112 u -> on_success_job_mode = JOB_FAIL ;
113113 u -> job_timeout = USEC_INFINITY ;
@@ -4149,15 +4149,21 @@ UnitFileState unit_get_unit_file_state(Unit *u) {
41494149
41504150 assert (u );
41514151
4152- if (u -> unit_file_state < 0 && u -> fragment_path ) {
4153- r = unit_file_get_state (
4154- u -> manager -> runtime_scope ,
4155- NULL ,
4156- u -> id ,
4157- & u -> unit_file_state );
4158- if (r < 0 )
4159- u -> unit_file_state = UNIT_FILE_BAD ;
4160- }
4152+ if (u -> unit_file_state >= 0 || !u -> fragment_path )
4153+ return u -> unit_file_state ;
4154+
4155+ /* If we know this is a transient unit no need to ask the unit file state for details. Let's bypass
4156+ * the more expensive on-disk check. */
4157+ if (u -> transient )
4158+ return (u -> unit_file_state = UNIT_FILE_TRANSIENT );
4159+
4160+ r = unit_file_get_state (
4161+ u -> manager -> runtime_scope ,
4162+ /* root_dir= */ NULL ,
4163+ u -> id ,
4164+ & u -> unit_file_state );
4165+ if (r < 0 )
4166+ u -> unit_file_state = UNIT_FILE_BAD ;
41614167
41624168 return u -> unit_file_state ;
41634169}
@@ -4167,24 +4173,26 @@ PresetAction unit_get_unit_file_preset(Unit *u) {
41674173
41684174 assert (u );
41694175
4170- if (u -> unit_file_preset < 0 && u -> fragment_path ) {
4171- _cleanup_free_ char * bn = NULL ;
4176+ if (u -> unit_file_preset >= 0 )
4177+ return u -> unit_file_preset ;
41724178
4173- r = path_extract_filename (u -> fragment_path , & bn );
4174- if (r < 0 )
4175- return (u -> unit_file_preset = r );
4179+ /* If this is a transient or perpetual unit file it doesn't make much sense to ask the preset
4180+ * database about this, because enabling/disabling makes no sense for either. Hence don't. */
4181+ if (!u -> fragment_path || u -> transient || u -> perpetual )
4182+ return (u -> unit_file_preset = - ENOEXEC );
41764183
4177- if (r == O_DIRECTORY )
4178- return (u -> unit_file_preset = - EISDIR );
4184+ _cleanup_free_ char * bn = NULL ;
4185+ r = path_extract_filename (u -> fragment_path , & bn );
4186+ if (r < 0 )
4187+ return (u -> unit_file_preset = r );
4188+ if (r == O_DIRECTORY )
4189+ return (u -> unit_file_preset = - EISDIR );
41794190
4180- u -> unit_file_preset = unit_file_query_preset (
4191+ return ( u -> unit_file_preset = unit_file_query_preset (
41814192 u -> manager -> runtime_scope ,
4182- NULL ,
4193+ /* root_dir= */ NULL ,
41834194 bn ,
4184- NULL );
4185- }
4186-
4187- return u -> unit_file_preset ;
4195+ /* cache= */ NULL ));
41884196}
41894197
41904198Unit * unit_ref_set (UnitRef * ref , Unit * source , Unit * target ) {
0 commit comments