Guillem Jover лет назад: 15
Родитель
Сommit
0053d6ef57
4 измененных файлов с 116 добавлено и 0 удалено
  1. 2 0
      lib/dpkg/Makefile.am
  2. 77 0
      lib/dpkg/fdio.c
  3. 35 0
      lib/dpkg/fdio.h
  4. 2 0
      lib/dpkg/libdpkg.Versions

+ 2 - 0
lib/dpkg/Makefile.am

@@ -33,6 +33,7 @@ libdpkg_a_SOURCES = \
 	dir.c \
 	dump.c \
 	ehandle.c \
+	fdio.c \
 	file.c \
 	fields.c \
 	glob.c \
@@ -72,6 +73,7 @@ pkginclude_HEADERS = \
 	dpkg.h \
 	dpkg-db.h \
 	ehandle.h \
+	fdio.h \
 	file.h \
 	glob.h \
 	macros.h \

+ 77 - 0
lib/dpkg/fdio.c

@@ -0,0 +1,77 @@
+/*
+ * libdpkg - main program for package management
+ * fdio.c - safe file descriptor based input/output
+ *
+ * Copyright © 2009-2010 Guillem Jover <guillem@debian.org>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <errno.h>
+#include <unistd.h>
+
+#include <dpkg/fdio.h>
+
+ssize_t
+fd_read(int fd, void *buf, size_t len)
+{
+	ssize_t total = 0;
+	char *ptr = buf;
+
+	while (len > 0) {
+		ssize_t n;
+
+		n = read(fd, ptr + total, len);
+		if (n == -1) {
+			if (errno == EINTR || errno == EAGAIN)
+				continue;
+			return total ? -total : n;
+		}
+		if (n == 0)
+			break;
+
+		total += n;
+		len -= n;
+	}
+
+	return total;
+}
+
+ssize_t
+fd_write(int fd, const void *buf, size_t len)
+{
+	ssize_t total = 0;
+	const char *ptr = buf;
+
+	while (len > 0) {
+		ssize_t n;
+
+		n = write(fd, ptr + total, len);
+		if (n == -1) {
+			if (errno == EINTR || errno == EAGAIN)
+				continue;
+			return total ? -total : n;
+		}
+		if (n == 0)
+			break;
+
+		total += n;
+		len -= n;
+	}
+
+	return total;
+}

+ 35 - 0
lib/dpkg/fdio.h

@@ -0,0 +1,35 @@
+/*
+ * libdpkg - main program for package management
+ * fdio.h - safe file descriptor based input/output
+ *
+ * Copyright © 2009-2010 Guillem Jover <guillem@debian.org>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef LIBDPKG_FDIO_H
+#define LIBDPKG_FDIO_H
+
+#include <sys/types.h>
+
+#include <dpkg/macros.h>
+
+DPKG_BEGIN_DECLS
+
+ssize_t fd_read(int fd, void *buf, size_t len);
+ssize_t fd_write(int fd, const void *buf, size_t len);
+
+DPKG_END_DECLS
+
+#endif /* LIBDPKG_FDIO_H */

+ 2 - 0
lib/dpkg/libdpkg.Versions

@@ -82,6 +82,8 @@ LIBDPKG_PRIVATE {
 	namevalue_find_by_name;
 
 	# Buffer I/O functions
+	fd_read;
+	fd_write;
 	buffer_hash;
 	buffer_copy_*;