[PATCH 3/8] tftp: split out allocation and cache initialization

Enrico Scholz enrico.scholz at sigma-chemnitz.de
Sun Aug 28 07:02:26 PDT 2022


Refactors existing code in a new function.

It also updates 'priv->state' now in error state.

Signed-off-by: Enrico Scholz <enrico.scholz at sigma-chemnitz.de>
---
 fs/tftp.c | 46 +++++++++++++++++++++++++++++++---------------
 1 file changed, 31 insertions(+), 15 deletions(-)

diff --git a/fs/tftp.c b/fs/tftp.c
index aaeb19590e93..610483d23c40 100644
--- a/fs/tftp.c
+++ b/fs/tftp.c
@@ -619,6 +619,31 @@ static void tftp_handle_data(struct file_priv *priv, uint16_t block,
 	}
 }
 
+static int tftp_allocate_transfer(struct file_priv *priv)
+{
+	debug_assert(!priv->fifo);
+
+	/* multiplication is safe; both operands were checked in tftp_parse_oack()
+	   and are small integers */
+	priv->fifo = kfifo_alloc(priv->blocksize * priv->windowsize);
+	if (!priv->fifo)
+		return -ENOMEM;
+
+	if (priv->push) {
+		priv->buf = xmalloc(priv->blocksize);
+		if (!priv->buf) {
+			kfifo_free(priv->fifo);
+			priv->fifo = NULL;
+			return -ENOMEM;
+		}
+	} else {
+		tftp_window_cache_init(&priv->cache,
+				       priv->blocksize, priv->windowsize);
+	}
+
+	return 0;
+}
+
 static void tftp_recv(struct file_priv *priv,
 			uint8_t *pkt, unsigned len, uint16_t uh_sport)
 {
@@ -723,22 +748,13 @@ static void tftp_handler(void *ctx, char *packet, unsigned len)
 
 static int tftp_start_transfer(struct file_priv *priv)
 {
-	/* multiplication is safe; both operands where checked in tftp_parse_oack()
-	   and are small integers */
-	priv->fifo = kfifo_alloc(priv->blocksize * priv->windowsize);
-	if (!priv->fifo)
-		return -ENOMEM;
+	int rc;
 
-	if (priv->push) {
-		priv->buf = xmalloc(priv->blocksize);
-		if (!priv->buf) {
-			kfifo_free(priv->fifo);
-			priv->fifo = NULL;
-			return -ENOMEM;
-		}
-	} else {
-		tftp_window_cache_init(&priv->cache,
-				       priv->blocksize, priv->windowsize);
+	rc = tftp_allocate_transfer(priv);
+	if (rc < 0) {
+		priv->err = rc;
+		priv->state = STATE_DONE;
+		return rc;
 	}
 
 	if (priv->push) {
-- 
2.37.2




More information about the barebox mailing list