Пример #1
0
int webgfarm_api_v1_read_dir(request_rec *r) {
    char *filepath = webgfarm_api_v1_getfilepath(r);
    if (filepath == NULL || strlen(filepath) == 0) {
        return HTTP_FORBIDDEN;
    }

    gfarm_error_t gerr;
    GFS_DirPlus gfs_dirplus;
    // open dir
    gerr = gfs_opendirplus(filepath, &gfs_dirplus);
    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:
                return HTTP_INTERNAL_SERVER_ERROR;
        }
    }

    // read dir
    struct gfs_dirent *dent;
    struct gfs_stat *st;

    gerr = gfs_readdirplus(gfs_dirplus, &dent, &st);
    while (dent != NULL) {
        if (gerr != GFARM_ERR_NO_ERROR) {
            gerr = gfs_closedirplus(gfs_dirplus);
            return HTTP_INTERNAL_SERVER_ERROR;
        }
        if (dent->d_type == GFS_DT_REG) {
            ap_rprintf(r, "%s\n", dent->d_name);
        } else if (dent->d_type == GFS_DT_DIR) {
            ap_rprintf(r, "%s/\n", dent->d_name);
        }

        gerr = gfs_readdirplus(gfs_dirplus, &dent, &st);
    }

    // close dir
    gerr = gfs_closedirplus(gfs_dirplus);
    if (gerr != GFARM_ERR_NO_ERROR) {
        return HTTP_INTERNAL_SERVER_ERROR;
    } else {
        return OK;
    }
}
Пример #2
0
static gfarm_error_t
gfs_readdir_caching_internal(GFS_Dir super, struct gfs_dirent **entryp)
{
	struct gfs_dir_caching *dir = (struct gfs_dir_caching *)super;
	struct gfs_dirent *ep;
	struct gfs_stat *stp;
	char *path;
	gfarm_error_t e = gfs_readdirplus(dir->dp, &ep, &stp);

	if (e != GFARM_ERR_NO_ERROR)
		return (e);

	if (ep != NULL) { /* i.e. not EOF */
		GFARM_MALLOC_ARRAY(path,
		    strlen(dir->path) + strlen(ep->d_name) + 1);
		if (path == NULL) {
			/*
			 * It's ok to fail in entering the cache,
			 * since it's merely cache.
			 */
			gflog_warning(GFARM_MSG_UNUSED,
			    "dircache: failed to cache %s%s due to no memory",
			    dir->path, ep->d_name);
		} else {
			struct timeval now;

			gettimeofday(&now, NULL);
			sprintf(path, "%s%s", dir->path, ep->d_name);
#ifdef DIRCACHE_DEBUG
			gflog_debug(GFARM_MSG_1000094,
			    "%ld.%06ld: gfs_readdir_caching()->"
			    "\"%s\" (%d)",
			    (long)now.tv_sec, (long)now.tv_usec,
			    path, stat_cache_count);
#endif
			/*
			 * It's ok to fail in entering the cache,
			 * since it's merely cache.
			 */
			if ((e = gfs_stat_cache_enter_internal(path, stp, &now))
			    != GFARM_ERR_NO_ERROR) {
				gflog_warning(GFARM_MSG_UNUSED,
				    "dircache: failed to cache %s: %s",
				    path, gfarm_error_string(e));
			}
			free(path);
		}
	}

	*entryp = ep;
	return (GFARM_ERR_NO_ERROR);
}