IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 4521


Ignore:
Timestamp:
Jul 8, 2005, 11:55:38 AM (21 years ago)
Author:
jhoblitt
Message:

change nebCopyFile(), nebCopyFilehandle(), & nebParseURI() to have a server param
consistently use nebSetErr()
minor cleanup in nebReplicate()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Nebulous/nebclient/src/nebclient.c

    r4520 r4521  
    44 * Copyright (C) 2005  Joshua Hoblitt
    55 *
    6  * $Id: nebclient.c,v 1.19 2005-07-08 21:32:18 jhoblitt Exp $
     6 * $Id: nebclient.c,v 1.20 2005-07-08 21:55:38 jhoblitt Exp $
    77 */
    88
     
    2727static bool nebSetErr(nebServer *server, const char *format, ...);
    2828static bool nebSetServerErr(nebServer *server);
    29 static off_t nebCopyFile(const char *source, const char *dest);
    30 static off_t nebCopyFilehandle(int sourceFH, int destFH);
    31 static int nebParseURI(const char *URI, char **filename);
     29static off_t nebCopyFile(nebServer *server, const char *source, const char *dest);
     30static off_t nebCopyFilehandle(nebServer *server, int sourceFH, int destFH);
     31static int nebParseURI(nebServer *server, const char *URI, char **filename);
    3232static bool nebNukeFile(const char *filename);
    3333
     
    7676    }
    7777
    78     if (!nebParseURI((char *)response.result, &filename)) {
     78    if (!nebParseURI(server, (char *)response.result, &filename)) {
    7979        nebSetErr(server, "can not parse URI");
    8080
     
    9292    nebFree(filename);
    9393
    94     if (URI) {
    95         *URI = xmalloc(strlen((char *)response.result));
    96         strcpy(*URI, (char *)response.result);
    97     }
    98 
    99     return file;
    100 }
    101 
    102 bool nebReplicate(nebServer *server, const char *key, const char *volume, char **URI)
    103 {
    104     struct ns1__replicate_USCOREobjectResponse response;
    105     char            *filename;
    106     int             sourceFH, destFH;
    107 
    108     if (soap_call_ns1__replicate_USCOREobject(server->soap, NULL, NULL, (char *)key, (char *)volume, (char **)&response) != SOAP_OK) {
    109         // server error
    110         return false;
    111     }
    112 
    113     if (!nebParseURI((char *)response.result, &filename)) {
    114         fprintf(stderr, "can not parse URI\n");
    115 
    116         return false;
    117     }
    118 
    11994    // if URI is not NULL
    12095    if (URI) {
     
    12398    }
    12499
     100    return file;
     101}
     102
     103bool nebReplicate(nebServer *server, const char *key, const char *volume, char **URI)
     104{
     105    struct ns1__replicate_USCOREobjectResponse response;
     106    char            *filename;
     107    int             sourceFH, destFH;
     108
     109    if (soap_call_ns1__replicate_USCOREobject(server->soap, NULL, NULL,
     110            (char *)key, (char *)volume, (char **)&response) != SOAP_OK) {
     111        nebSetServerErr(server);
     112
     113        return false;
     114    }
     115
     116    if (!nebParseURI(server, (char *)response.result, &filename)) {
     117        nebSetErr(server, "can not parse URI");
     118
     119        return false;
     120    }
     121
    125122    // need a read lock
    126123    sourceFH = nebOpen(server, (char *)key, NEB_READ);
    127124    if (sourceFH == -1) {
    128         fprintf(stderr, "can not open key %s\n", key );
     125        nebSetErr(server, "can not open key %s", key);
    129126        nebFree(filename);
    130127
     
    134131    destFH = open(filename, O_RDWR|O_TRUNC, 0660);
    135132    if (destFH == -1) {
    136         fprintf(stderr, "can not open %s: %s\n", filename, strerror(errno));
     133        nebSetErr(server, "can not open %s: %s\n", filename, strerror(errno));
    137134        nebFree(filename);
    138135
     
    140137    }
    141138
    142     nebCopyFilehandle(sourceFH, destFH);
     139    nebCopyFilehandle(server, sourceFH, destFH);
    143140
    144141    if (close(sourceFH) == -1) {
    145         fprintf(stderr, "can not close file: %s", strerror(errno));
     142        nebSetErr(server, "can not close file: %s", strerror(errno));
    146143        nebFree(filename);
    147144
     
    150147
    151148    if (close(destFH) == -1) {
    152         fprintf(stderr, "can not close %s: %s", filename, strerror(errno));
     149        nebSetErr(server, "can not close %s: %s", filename, strerror(errno));
    153150        nebFree(filename);
    154151
     
    158155    nebFree(filename);
    159156
     157    // if URI is not NULL
     158    if (URI) {
     159        *URI = xmalloc(strlen((char *)response.result));
     160        strcpy(*URI, (char *)response.result);
     161    }
     162
    160163    return true;
    161164}
     
    166169
    167170    if (nebFindInstances(server, key, NULL, &locations) < 2) {
    168         fprintf(stderr, "can not cull - not enough instances");
     171        nebSetErr(server, "can not cull - not enough instances");
    169172
    170173        return false;
     
    236239    }
    237240
    238     if (!nebParseURI(locations[0], &filename)) {
     241    if (!nebParseURI(server, locations[0], &filename)) {
    239242        nebSetErr(server, "can not parse URI");
    240243        nebFree(locations);
     
    255258
    256259    if (nebFindInstances(server, key, NULL, &locations) < 0) {
    257         fprintf(stderr, "no instances found\n");
    258 
    259         return -1;
    260     }
    261 
    262     if (!nebParseURI(locations[0], &filename)) {
    263         fprintf(stderr, "can not parse URI\n");
     260        nebSetErr(server, "no instances found");
     261
     262        return -1;
     263    }
     264
     265    if (!nebParseURI(server, locations[0], &filename)) {
     266        nebSetErr(server, "can not parse URI");
    264267
    265268        return -1;
     
    275278
    276279    if (fh < 0) {
    277         perror("open");
     280        nebSetErr(server, "open: %s", strerror(errno));
    278281
    279282        return -1;
     
    331334    }
    332335
    333     nebCopyFilehandle(sourceFH, destFH);
     336    nebCopyFilehandle(server, sourceFH, destFH);
    334337
    335338    if (close(sourceFH) == -1) {
     
    367370    bool            status;
    368371
    369     if (!nebParseURI(URI, &filename)) {
     372    if (!nebParseURI(server, URI, &filename)) {
    370373        fprintf(stderr, "can not parse URI\n");
    371374
     
    416419
    417420        if (err_size < 0) {
    418             fprintf(stderr, "failed to set error message\n");
     421            fprintf(stderr, "failed to set error message");
    419422
    420423            return false;
     
    446449}
    447450
    448 static off_t nebCopyFile(const char *source, const char *dest)
     451static off_t nebCopyFile(nebServer *server, const char *source, const char *dest)
    449452{
    450453    int             sourceFH;
     
    454457    sourceFH = open(source, O_RDONLY);
    455458    if (sourceFH == -1) {
    456         fprintf(stderr, "can not open %s: %s\n", source, strerror(errno));
     459        nebSetErr(server, "can not open %s: %s", source, strerror(errno));
    457460
    458461        return -1;
     
    461464    destFH = open(dest, O_WRONLY|O_CREAT|O_CREAT, 0600);
    462465    if (destFH == -1) {
    463         fprintf(stderr, "can not open %s: %s\n", dest, strerror(errno));
    464 
    465         return -1;
    466     }
    467 
    468     bytesCopied = nebCopyFilehandle(sourceFH, destFH);
     466        nebSetErr(server, "can not open %s: %s", dest, strerror(errno));
     467
     468        return -1;
     469    }
     470
     471    bytesCopied = nebCopyFilehandle(server, sourceFH, destFH);
    469472    if (bytesCopied < 0) {
    470473        return -1;
     
    472475
    473476    if (close(sourceFH) == -1) {
    474         fprintf(stderr, "can not close %s: %s", source, strerror(errno));
     477        nebSetErr(server, "can not close %s: %s", source, strerror(errno));
    475478
    476479        return -1;
     
    478481
    479482    if (close(destFH) == -1) {
    480         fprintf(stderr, "can not close %s: %s", dest, strerror(errno));
     483        nebSetErr(server, "can not close %s: %s", dest, strerror(errno));
    481484
    482485        return -1;
     
    486489}
    487490
    488 static off_t nebCopyFilehandle(int sourceFH, int destFH)
     491static off_t nebCopyFilehandle(nebServer *server, int sourceFH, int destFH)
    489492{
    490493    off_t           bytesTotal;
     
    495498
    496499    if(fstat(sourceFH, &sourceStat)) {
    497         fprintf(stderr, "can not stat filehandles: %s\n", strerror(errno));
     500        nebSetErr(server, "can not stat filehandles: %s", strerror(errno));
    498501
    499502        return -1;
     
    519522
    520523        if (read(sourceFH, writeBuf, writeSize) != writeSize) {
    521             fprintf(stderr, "can not read filehandle: %s", strerror(errno));
     524            nebSetErr(server, "can not read filehandle: %s", strerror(errno));
    522525
    523526            return -1;
     
    525528
    526529        if (write(destFH, writeBuf, writeSize) != writeSize) {
    527             fprintf(stderr, "can not write filehandles: %s", strerror(errno));
     530            nebSetErr(server, "can not write filehandles: %s", strerror(errno));
    528531
    529532            return -1;
     
    536539}
    537540
    538 static int nebParseURI(const char *URI, char **filename)
     541static int nebParseURI(nebServer *server, const char *URI, char **filename)
    539542{
    540543    regex_t         myregex;
     
    553556    if (status != 0) {
    554557        regerror(status, &myregex, errbuf, ERRBUF_SIZE);
    555         fprintf(stderr, "regcomp error: %s\n", errbuf);
     558        nebSetErr(server, "regcomp error: %s", errbuf);
    556559
    557560        return 0;
     
    561564    if (status != 0) {
    562565        regerror(status, &myregex, errbuf, ERRBUF_SIZE);
    563         fprintf(stderr, "regexec error: %s\n", errbuf);
     566        nebSetErr(server, "regexec error: %s", errbuf);
    564567
    565568        return 0;
Note: See TracChangeset for help on using the changeset viewer.