bool workflow_get_update_file_by_name()

in src/utils/workflow_utils/src/workflow_utils.c [2277:2383]


bool workflow_get_update_file_by_name(ADUC_WorkflowHandle handle, const char* fileName, ADUC_FileEntity* entity)
{
    if (entity == NULL)
    {
        return false;
    }

    size_t count = workflow_get_update_files_count(handle);
    if (count == 0)
    {
        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;
    }

    // Find file by name.
    for (size_t i = 0; i < count; i++)
    {
        if ((file = json_value_get_object(json_object_get_value_at(files, i))) != NULL
            && ADUCPAL_strcasecmp(json_object_get_string(file, "fileName"), fileName) == 0)
        {
            fileId = json_object_get_name(files, i);
            break;
        }
    }

    if (fileId == 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);
    }

    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 fileId", fileId);
        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;
    }

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

    succeeded = true;

done:
    if (!succeeded)
    {
        entity->Hash = NULL; // will be freed with tempHash below
        ADUC_FileEntity_Uninit(entity);

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

    return succeeded;
}