On 04/10/2014 02:52 PM, Natanael Copa wrote:
>> I suppose we could use helper function to make it more
readable:
>>
> int virReaddir(DIR *dirp, struct dirent **ent)
> {
> errno = 0;
> *ent = readdir(dirp);
> if (!*ent && errno) {
> virReportSystemError(errno, _("unable to read directory"))
> return -1;
> }
> return *ent ? 1 : 0;
or shorter, return !!*ent;
> }
>
> and used as:
>
> while ((ret = virReaddir(dirp, &entry)) > 0) {
> process entry
> }
> if (ret < 0)
> goto error;
This looks better yes.
Should I prepare a new patch with this? And grep for more readdirs?
Sure; and while at it, update cfg.mk to add a new syntax check to
enforce this style. We've wrapped other awkward standard functions that
require errno manipulation for correct use (such as strtol and
getpwuid_r), precisely because code is more maintainable when we can
enforce that the awkward functions are always used correctly.
Food for thought: depending on how many clients you find, it may be
worth a version of virReaddir() that takes a flag argument, for
auto-filtering '.' and '..' entries.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library
http://libvirt.org