Permalink
Browse files

change to a perl-based impl.

  • Loading branch information...
kazuho committed Nov 25, 2009
1 parent c230988 commit ebc6d7aa0b1c4306c21ce1c9f5c9bd07290c02ac
Showing with 39 additions and 60 deletions.
  1. +39 −0 blockdiff_merge
  2. +0 −60 blockdiff_merge.c
View
@@ -0,0 +1,39 @@
+#! /usr/bin/perl
+
+use 5.008;
+use strict;
+use warnings;
+
+my $DEBUG = $ENV{DEBUG} || undef;
+
+my $fn = shift @ARGV
+ or die "Usage: $0 <file>\n";
+
+open my $fh, '+>>', $fn
+ or die "failed to open file:$fn:$!";
+
+my $blocksize = do {
+ read(STDIN, my $bsbin, 4) == 4
+ or die "invalid input";
+ unpack'V', $bsbin;
+};
+
+while (sysread(STDIN, my $offbin, 8) == 8) {
+ my $rlen = sysread(STDIN, my $block, $blocksize);
+ die "unexpected eof while reading input:$!"
+ if $rlen <= 0;
+ my $offset = do {
+ my @o = unpack 'V2', $offbin;
+ $o[0] + $o[1] * (1<<32);
+ };
+ warn "writing at:$offset" if $DEBUG;
+ sysseek $fh, $offset, 0
+ or die "seek failed:$!";
+ syswrite($fh, $block, $rlen) == $rlen
+ or die "write failed:$!";
+}
+
+close $fh
+ or die "failed to close file:$fn:$!";
+
+exit 0;
View
@@ -1,60 +0,0 @@
-#define _LARGEFILE64_SOURCE
-#include <fcntl.h>
-#include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <openssl/crypto.h>
-
-/* read patch data from STDIN and apply to argv[0] */
-
-int main(int argc, char **argv)
-{
- int fd;
- unsigned char* block;
- int blocksize;
- size_t rlen;
- long long offset;
- int debug = getenv("DEBUG") != NULL;
-
- if (argc != 2) {
- fprintf(stderr, "Usage: %s <file>\n", argv[0]);
- exit(99);
- }
-
- if ((fd = open(argv[1], O_RDWR | O_CREAT, 0666)) == -1) {
- perror("failed to open file");
- exit(1);
- }
-
- if (fread(&blocksize, sizeof(blocksize), 1, stdin) != 1) {
- perror("unexpected eof");
- exit(2);
- }
-
- if ((block = malloc(blocksize)) == NULL) {
- perror("no memory");
- exit(2);
- }
-
- while (fread(&offset, 1, sizeof(offset), stdin) == sizeof(offset)) {
- if ((rlen = fread(block, 1, blocksize, stdin)) <= 0) {
- perror("unexpected eof");
- exit(2);
- }
- if (debug)
- fprintf(stderr, "writing at:%lld\n", offset);
- if (lseek64(fd, offset, SEEK_SET) == -1) {
- perror("seek failed");
- exit(3);
- }
- if (write(fd, block, rlen) != rlen) {
- perror("write failed");
- exit(3);
- }
- }
-
- close(fd);
-
- return 0;
-}

0 comments on commit ebc6d7a

Please sign in to comment.