• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     while ((len = gzread(gz, toybuf, sizeof(toybuf))) > 0)
82       if (len != writeall(out_fd, toybuf, len)) break;
83   } else {
84     while ((len = read(in_fd, toybuf, sizeof(toybuf))) > 0)
85       if (len != gzwrite(gz, toybuf, len))  break;
86   }
87 
88   err = !!len;
89   if (len>0 || err == Z_ERRNO) perror_msg(dd ? "write" : "read");
90   if (len<0)
91     error_msg("%s%s: %s", "gz", dd ? "read" : "write", gzerror(gz, &len));
92 
93   if (gzclose(gz) != Z_OK) perror_msg("gzclose"), err++;
94 
95   return err;
96 }
97 
98 // Use toybox's builtin lib/deflate.c
99 #else
100 
101 // Read from in_fd, write to out_fd, decompress if dd else compress
102 static int do_deflate(int in_fd, int out_fd, int dd, int level)
103 {
104   int x;
105 
106   if (dd) WOULD_EXIT(x, gunzip_fd(in_fd, out_fd));
107   else WOULD_EXIT(x, gzip_fd(in_fd, out_fd));
108 
109   return x;
110 }
111 
112 #endif
113 
do_gzip(int ifd,char * in)114 static void do_gzip(int ifd, char *in)
115 {
116   struct stat sb;
117   char *out = 0;
118   int ofd = 0;
119 
120   // Are we writing to stdout?
121   if (!ifd || FLAG(c)) ofd = 1;
122   if (isatty(ifd)) {
123     if (!FLAG(f)) return error_msg("%s:need -f to read TTY"+3*!!ifd, in);
124     else ofd = 1;
125   }
126 
127   // Are we reading file.gz to write to file?
128   if (!ofd) {
129     if (fstat(ifd, &sb)) return perror_msg("%s", in);
130 
131     // Add or remove .gz suffix as necessary
132     if (!FLAG(d)) out = xmprintf("%s%s", in, ".gz");
133     else if ((out = strend(in, ".gz"))>in) out = xstrndup(in, out-in);
134     else return error_msg("no .gz: %s", in);
135 
136     ofd = xcreate(out, O_CREAT|O_WRONLY|WARN_ONLY|(O_EXCL*!FLAG(f)),sb.st_mode);
137     if (ofd == -1) return;
138   }
139 
140   if (do_deflate(ifd, ofd, FLAG(d), TT.level)) in = out;
141 
142   if (out) {
143     struct timespec times[] = {sb.st_atim, sb.st_mtim};
144 
145     if (utimensat(AT_FDCWD, out, times, 0)) perror_exit("utimensat");
146     if (chmod(out, sb.st_mode)) perror_exit("chmod");
147     close(ofd);
148     if (!FLAG(k) && in && unlink(in)) perror_msg("unlink %s", in);
149     free(out);
150   }
151 }
152 
gzip_main(void)153 void gzip_main(void)
154 {
155   // This depends on 1-9 being at the end of the option list
156   for (TT.level = 0; TT.level<9; TT.level++)
157     if ((toys.optflags>>TT.level)&1) break;
158   if (!(TT.level = 9-TT.level)) TT.level = 6;
159 
160   loopfiles(toys.optargs, do_gzip);
161 }
162 
gunzip_main(void)163 void gunzip_main(void)
164 {
165   toys.optflags |= FLAG_d;
166   gzip_main();
167 }
168 
zcat_main(void)169 void zcat_main(void)
170 {
171   toys.optflags |= (FLAG_c|FLAG_d);
172   gzip_main();
173 }
174