Beispiel #1
0
struct sol_str_slice
sol_file_reader_get_all(const struct sol_file_reader *fr)
{
    return (struct sol_str_slice) {
               .len = fr->st.st_size,
               .data = fr->contents,
    };
}

const struct stat *
sol_file_reader_get_stat(const struct sol_file_reader *fr)
{
    return &fr->st;
}

struct sol_blob_file_reader {
    struct sol_blob base;
    struct sol_file_reader *fr;
};

static void
_sol_blob_type_file_reader_close(struct sol_blob *blob)
{
    struct sol_blob_file_reader *b = (struct sol_blob_file_reader *)blob;

    sol_file_reader_close(b->fr);
    free(blob);
}

static const struct sol_blob_type _SOL_BLOB_TYPE_FILE_READER = {
#ifndef SOL_NO_API_VERSION
    .api_version = SOL_BLOB_TYPE_API_VERSION,
    .sub_api = 1,
#endif
    .free = _sol_blob_type_file_reader_close
};

SOL_API struct sol_blob *
sol_file_reader_to_blob(struct sol_file_reader *fr)
{
    struct sol_blob_file_reader *b;
    struct sol_str_slice c = sol_file_reader_get_all(fr);

    b = calloc(1, sizeof(struct sol_blob_file_reader));
    SOL_NULL_CHECK_GOTO(b, error);

    sol_blob_setup(&b->base, &_SOL_BLOB_TYPE_FILE_READER, c.data, c.len);
    b->fr = fr;
    return &b->base;

error:
    sol_file_reader_close(fr);
    return NULL;
}
Beispiel #2
0
static void
file_reader_blob_free(struct sol_blob *blob)
{
    struct sol_file_reader *reader = blob->mem;

    sol_file_reader_close(reader);
    free(blob);
}
Beispiel #3
0
static void
close_files(struct runner *r)
{
    struct sol_file_reader *fr;
    uint16_t i;

    SOL_PTR_VECTOR_FOREACH_IDX (&r->file_readers, fr, i)
        sol_file_reader_close(fr);
    sol_ptr_vector_clear(&r->file_readers);
}
Beispiel #4
0
static int
hostname_start(const struct sol_platform_linux_micro_module *mod, const char *service)
{
    struct sol_file_reader *reader;
    struct sol_str_slice str;
    const char *s, *p, *end;
    int err = 0;

    reader = sol_file_reader_open("/etc/hostname");
    SOL_NULL_CHECK_MSG(reader, -errno, "could not read /etc/hostname");

    str = sol_file_reader_get_all(reader);
    s = p = str.data;
    end = s + str.len;

    for (; s < end; s++) {
        if (!isblank(*s))
            break;
    }

    for (p = end - 1; p > s; p--) {
        if (!isblank(*p))
            break;
    }

    if (s >= p) {
        SOL_WRN("no hostname in /etc/hostname");
        err = -ENOENT;
    } else if (sethostname(s, p - s) < 0) {
        SOL_WRN("could not set hostname: %s", sol_util_strerrora(errno));
        err = -errno;
    }

    sol_file_reader_close(reader);

    if (err == 0)
        sol_platform_linux_micro_inform_service_state(service, SOL_PLATFORM_SERVICE_STATE_ACTIVE);
    else
        sol_platform_linux_micro_inform_service_state(service, SOL_PLATFORM_SERVICE_STATE_FAILED);

    return err;
}
Beispiel #5
0
static int
read_file(void *data, const char *name, const char **buf, size_t *size)
{
    struct runner *r = data;
    struct sol_file_reader *fr = NULL;
    struct sol_str_slice slice;
    char *path;
    int err;

    err = asprintf(&path, "%s/%s", r->dirname, name);
    if (err < 0) {
        err = -ENOMEM;
        goto error;
    }

    fr = sol_file_reader_open(path);
    if (!fr) {
        err = -errno;
        SOL_ERR("Couldn't open input file '%s': %s", path, sol_util_strerrora(errno));
        goto error;
    }

    err = sol_ptr_vector_append(&r->file_readers, fr);
    if (err < 0)
        goto error;

    free(path);
    slice = sol_file_reader_get_all(fr);
    *buf = slice.data;
    *size = slice.len;
    return 0;

error:
    free(path);
    if (fr)
        sol_file_reader_close(fr);
    return err;
}
Beispiel #6
0
static int
file_reader_load(struct file_reader_data *mdata)
{
    struct sol_file_reader *reader;
    struct sol_str_slice slice;

    if (!mdata->path)
        return 0;

    reader = sol_file_reader_open(mdata->path);
    if (!reader) {
        sol_flow_send_error_packet(mdata->node, errno,
                                   "Could not load \"%s\": %s", mdata->path, sol_util_strerrora(errno));
        return -errno;
    }
    slice = sol_file_reader_get_all(reader);

    SOL_DBG("loaded path=\"%s\", data=%p, len=%zd", mdata->path, slice.data, slice.len);
    mdata->reader_blob = sol_blob_new(&file_reader_blob_type, NULL,
                                      reader, sizeof(reader));
    SOL_NULL_CHECK_GOTO(mdata->reader_blob, err_reader);

    mdata->content_blob = sol_blob_new(SOL_BLOB_TYPE_NOFREE,
                                       mdata->reader_blob,
                                       slice.data, slice.len);
    SOL_NULL_CHECK_GOTO(mdata->content_blob, err_content);

    return sol_flow_send_blob_packet(mdata->node,
                                     SOL_FLOW_NODE_TYPE_FILE_READER__OUT__OUT,
                                     mdata->content_blob);

err_content:
    sol_blob_unref(mdata->reader_blob);
err_reader:
    sol_file_reader_close(reader);
    return -ENOMEM;
}