[libvirt] [PATCH 3/6] storage: fs: Don't attempt directory creation if it already exists

Cole Robinson crobinso at redhat.com
Wed Apr 29 01:46:24 UTC 2015


On 04/28/2015 08:19 AM, Peter Krempa wrote:
> On Mon, Apr 27, 2015 at 16:48:41 -0400, Cole Robinson wrote:
>> The current code attempts to handle this, but it only catches mkdir
>> failing with EEXIST. However if say trying to build /tmp for an
>> unprivileged qemu:///session, mkdir will fail with EPERM.
>>
>> Rather than catch any errors, just don't attempt mkdir if the directory
>> already exists.
>> ---
>>  src/util/virfile.c | 13 +++++++------
>>  1 file changed, 7 insertions(+), 6 deletions(-)
>>
>> diff --git a/src/util/virfile.c b/src/util/virfile.c
>> index 87d121d..23a1655 100644
>> --- a/src/util/virfile.c
>> +++ b/src/util/virfile.c
>> @@ -2289,12 +2289,13 @@ virDirCreateNoFork(const char *path,
>>      int ret = 0;
>>      struct stat st;
>>  
>> -    if ((mkdir(path, mode) < 0)
>> -        && !((errno == EEXIST) && (flags & VIR_DIR_CREATE_ALLOW_EXIST))) {
>> -        ret = -errno;
>> -        virReportSystemError(errno, _("failed to create directory '%s'"),
>> -                             path);
>> -        goto error;
>> +    if (!(flags & VIR_DIR_CREATE_ALLOW_EXIST) || !virFileExists(path)) {
> 
> De Morgan's law allows to optimize this into a more readable form:
>         if (!(virFileExists(path) && (flags & VIR_DIR_CREATE_ALLOW_EXIST))) {
> 
> It's one set of parentheses more but it's readable.
> 

Huh, personally I find that form much less readable. You need to mentally
evaluate the parenthesized expression, then remember to take the opposite.

The first one I can read out loud from left to right:

if not ALLOW_EXIST or not file exists:
    stuff

Maybe I've just written too much python :)

Regardless I've applied your suggestion locally, will push after the release.

Thanks,
Cole


>> +        if (mkdir(path, mode) < 0) {
>> +            ret = -errno;
>> +            virReportSystemError(errno, _("failed to create directory '%s'"),
>> +                                 path);
>> +            goto error;
>> +        }
> 
> ACK with the condition made human readable.
> 
> Peter
> 




More information about the libvir-list mailing list