提交 f0ced708 编写于 作者: J Ján Tomko

Introduce virDirOpen

A helper that calls opendir and reports an error if it fails.
上级 bdb86810
...@@ -1495,6 +1495,7 @@ safezero; ...@@ -1495,6 +1495,7 @@ safezero;
virBuildPathInternal; virBuildPathInternal;
virDirClose; virDirClose;
virDirCreate; virDirCreate;
virDirOpen;
virDirRead; virDirRead;
virFileAbsPath; virFileAbsPath;
virFileAccessibleAs; virFileAccessibleAs;
......
...@@ -2732,6 +2732,31 @@ virFileRemove(const char *path, ...@@ -2732,6 +2732,31 @@ virFileRemove(const char *path,
} }
#endif /* WIN32 */ #endif /* WIN32 */
static int
virDirOpenInternal(DIR **dirp, const char *name)
{
*dirp = opendir(name);
if (!*dirp) {
virReportSystemError(errno, _("cannot open directory '%s'"), name);
return -1;
}
return 1;
}
/**
* virDirOpen
* @dirp: directory stream
* @name: path of the directory
*
* Returns 1 on success.
* On failure, -1 is returned and an error is reported.
*/
int
virDirOpen(DIR **dirp, const char *name)
{
return virDirOpenInternal(dirp, name);
}
/** /**
* virDirRead: * virDirRead:
* @dirp: directory to read * @dirp: directory to read
...@@ -2740,13 +2765,13 @@ virFileRemove(const char *path, ...@@ -2740,13 +2765,13 @@ virFileRemove(const char *path,
* *
* Wrapper around readdir. Typical usage: * Wrapper around readdir. Typical usage:
* struct dirent ent; * struct dirent ent;
* int value; * int rc;
* DIR *dir; * DIR *dir;
* if (!(dir = opendir(name))) * if (virDirOpen(&dir, name) < 0)
* goto error; * goto error;
* while ((value = virDirRead(dir, &ent, name)) > 0) * while ((rc = virDirRead(dir, &ent, name)) > 0)
* process ent; * process ent;
* if (value < 0) * if (rc < 0)
* goto error; * goto error;
* *
* Returns -1 on error, with error already reported if @name was * Returns -1 on error, with error already reported if @name was
......
...@@ -230,6 +230,8 @@ enum { ...@@ -230,6 +230,8 @@ enum {
}; };
int virDirCreate(const char *path, mode_t mode, uid_t uid, gid_t gid, int virDirCreate(const char *path, mode_t mode, uid_t uid, gid_t gid,
unsigned int flags) ATTRIBUTE_RETURN_CHECK; unsigned int flags) ATTRIBUTE_RETURN_CHECK;
int virDirOpen(DIR **dirp, const char *dirname)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
int virDirRead(DIR *dirp, struct dirent **ent, const char *dirname) int virDirRead(DIR *dirp, struct dirent **ent, const char *dirname)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
void virDirClose(DIR **dirp) void virDirClose(DIR **dirp)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册