--- conduits/backup/backup_conduit.c~	2003-03-18 10:12:25.000000000 -0500
+++ conduits/backup/backup_conduit.c	2005-06-26 18:21:51.000000000 -0400
@@ -54,7 +54,7 @@
 typedef struct file_db {
         gchar fname[256];
 	struct DBInfo info;
-        gint maxblock;
+        size_t maxblock;
 	gint entries;
 } file_db;
 
@@ -377,7 +377,7 @@
 					gpointer _cfg)
 {
 	char *name;
-	struct pi_file *f;
+	struct pi_file *f = NULL;
 	struct stat statb;
 	struct utimbuf times;
 	GList *iterator;
@@ -387,9 +387,10 @@
 	int keep_reading;
 	int entries;
 	int err;
-	unsigned char *buffer;
 	PilotRecord remote;
 	int wrote;
+	pi_buffer_t *piBuf = NULL;
+	int len;
 
 	g_return_val_if_fail (conduit != NULL, -1);
 	g_return_val_if_fail (dbinfo != NULL, -1);
@@ -473,21 +474,14 @@
 		goto db_close;
 	}		
 
-	buffer = g_new0 (unsigned char, 0xffff);
-	remote.buffer = buffer;
-
-	{
-		int l;
-		l = dlp_ReadAppBlock (dbinfo->pilot_socket,
-				      dbinfo->db_handle,
-				      0,
-				      buffer,
-				      0xffff);
-		if (l>0) {
-			pi_file_set_app_info (f, buffer, l);
-		}
-	}
-	
+	piBuf = pi_buffer_new (0xffff);
+	len = dlp_ReadAppBlock (dbinfo->pilot_socket,
+				dbinfo->db_handle,
+				0,
+				-1,
+				piBuf);
+	if (len > 0)
+		pi_file_set_app_info (f, piBuf->data, len);
 
 	index = 0;
 	keep_reading = 1;
@@ -500,19 +494,18 @@
 			keep_reading = (dlp_ReadResourceByIndex (dbinfo->pilot_socket, 
 								 dbinfo->db_handle,
 								 index, 
-								 remote.buffer,
+								 piBuf,
 								 &type,
-								 &id,
-								 &remote.len) >= 0);
+								 &id) >= 0);
 #ifdef DEBUG_REC_IO
 			g_message ("read resource %d, type = %s, size %d, , index %d/%d",
-				   id, pi_unmktag (type), remote.len,
+				   id, pi_unmktag (type), piBuf->used,
 				   index, entries);
 #endif
-			if (keep_reading) {				
-				err = pi_file_append_resource (f, 
-							       remote.buffer, 
-							       remote.len, 
+			if (keep_reading > 0) {
+				err = pi_file_append_resource (f,
+							       piBuf->data,
+							       piBuf->used,
 							       type,
 							       id);
 				if (err < 0) {
@@ -521,7 +514,7 @@
 					wrote++;
 #ifdef DEBUG_REC_IO
 					g_message ("write resource %d, type = %s, size %d, index %d/%d",
-						   id, pi_unmktag (type), remote.len,
+						   id, pi_unmktag (type), piBuf->used,
 						   index, entries);
 #endif
 				}
@@ -530,22 +523,21 @@
 			keep_reading = (dlp_ReadRecordByIndex (dbinfo->pilot_socket, 
 							       dbinfo->db_handle,
 							       index, 
-							       remote.buffer,
+							       piBuf,
 							       &remote.recID,
-							       &remote.len,
 							       &remote.flags,
 							       &remote.catID) >= 0);
 
 #ifdef DEBUG_REC_IO
 			g_message ("read record %d, size %d, index %d/%d",
-				   remote.recID, remote.len,
+				   remote.recID, piBuf->used,
 				   index, entries);
 #endif
 
-			if (keep_reading) {
+			if (keep_reading > 0) {
 				err = pi_file_append_record (f, 
-							     remote.buffer, 
-							     remote.len, 
+							     piBuf->data, 
+							     piBuf->used, 
 							     remote.flags,
 							     remote.catID,
 							     remote.recID);
@@ -555,7 +547,7 @@
 					wrote++;
 #ifdef DEBUG_REC_IO
 					g_message ("write record %d, size %d, index %d/%d",
-						   id, remote.len,
+						   id, piBuf->used,
 						   index, entries);
 #endif
 				}
@@ -567,17 +559,17 @@
 		gnome_pilot_conduit_send_progress (GNOME_PILOT_CONDUIT (conduit),
 						   entries,
 						   index);
-	} while (keep_reading && index < entries);
+	} while ((keep_reading > 0) && (index < entries));
 
-	g_free (buffer);
-		
 	if (pi_file_close (f) < 0) {
 		g_warning("backup conduit can't write file");
 		gnome_pilot_conduit_send_error(GNOME_PILOT_CONDUIT(conduit),
 					       "Unable to write file to backup directory specified");
 		result = -1;
+		f = NULL;
 		goto db_close;
 	}
+	f = NULL;
 	
 	g_message ("Wrote %d of %d %s, which is %s",
 		   wrote, entries, PI_DBINFO (dbinfo)->flags & dlpDBFlagResource ? "resources" : "records",
@@ -589,7 +581,9 @@
  db_close:
 	dlp_CloseDB (dbinfo->pilot_socket, dbinfo->db_handle);
  exit_and_free:
+	pi_file_close (f);
 	g_free (name);
+	pi_buffer_free (piBuf);
  exit:	
 	return result;	
 }
@@ -621,10 +615,10 @@
 {
 	DIR *dir;
 	struct dirent *dirent;
-	GnomePilotDBInfo info;
 	file_db **db;
 	int dbcount = 0;
-	int i, j, max, size;
+	int i, j, max;
+	size_t size;
 	struct pi_file *file;
 	char *source;
 	int result = 0;
@@ -679,7 +673,7 @@
 
 		/* Find biggest record */
 		for (i = 0; i < db[dbcount]->entries; i++) {
-			if (PI_DBINFO (&info)->flags & dlpDBFlagResource)
+			if (db[dbcount]->info.flags & dlpDBFlagResource)
 				pi_file_read_resource(file, i, 0, &size, 0, 0);
 			else
 				pi_file_read_record(file, i, 0, &size, 0, 0,0 );
