Reimplement mksplit.pl in C

2010-04-02 Thread Marcelo E. Magallon
Hi,

 somewhere I read that a reimplementation of mksplit.pl in C was
 needed in order to remove a dependency of dpkg on Perl, but I
 can't find the reference anymore.  Oddly enough I don't see a
 dependency of dpkg on perl either:

Package: dpkg
Version: 1.15.6.1
Replaces: manpages-de (<= 0.4-3), manpages-pl (<= 20051117-1)
Pre-Depends: libbz2-1.0, libc6 (>= 2.3), libselinux1 (>= 1.32), zlib1g (>= 
1:1.1.4), coreutils (>= 5.93-1), xz-utils
Suggests: apt
Conflicts: apt (<< 0.7.7), aptitude (<< 0.4.7-1), dpkg-dev (<< 1.14.16), 
dpkg-iasearch (<< 0.11), sysvinit (<< 2.82-1)
Breaks: emacs21 (<< 21.4a+1-5.7), emacs21-nox (<< 21.4a+1-5.7), emacs22 (<= 
22.3+1-1), emacs22-gtk (<= 22.3+1-1), emacs22-nox (<= 22.3+1-1), jed (<< 
1:0.99.18+dfsg.1-13), jed-extra (<= 2.5.3-2), konqueror (<= 4:4.2.96-1), pinfo 
(<< 0.6.9-3.1), tkinfo (<< 2.8-3.1), xemacs21-support (<< 21.4.22-2), xjed (<< 
1:0.99.18+dfsg.1-13)

 Anyways, attached is a patch to do just that.  It's implemented
 against the master Git branch.  As indicated in the patch, this
 is a straight translation and it would like to have some
 refactoring love.  I did compare the output with the Perl
 version in several test cases and it's identical.

 Please Cc: me, I'm not subscribed.

 Cheers,

 Marcelo
>From 267685fb5750a47b536ab817a72597836abe5c5f Mon Sep 17 00:00:00 2001
From: Marcelo E. Magallon 
Date: Fri, 2 Apr 2010 22:27:50 -0600
Subject: [PATCH] Reimplement mksplit in C

My original intention was to do without some of the pipes, but for the
initial implementation this is just a translation of the original Perl
program into C, without much in the way of optimization.  It produces
the same output and exits in the same way as the original program (mod
exit status probably).

The reimplementation can use some refactoring love.
---
 dpkg-split/Makefile.am |   10 ++-
 dpkg-split/mksplit.c   |  279 
 2 files changed, 287 insertions(+), 2 deletions(-)
 create mode 100644 dpkg-split/mksplit.c

diff --git a/dpkg-split/Makefile.am b/dpkg-split/Makefile.am
index 6f6043c..c43025d 100644
--- a/dpkg-split/Makefile.am
+++ b/dpkg-split/Makefile.am
@@ -9,7 +9,7 @@ AM_CPPFLAGS = \
 	-I$(top_srcdir)/lib
 
 
-bin_PROGRAMS = dpkg-split
+bin_PROGRAMS = dpkg-split mksplit
 
 dpkg_split_SOURCES = \
 	dpkg-split.h \
@@ -24,9 +24,15 @@ dpkg_split_LDADD = \
 	../lib/compat/libcompat.a \
 	$(LIBINTL)
 
+mksplit_SOURCES = \
+	mksplit.c
+
+mksplit_LDADD = \
+	../lib/dpkg/libdpkg.a \
+	../lib/compat/libcompat.a \
+	$(LIBINTL)
 
 pkglib_SCRIPTS = mksplit
-EXTRA_DIST = mksplit.pl
 CLEANFILES = $(pkglib_SCRIPTS)
 
 
diff --git a/dpkg-split/mksplit.c b/dpkg-split/mksplit.c
new file mode 100644
index 000..5ca34d6
--- /dev/null
+++ b/dpkg-split/mksplit.c
@@ -0,0 +1,279 @@
+/* This program is only supposed to be called by dpkg-split.
+ * Its arguments are:
+ *  
+ * Stdin is also redirected from the source archive by dpkg-split.
+ *
+ * Copyright © 1995 Ian Jackson 
+ * Copyright © 2010 Marcelo E. Magallon 
+ *
+ * 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 .
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define MSDOS_PATH_MAX 10
+
+void checked_free(void *ptr)
+{
+if (ptr) free(ptr);
+}
+
+void add(FILE *stream, char* id, char* data, size_t len)
+{
+time_t current_time;
+
+current_time = time(NULL);
+fprintf(stream, "%-16s%-12d0 0 100644  %-10d%c\n",
+id, (int)current_time, (int)len, 0140);
+fwrite(data, 1, len, stream);
+fprintf(stream, "%s", (len & 1) ? "\n" : "");
+}
+
+char* output(char *cmd)
+{
+FILE * file = NULL;
+int len = getpagesize();
+char *result = (char *)malloc(len);
+char *p = result;
+
+if (result == NULL)
+goto error_out;
+
+if ((file = popen(cmd, "r")) == NULL)
+goto error_out;
+
+while (len > 0 && !feof(file))
+{
+int n;
+n = fread(p, 1, len, file);
+if (ferror(file))
+goto error_out;
+p += n;
+len -= n;
+}
+goto out;
+
+error_out:
+if (result)
+{
+free(result);
+result = NULL;
+}
+
+out:
+if (file)
+pclose(file);
+return result;
+}
+
+char* get_field(char *filename, char *field)
+{
+char *result;
+char cmd[PATH_MAX];
+sprintf(cmd, "dpkg-deb --field %s %s", filename, fie

Re: Reimplement mksplit.pl in C

2010-04-02 Thread Jonathan Nieder
Hi,

Marcelo E. Magallon wrote:

>  somewhere I read that a reimplementation of mksplit.pl in C was
>  needed in order to remove a dependency of dpkg on Perl, but I
>  can't find the reference anymore.

I could only find approx one reference for this goal [1].

Luckily, even if that doesn’t pan out, there are plenty of other
benefits to well written C code.  Most importantly, it makes code
sharing a little easier.  I guess this would also make repairing a
broken perl installation a little simpler on some system with only
access to small media, though I haven’t encountered a need for
dpkg-split in a long while.  Thanks for working on it.

>  Oddly enough I don't see a
>  dependency of dpkg on perl either

$ apt-cache show perl-base | grep Essential
Essential: yes

>  I did compare the output with the Perl
>  version in several test cases and it's identical.

Some test cases (or methods for producing them) would make me very
happy.  dpkg doesn’t have enough automated tests of basic functionality.
Automake provides a rudimentary test harness [2].

> Subject: [PATCH] Reimplement mksplit in C
> 
> My original intention was to do without some of the pipes, but for the
> initial implementation this is just a translation of the original Perl
> program into C, without much in the way of optimization.  It produces
> the same output and exits in the same way as the original program (mod
> exit status probably).

I will let the dpkg-split experts comment on the important part.

> --- a/dpkg-split/Makefile.am
> +++ b/dpkg-split/Makefile.am
> @@ -9,7 +9,7 @@ AM_CPPFLAGS = \
>   -I$(top_srcdir)/lib
>  
>  
> -bin_PROGRAMS = dpkg-split
> +bin_PROGRAMS = dpkg-split mksplit

I think this should be

 bin_PROGRAMS = dpkg-split
 pkglib_PROGRAMS = mksplit

so that mksplit stays in /usr/lib/dpkg.

Hmm?
Jonathan


-- 
To UNSUBSCRIBE, email to debian-dpkg-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/20100403061933.ga23...@progeny.tock



Re: Reimplement mksplit.pl in C

2010-04-02 Thread Russ Allbery
Jonathan Nieder  writes:

> Some test cases (or methods for producing them) would make me very
> happy.  dpkg doesn’t have enough automated tests of basic functionality.
> Automake provides a rudimentary test harness [2].

Given that a fair bit of dpkg is written in Perl, which means that people
are already familiar with the Perl test harness, you may want to consider
using my C TAP Harness framework for writing tests instead.

http://www.eyrie.org/~eagle/software/c-tap-harness/

It implements a pure C version of Perl's Test::Harness and prove (which is
useful because prove isn't entirely happy with tests not written in Perl),
but probably more relevantly, it provides TAP libraries for writing tests
in C and shell.

The Automake test harness is okay, but I really like the Perl test
framework output and its test protocol.

-- 
Russ Allbery (r...@debian.org)   


--
To UNSUBSCRIBE, email to debian-dpkg-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/871vexm4lp@windlord.stanford.edu



Re: Reimplement mksplit.pl in C

2010-04-02 Thread Jonathan Nieder
Russ Allbery wrote:
> Jonathan Nieder  writes:

>> Automake provides a rudimentary test harness [2].
>
> Given that a fair bit of dpkg is written in Perl, which means that people
> are already familiar with the Perl test harness, you may want to consider
> using my C TAP Harness framework for writing tests instead.
> 
> http://www.eyrie.org/~eagle/software/c-tap-harness/

Looks neat.  Thanks for pointing me to it.

Cheers,
Jonathan

My last message was missing some footnotes.  Oops.

[1] http://lists.debian.org/debian-devel/2001/03/msg00250.html
http://lists.debian.org/debian-embedded/2009/04/msg00064.html

[2] http://www.gnu.org/software/hello/manual/automake/Simple-Tests.html


-- 
To UNSUBSCRIBE, email to debian-dpkg-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/20100403065849.ga23...@progeny.tock