summaryrefslogtreecommitdiff
path: root/appl/cmd/gunzip.b
blob: 06ccd4a5584c7e6d92d907fee8deb1390d0ba684 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
implement Gunzip;

include "sys.m";
	sys:	Sys;
	fprint, sprint: import sys;

include "draw.m";

include "string.m";
	str: String;

include "bufio.m";
	bufio:	Bufio;
	Iobuf:	import bufio;

include "filter.m";
	inflate: Filter;

Gunzip: module
{
	init:	fn(ctxt: ref Draw->Context, argv: list of string);
};

argv0:	con "gunzip";
stderr:	ref Sys->FD;

INFLATEPATH: con "/dis/lib/inflate.dis";

init(nil: ref Draw->Context, argv: list of string)
{
	sys = load Sys Sys->PATH;
	stderr = sys->fildes(2);
	bufio = load Bufio Bufio->PATH;
	if (bufio == nil)
		fatal(sys->sprint("cannot load %s: %r", Bufio->PATH));
	str = load String String->PATH;
	if (str == nil)
		fatal(sys->sprint("cannot load %s: %r", String->PATH));
	inflate = load Filter INFLATEPATH;
	if (inflate == nil)
		fatal(sys->sprint("cannot load %s: %r", INFLATEPATH));

	inflate->init();

	if(argv != nil)
		argv = tl argv;

	ok := 1;
	if(len argv == 0){
		bin := bufio->fopen(sys->fildes(0), Bufio->OREAD);
		bout := bufio->fopen(sys->fildes(1), Bufio->OWRITE);
		ok = gunzip(bin, bout, "stdin", "stdout");
		bout.close();
	} else {
		for(; argv != nil; argv = tl argv)
			ok &= gunzipf(hd argv);
	}
	if(ok == 0)
		raise "fail:errors";
}

gunzipf(file: string): int
{
	bin := bufio->open(file, Bufio->OREAD);
	if(bin == nil){
		fprint(stderr, "%s: can't open %s: %r\n", argv0, file);
		return 0;
	}

	(nil, ofile) := str->splitr(file, "/");
	n := len ofile;
	if(n < 4 || ofile[n-3:] != ".gz"){
		fprint(stderr, "%s: .gz extension required: %s\n", argv0, file);
		bin.close();
		return 0;
	} else
		ofile = ofile[:n-3];
	bout := bufio->create(ofile, Bufio->OWRITE, 8r666);
	if(bout == nil){
		fprint(stderr, "%s: can't open %s: %r\n", argv0, ofile);
		bin.close();
		return 0;
	}

	ok := gunzip(bin, bout, file, ofile);
	bin.close();
	bout.close();
	if(ok) {
		# did possibly rename file and update modification time here.
		if (sys->remove(file) == -1)
			sys->fprint(stderr, "%s: cannot remove %s: %r\n", argv0, file);
	}

	return ok;
}

gunzip(bin, bout: ref Iobuf, fin, fout: string): int
{
	rq := inflate->start("h");
	for(;;) {
		pick m := <-rq {
		Fill =>
			n := bin.read(m.buf, len m.buf);
			m.reply <-= n;
			if (n == -1) {
				sys->fprint(stderr, "%s: %s: read error: %r\n", argv0, fin);
				return 0;
			}
		Result =>
			if (len m.buf > 0) {
				n := bout.write(m.buf, len m.buf);
				if (n != len m.buf) {
					m.reply <-= -1;
					sys->fprint(stderr, "%s: %s: write error: %r\n", argv0, fout);
					return 0;
				}
				m.reply <-= 0;
			}
		#Info =>
		#	if m.msg begins with "file", it's the original filename of the compressed file.
		#	if m.msg begins with "mtime", it's the original modification time.
		Finished =>
			if (bout.flush() != 0) {
				sys->fprint(stderr, "%s: %s: flush error: %r\n", argv0, fout);
				return 0;
			}
			return 1;
		Error =>
			sys->fprint(stderr, "%s: %s: inflate error: %s\n", argv0, fin, m.e);
			return 0;
		}
	}
}

fatal(msg: string)
{
	fprint(stderr, "%s: %s\n", argv0, msg);
	raise "fail:error";
}