bake-copy-to.c 4.39 KB
Newer Older
1 2 3 4 5 6
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

7 8
#include "bake-config.h"

9
#include <stdio.h>
10
#include <stdlib.h>
11 12
#include <assert.h>
#include <unistd.h>
Philip Carns's avatar
Philip Carns committed
13 14 15 16
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
17

18
#include "bake-client.h"
19

20
/* client program that will copy a POSIX file into a BAKE region */
21 22 23

int main(int argc, char **argv) 
{
24 25 26 27 28
    int i;
    char cli_addr_prefix[64] = {0};
    char *svr_addr_str;
    hg_addr_t svr_addr;
    margo_instance_id mid;
29
    bake_target_id_t bti;
30 31
    hg_return_t hret;
    int ret;
32
    bake_region_id_t rid;
Philip Carns's avatar
Philip Carns committed
33 34 35
    int fd;
    struct stat statbuf;
    char* local_region;
36 37
    int region_fd;
    char region_file[128];
38
    uint64_t  check_size;
39
 
Philip Carns's avatar
Philip Carns committed
40
    if(argc != 3)
41
    {
42 43
        fprintf(stderr, "Usage: bake-copy-to <local file> <server addr>\n");
        fprintf(stderr, "  Example: ./bake-copy-to /tmp/foo.dat tcp://localhost:1234\n");
44
        return(-1);
45 46
    }
    svr_addr_str = argv[2];
47

Philip Carns's avatar
Philip Carns committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    fd = open(argv[1], O_RDONLY);
    if(fd < 0)
    {
        perror("open");
        return(-1);
    }
    ret = fstat(fd, &statbuf);
    if(ret < 0)
    {
        perror("fstat");
        close(fd);
        return(-1);
    }

    local_region = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
63
    if(local_region == MAP_FAILED)
Philip Carns's avatar
Philip Carns committed
64 65 66 67 68 69
    {
        perror("mmap");
        close(fd);
        return(-1);
    }

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    /* initialize Margo using the transport portion of the server
     * address (i.e., the part before the first : character if present)
     */
    for(i=0; (i<63 && svr_addr_str[i] != '\0' && svr_addr_str[i] != ':'); i++)
        cli_addr_prefix[i] = svr_addr_str[i];

    mid = margo_init(cli_addr_prefix, MARGO_CLIENT_MODE, 0, -1);
    if(mid == MARGO_INSTANCE_NULL)
    {
        fprintf(stderr, "Error: margo_init()\n");
        munmap(local_region, statbuf.st_size);
        close(fd);
        return -1;
    }

    hret = margo_addr_lookup(mid, svr_addr_str, &svr_addr);
    if(hret != HG_SUCCESS)
    {
        fprintf(stderr, "Error: margo_addr_lookup()\n");
        margo_finalize(mid);
        munmap(local_region, statbuf.st_size);
        close(fd);
        return(-1);
    }

    ret = bake_probe_instance(mid, svr_addr, &bti);
96 97
    if(ret < 0)
    {
98 99
        margo_addr_free(mid, svr_addr);
        margo_finalize(mid);
Philip Carns's avatar
Philip Carns committed
100 101
        munmap(local_region, statbuf.st_size);
        close(fd);
102 103 104 105
        fprintf(stderr, "Error: bake_probe_instance()\n");
        return(-1);
    }

Philip Carns's avatar
Philip Carns committed
106
    /* create region */
107
    ret = bake_create(bti, statbuf.st_size, &rid);
Philip Carns's avatar
Philip Carns committed
108
    if(ret != 0)
109
    {
Philip Carns's avatar
Philip Carns committed
110
        bake_release_instance(bti);
111 112
        margo_addr_free(mid, svr_addr);
        margo_finalize(mid);
Philip Carns's avatar
Philip Carns committed
113 114
        munmap(local_region, statbuf.st_size);
        close(fd);
115
        fprintf(stderr, "Error: bake_create()\n");
116 117 118
        return(-1);
    }

Philip Carns's avatar
Philip Carns committed
119
    /* transfer data */
120
    ret = bake_write(
Philip Carns's avatar
Philip Carns committed
121 122 123 124 125 126 127 128
        bti,
        rid,
        0,
        local_region,
        statbuf.st_size);
    if(ret != 0)
    {
        bake_release_instance(bti);
129 130
        margo_addr_free(mid, svr_addr);
        margo_finalize(mid);
Philip Carns's avatar
Philip Carns committed
131 132
        munmap(local_region, statbuf.st_size);
        close(fd);
133
        fprintf(stderr, "Error: bake_write()\n");
Philip Carns's avatar
Philip Carns committed
134 135
        return(-1);
    }
Philip Carns's avatar
Philip Carns committed
136

Philip Carns's avatar
Philip Carns committed
137 138
    munmap(local_region, statbuf.st_size);
    close(fd);
Philip Carns's avatar
Philip Carns committed
139

140
    ret = bake_persist(bti, rid);
Philip Carns's avatar
Philip Carns committed
141 142 143
    if(ret != 0)
    {
        bake_release_instance(bti);
144 145
        margo_addr_free(mid, svr_addr);
        margo_finalize(mid);
146
        fprintf(stderr, "Error: bake_persist()\n");
Philip Carns's avatar
Philip Carns committed
147 148
        return(-1);
    }
149

150
    /* safety check size */
151
    ret = bake_get_size(bti, rid, &check_size);
152 153 154
    if(ret != 0)
    {
        bake_release_instance(bti);
155 156
        margo_addr_free(mid, svr_addr);
        margo_finalize(mid);
157
        fprintf(stderr, "Error: bake_get_size()\n");
158 159 160 161
        return(-1);
    }
    
    bake_release_instance(bti);
162 163
    margo_addr_free(mid, svr_addr);
    margo_finalize(mid);
164 165 166 167 168 169 170

    if(check_size != statbuf.st_size)
    {
        fprintf(stderr, "Error: size mismatch!\n");
        return(-1);
    }

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    sprintf(region_file, "/tmp/bb-copy-rid.XXXXXX");
    region_fd = mkstemp(region_file);
    if(region_fd < 0)
    {
        perror("mkstemp");
    }
    else
    {
        ret = write(region_fd, &rid, sizeof(rid));
        if(ret < 0)
        {
            perror("write");
        }
        else
        {
            printf("RID written to %s\n", region_file);
187
            close(region_fd);
188 189
        }
    }
Philip Carns's avatar
Philip Carns committed
190
   
191 192 193
    return(0);
}