[libvirt] PATCH: Support storage copy on write volumes

Jim Meyering jim at meyering.net
Fri Jan 23 11:27:41 UTC 2009


Miloslav Trmač <mitr at redhat.com> wrote:

> Jim Meyering píše v Čt 22. 01. 2009 v 19:15 +0100:
>> > +static int
>> > +cowGetBackingStore(virConnectPtr conn,
>> > +                   char **res,
>> > +                   const unsigned char *buf,
>> > +                   size_t buf_size)
>> > +{
>> > +    size_t len;
>> > +
>> > +    *res = NULL;
>> > +    if (buf_size < 4+4+1024)
>> > +        return BACKING_STORE_INVALID;
>> > +    if (buf[4+4] == '\0') /* cow_header_v2.backing_file[0] */
>> > +        return BACKING_STORE_OK;
>> > +
>> > +    len = 1024;
>> > +    if (VIR_ALLOC_N(*res, len + 1) < 0) {
>> > +        virStorageReportError(conn, VIR_ERR_NO_MEMORY, _("backing store path"));
>> > +        return BACKING_STORE_ERROR;
>> > +    }
>> > +    memcpy(*res, buf + 4+4, len); /* cow_header_v2.backing_file */
>> > +    (*res)[len] = '\0';
>> > +    if (VIR_REALLOC_N(*res, strlen(*res) + 1) < 0) {
>>
>> Is this just-copied 1024-byte block of data guaranteed
>> not to contain any NUL bytes?  Or maybe you just want that
>> NUL-terminated string?

> The 1024 bytes should contain a NUL-terminated string.  This code
> ensures there is a trailing NUL, then resizes *res to only contain the
> first NUL-terminated string.  We could avoid the realloc() by using
> strnlen() from gnulib.

Hi Mirek,

Thanks for clarifying the intent.
In that case, we can just use strndup:

        *res = strndup (buf + 4+4, len);
        if (*res == NULL) {
            virStorageReportError(conn, VIR_ERR_NO_MEMORY,
                                  _("backing store path"));
            return BACKING_STORE_ERROR;
        }




More information about the libvir-list mailing list