Ejemplo n.º 1
0
void freeResponse(struct Response *resp)
{
    if (resp) {
        freeResponseBuffer(resp->body);
        freeResponseBuffer(resp->header);
        free(resp);
        resp = NULL;
    }
}
Ejemplo n.º 2
0
static int hdfsReadImpl(hdfsFS fs, hdfsFile file, void* buffer, tSize off,
                        tSize length, tSize *numRead)
{
    int ret = 0;
    char *url = NULL;
    struct Response *resp = NULL;

    if (fs == NULL || file == NULL || file->type != INPUT || buffer == NULL ||
            length < 0) {
        ret = EINVAL;
        goto done;
    }
    if (length == 0) {
        // Special case: the user supplied a buffer of zero length, so there is
        // nothing to do.
        *numRead = 0;
        goto done;
    }
    resp = calloc(1, sizeof(*resp)); // resp is actually a pointer type
    if (!resp) {
        ret = ENOMEM;
        goto done;
    }
    ret = initResponseBuffer(&(resp->header));
    if (ret) {
        goto done;
    }
    ret = initResponseBuffer(&(resp->body));
    if (ret) {
        goto done;
    }
    memset(buffer, 0, length);
    resp->body->content = buffer;
    resp->body->remaining = length;
    
    ret = createUrlForOPEN(fs->nn, fs->port, file->file->absPath,
                           fs->userName, off, length, &url);
    if (ret) {
        goto done;
    }
    ret = launchOPEN(url, resp);
    if (ret) {
        goto done;
    }
    ret = parseOPEN(resp->header->content, resp->body->content);
    if (ret == -1) {
        // Special case: if parseOPEN returns -1, we asked for a byte range
        // with outside what the file contains.  In this case, hdfsRead and
        // hdfsPread return 0, meaning end-of-file.
        *numRead = 0;
    } else if (ret == 0) {
        *numRead = (tSize) resp->body->offset;
    }
done:
    if (resp) {
        freeResponseBuffer(resp->header);
        free(resp->body);
    }
    free(resp);
    free(url);
    return ret;
}