From: Andre Noll <maan@tuebingen.mpg.de>
Date: Sun, 26 Apr 2015 16:58:22 +0000 (+0200)
Subject: afs: Introduce flush_and_free_pb().
X-Git-Tag: v0.5.6~94^2~29
X-Git-Url: https://git.tue.mpg.de/?a=commitdiff_plain;h=79bfc7a2a6577000c405be6344ba91ec3f8745e9;p=paraslash.git

afs: Introduce flush_and_free_pb().

This kills some code which is duplicated in all command handlers.
---

diff --git a/afs.c b/afs.c
index eb848089..0b2a4765 100644
--- a/afs.c
+++ b/afs.c
@@ -564,6 +564,20 @@ int afs_cb_result_handler(struct osl_object *result, uint8_t band,
 	return send_sb(&cc->scc, result->data, result->size, band, true);
 }
 
+void flush_and_free_pb(struct para_buffer *pb)
+{
+	int ret;
+	struct afs_max_size_handler_data *amshd = pb->private_data;
+
+	if (pb->buf && pb->size > 0) {
+		ret = pass_buffer_as_shm(amshd->fd, amshd->band, pb->buf,
+			pb->offset);
+		if (ret < 0)
+			PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+	}
+	free(pb->buf);
+}
+
 static void com_select_callback(int fd, const struct osl_object *query)
 {
 	struct para_buffer pb = {
@@ -602,9 +616,7 @@ static void com_select_callback(int fd, const struct osl_object *query)
 			current_mop?  current_mop : "dummy mood",
 			num_admissible);
 out:
-	if (pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-	free(pb.buf);
+	flush_and_free_pb(&pb);
 }
 
 int com_select(struct command_context *cc)
@@ -1022,9 +1034,7 @@ static void create_tables_callback(int fd, const struct osl_object *query)
 out:
 	if (ret < 0)
 		para_printf(&pb, "%s\n", para_strerror(-ret));
-	if (pb.buf)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-	free(pb.buf);
+	flush_and_free_pb(&pb);
 }
 
 int com_init(struct command_context *cc)
diff --git a/afs.h b/afs.h
index cccb143f..feada5b6 100644
--- a/afs.h
+++ b/afs.h
@@ -223,6 +223,7 @@ int send_standard_callback_request(int argc,  char * const * const argv,
 		void *private_result_data);
 int string_compare(const struct osl_object *obj1, const struct osl_object *obj2);
 int for_each_matching_row(struct pattern_match_data *pmd);
+void flush_and_free_pb(struct para_buffer *pb);
 
 /* score */
 void score_init(struct afs_table *t);
diff --git a/aft.c b/aft.c
index 42153d46..16de6763 100644
--- a/aft.c
+++ b/aft.c
@@ -1344,9 +1344,7 @@ static void com_ls_callback(int fd, const struct osl_object *query)
 				goto out;
 		}
 out:
-	if (b.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, b.buf, b.offset);
-	free(b.buf);
+	flush_and_free_pb(&b);
 	free(opts->data);
 	free(opts->data_ptr);
 	free(opts->patterns);
@@ -1739,9 +1737,7 @@ static void com_add_callback(int fd, const struct osl_object *query)
 out:
 	if (ret < 0)
 		para_printf(&msg, "%s\n", para_strerror(-ret));
-	if (msg.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, msg.buf, msg.offset);
-	free(msg.buf);
+	flush_and_free_pb(&msg);
 }
 
 /** Used by com_add(). */
@@ -2042,9 +2038,7 @@ static void com_touch_callback(int fd, const struct osl_object *query)
 		para_printf(&tad.pb, "%s\n", para_strerror(-ret));
 	else if (pmd.num_matches == 0)
 		para_printf(&tad.pb, "no matches\n");
-	if (tad.pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, tad.pb.buf, tad.pb.offset);
-	free(tad.pb.buf);
+	flush_and_free_pb(&tad.pb);
 }
 
 int com_touch(struct command_context *cc)
@@ -2187,9 +2181,7 @@ static void com_rm_callback(int fd, const struct osl_object *query)
 		para_printf(&crd.pb, "no matches -- nothing removed\n");
 	else if (crd.flags & RM_FLAG_VERBOSE)
 		para_printf(&crd.pb, "removed %u files\n", pmd.num_matches);
-	if (ret >= 0 && crd.pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, crd.pb.buf, crd.pb.offset);
-	free(crd.pb.buf);
+	flush_and_free_pb(&crd.pb);
 }
 
 /* TODO options: -r (recursive) */
@@ -2332,9 +2324,7 @@ out:
 				"to %u files\n", source_path, pmd.num_matches);
 	} else
 		para_printf(&cad.pb, "no matches - nothing copied\n");
-	if (cad.pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, cad.pb.buf, cad.pb.offset);
-	free(cad.pb.buf);
+	flush_and_free_pb(&cad.pb);
 }
 
 int com_cpsi(struct command_context *cc)
@@ -2479,9 +2469,7 @@ static void com_setatt_callback(int fd, const struct osl_object *query)
 out:
 	if (ret < 0)
 		para_printf(&cad.pb, "%s\n", para_strerror(-ret));
-	if (cad.pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, cad.pb.buf, cad.pb.offset);
-	free(cad.pb.buf);
+	flush_and_free_pb(&cad.pb);
 }
 
 int com_setatt(struct command_context *cc)
@@ -2582,9 +2570,7 @@ void aft_check_callback(int fd, __a_unused const struct osl_object *query)
 	};
 	para_printf(&pb, "checking audio file table...\n");
 	audio_file_loop(&pb, check_audio_file);
-	if (pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-	free(pb.buf);
+	flush_and_free_pb(&pb);
 }
 
 /**
diff --git a/attribute.c b/attribute.c
index 435f22b5..0c23addf 100644
--- a/attribute.c
+++ b/attribute.c
@@ -178,9 +178,7 @@ static void com_lsatt_callback(int fd, const struct osl_object *query)
 	if (laad.flags & LSATT_FLAG_REVERSE)
 		pmd.pm_flags |= PM_REVERSE_LOOP;
 	for_each_matching_row(&pmd);
-	if (laad.pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, laad.pb.buf, laad.pb.offset);
-	free(laad.pb.buf);
+	flush_and_free_pb(&laad.pb);
 }
 
 int com_lsatt(struct command_context *cc)
@@ -286,9 +284,7 @@ static void com_addatt_callback(int fd, const struct osl_object *query)
 out:
 	if (ret < 0)
 		para_printf(&pb, "%s: %s\n", p, para_strerror(-ret));
-	if (pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-	free(pb.buf);
+	flush_and_free_pb(&pb);
 }
 
 int com_addatt(struct command_context *cc)
@@ -332,9 +328,7 @@ out:
 		para_printf(&pb, "%s\n", para_strerror(-ret));
 	else
 		afs_event(ATTRIBUTE_RENAME, &pb, NULL);
-	if (pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-	free(pb.buf);
+	flush_and_free_pb(&pb);
 }
 
 int com_mvatt(struct command_context *cc)
@@ -412,9 +406,7 @@ static void com_rmatt_callback(int fd, const struct osl_object *query)
 		para_printf(&raad.pb, "%s\n", para_strerror(-ret));
 	else if (!raad.num_removed)
 		para_printf(&raad.pb, "no match -- nothing removed\n");
-	if (raad.pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, raad.pb.buf, raad.pb.offset);
-	free(raad.pb.buf);
+	flush_and_free_pb(&raad.pb);
 }
 
 int com_rmatt(struct command_context *cc)
diff --git a/blob.c b/blob.c
index 4f4a034a..47253b5e 100644
--- a/blob.c
+++ b/blob.c
@@ -166,9 +166,7 @@ static void com_lsblob_callback(struct osl_table *table,
 		para_printf(&lbad.pb, "%s\n", para_strerror(-ret));
 	else if (pmd.num_matches == 0 && pmd.patterns.size > 0)
 		para_printf(&lbad.pb, "no matches\n");
-	if (lbad.pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, lbad.pb.buf, lbad.pb.offset);
-	free(lbad.pb.buf);
+	flush_and_free_pb(&lbad.pb);
 }
 
 static int com_lsblob(callback_function *f, struct command_context *cc)
@@ -297,9 +295,7 @@ static void com_rmblob_callback(struct osl_table *table, int fd,
 		para_printf(&rmbd.pb, "removed %d blobs\n", pmd.num_matches);
 		afs_event(BLOB_RENAME, NULL, table);
 	}
-	if (rmbd.pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, rmbd.pb.buf, rmbd.pb.offset);
-	free(rmbd.pb.buf);
+	flush_and_free_pb(&rmbd.pb);
 }
 
 static int com_rmblob(callback_function *f, struct command_context *cc)
@@ -499,9 +495,7 @@ static void com_mvblob_callback(struct osl_table *table, int fd,
 	}
 	afs_event(BLOB_RENAME, NULL, table);
 out:
-	if (pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-	free(pb.buf);
+	flush_and_free_pb(&pb);
 }
 
 static int com_mvblob(callback_function *f, struct command_context *cc)
diff --git a/mood.c b/mood.c
index cbb2f438..13b5b180 100644
--- a/mood.c
+++ b/mood.c
@@ -447,9 +447,7 @@ void mood_check_callback(int fd, __a_unused const struct osl_object *query)
 	para_printf(&pb, "checking moods...\n");
 	osl_rbtree_loop(moods_table, BLOBCOL_ID, &pb,
 		check_mood);
-	if (pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-	free(pb.buf);
+	flush_and_free_pb(&pb);
 }
 
 static int64_t normalized_value(int64_t x, int64_t n, int64_t sum, int64_t qd)
diff --git a/playlist.c b/playlist.c
index 46333a00..2c5b6777 100644
--- a/playlist.c
+++ b/playlist.c
@@ -140,9 +140,7 @@ void playlist_check_callback(int fd, __a_unused const struct osl_object *query)
 	para_printf(&pb, "checking playlists...\n");
 	osl_rbtree_loop(playlists_table, BLOBCOL_ID, &pb,
 		check_playlist);
-	if (pb.offset)
-		pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-	free(pb.buf);
+	flush_and_free_pb(&pb);
 }
 
 /**