1 /* gzip.c - gzip/gunzip/zcat
2 *
3 * Copyright 2017 The Android Open Source Project
4 *
5 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/gzip.html
6 * GZIP RFC: http://www.ietf.org/rfc/rfc1952.txt
7 *
8 * todo: qtv --rsyncable
9
10 // gzip.net version allows all options for all commands.
11 USE_GZIP(NEWTOY(gzip, "ncdfk123456789[-123456789]", TOYFLAG_USR|TOYFLAG_BIN))
12 USE_GUNZIP(NEWTOY(gunzip, "cdfk123456789[-123456789]", TOYFLAG_USR|TOYFLAG_BIN))
13 USE_ZCAT(NEWTOY(zcat, "cdfk123456789[-123456789]", TOYFLAG_USR|TOYFLAG_BIN))
14
15 config GZIP
16 bool "gzip"
17 default n
18 help
19 usage: gzip [-19cdfk] [FILE...]
20
21 Compress files. With no files, compresses stdin to stdout.
22 On success, the input files are removed and replaced by new
23 files with the .gz suffix.
24
25 -c Output to stdout
26 -d Decompress (act as gunzip)
27 -f Force: allow overwrite of output file
28 -k Keep input files (default is to remove)
29 -# Compression level 1-9 (1:fastest, 6:default, 9:best)
30
31 config GUNZIP
32 bool "gunzip"
33 default y
34 help
35 usage: gunzip [-cfk] [FILE...]
36
37 Decompress files. With no files, decompresses stdin to stdout.
38 On success, the input files are removed and replaced by new
39 files without the .gz suffix.
40
41 -c Output to stdout (act as zcat)
42 -f Force: allow read from tty
43 -k Keep input files (default is to remove)
44
45 config ZCAT
46 bool "zcat"
47 default y
48 help
49 usage: zcat [FILE...]
50
51 Decompress files to stdout. Like `gzip -dc`.
52
53 -f Force: allow read from tty
54 */
55
56 #define FORCE_FLAGS
57 #define FOR_gzip
58 #include "toys.h"
59
GLOBALS(int level;)60 GLOBALS(
61 int level;
62 )
63
64 // Use assembly optimized zlib code?
65 #if CFG_TOYBOX_LIBZ
66 #include <zlib.h>
67
68 // Read from in_fd, write to out_fd, decompress if dd else compress
69 static int do_deflate(int in_fd, int out_fd, int dd, int level)
70 {
71 int len, err = 0;
72 char *b = "r";
73 gzFile gz;
74
75 if (!dd) {
76 sprintf(b = toybuf, "w%d", level);
77 if (out_fd == 1) out_fd = xdup(out_fd);
78 }
79 if (!(gz = gzdopen(dd ? in_fd : out_fd, b))) perror_exit("gzdopen");
80 if (dd) {
81 if (gzdirect(gz)) error_exit("not gzip");
82 while ((len = gzread(gz, toybuf, sizeof(toybuf))) > 0)
83 if (len != writeall(out_fd, toybuf, len)) break;
84 } else {
85 while ((len = read(in_fd, toybuf, sizeof(toybuf))) > 0)
86 if (len != gzwrite(gz, toybuf, len)) break;
87 }
88
89 err = !!len;
90 if (len>0 || err == Z_ERRNO) perror_msg(dd ? "write" : "read");
91 if (len<0)
92 error_msg("%s%s: %s", "gz", dd ? "read" : "write", gzerror(gz, &len));
93
94 if (gzclose(gz) != Z_OK) perror_msg("gzclose"), err++;
95
96 return err;
97 }
98
99 // Use toybox's builtin lib/deflate.c
100 #else
101
102 // Read from in_fd, write to out_fd, decompress if dd else compress
103 static int do_deflate(int in_fd, int out_fd, int dd, int level)
104 {
105 int x;
106
107 if (dd) WOULD_EXIT(x, gunzip_fd(in_fd, out_fd));
108 else WOULD_EXIT(x, gzip_fd(in_fd, out_fd));
109
110 return x;
111 }
112
113 #endif
114
do_gzip(int ifd,char * in)115 static void do_gzip(int ifd, char *in)
116 {
117 struct stat sb;
118 char *out = 0;
119 int ofd = 0;
120
121 // Are we writing to stdout?
122 if (!ifd || FLAG(c)) ofd = 1;
123 if (isatty(ifd)) {
124 if (!FLAG(f)) return error_msg("%s:need -f to read TTY"+3*!!ifd, in);
125 else ofd = 1;
126 }
127
128 // Are we reading file.gz to write to file?
129 if (!ofd) {
130 if (fstat(ifd, &sb)) return perror_msg("%s", in);
131
132 // Add or remove .gz suffix as necessary
133 if (!FLAG(d)) out = xmprintf("%s%s", in, ".gz");
134 else if ((out = strend(in, ".gz"))>in) out = xstrndup(in, out-in);
135 else return error_msg("no .gz: %s", in);
136
137 ofd = xcreate(out, O_CREAT|O_WRONLY|WARN_ONLY|(O_EXCL*!FLAG(f)),sb.st_mode);
138 if (ofd == -1) return;
139 }
140
141 if (do_deflate(ifd, ofd, FLAG(d), TT.level)) in = out;
142
143 if (out) {
144 struct timespec times[] = {sb.st_atim, sb.st_mtim};
145
146 if (utimensat(AT_FDCWD, out, times, 0)) perror_exit("utimensat");
147 if (chmod(out, sb.st_mode)) perror_exit("chmod");
148 close(ofd);
149 if (!FLAG(k) && in && unlink(in)) perror_msg("unlink %s", in);
150 free(out);
151 }
152 }
153
gzip_main(void)154 void gzip_main(void)
155 {
156 // This depends on 1-9 being at the end of the option list
157 for (TT.level = 0; TT.level<9; TT.level++)
158 if ((toys.optflags>>TT.level)&1) break;
159 if (!(TT.level = 9-TT.level)) TT.level = 6;
160
161 loopfiles(toys.optargs, do_gzip);
162 }
163
gunzip_main(void)164 void gunzip_main(void)
165 {
166 toys.optflags |= FLAG_d;
167 gzip_main();
168 }
169
zcat_main(void)170 void zcat_main(void)
171 {
172 toys.optflags |= (FLAG_c|FLAG_d);
173 gzip_main();
174 }
175