IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 7, 2005, 12:57:40 PM (21 years ago)
Author:
jhoblitt
Message:

make as many char * params const as possible
minor source reorganization

Location:
trunk/Nebulous/nebclient/src
Files:
2 edited

Legend:

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

    r4506 r4507  
    44 * Copyright (C) 2005  Joshua Hoblitt
    55 *
    6  * $Id: nebclient.c,v 1.12 2005-07-07 22:47:40 jhoblitt Exp $
     6 * $Id: nebclient.c,v 1.13 2005-07-07 22:57:40 jhoblitt Exp $
    77 */
    88
     
    2727static bool nebSetErr(nebServer *server, const char *format, ...);
    2828static bool nebSetServerErr(nebServer *server);
    29 static off_t nebCopyFile(char *source, char *dest);
     29static off_t nebCopyFile(const char *source, const char *dest);
    3030static off_t nebCopyFilehandle(int sourceFH, int destFH);
    3131static int nebParseURI(const char *URI, char **filename);
     
    5656}
    5757
    58 int nebCreate(nebServer *server, char *key, unsigned int class, char *volume, char *comment, char **URI)
     58int nebCreate(nebServer *server, const char *key, unsigned int class, const char *volume, const char *comment, char **URI)
    5959{
    6060    struct ns1__create_USCOREobjectResponse response;
     
    6262    int             file;
    6363
    64     if (soap_call_ns1__create_USCOREobject(server->soap, NULL, NULL, key, class, volume, comment, (char **)&response) == SOAP_OK) {
     64    if (soap_call_ns1__create_USCOREobject(server->soap, NULL, NULL, (char *)key, class, (char *)volume, (char *)comment, (char **)&response) == SOAP_OK) {
    6565        *URI = xmalloc(strlen((char *)response.result));
    6666        strcpy(*URI, (char *)response.result);
     
    170170}
    171171
    172 int nebLock(nebServer *server, char *key, rw flag )
     172int nebLock(nebServer *server, const char *key, rw flag)
    173173{
    174174
     
    176176}
    177177
    178 int nebUnlock(nebServer *server, char *key, rw flag )
     178int nebUnlock(nebServer *server, const char *key, rw flag)
    179179{
    180180
     
    182182}
    183183
    184 int nebFindInstances(nebServer *server, char *key, char *volume, char ***locations)
     184int nebFindInstances(nebServer *server, const char *key, const char *volume, char ***locations)
    185185{
    186186    struct ns1__find_USCOREinstancesResponse response;
     
    191191
    192192    // FIXME is this leaking memory when response goes out of scope?
    193     if (soap_call_ns1__find_USCOREinstances(server->soap, NULL, NULL, key, volume, &response) == SOAP_OK) {
     193    if (soap_call_ns1__find_USCOREinstances(server->soap, NULL, NULL, (char *)key, (char *)volume, &response) == SOAP_OK) {
    194194
    195195        resultElements = response.result->__size;
     
    213213}
    214214
    215 int nebFind(nebServer *server, char *key )
     215int nebFind(nebServer *server, const char *key)
    216216{
    217217
     
    283283}
    284284
    285 int nebCopy(nebServer *server, char *key, char *newKey )
     285int nebCopy(nebServer *server, const char *key, const char *newKey)
    286286{
    287287
     
    289289}
    290290
    291 int nebMove(nebServer *server, char *key, char *newKey )
     291int nebMove(nebServer *server, const char *key, const char *newKey)
    292292{
    293293
     
    324324}
    325325
    326 int nebStat(nebServer *server, char *key )
     326int nebStat(nebServer *server, const char *key)
    327327{
    328328
     
    333333{
    334334    return server->err_buf;
     335}
     336
     337void nebFree(void *ptr) {
     338    xfree(ptr);
    335339}
    336340
     
    374378
    375379    return nebSetErr(server, "%s - %s", *code, *string);
     380}
     381
     382static off_t nebCopyFile(const char *source, const char *dest)
     383{
     384    int             sourceFH;
     385    int             destFH;
     386    off_t           bytesCopied;
     387
     388    sourceFH = open(source, O_RDONLY);
     389    if (sourceFH == -1) {
     390        fprintf(stderr, "can not open %s: %s\n", source, strerror(errno));
     391
     392        return -1;
     393    }
     394
     395    destFH = open(dest, O_WRONLY|O_CREAT|O_CREAT, 0600);
     396    if (destFH == -1) {
     397        fprintf(stderr, "can not open %s: %s\n", dest, strerror(errno));
     398
     399        return -1;
     400    }
     401
     402    bytesCopied = nebCopyFilehandle(sourceFH, destFH);
     403    if (bytesCopied < 0) {
     404        return -1;
     405    }
     406
     407    if (close(sourceFH) == -1) {
     408        fprintf(stderr, "can not close %s: %s", source, strerror(errno));
     409
     410        return -1;
     411    }
     412
     413    if (close(destFH) == -1) {
     414        fprintf(stderr, "can not close %s: %s", dest, strerror(errno));
     415
     416        return -1;
     417    }
     418
     419    return bytesCopied;
     420}
     421
     422static off_t nebCopyFilehandle(int sourceFH, int destFH)
     423{
     424    off_t           bytesTotal;
     425    off_t           bytesRemaining;
     426    off_t           writeSize;
     427    char            writeBuf[64 * 1024];
     428    struct stat     sourceStat;
     429
     430    if(fstat(sourceFH, &sourceStat)) {
     431        fprintf(stderr, "can not stat filehandles: %s\n", strerror(errno));
     432
     433        return -1;
     434    }
     435
     436    // the size of the file to copied
     437    bytesTotal = sourceStat.st_size;
     438
     439    /*
     440     * If the file size is <= the buffer size call write(2) with the
     441     * file size.  If the file size is > the buffer call write with the
     442     * buffer size and loop until file size <= buffer size is true.
     443    */
     444
     445    bytesRemaining = bytesTotal;
     446
     447    while (bytesRemaining) {
     448        if (bytesRemaining <= sizeof(writeBuf)) {
     449            writeSize = bytesRemaining;
     450        } else {
     451            writeSize = sizeof(writeBuf);
     452        }
     453
     454        if (read(sourceFH, writeBuf, writeSize) != writeSize) {
     455            fprintf(stderr, "can not read filehandle: %s", strerror(errno));
     456
     457            return -1;
     458        }
     459
     460        if (write(destFH, writeBuf, writeSize) != writeSize) {
     461            fprintf(stderr, "can not write filehandles: %s", strerror(errno));
     462
     463            return -1;
     464        }
     465
     466        bytesRemaining -= writeSize;
     467    }
     468
     469    return bytesTotal;
    376470}
    377471
     
    423517}
    424518
    425 void nebFree(void *ptr) {
    426     xfree(ptr);
    427 }
    428 
    429 static off_t nebCopyFile(char *source, char *dest)
    430 {
    431     int             sourceFH;
    432     int             destFH;
    433     off_t           bytesCopied;
    434 
    435     sourceFH = open(source, O_RDONLY);
    436     if (sourceFH == -1) {
    437         fprintf(stderr, "can not open %s: %s\n", source, strerror(errno));
    438 
    439         return -1;
    440     }
    441 
    442     destFH = open(dest, O_WRONLY|O_CREAT|O_CREAT, 0600);
    443     if (destFH == -1) {
    444         fprintf(stderr, "can not open %s: %s\n", dest, strerror(errno));
    445 
    446         return -1;
    447     }
    448 
    449     bytesCopied = nebCopyFilehandle(sourceFH, destFH);
    450     if (bytesCopied < 0) {
    451         return -1;
    452     }
    453 
    454     if (close(sourceFH) == -1) {
    455         fprintf(stderr, "can not close %s: %s", source, strerror(errno));
    456 
    457         return -1;
    458     }
    459 
    460     if (close(destFH) == -1) {
    461         fprintf(stderr, "can not close %s: %s", dest, strerror(errno));
    462 
    463         return -1;
    464     }
    465 
    466     return bytesCopied;
    467 }
    468 
    469 static off_t nebCopyFilehandle(int sourceFH, int destFH)
    470 {
    471     off_t           bytesTotal;
    472     off_t           bytesRemaining;
    473     off_t           writeSize;
    474     char            writeBuf[64 * 1024];
    475     struct stat     sourceStat;
    476 
    477     if(fstat(sourceFH, &sourceStat)) {
    478         fprintf(stderr, "can not stat filehandles: %s\n", strerror(errno));
    479 
    480         return -1;
    481     }
    482 
    483     // the size of the file to copied
    484     bytesTotal = sourceStat.st_size;
    485 
    486     /*
    487      * If the file size is <= the buffer size call write(2) with the
    488      * file size.  If the file size is > the buffer call write with the
    489      * buffer size and loop until file size <= buffer size is true.
    490     */
    491 
    492     bytesRemaining = bytesTotal;
    493 
    494     while (bytesRemaining) {
    495         if (bytesRemaining <= sizeof(writeBuf)) {
    496             writeSize = bytesRemaining;
    497         } else {
    498             writeSize = sizeof(writeBuf);
    499         }
    500 
    501         if (read(sourceFH, writeBuf, writeSize) != writeSize) {
    502             fprintf(stderr, "can not read filehandle: %s", strerror(errno));
    503 
    504             return -1;
    505         }
    506 
    507         if (write(destFH, writeBuf, writeSize) != writeSize) {
    508             fprintf(stderr, "can not write filehandles: %s", strerror(errno));
    509 
    510             return -1;
    511         }
    512 
    513         bytesRemaining -= writeSize;
    514     }
    515 
    516     return bytesTotal;
    517 }
    518 
    519519static bool nebNukeFile(const char *filename)
    520520{
  • trunk/Nebulous/nebclient/src/nebclient.h

    r4506 r4507  
    44 * Copyright (C) 2005  Joshua Hoblitt
    55 *
    6  * $Id: nebclient.h,v 1.14 2005-07-07 22:47:40 jhoblitt Exp $
     6 * $Id: nebclient.h,v 1.15 2005-07-07 22:57:40 jhoblitt Exp $
    77 */
    88
     
    1414
    1515typedef enum { NEB_READ, NEB_WRITE } rw;
     16
    1617typedef struct {
    1718    struct soap     *soap;
     
    2021}nebServer;
    2122
    22 //void nebServerInit(nebServer *server);
    2323nebServer *nebServerAlloc(void);
    2424
    2525void nebServerCleanup(nebServer *server);
    2626
    27 int nebCreate(nebServer *server, char *key, unsigned int class, char *volume, char *comment, char **URI);
     27int nebCreate(nebServer *server, const char *key, unsigned int class, const char *volume, const char *comment, char **URI);
    2828
    2929bool nebReplicate(nebServer *server, const char *key, const char *volume, char **URI);
     
    3131bool nebCull(nebServer *server, const char *key);
    3232
    33 int nebLock(nebServer *server, char *key, rw flag);
     33int nebLock(nebServer *server, const char *key, rw flag);
    3434
    35 int nebUnlock(nebServer *server, char *key, rw flag);
     35int nebUnlock(nebServer *server, const char *key, rw flag);
    3636
    37 int nebFindInstances(nebServer *server, char *key, char *volume, char ***locations);
     37int nebFindInstances(nebServer *server, const char *key, const char *volume, char ***locations);
    3838
    39 int nebFind(nebServer *server, char *key);
     39int nebFind(nebServer *server, const char *key);
    4040
    4141int nebOpen(nebServer *server, const char *key, rw flag);
     
    4343bool nebDelete(nebServer *server, const char *key);
    4444
    45 int nebCopy(nebServer *server, char *key, char *newKey);
     45int nebCopy(nebServer *server, const char *key, const char *newKey);
    4646
    47 int nebMove(nebServer *server, char *key, char *newKey);
     47int nebMove(nebServer *server, const char *key, const char *newKey);
    4848
    4949bool nebDeleteInstance(nebServer *server, const char *URI);
    5050
    51 int nebStat(nebServer *server, char *key);
     51int nebStat(nebServer *server, const char *key);
    5252
    5353char *nebErr(nebServer *server);
Note: See TracChangeset for help on using the changeset viewer.