[libvirt] [PATCH] nodeinfo: Make sure we always reset errno before calling readdir
Eric Blake
eblake at redhat.com
Thu Apr 10 21:53:57 UTC 2014
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 604 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20140410/f94db9c3/attachment-0001.sig>
More information about the libvir-list
mailing list