Ejemplo n.º 1
0
static char *
gfs_pio_view_global_read(GFS_File gf, char *buffer, size_t size,
			 size_t *lengthp)
{
	struct gfs_file_global_context *gc = gf->view_context;
	char *e = gfs_pio_view_global_adjust(gf, buffer, &size);
	int length; /* XXX - should be size_t */

	if (e != NULL)
		return (e);
	e = gfs_pio_read(gc->fragment_gf, buffer, size, &length);
	if (e != NULL)
		return (e);
	*lengthp = length;
	return (NULL);
}
Ejemplo n.º 2
0
static gfarm_error_t
gfs_pio_view_global_read(GFS_File gf, char *buffer, size_t size,
			 size_t *lengthp)
{
	struct gfs_file_global_context *gc = gf->view_context;
	gfarm_error_t e = gfs_pio_view_global_adjust(gf, buffer, &size);
	int length; /* XXX - should be size_t */

	if (e != GFARM_ERR_NO_ERROR)
		return (e);
	e = gfs_pio_read(gc->fragment_gf, buffer, size, &length);
	if (e != GFARM_ERR_NO_ERROR)
		return (e);
	*lengthp = length;
	return (GFARM_ERR_NO_ERROR);
}
Ejemplo n.º 3
0
ssize_t
__read(int filedes, void *buf, size_t nbyte)
{
	GFS_File gf;
	char *e;
	int n;

	_gfs_hook_debug_v(fprintf(stderr, "Hooking __read(%d, , %d)\n",
	    filedes, nbyte));

	if ((gf = gfs_hook_is_open(filedes)) == NULL)
		return syscall(SYS_read, filedes, buf, nbyte);

	if (gfs_hook_gfs_file_type(filedes) == GFS_DT_DIR) {
		_gfs_hook_debug(fprintf(stderr,
					"GFS: Hooking __read(%d, , %d)\n",
	    				filedes, nbyte));

		e = GFARM_ERR_IS_A_DIRECTORY;
		goto error;
	}

	_gfs_hook_debug(fprintf(stderr, "GFS: Hooking __read(%d(%d), , %d)\n",
	    filedes, gfs_pio_fileno(gf), nbyte));

	e = gfs_pio_read(gf, buf, nbyte, &n);
	if (e == NULL) {
		_gfs_hook_debug_v(fprintf(stderr,
		    "GFS: Hooking __read --> %d\n", n));
		return (n);
	}
error:

	_gfs_hook_debug(fprintf(stderr, "GFS: __read: %s\n", e));
	errno = gfarm_error_to_errno(e);
	return (-1);
}
Ejemplo n.º 4
0
int webgfarm_api_v1_read_file(request_rec *r) {
    // get filepath from request
    char *filepath = webgfarm_api_v1_getfilepath(r);

    if (filepath == NULL || strlen(filepath) == 0) {
        return HTTP_FORBIDDEN;
    }

    webgfarm_config *wconfig = ap_get_module_config(r->server->module_config, &webgfarm_module);

    gfarm_error_t gerr;
    GFS_File gfs_file;

    if (wconfig->read_redirect) {
        //        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "[read] read_redirect is on.");

        char *redirect_url = apr_palloc(r->pool, sizeof (char)*WEBGFARM_BUFFER_SIZE);

        const char *port;

        int available_nhosts;
        struct gfarm_host_sched_info *available_hosts;
        gerr = gfarm_schedule_hosts_domain_by_file(filepath, GFARM_FILE_RDONLY, "", &available_nhosts, &available_hosts);
        if (gerr == GFARM_ERR_NO_ERROR) {
            //            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "[read] gfarm_schedule_hosts_domain_by_file() has no error.");
            int *ports;
            char **hosts;
            int nhosts = available_nhosts;
            int i;
            GFARM_MALLOC_ARRAY(hosts, available_nhosts);
            GFARM_MALLOC_ARRAY(ports, available_nhosts);
            gerr = gfarm_schedule_hosts_acyclic(filepath, available_nhosts, available_hosts, &nhosts, hosts, ports);

            if (nhosts > 0 && gerr == GFARM_ERR_NO_ERROR) {
                //                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "[read] gfarm_schedule_hosts_acyclic() has no error.");
                for (i = 0; i < nhosts; i++) {
                    port = apr_table_get(wconfig->hosts, hosts[i]);
                    if (port != NULL) {
                        if (strcmp(wconfig->localhost, hosts[i]) != 0) {
                            if (wconfig->ssl) {
                                snprintf(redirect_url, WEBGFARM_BUFFER_SIZE, "https://%s:%s%s", hosts[i], port, r->uri);
                            } else {
                                snprintf(redirect_url, WEBGFARM_BUFFER_SIZE, "http://%s:%s%s", hosts[i], port, r->uri);
                            }
                            apr_table_set(r->headers_out, "Location", redirect_url);
                            free(ports);
                            free(hosts);
                            return HTTP_TEMPORARY_REDIRECT;
                        } else {
                            break;
                        }
                    }
                }
            }

            free(ports);
            free(hosts);
        } else if (gerr != GFARM_ERR_NO_SUCH_FILE_OR_DIRECTORY) {
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "gfarm_schedule_hosts_domain_by_file can not get the hosts of the file(%s) with error: %s", filepath, gfarm_error_string(gerr));
            return HTTP_INTERNAL_SERVER_ERROR;
        }
    }

    // open
    gerr = gfs_pio_open(filepath, GFARM_FILE_RDONLY, &gfs_file);
    if (gerr != GFARM_ERR_NO_ERROR) {
        switch (gerr) {
            case GFARM_ERR_NO_SUCH_FILE_OR_DIRECTORY:
                return HTTP_NOT_FOUND;
                break;
            case GFARM_ERR_PERMISSION_DENIED:
                return HTTP_FORBIDDEN;
                break;
                //FIXEME: Support more error;
            default:
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "gfs_pio_open can not open the file(%s) with error: %s", filepath, gfarm_error_string(gerr));
                return HTTP_INTERNAL_SERVER_ERROR;
        }
    }

    // get stat
    struct gfs_stat gs;
    gerr = gfs_pio_stat(gfs_file, &gs);
    if (gerr != GFARM_ERR_NO_ERROR) {
        gfs_pio_close(gfs_file);
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "gfs_pio_stat can not get the stat of the file(%s) with error: %s", filepath, gfarm_error_string(gerr));
        return HTTP_INTERNAL_SERVER_ERROR;
    }
    char *stat_buffer = apr_palloc(r->pool, sizeof (char)*WEBGFARM_BUFFER_SIZE);
    snprintf(stat_buffer, WEBGFARM_BUFFER_SIZE, "%lu", gs.st_ino);
    apr_table_set(r->headers_out, "X-WEBGFARM-STAT-INO", stat_buffer);
    snprintf(stat_buffer, WEBGFARM_BUFFER_SIZE, "%lu", gs.st_gen);
    apr_table_set(r->headers_out, "X-WEBGFARM-STAT-GEN", stat_buffer);
    snprintf(stat_buffer, WEBGFARM_BUFFER_SIZE, "%o", gs.st_mode);
    apr_table_set(r->headers_out, "X-WEBGFARM-STAT-MODE", stat_buffer);
    snprintf(stat_buffer, WEBGFARM_BUFFER_SIZE, "%lu", gs.st_nlink);
    apr_table_set(r->headers_out, "X-WEBGFARM-STAT-NLINK", stat_buffer);
    apr_table_set(r->headers_out, "X-WEBGFARM-STAT-USER", gs.st_user);
    apr_table_set(r->headers_out, "X-WEBGFARM-STAT-GROUP", gs.st_group);
    snprintf(stat_buffer, WEBGFARM_BUFFER_SIZE, "%lu", gs.st_size);
    apr_table_set(r->headers_out, "X-WEBGFARM-STAT-SIZE", stat_buffer);
    snprintf(stat_buffer, WEBGFARM_BUFFER_SIZE, "%lu", gs.st_ncopy);
    apr_table_set(r->headers_out, "X-WEBGFARM-STAT-NCOPY", stat_buffer);

    //    gerr == GFARM_ERR_NO_ERROR
    //read and puts
    if (!r->header_only) {
        char *buf = apr_palloc(r->pool, sizeof (char)*WEBGFARM_BUFFER_SIZE);
        int read_size = 0;
        do {
            gerr = gfs_pio_read(gfs_file, buf, WEBGFARM_BUFFER_SIZE, &read_size);
            if (gerr != GFARM_ERR_NO_ERROR) {
                gerr = gfs_pio_close(gfs_file);
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "gfs_pio_read can not read the file(%s) with error: %s", filepath, gfarm_error_string(gerr));
                return HTTP_INTERNAL_SERVER_ERROR;
            } else {
                if (read_size != 0) {
                    ap_rwrite(buf, read_size, r);
                }
            }
        } while (gerr == GFARM_ERR_NO_ERROR && read_size != 0);
    }

    // close
    gerr = gfs_pio_close(gfs_file);
    if (gerr != GFARM_ERR_NO_ERROR) {
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "gfs_pio_close can not close the file(%s) with error: %s", filepath, gfarm_error_string(gerr));

        return HTTP_INTERNAL_SERVER_ERROR;
    } else {
        return OK;
    }
}