bool ADUC_FileEntity_Init()

in src/utils/parser_utils/src/parser_utils.c [153:236]


bool ADUC_FileEntity_Init(
    ADUC_FileEntity* fileEntity,
    const char* fileId,
    const char* targetFileName,
    const char* downloadUri,
    const char* arguments,
    ADUC_Hash* hashArray,
    size_t hashCount,
    size_t sizeInBytes)
{
    bool success = false;
    ADUC_Hash* tempHashArray = NULL;

    if (fileEntity == NULL)
    {
        return false;
    }

    // Note: downloadUri could be empty when the agent resuming 'install' or 'apply' action.
    if (fileId == NULL || targetFileName == NULL || hashArray == NULL)
    {
        return false;
    }

    memset(fileEntity, 0, sizeof(*fileEntity));

    if (mallocAndStrcpy_s(&(fileEntity->FileId), fileId) != 0)
    {
        goto done;
    }

    if (mallocAndStrcpy_s(&(fileEntity->TargetFilename), targetFileName) != 0)
    {
        goto done;
    }

    if (downloadUri == NULL)
    {
        fileEntity->DownloadUri = NULL;
    }
    else if (mallocAndStrcpy_s(&(fileEntity->DownloadUri), downloadUri) != 0)
    {
        goto done;
    }

    if (arguments != NULL && mallocAndStrcpy_s(&(fileEntity->Arguments), arguments) != 0)
    {
        goto done;
    }

    // Make a deep copy of hashArray
    tempHashArray = (ADUC_Hash*)calloc(hashCount, sizeof(*hashArray));
    if (tempHashArray == NULL)
    {
        goto done;
    }

    for (size_t i = 0; i < hashCount; ++i)
    {
        if (!ADUC_Hash_Init(&tempHashArray[i], hashArray[i].value, hashArray[i].type))
        {
            goto done;
        }
    }

    fileEntity->Hash = tempHashArray;
    tempHashArray = NULL;

    fileEntity->HashCount = hashCount;

    fileEntity->SizeInBytes = sizeInBytes;

    success = true;

done:
    ADUC_Hash_FreeArray(hashCount, tempHashArray);

    if (!success)
    {
        ADUC_FileEntity_Uninit(fileEntity);
    }

    return success;
}