diff mbox series

[alsa-utils,05/14] axfer: open file descriptor outside of container module

Message ID 20210311052146.404003-6-o-takashi@sakamocchi.jp
State New
Headers show
Series axfer: reduce test time | expand

Commit Message

Takashi Sakamoto March 11, 2021, 5:21 a.m. UTC
Internal container module operates file descriptor to media file. For
this purpose, the structure has fd member and any file operation is done
internally. However, the case to use special file descriptor such as
memfd requires to maintain file descriptor externally.

This commit opens file descriptor outside of container module. The
internal APIs to initialize container get an argument for the file
descriptor instead of file path.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
---
 axfer/container.c           | 35 ++++++-----------------------------
 axfer/container.h           |  9 ++++-----
 axfer/subcmd-transfer.c     | 26 ++++++++++++++++++++++----
 axfer/test/container-test.c | 12 ++++++++++--
 axfer/test/mapper-test.c    | 19 ++++++++++++++-----
 5 files changed, 56 insertions(+), 45 deletions(-)
diff mbox series

Patch

diff --git a/axfer/container.c b/axfer/container.c
index b4646b9..255f12c 100644
--- a/axfer/container.c
+++ b/axfer/container.c
@@ -143,23 +143,21 @@  static int set_nonblock_flag(int fd)
 	return 0;
 }
 
-int container_parser_init(struct container_context *cntr,
-			  const char *const path, unsigned int verbose)
+int container_parser_init(struct container_context *cntr, int fd,
+			  unsigned int verbose)
 {
 	const struct container_parser *parsers[] = {
 		[CONTAINER_FORMAT_RIFF_WAVE] = &container_parser_riff_wave,
 		[CONTAINER_FORMAT_AU] = &container_parser_au,
 		[CONTAINER_FORMAT_VOC] = &container_parser_voc,
 	};
-	int fd;
 	const struct container_parser *parser;
 	unsigned int size;
 	int i;
 	int err;
 
 	assert(cntr);
-	assert(path);
-	assert(path[0] != '\0');
+	assert(fd >= 0);
 
 	// Detect forgotten to destruct.
 	assert(cntr->fd == 0);
@@ -167,14 +165,6 @@  int container_parser_init(struct container_context *cntr,
 
 	memset(cntr, 0, sizeof(*cntr));
 
-	// Open a target descriptor.
-	if (!strcmp(path, "-")) {
-		fd = fileno(stdin);
-	} else {
-		fd = open(path, O_RDONLY);
-		if (fd < 0)
-			return -errno;
-	}
 	cntr->fd = fd;
 
 	cntr->stdio = (cntr->fd == fileno(stdin));
@@ -231,9 +221,8 @@  int container_parser_init(struct container_context *cntr,
 	return 0;
 }
 
-int container_builder_init(struct container_context *cntr,
-			   const char *const path, enum container_format format,
-			   unsigned int verbose)
+int container_builder_init(struct container_context *cntr, int fd,
+			   enum container_format format, unsigned int verbose)
 {
 	const struct container_builder *builders[] = {
 		[CONTAINER_FORMAT_RIFF_WAVE] = &container_builder_riff_wave,
@@ -241,13 +230,11 @@  int container_builder_init(struct container_context *cntr,
 		[CONTAINER_FORMAT_VOC] = &container_builder_voc,
 		[CONTAINER_FORMAT_RAW] = &container_builder_raw,
 	};
-	int fd;
 	const struct container_builder *builder;
 	int err;
 
 	assert(cntr);
-	assert(path);
-	assert(path[0] != '\0');
+	assert(fd >= 0);
 
 	// Detect forgotten to destruct.
 	assert(cntr->fd == 0);
@@ -255,16 +242,6 @@  int container_builder_init(struct container_context *cntr,
 
 	memset(cntr, 0, sizeof(*cntr));
 
-	// Open a target descriptor.
-	if (path == NULL || *path == '\0')
-		return -EINVAL;
-	if (!strcmp(path, "-")) {
-		fd = fileno(stdout);
-	} else {
-		fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
-		if (fd < 0)
-			return -errno;
-	}
 	cntr->fd = fd;
 
 	cntr->stdio = (cntr->fd == fileno(stdout));
diff --git a/axfer/container.h b/axfer/container.h
index cb64816..0840369 100644
--- a/axfer/container.h
+++ b/axfer/container.h
@@ -61,11 +61,10 @@  struct container_context {
 
 const char *const container_suffix_from_format(enum container_format format);
 enum container_format container_format_from_path(const char *path);
-int container_parser_init(struct container_context *cntr,
-			  const char *const path, unsigned int verbose);
-int container_builder_init(struct container_context *cntr,
-			   const char *const path, enum container_format format,
-			   unsigned int verbose);
+int container_parser_init(struct container_context *cntr, int fd,
+			  unsigned int verbose);
+int container_builder_init(struct container_context *cntr, int fd,
+			   enum container_format format, unsigned int verbose);
 void container_context_destroy(struct container_context *cntr);
 int container_context_pre_process(struct container_context *cntr,
 				  snd_pcm_format_t *format,
diff --git a/axfer/subcmd-transfer.c b/axfer/subcmd-transfer.c
index 6962208..52c32d5 100644
--- a/axfer/subcmd-transfer.c
+++ b/axfer/subcmd-transfer.c
@@ -185,10 +185,19 @@  static int capture_pre_process(struct context *ctx, snd_pcm_access_t *access,
 
 	*total_frame_count = 0;
 	for (i = 0; i < ctx->cntr_count; ++i) {
+		const char *path = ctx->xfer.paths[i];
+		int fd;
 		uint64_t frame_count;
 
-		err = container_builder_init(ctx->cntrs + i,
-					     ctx->xfer.paths[i],
+		if (!strcmp(path, "-")) {
+			fd = fileno(stdout);
+		} else {
+			fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
+			if (fd < 0)
+				return -errno;
+		}
+
+		err = container_builder_init(ctx->cntrs + i, fd,
 					     ctx->xfer.cntr_format,
 					     ctx->xfer.verbose > 1);
 		if (err < 0)
@@ -226,13 +235,22 @@  static int playback_pre_process(struct context *ctx, snd_pcm_access_t *access,
 		return err;
 
 	for (i = 0; i < ctx->cntr_count; ++i) {
+		const char *path = ctx->xfer.paths[i];
+		int fd;
 		snd_pcm_format_t format;
 		unsigned int channels;
 		unsigned int rate;
 		uint64_t frame_count;
 
-		err = container_parser_init(ctx->cntrs + i,
-					    ctx->xfer.paths[i],
+		if (!strcmp(path, "-")) {
+			fd = fileno(stdin);
+		} else {
+			fd = open(path, O_RDONLY);
+			if (fd < 0)
+				return -errno;
+		}
+
+		err = container_parser_init(ctx->cntrs + i, fd,
 					    ctx->xfer.verbose > 1);
 		if (err < 0)
 			return err;
diff --git a/axfer/test/container-test.c b/axfer/test/container-test.c
index 9b30ae3..fbef3a4 100644
--- a/axfer/test/container-test.c
+++ b/axfer/test/container-test.c
@@ -33,6 +33,7 @@  static void test_builder(struct container_context *cntr,
 			 void *frame_buffer, unsigned int frame_count,
 			 bool verbose)
 {
+	int fd;
 	snd_pcm_format_t sample;
 	unsigned int channels;
 	unsigned int rate;
@@ -41,7 +42,10 @@  static void test_builder(struct container_context *cntr,
 	uint64_t total_frame_count;
 	int err;
 
-	err = container_builder_init(cntr, name, format, verbose);
+	fd = open(name, O_RDWR | O_CREAT | O_TRUNC, 0644);
+	assert(fd >= 0);
+
+	err = container_builder_init(cntr, fd, format, verbose);
 	assert(err == 0);
 
 	sample = sample_format;
@@ -79,6 +83,7 @@  static void test_parser(struct container_context *cntr,
 		        void *frame_buffer, unsigned int frame_count,
 			bool verbose)
 {
+	int fd;
 	snd_pcm_format_t sample;
 	unsigned int channels;
 	unsigned int rate;
@@ -86,7 +91,10 @@  static void test_parser(struct container_context *cntr,
 	unsigned int handled_frame_count;
 	int err;
 
-	err = container_parser_init(cntr, name, verbose);
+	fd = open(name, O_RDONLY);
+	assert(fd >= 0);
+
+	err = container_parser_init(cntr, fd, verbose);
 	assert(err == 0);
 
 	sample = sample_format;
diff --git a/axfer/test/mapper-test.c b/axfer/test/mapper-test.c
index f0376c7..6252900 100644
--- a/axfer/test/mapper-test.c
+++ b/axfer/test/mapper-test.c
@@ -66,7 +66,6 @@  static int test_demux(struct mapper_trial *trial, snd_pcm_access_t access,
 		      unsigned int cntr_count)
 {
 	struct container_context *cntrs = trial->cntrs;
-	char **paths = trial->paths;
 	enum container_format cntr_format = trial->cntr_format;
 	unsigned int bytes_per_sample;
 	uint64_t total_frame_count;
@@ -74,12 +73,17 @@  static int test_demux(struct mapper_trial *trial, snd_pcm_access_t access,
 	int err = 0;
 
 	for (i = 0; i < cntr_count; ++i) {
+		const char *path = trial->paths[i];
+		int fd;
 		snd_pcm_format_t format;
 		unsigned int channels;
 		unsigned int rate;
 
-		err = container_builder_init(cntrs + i, paths[i], cntr_format,
-					     0);
+		fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
+		if (fd < 0)
+			return -errno;
+
+		err = container_builder_init(cntrs + i, fd, cntr_format, 0);
 		if (err < 0)
 			goto end;
 
@@ -159,18 +163,23 @@  static int test_mux(struct mapper_trial *trial, snd_pcm_access_t access,
 		    unsigned int cntr_count)
 {
 	struct container_context *cntrs = trial->cntrs;
-	char **paths = trial->paths;
 	unsigned int bytes_per_sample;
 	uint64_t total_frame_count;
 	int i;
 	int err = 0;
 
 	for (i = 0; i < cntr_count; ++i) {
+		const char *path = trial->paths[i];
+		int fd;
 		snd_pcm_format_t format;
 		unsigned int channels;
 		unsigned int rate;
 
-		err = container_parser_init(cntrs + i, paths[i], 0);
+		fd = open(path, O_RDONLY);
+		if (fd < 0)
+			return -errno;
+
+		err = container_parser_init(cntrs + i, fd, 0);
 		if (err < 0)
 			goto end;