From 199f9b0a79de0fb0fd57ce9307b36390339ee7e7 Mon Sep 17 00:00:00 2001 From: Picnoir Date: Fri, 5 Apr 2024 11:33:17 +0200 Subject: feat(tvix/nix-compat): read client setting from wire Add the primitives necessary to read the client settings from the Nix daemon wire protocol. Introducing the read_string primitive. This trivial primitive parses a read_bytes call, check the bytes are valid utf-8 bytes and wraps the result in a String. Change-Id: Ie1253523a6bd4e31e7924e9898a0898109da2fa0 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11358 Reviewed-by: flokli Tested-by: BuildkiteCI --- tvix/nix-compat/src/wire/bytes.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'tvix/nix-compat/src/wire/bytes.rs') diff --git a/tvix/nix-compat/src/wire/bytes.rs b/tvix/nix-compat/src/wire/bytes.rs index 1a25daca8b4f..070f2368fdc3 100644 --- a/tvix/nix-compat/src/wire/bytes.rs +++ b/tvix/nix-compat/src/wire/bytes.rs @@ -1,4 +1,7 @@ -use std::ops::RangeBounds; +use std::{ + io::{Error, ErrorKind}, + ops::RangeBounds, +}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; @@ -58,6 +61,21 @@ where Ok(buf) } +/// Read a Nix daemon string from the AsyncWrite, encoded as utf8. +/// Rejects reading more than `allowed_size` bytes +/// +/// A Nix daemon string is made up of two distincts parts: +/// 1. Its lenght, LE-encoded on 64 bits. +/// 2. Its content. 0-padded on 64 bits. +pub async fn read_string(r: &mut R, allowed_size: S) -> std::io::Result +where + R: AsyncReadExt + Unpin, + S: RangeBounds, +{ + let bytes = read_bytes(r, allowed_size).await?; + String::from_utf8(bytes).map_err(|e| Error::new(ErrorKind::InvalidData, e)) +} + /// Writes a sequence of sized bits to a (hopefully buffered) /// [AsyncWriteExt] handle. /// -- cgit 1.4.1