diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index e6c8df3af2d4e3f209c2f368a1e88f12c393c6d5..a5b2f03eb970af280d252af163753f2c504c5573 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1496,6 +1496,7 @@ virBuildPathInternal; virDirClose; virDirCreate; virDirOpen; +virDirOpenIfExists; virDirRead; virFileAbsPath; virFileAccessibleAs; diff --git a/src/util/virfile.c b/src/util/virfile.c index bf5e2b3a0c57498e0554af691d4a7512809f1c7a..ed044ba9c65d2afc92cbbd417e6437ffb102224e 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -2723,10 +2723,12 @@ virFileRemove(const char *path, #endif /* WIN32 */ static int -virDirOpenInternal(DIR **dirp, const char *name) +virDirOpenInternal(DIR **dirp, const char *name, bool ignoreENOENT) { *dirp = opendir(name); if (!*dirp) { + if (ignoreENOENT && errno == ENOENT) + return 0; virReportSystemError(errno, _("cannot open directory '%s'"), name); return -1; } @@ -2744,7 +2746,22 @@ virDirOpenInternal(DIR **dirp, const char *name) int virDirOpen(DIR **dirp, const char *name) { - return virDirOpenInternal(dirp, name); + return virDirOpenInternal(dirp, name, false); +} + +/** + * virDirOpenIfExists + * @dirp: directory stream + * @name: path of the directory + * + * Returns 1 on success. + * If opendir returns ENOENT, 0 is returned without reporting an error. + * On other errors, -1 is returned and an error is reported. + */ +int +virDirOpenIfExists(DIR **dirp, const char *name) +{ + return virDirOpenInternal(dirp, name, true); } /** diff --git a/src/util/virfile.h b/src/util/virfile.h index c618842fc5928990f6e78f0cc376c09f0331243e..42c65f2d1be3b1b24af022fe2d94278e10af33aa 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -232,6 +232,8 @@ int virDirCreate(const char *path, mode_t mode, uid_t uid, gid_t gid, unsigned int flags) ATTRIBUTE_RETURN_CHECK; int virDirOpen(DIR **dirp, const char *dirname) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; +int virDirOpenIfExists(DIR **dirp, const char *dirname) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; int virDirRead(DIR *dirp, struct dirent **ent, const char *dirname) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; void virDirClose(DIR **dirp)