Skip to content

Commit 339731d

Browse files
committed
portable: properly handle if the unit file directory for portable service images doesn't exist
if the dir doesn#t exist then let's consider this indication for "this image isn't attached".
1 parent 40a7b23 commit 339731d

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/portable/portable.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,8 +1150,12 @@ int portable_detach(
11501150
where = attached_path(&paths, flags);
11511151

11521152
d = opendir(where);
1153-
if (!d)
1153+
if (!d) {
1154+
if (errno == ENOENT)
1155+
goto not_found;
1156+
11541157
return log_debug_errno(errno, "Failed to open '%s' directory: %m", where);
1158+
}
11551159

11561160
unit_files = set_new(&string_hash_ops);
11571161
if (!unit_files)
@@ -1213,10 +1217,8 @@ int portable_detach(
12131217
}
12141218
}
12151219

1216-
if (set_isempty(unit_files)) {
1217-
log_debug("No unit files associated with '%s' found. Image not attached?", name_or_path);
1218-
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "No unit files associated with '%s' found. Image not attached?", name_or_path);
1219-
}
1220+
if (set_isempty(unit_files))
1221+
goto not_found;
12201222

12211223
SET_FOREACH(item, unit_files, iterator) {
12221224
_cleanup_free_ char *md = NULL;
@@ -1290,6 +1292,10 @@ int portable_detach(
12901292
}
12911293

12921294
return ret;
1295+
1296+
not_found:
1297+
log_debug("No unit files associated with '%s' found. Image not attached?", name_or_path);
1298+
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "No unit files associated with '%s' found. Image not attached?", name_or_path);
12931299
}
12941300

12951301
static int portable_get_state_internal(
@@ -1317,8 +1323,15 @@ static int portable_get_state_internal(
13171323
where = attached_path(&paths, flags);
13181324

13191325
d = opendir(where);
1320-
if (!d)
1326+
if (!d) {
1327+
if (errno == ENOENT) {
1328+
/* If the 'attached' directory doesn't exist at all, then we know for sure this image isn't attached. */
1329+
*ret = PORTABLE_DETACHED;
1330+
return 0;
1331+
}
1332+
13211333
return log_debug_errno(errno, "Failed to open '%s' directory: %m", where);
1334+
}
13221335

13231336
unit_files = set_new(&string_hash_ops);
13241337
if (!unit_files)

0 commit comments

Comments
 (0)