bool workflow_get_update_file()

in src/utils/workflow_utils/src/workflow_utils.c [2173:2275]


bool workflow_get_update_file(ADUC_WorkflowHandle handle, size_t index, ADUC_FileEntity* entity)
{
    if (entity == NULL)
    {
        return false;
    }

    size_t count = workflow_get_update_files_count(handle);
    if (index >= count)
    {
        return false;
    }

    bool succeeded = false;
    const JSON_Object* files = NULL;
    const JSON_Object* file = NULL;
    const JSON_Object* fileUrls = NULL;
    const char* uri = NULL;
    const char* fileId = NULL;
    const char* name = NULL;
    const char* arguments = NULL;
    size_t tempHashCount = 0;
    ADUC_Hash* tempHash = NULL;

    if ((files = _workflow_get_update_manifest_files_map(handle)) == NULL)
    {
        goto done;
    }

    fileId = json_object_get_name(files, index);

    if ((file = json_value_get_object(json_object_get_value_at(files, index))) == NULL)
    {
        goto done;
    }

    // Find fileurls map in this workflow, and its enclosing workflow(s).
    ADUC_WorkflowHandle h = handle;

    do
    {
        if ((fileUrls = _workflow_get_fileurls_map(h)) != NULL)
        {
            uri = json_object_get_string(fileUrls, fileId);
        }
        h = workflow_get_parent(h);
    } while (uri == NULL && h != NULL);

    if (uri == NULL)
    {
        Log_Error("Cannot find URL for fileId '%s'", fileId);
        goto done;
    }

    name = json_object_get_string(file, ADUCITF_FIELDNAME_FILENAME);
    arguments = json_object_get_string(file, ADUCITF_FIELDNAME_ARGUMENTS);

    const JSON_Object* hashObj = json_object_get_object(file, ADUCITF_FIELDNAME_HASHES);

    tempHash = ADUC_HashArray_AllocAndInit(hashObj, &tempHashCount);
    if (tempHash == NULL)
    {
        Log_Error("Unable to parse hashes for file @ %zu", index);
        goto done;
    }

    size_t sizeInBytes = 0;
    if (json_object_has_value(file, ADUCITF_FIELDNAME_SIZEINBYTES))
    {
        sizeInBytes = (size_t)json_object_get_number(file, ADUCITF_FIELDNAME_SIZEINBYTES);
    }

    if (!ADUC_FileEntity_Init(entity, fileId, name, uri, arguments, tempHash, tempHashCount, sizeInBytes))
    {
        Log_Error("Invalid file entity arguments");
        goto done;
    }

    // ADUC_FileEntity_Init makes a deep copy of the hash so must free tempHash to avoid memory leak.
    ADUC_Hash_FreeArray(tempHashCount, tempHash);
    tempHash = NULL;

    if (!ParseFileEntityDownloadHandler(handle, file, entity))
    {
        goto done;
    }

    succeeded = true;

done:
    if (!succeeded)
    {
        entity->Hash = NULL; // Manually free hash array below that is pointed to by tempHash...
        ADUC_FileEntity_Uninit(entity);
    }

    if (tempHash != NULL)
    {
        ADUC_Hash_FreeArray(tempHashCount, tempHash);
    }

    return succeeded;
}