Selaa lähdekoodia

libdpkg: Add new buffer_skip_Int() and switch fd_null_copy() to it

Add a new buffer_skip() function and the externally visible
buffer_skip_Int(), this way we hide the implementation details
of fd_null_copy().
Guillem Jover 15 vuotta sitten
vanhempi
commit
e8c1a58635
2 muutettua tiedostoa jossa 46 lisäystä ja 9 poistoa
  1. 43 0
      lib/dpkg/buffer.c
  2. 3 9
      lib/dpkg/buffer.h

+ 43 - 0
lib/dpkg/buffer.c

@@ -25,6 +25,7 @@
 
 
 #include <sys/types.h>
 #include <sys/types.h>
 
 
+#include <errno.h>
 #include <string.h>
 #include <string.h>
 #include <unistd.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdlib.h>
@@ -247,3 +248,45 @@ buffer_copy_IntPtr(int Iin, int Tin,
 
 
 	return ret;
 	return ret;
 }
 }
+
+static off_t
+buffer_skip(struct buffer_data *input, off_t limit, const char *desc)
+{
+	struct buffer_data output;
+
+	switch (input->type) {
+	case BUFFER_READ_FD:
+		if (lseek(input->arg.i, limit, SEEK_CUR) != -1)
+			return limit;
+		if (errno != ESPIPE)
+			ohshite(_("failed to seek %s"), desc);
+		break;
+	default:
+		internerr("unknown data type '%i' in buffer_skip\n",
+		          input->type);
+	}
+
+	output.type = BUFFER_WRITE_NULL;
+	output.arg.ptr = NULL;
+
+	return buffer_copy(input, &output, limit, desc);
+}
+
+off_t
+buffer_skip_Int(int I, int T, off_t limit, const char *desc_fmt, ...)
+{
+	va_list args; \
+	struct buffer_data input = { .type = T, .arg.i = I };
+	struct varbuf v = VARBUF_INIT;
+	off_t ret;
+
+	va_start(args, desc_fmt);
+	varbuf_vprintf(&v, desc_fmt, args);
+	va_end(args);
+
+	ret = buffer_skip(&input, limit, v.buf);
+
+	varbuf_destroy(&v);
+
+	return ret;
+}

+ 3 - 9
lib/dpkg/buffer.h

@@ -26,8 +26,6 @@
 
 
 #include <sys/types.h>
 #include <sys/types.h>
 
 
-#include <errno.h>
-
 #include <dpkg/macros.h>
 #include <dpkg/macros.h>
 
 
 DPKG_BEGIN_DECLS
 DPKG_BEGIN_DECLS
@@ -60,13 +58,7 @@ struct buffer_data {
 	buffer_copy_IntPtr(fd, BUFFER_READ_FD, buf, BUFFER_WRITE_VBUF, \
 	buffer_copy_IntPtr(fd, BUFFER_READ_FD, buf, BUFFER_WRITE_VBUF, \
 	                   limit, __VA_ARGS__)
 	                   limit, __VA_ARGS__)
 # define fd_null_copy(fd, limit, ...) \
 # define fd_null_copy(fd, limit, ...) \
-	if (lseek(fd, limit, SEEK_CUR) == -1) { \
-		if (errno != ESPIPE) \
-			ohshite(__VA_ARGS__); \
-		buffer_copy_IntPtr(fd, BUFFER_READ_FD, \
-		                   NULL, BUFFER_WRITE_NULL, \
-		                   limit, __VA_ARGS__); \
-	}
+	buffer_skip_Int(fd, BUFFER_READ_FD, limit, __VA_ARGS__)
 
 
 off_t buffer_copy_IntPtr(int i, int typeIn, void *p, int typeOut,
 off_t buffer_copy_IntPtr(int i, int typeIn, void *p, int typeOut,
                          off_t limit, const char *desc,
                          off_t limit, const char *desc,
@@ -74,6 +66,8 @@ off_t buffer_copy_IntPtr(int i, int typeIn, void *p, int typeOut,
 off_t buffer_copy_IntInt(int i1, int typeIn, int i2, int typeOut,
 off_t buffer_copy_IntInt(int i1, int typeIn, int i2, int typeOut,
                          off_t limit, const char *desc,
                          off_t limit, const char *desc,
                          ...) DPKG_ATTR_PRINTF(6);
                          ...) DPKG_ATTR_PRINTF(6);
+off_t buffer_skip_Int(int I, int T, off_t limit, const char *desc_fmt, ...)
+                      DPKG_ATTR_PRINTF(4);
 off_t buffer_hash(const void *buf, void *hash, int typeOut, off_t length);
 off_t buffer_hash(const void *buf, void *hash, int typeOut, off_t length);
 
 
 DPKG_END_DECLS
 DPKG_END_DECLS