(open_archive) switch from forking unpacker to using librpm --- URPM.xs 2011-12-06 14:57:46.606581011 +0000 +++ /URPM.xs 2011-10-11 14:59:42.972840095 +0000 @@ -1000,9 +1000,10 @@ update_provides_files(URPM__Package pkg, } } -int +FD_t open_archive(char *filename, pid_t *pid, int *empty_archive) { int fd; + FD_t rfd = NULL; struct { char header[4]; char toc_d_count[4]; @@ -1019,65 +1020,26 @@ open_archive(char *filename, pid_t *pid, if (read(fd, &buf, sizeof(buf)) != sizeof(buf) || strncmp(buf.header, "cz[0", 4) || strncmp(buf.trailer, "0]cz", 4)) { /* this is not an archive, open it without magic, but first rewind at begin of file */ lseek(fd, 0, SEEK_SET); + return fdDup(fd); } else if (pos == 0) { *empty_archive = 1; - fd = -1; } else { - /* this is an archive, create a pipe and fork for reading with uncompress defined inside */ - int fdno[2]; - - if (!pipe(fdno)) { - if ((*pid = fork()) != 0) { - fd_set readfds; - struct timeval timeout; - - FD_ZERO(&readfds); - FD_SET(fdno[0], &readfds); - timeout.tv_sec = 1; - timeout.tv_usec = 0; - select(fdno[0]+1, &readfds, NULL, NULL, &timeout); - - close(fd); - fd = fdno[0]; - close(fdno[1]); - } else { - char *unpacker[22]; /* enough for 40 bytes in uncompress to never overbuf */ - char *p = buf.uncompress; - int ip = 0; - char *ld_loader = getenv("LD_LOADER"); - - if (ld_loader && *ld_loader) { - unpacker[ip++] = ld_loader; - } - - buf.trailer[0] = 0; /* make sure end-of-string is right */ - while (*p) { - if (*p == ' ' || *p == '\t') *p++ = 0; - else { - unpacker[ip++] = p; - while (*p && *p != ' ' && *p != '\t') ++p; - } - } - unpacker[ip] = NULL; /* needed for execlp */ - - lseek(fd, 0, SEEK_SET); - dup2(fd, STDIN_FILENO); close(fd); - dup2(fdno[1], STDOUT_FILENO); close(fdno[1]); - - /* get rid of "decompression OK, trailing garbage ignored" */ - fd = open("/dev/null", O_WRONLY); - dup2(fd, STDERR_FILENO); close(fd); - - execvp(unpacker[0], unpacker); - exit(1); - } + /* this is an archive, prepare for reading with uncompress defined inside */ + rfd = Fopen(filename, "r.fdio"); + if (strcmp(buf.uncompress, "gzip")) { + rfd = Fdopen(rfd, "r.gzip"); + } else if (strcmp(buf.uncompress, "bzip")) { + rfd = Fdopen(rfd, "r.bzip2"); + } else if (strcmp(buf.uncompress, "xz") || strcmp(buf.uncompress, "lzma")) { + rfd = Fdopen(rfd, "r.xz"); } else { - close(fd); - fd = -1; + free(rfd); + rfd = NULL; } } } - return fd; + close(fd); + return rfd; } static int @@ -3439,18 +3403,17 @@ Urpm_parse_hdlist__XS(urpm, filename, .. if (depslist != NULL) { pid_t pid = 0; - int d; int empty_archive = 0; FD_t fd; - d = open_archive(filename, &pid, &empty_archive); - fd = fdDup(d); - close(d); + fd = open_archive(filename, &pid, &empty_archive); if (empty_archive) { XPUSHs(sv_2mortal(newSViv(1 + av_len(depslist)))); XPUSHs(sv_2mortal(newSViv(av_len(depslist)))); - } else if (d >= 0 && fd) { + } else if (fd == NULL || Ferror(fd)) { + fprintf(stderr, "Failed to open hdlist: %s\n", Fstrerror(fd)); + } else if (fd) { Header header; int start_id = 1 + av_len(depslist); int packing = 0; (open_archive) clean API diff -p -up ./URPM.xs.tv2 ./URPM.xs --- ./URPM.xs.tv2 2011-12-06 15:20:56.832673044 +0000 +++ ./URPM.xs 2011-12-06 15:20:41.735661297 +0000 @@ -1001,7 +1001,7 @@ update_provides_files(URPM__Package pkg, } FD_t -open_archive(char *filename, pid_t *pid, int *empty_archive) { +open_archive(char *filename, int *empty_archive) { int fd; FD_t rfd = NULL; struct { @@ -3402,11 +3402,10 @@ Urpm_parse_hdlist__XS(urpm, filename, .. HV *obsoletes = fobsoletes && SvROK(*fobsoletes) && SvTYPE(SvRV(*fobsoletes)) == SVt_PVHV ? (HV*)SvRV(*fobsoletes) : NULL; if (depslist != NULL) { - pid_t pid = 0; int empty_archive = 0; FD_t fd; - fd = open_archive(filename, &pid, &empty_archive); + fd = open_archive(filename, &empty_archive); if (empty_archive) { XPUSHs(sv_2mortal(newSViv(1 + av_len(depslist)))); @@ -3462,13 +3461,7 @@ Urpm_parse_hdlist__XS(urpm, filename, .. int ok = Fclose(fd) == 0; - if (pid) { - kill(pid, SIGTERM); - int status; - int rc = waitpid(pid, &status, 0); - ok = rc != -1 && WEXITSTATUS(status) != 1; /* in our standard case, gzip will exit with status code 2, meaning "decompression OK, trailing garbage ignored" */ - pid = 0; - } else if (!empty_archive) { + if (!empty_archive) { ok = av_len(depslist) >= start_id; } SPAGAIN;