Patchwork [08,of,14,RFC,c-hglib:level0] hg_rawread: reading some unparse data from command server

login
register
mail settings
Submitter Iulian Stana
Date Sept. 3, 2013, 8:30 p.m.
Message ID <41b2f0094c3a6257d4f1.1378240247@doppler>
Download mbox | patch
Permalink /patch/2313/
State Deferred, archived
Headers show

Comments

Iulian Stana - Sept. 3, 2013, 8:30 p.m.
# HG changeset patch
# User Iulian Stana <julian.stana@gmail.com>
# Date 1378235539 -10800
#      Tue Sep 03 22:12:19 2013 +0300
# Node ID 41b2f0094c3a6257d4f18cbeb9962ba743c1aff6
# Parent  a2462b15fca8923a230647f906de40f6ebd585f0
hg_rawread: reading some unparse data from command server
Giovanni Gherdovich - Sept. 12, 2013, 4:49 a.m.
:::: # HG changeset patch
:::: # User Iulian Stana <julian.stana at gmail.com>
:::: # Date 1378235539 -10800
:::: #      Tue Sep 03 22:12:19 2013 +0300
:::: # Node ID 41b2f0094c3a6257d4f18cbeb9962ba743c1aff6
:::: # Parent  a2462b15fca8923a230647f906de40f6ebd585f0
:::: hg_rawread: reading some unparse data from command server
::::
:::: diff --git a/client.c b/client.c
:::: --- a/client.c
:::: +++ b/client.c
:::: @@ -269,4 +269,35 @@
::::   return 0;
::::  }
::::
:::: +/*
:::: + * Reading some unparse data from the server.
:::: + * */
:::: +int hg_rawread(hg_handle *handle, char *buffer, size_t sizebuff)
:::: +{
:::: + int length = handle->next_header.length;
:::: + if(!handle) {
:::: + errno = EINVAL;
:::: + return -1;
:::: + }
::::
:::: + /* If the current channel is not an input channel return 0. */
:::: + if(!(hg_next_channel(handle) == 'o' || hg_next_channel(handle) == 'e'))
:::: + return 0;
:::: +
:::: + length = (length > sizebuff)? sizebuff : length;


Here you make clear that you are not gonna read more than
the size of the packet, but...


:::: +
:::: + if(read(handle->p_read, buffer, length) < 0){
:::: + return -1;
:::: + }
:::: +
:::: + buffer[length] = '\0';
:::: + handle->next_header.length -= length;
:::: +
:::: + if(!handle->next_header.length){
:::: + if(read_header(handle) < 0){


... if you actually reach the end of the packet, you read
the header of packet N+1. Contradictory and confusing. (see comments
on read_header())


:::: + return -1;
:::: + }
:::: + }
:::: +
:::: + return length;
:::: +}
:::: diff --git a/client.h b/client.h
:::: --- a/client.h
:::: +++ b/client.h
:::: @@ -101,4 +101,21 @@
::::   * */
::::  int hg_rawcommand(hg_handle *handle, char *const command[]);
::::
:::: +/**
:::: + * \brief Reading some unparse data from the server.
:::: + *
:::: + * Will read just a 'line', the header that is received from
server and the
:::: + * data that comes after the header
:::: + * \param handle The handle of the connection, wherewith I want
to communicate
:::: + * \param buffer A character array where the read content will be stored.
:::: + * \param sizebuff The number of bytes to read.
:::: + * \retval Number  the number of bytes that were read.
:::: + * \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 hg_rawread(hg_handle *handle, char *buffer, size_t sizebuff);
:::: +
::::  #endif

Patch

diff --git a/client.c b/client.c
--- a/client.c
+++ b/client.c
@@ -269,4 +269,35 @@ 
 	return 0;
 }
 
+/*
+ * Reading some unparse data from the server.
+ * */
+int hg_rawread(hg_handle *handle, char *buffer, size_t sizebuff)
+{
+	int length = handle->next_header.length;
+	if(!handle) {
+		errno = EINVAL;
+		return -1;
+	}
 
+	/* If the current channel is not an input channel return 0. */
+	if(!(hg_next_channel(handle) == 'o' || hg_next_channel(handle) == 'e'))
+		return 0;
+
+	length = (length > sizebuff)? sizebuff : length;
+
+	if(read(handle->p_read, buffer, length) < 0){
+		return -1;
+	}
+
+	buffer[length] = '\0';
+	handle->next_header.length -= length;
+
+	if(!handle->next_header.length){
+		if(read_header(handle) < 0){
+			return -1;
+		}
+	}
+
+	return length;
+}
diff --git a/client.h b/client.h
--- a/client.h
+++ b/client.h
@@ -101,4 +101,21 @@ 
  * */
 int hg_rawcommand(hg_handle *handle, char *const command[]);
 
+/**
+ * \brief Reading some unparse data from the server. 
+ *
+ * Will read just a 'line', the header that is received from server and the 
+ * data that comes after the header
+ * \param handle The handle of the connection, wherewith I want to communicate
+ * \param buffer A character array where the read content will be stored.
+ * \param sizebuff The number of bytes to read.
+ * \retval Number  the number of bytes that were read.
+ * \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 hg_rawread(hg_handle *handle, char *buffer, size_t sizebuff);
+
 #endif