Index: /trunk/Nebulous/nebclient/src/nebclient.c
===================================================================
--- /trunk/Nebulous/nebclient/src/nebclient.c	(revision 4520)
+++ /trunk/Nebulous/nebclient/src/nebclient.c	(revision 4521)
@@ -4,5 +4,5 @@
  * Copyright (C) 2005  Joshua Hoblitt
  *
- * $Id: nebclient.c,v 1.19 2005-07-08 21:32:18 jhoblitt Exp $
+ * $Id: nebclient.c,v 1.20 2005-07-08 21:55:38 jhoblitt Exp $
  */
 
@@ -27,7 +27,7 @@
 static bool nebSetErr(nebServer *server, const char *format, ...);
 static bool nebSetServerErr(nebServer *server);
-static off_t nebCopyFile(const char *source, const char *dest);
-static off_t nebCopyFilehandle(int sourceFH, int destFH);
-static int nebParseURI(const char *URI, char **filename);
+static off_t nebCopyFile(nebServer *server, const char *source, const char *dest);
+static off_t nebCopyFilehandle(nebServer *server, int sourceFH, int destFH);
+static int nebParseURI(nebServer *server, const char *URI, char **filename);
 static bool nebNukeFile(const char *filename);
 
@@ -76,5 +76,5 @@
     }
 
-    if (!nebParseURI((char *)response.result, &filename)) {
+    if (!nebParseURI(server, (char *)response.result, &filename)) {
         nebSetErr(server, "can not parse URI");
 
@@ -92,29 +92,4 @@
     nebFree(filename);
 
-    if (URI) {
-        *URI = xmalloc(strlen((char *)response.result));
-        strcpy(*URI, (char *)response.result);
-    }
-
-    return file;
-}
-
-bool nebReplicate(nebServer *server, const char *key, const char *volume, char **URI)
-{
-    struct ns1__replicate_USCOREobjectResponse response;
-    char            *filename;
-    int             sourceFH, destFH;
-
-    if (soap_call_ns1__replicate_USCOREobject(server->soap, NULL, NULL, (char *)key, (char *)volume, (char **)&response) != SOAP_OK) {
-        // server error
-        return false;
-    }
-
-    if (!nebParseURI((char *)response.result, &filename)) {
-        fprintf(stderr, "can not parse URI\n");
-
-        return false;
-    }
-
     // if URI is not NULL
     if (URI) {
@@ -123,8 +98,30 @@
     }
 
+    return file;
+}
+
+bool nebReplicate(nebServer *server, const char *key, const char *volume, char **URI)
+{
+    struct ns1__replicate_USCOREobjectResponse response;
+    char            *filename;
+    int             sourceFH, destFH;
+
+    if (soap_call_ns1__replicate_USCOREobject(server->soap, NULL, NULL,
+            (char *)key, (char *)volume, (char **)&response) != SOAP_OK) {
+        nebSetServerErr(server);
+
+        return false;
+    }
+
+    if (!nebParseURI(server, (char *)response.result, &filename)) {
+        nebSetErr(server, "can not parse URI");
+
+        return false;
+    }
+
     // need a read lock
     sourceFH = nebOpen(server, (char *)key, NEB_READ);
     if (sourceFH == -1) {
-        fprintf(stderr, "can not open key %s\n", key );
+        nebSetErr(server, "can not open key %s", key);
         nebFree(filename);
 
@@ -134,5 +131,5 @@
     destFH = open(filename, O_RDWR|O_TRUNC, 0660);
     if (destFH == -1) {
-        fprintf(stderr, "can not open %s: %s\n", filename, strerror(errno));
+        nebSetErr(server, "can not open %s: %s\n", filename, strerror(errno));
         nebFree(filename);
 
@@ -140,8 +137,8 @@
     }
 
-    nebCopyFilehandle(sourceFH, destFH);
+    nebCopyFilehandle(server, sourceFH, destFH);
 
     if (close(sourceFH) == -1) {
-        fprintf(stderr, "can not close file: %s", strerror(errno));
+        nebSetErr(server, "can not close file: %s", strerror(errno));
         nebFree(filename);
 
@@ -150,5 +147,5 @@
 
     if (close(destFH) == -1) {
-        fprintf(stderr, "can not close %s: %s", filename, strerror(errno));
+        nebSetErr(server, "can not close %s: %s", filename, strerror(errno));
         nebFree(filename);
 
@@ -158,4 +155,10 @@
     nebFree(filename);
 
+    // if URI is not NULL
+    if (URI) {
+        *URI = xmalloc(strlen((char *)response.result));
+        strcpy(*URI, (char *)response.result);
+    }
+
     return true;
 }
@@ -166,5 +169,5 @@
 
     if (nebFindInstances(server, key, NULL, &locations) < 2) {
-        fprintf(stderr, "can not cull - not enough instances");
+        nebSetErr(server, "can not cull - not enough instances");
 
         return false;
@@ -236,5 +239,5 @@
     }
 
-    if (!nebParseURI(locations[0], &filename)) {
+    if (!nebParseURI(server, locations[0], &filename)) {
         nebSetErr(server, "can not parse URI");
         nebFree(locations);
@@ -255,11 +258,11 @@
 
     if (nebFindInstances(server, key, NULL, &locations) < 0) {
-        fprintf(stderr, "no instances found\n");
-
-        return -1;
-    }
-
-    if (!nebParseURI(locations[0], &filename)) {
-        fprintf(stderr, "can not parse URI\n");
+        nebSetErr(server, "no instances found");
+
+        return -1;
+    }
+
+    if (!nebParseURI(server, locations[0], &filename)) {
+        nebSetErr(server, "can not parse URI");
 
         return -1;
@@ -275,5 +278,5 @@
 
     if (fh < 0) {
-        perror("open");
+        nebSetErr(server, "open: %s", strerror(errno));
 
         return -1;
@@ -331,5 +334,5 @@
     }
 
-    nebCopyFilehandle(sourceFH, destFH);
+    nebCopyFilehandle(server, sourceFH, destFH);
 
     if (close(sourceFH) == -1) {
@@ -367,5 +370,5 @@
     bool            status;
 
-    if (!nebParseURI(URI, &filename)) {
+    if (!nebParseURI(server, URI, &filename)) {
         fprintf(stderr, "can not parse URI\n");
 
@@ -416,5 +419,5 @@
 
         if (err_size < 0) {
-            fprintf(stderr, "failed to set error message\n");
+            fprintf(stderr, "failed to set error message");
 
             return false;
@@ -446,5 +449,5 @@
 }
 
-static off_t nebCopyFile(const char *source, const char *dest)
+static off_t nebCopyFile(nebServer *server, const char *source, const char *dest)
 {
     int             sourceFH;
@@ -454,5 +457,5 @@
     sourceFH = open(source, O_RDONLY);
     if (sourceFH == -1) {
-        fprintf(stderr, "can not open %s: %s\n", source, strerror(errno));
+        nebSetErr(server, "can not open %s: %s", source, strerror(errno));
 
         return -1;
@@ -461,10 +464,10 @@
     destFH = open(dest, O_WRONLY|O_CREAT|O_CREAT, 0600);
     if (destFH == -1) {
-        fprintf(stderr, "can not open %s: %s\n", dest, strerror(errno));
-
-        return -1;
-    }
-
-    bytesCopied = nebCopyFilehandle(sourceFH, destFH);
+        nebSetErr(server, "can not open %s: %s", dest, strerror(errno));
+
+        return -1;
+    }
+
+    bytesCopied = nebCopyFilehandle(server, sourceFH, destFH);
     if (bytesCopied < 0) {
         return -1;
@@ -472,5 +475,5 @@
 
     if (close(sourceFH) == -1) {
-        fprintf(stderr, "can not close %s: %s", source, strerror(errno));
+        nebSetErr(server, "can not close %s: %s", source, strerror(errno));
 
         return -1;
@@ -478,5 +481,5 @@
 
     if (close(destFH) == -1) {
-        fprintf(stderr, "can not close %s: %s", dest, strerror(errno));
+        nebSetErr(server, "can not close %s: %s", dest, strerror(errno));
 
         return -1;
@@ -486,5 +489,5 @@
 }
 
-static off_t nebCopyFilehandle(int sourceFH, int destFH)
+static off_t nebCopyFilehandle(nebServer *server, int sourceFH, int destFH)
 {
     off_t           bytesTotal;
@@ -495,5 +498,5 @@
 
     if(fstat(sourceFH, &sourceStat)) {
-        fprintf(stderr, "can not stat filehandles: %s\n", strerror(errno));
+        nebSetErr(server, "can not stat filehandles: %s", strerror(errno));
 
         return -1;
@@ -519,5 +522,5 @@
 
         if (read(sourceFH, writeBuf, writeSize) != writeSize) {
-            fprintf(stderr, "can not read filehandle: %s", strerror(errno));
+            nebSetErr(server, "can not read filehandle: %s", strerror(errno));
 
             return -1;
@@ -525,5 +528,5 @@
 
         if (write(destFH, writeBuf, writeSize) != writeSize) {
-            fprintf(stderr, "can not write filehandles: %s", strerror(errno));
+            nebSetErr(server, "can not write filehandles: %s", strerror(errno));
 
             return -1;
@@ -536,5 +539,5 @@
 }
 
-static int nebParseURI(const char *URI, char **filename)
+static int nebParseURI(nebServer *server, const char *URI, char **filename)
 {
     regex_t         myregex;
@@ -553,5 +556,5 @@
     if (status != 0) {
         regerror(status, &myregex, errbuf, ERRBUF_SIZE);
-        fprintf(stderr, "regcomp error: %s\n", errbuf);
+        nebSetErr(server, "regcomp error: %s", errbuf);
 
         return 0;
@@ -561,5 +564,5 @@
     if (status != 0) {
         regerror(status, &myregex, errbuf, ERRBUF_SIZE);
-        fprintf(stderr, "regexec error: %s\n", errbuf);
+        nebSetErr(server, "regexec error: %s", errbuf);
 
         return 0;
