示例#1
0
bool cloud_config_int(const GNode* node, int *i) {
	return cloud_config_int_base(node, i, 10);
}
示例#2
0
static void write_files_item(GNode* node, gpointer data) {
    const GNode* content;
    const GNode* path;
    const GNode* permissions;
    const GNode* owner;
    gchar **tokens;
    guint tokens_size;
    mode_t mode;
    const gchar* username = "";
    const gchar* groupname = "";

    CLOUD_CONFIG_KEY(CONTENT, "content");
    CLOUD_CONFIG_KEY(PATH, "path");
    CLOUD_CONFIG_KEY(OWNER, "owner");
    CLOUD_CONFIG_KEY(PERMISSIONS, "permissions");

    content = cloud_config_find(node, CONTENT);
    if (!content) {
        LOG(MOD "Unable to write file without \"content\" value.\n");
        return;
    }

    path = cloud_config_find(node, PATH);
    if (!path) {
        LOG(MOD "Unable to write file without \"path\" value.\n");
        return;
    }

    permissions = cloud_config_find(node, PERMISSIONS);
    owner = cloud_config_find(node, OWNER);

    /* assure the folder exists, and create if nexessary */
    char* dir = strdup((char *)path->data);
    dir = dirname(dir);
    int r = access(dir, W_OK);
    if (r == -1) {
        if (errno & ENOENT) {
            LOG(MOD "Creating part or all of %s\n", dir);
            gchar command[LINE_MAX];
            command[0] = 0;
            g_snprintf(command, LINE_MAX, "mkdir -p %s", dir);
            exec_task(command);
        } else {
            LOG(MOD "Path error: %s", strerror(errno));
            free(dir);
            return;
        }
    }
    free(dir);

    LOG(MOD "Writing to file %s: %s\n", (char*)path->data, (char*)content->data);

    const int fd = open(path->data, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
    if (fd == -1) {
        LOG(MOD "Cannot open %s.\n", (char*)path->data);
        return;
    }

    write(fd, content->data, strlen(content->data));

    if (permissions) {
        if (cloud_config_int_base(permissions, (int *)&mode, 8)) {
            fchmod(fd, mode);
        }
    }

    close(fd);

    if (owner) {
        tokens = g_strsplit_set(owner->data, ":.", 2);
        tokens_size = g_strv_length(tokens);
        if (tokens_size > 0) {
            username = tokens[0];
            if (tokens_size > 1) {
                groupname = tokens[1];
            }
            chown_path(path->data, username, groupname);
        }
        g_strfreev(tokens);
    }
}
示例#3
0
static void write_files_item(GNode* node, gpointer data) {
    const GNode* content;
    const GNode* path;
    const GNode* permissions;
    const GNode* owner;
    gchar **tokens;
    guint tokens_size;
    mode_t mode;
    const gchar* username = "";
    const gchar* groupname = "";

    CLOUD_CONFIG_KEY(CONTENT, "content");
    CLOUD_CONFIG_KEY(PATH, "path");
    CLOUD_CONFIG_KEY(OWNER, "owner");
    CLOUD_CONFIG_KEY(PERMISSIONS, "permissions");

    content = cloud_config_find(node, CONTENT);
    if (!content) {
        LOG(MOD "Unable to write file without \"content\" value.\n");
        return;
    }

    path = cloud_config_find(node, PATH);
    if (!path) {
        LOG(MOD "Unable to write file without \"path\" value.\n");
        return;
    }

    permissions = cloud_config_find(node, PERMISSIONS);
    owner = cloud_config_find(node, OWNER);

    LOG(MOD "Writing to file %s: %s\n", (char*)path->data, (char*)content->data);

    const int fd = open(path->data, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
    if (fd == -1) {
        LOG(MOD "Cannot open %s.\n", (char*)path->data);
        return;
    }

    write(fd, content->data, strlen(content->data));

    if (permissions) {
        if (cloud_config_int_base(permissions, (int *)&mode, 8)) {
            fchmod(fd, mode);
        }
    }

    close(fd);

    if (owner) {
        tokens = g_strsplit_set(owner->data, ":.", 2);
        tokens_size = g_strv_length(tokens);
        if (tokens_size > 0) {
            username = tokens[0];
            if (tokens_size > 1) {
                groupname = tokens[1];
            }
            chown_path(path->data, username, groupname);
        }
        g_strfreev(tokens);
    }
}