[PATCH 04 of 14 RFC c-hglib:level0] hg_open: additional functions (read_hello and read_header)

Iulian Stana julian.stana at gmail.com
Tue Sep 3 15:30:43 CDT 2013


# HG changeset patch
# User Iulian Stana <julian.stana at gmail.com>
# Date 1378234490 -10800
#      Tue Sep 03 21:54:50 2013 +0300
# Node ID 3b4648c6815ba9e1c46a7e2d1184a72952c6e537
# Parent  806f59433d9ca5e4db5abd79fea365dc89c4c9f5
hg_open: additional functions (read_hello and read_header)

read_hello: reading the hello msg from cmd server
read_header: reading the header from cmd server

It's also an inside function that comes with hg_open function.
Users are not allowed to call this function. I think that it's a protection for
them. This function is called from hg_open function, hg_rawread and hg_rawwrite
functions

diff --git a/client.c b/client.c
--- a/client.c
+++ b/client.c
@@ -10,6 +10,86 @@
 #include <signal.h>
 
 
+/**
+ * \brief Reading the header from cmdsrv.
+ *
+ * The function will read the header from the command server and will save it to
+ * the header parameter of the handle structure.
+ * \param handle The handle of the connection, wherewith I want to communicate
+ * \retval 0 if succesfull 
+ * \retval -1 to indicate an error, with errno set appropriately
+ * 
+ * errno can be:
+ *       - EINVAL  - Invalid argument (handle it's set to a null pointer)
+ *       - read(2) command errors
+ * */
+int read_header(hg_handle *handle)
+{
+	uint32_t length;
+	char ch_char;
+
+	if(!handle) {
+		errno = EINVAL;
+		return -1;
+	}
+	handle->current_header = handle->next_header;
+	if(read(handle->p_read, &ch_char, 1) < 0){
+		return -1;
+	}
+	if(read(handle->p_read, &length, sizeof(uint32_t)) < 0){
+		return -1;
+	}
+	handle->next_header.channel = ch_char;
+	handle->next_header.length = swap_uint32(length);
+
+	return 0;
+}
+
+/**
+ * \brief Reading the hello msg from cmd server.
+ *
+ * After the connection, the command server sends a hello message.
+ * The message contains the command server capabilities and the messages
+ * encoding.
+ * \param handle The handle of the connection, wherewith I want to communicate
+ * \retval  0 if succesfull 
+ * \retval -1 to indicate an error, with errno set appropriately
+ * 
+ * errno can be:
+ *       - EINVAL  - Invalid argument (handle it's set to a null pointer)
+ *       - read(2) command errors
+ * */
+int read_hello(hg_handle *handle)
+{
+	char *buffer;
+	hg_header ch;
+
+	if(!handle) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	read_header(handle);
+	ch = handle->next_header;
+
+	if(ch.length == 0){
+		return -1;
+	}
+
+	buffer = malloc(ch.length + 1);
+
+	if(read(handle->p_read, buffer, ch.length) < 0){
+		free(buffer);
+		return -1;
+	}
+	buffer[ch.length] = '\0';
+
+	free(buffer);
+
+	return 0;
+}
+
+
 /*
  * Open the connection with the mercurial command server.
  * */


More information about the Mercurial-devel mailing list