File: utils.rb

package info (click to toggle)
dpkg-ruby 0.3.6%2Bnmu2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 348 kB
  • ctags: 386
  • sloc: ruby: 2,841; fortran: 90; makefile: 75; cpp: 36; sh: 8
file content (111 lines) | stat: -rw-r--r-- 2,589 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# utils.rb - ruby utilities interface for debian.rb (gunzip, tar, ..)
# Copyright (c) 2001 Fumitoshi UKAI <ukai@debian.or.jp>
#
# This program 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 program 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# $Id: utils.rb,v 1.2 2003/10/07 17:07:02 ukai Exp $
#

module Debian
  module Utils
    GUNZIP = '/bin/gunzip'
    TAR = '/bin/tar'
    TAR_EXTRACT = '-x'
    TAR_LIST = '-t'
    
    def Utils.pipeline(io,progs,stderr = false)
      Signal.trap('CHLD', 'IGNORE')
      # wr0 -> rd0 [gunzip] wr -> rd
      rd,wr = IO.pipe
      rde,wre = IO.pipe
      pid = fork
      if pid
	# parent
	wr.close
	wre.close
	if block_given?
	  return yield(rd, rde)
	else
	  return rd, rde
	end
      else
	# child
	rd.close
	rd0, wr0 = IO.pipe
        pid2 = fork
	if pid2
	  # gunzip
	  wr0.close
          STDOUT.reopen(wr)
          STDERR.reopen(wre) if stderr
          STDIN.reopen(rd0)
          ENV["LANG"] = "C"
          ENV["LC_ALL"] = "C"
	  exec(*progs)
          # XXX: waitpid(pid2?)
	else
	  rd0.close
	  while ! io.eof?
	    wr0.write(io.read(4096))
	  end
	  exit 0
	end
      end
      Process.waitpid(pid, 0)
      Process.wait
    end

    def gunzip(io)
      Utils.pipeline(io, [GUNZIP]) {|fp,fpe|
        fpe.close
	if block_given?
	  return yield(fp)
	else
	  return fp
	end
      }
    end
    def tar(io,op,*pat)
      progs = [TAR, op, '--wildcards', '-f', '-']
      if pat[0]
	progs += ['--to-stdout', *pat]
      end
      Utils.pipeline(io,progs,op == "-t") {|fp,fpe|
        if op == "-t"
          pid = fork
          if pid
            # parent
          else
            while !fpe.eof? && str = fpe.readline
              unless str.match(/^\/bin\/tar: Record size = [0-9]+ blocks$/)
                STDERR.puts str
              end
            end
            exit
          end
        end
        fpe.close
	if block_given?
	  return yield(fp)
	else
	  return fp
	end
      }
    end

    module_function :gunzip, :tar
  end
end