OSDN Git Service

894a291496aff099328fd3bf09752bf7b95b8d3d
[android-x86/external-toybox.git] / toys / pending / gzip.c
1 /* gzip.c - gzip/gunzip/zcat
2  *
3  * Copyright 2017 The Android Open Source Project
4  *
5  * GZIP RFC: http://www.ietf.org/rfc/rfc1952.txt
6
7 // Existing implementations allow all options for all commands.
8 USE_GZIP(NEWTOY(gzip,     "cdfk123456789[-123456789]", TOYFLAG_USR|TOYFLAG_BIN))
9 USE_GUNZIP(NEWTOY(gunzip, "cdfk123456789[-123456789]", TOYFLAG_USR|TOYFLAG_BIN))
10 USE_ZCAT(NEWTOY(zcat,     "cdfk123456789[-123456789]", TOYFLAG_USR|TOYFLAG_BIN))
11
12 config GZIP
13   bool "gzip"
14   default y
15   depends on TOYBOX_LIBZ
16   help
17     usage: gzip [-19cdfk] [FILE...]
18
19     Compress files. With no files, compresses stdin to stdout.
20     On success, the input files are removed and replaced by new
21     files with the .gz suffix.
22
23     -c  Output to stdout
24     -d  Decompress (act as gunzip)
25     -f  Force: allow overwrite of output file
26     -k  Keep input files (default is to remove)
27     -#  Compression level 1-9 (1:fastest, 6:default, 9:best)
28
29 config GUNZIP
30   bool "gunzip"
31   default y
32   depends on TOYBOX_LIBZ
33   help
34     usage: gunzip [-cfk] [FILE...]
35
36     Decompress files. With no files, decompresses stdin to stdout.
37     On success, the input files are removed and replaced by new
38     files without the .gz suffix.
39
40     -c  Output to stdout (act as zcat)
41     -f  Force: allow read from tty
42     -k  Keep input files (default is to remove)
43
44 config ZCAT
45   bool "zcat"
46   default y
47   depends on TOYBOX_LIBZ
48   help
49     usage: zcat [FILE...]
50
51     Decompress files to stdout. Like `gzip -dc`.
52
53     -c  Output to stdout (default)
54     -f  Force: allow read from tty
55 */
56
57 #define FORCE_FLAGS
58 #define FOR_gzip
59 #include "toys.h"
60
61 #include <zlib.h>
62
63 GLOBALS(
64   int level;
65 )
66
67 static void fix_time(const char *path, struct stat *sb)
68 {
69   struct timespec times[] = { sb->st_atim, sb->st_mtim };
70
71   if (utimensat(AT_FDCWD, path, times, 0)) perror_exit("utimensat");
72 }
73
74 static void gzerror_msg(gzFile f, char *what)
75 {
76   int err;
77   const char *msg = gzerror(f, &err);
78
79   ((err == Z_ERRNO) ? perror_msg : error_msg)("%s: %s", what, msg);
80 }
81
82
83 static int zlib_inflate(int in_fd, int out_fd)
84 {
85   int len, err = 0;
86   gzFile in;
87
88   if (!(in = gzdopen(in_fd, "r"))) perror_exit("gzdopen");
89   while ((len = gzread(in, toybuf, sizeof(toybuf))) > 0)
90     if (len != writeall(out_fd, toybuf, len)) break;
91   if (len) err = 1;
92   if (len>0) perror_msg("write");
93   if (len<0) gzerror_msg(in, "gzread");
94   if (gzclose(in) != Z_OK) perror_msg("gzclose"), err++;
95
96   return err;
97 }
98
99 static void do_gunzip(int in_fd, char *arg)
100 {
101   struct stat sb;
102   int len, out_fd = 0;
103   char *out_name = 0;
104
105   // Are we writing to stderr?
106   if (!in_fd || (toys.optflags&FLAG_c)) out_fd = 1;
107   if (isatty(in_fd)) {
108     if (!(toys.optflags&FLAG_f)) {
109       error_msg("%s:need -f to read TTY"+3*!!in_fd, arg);
110       return;
111     } else out_fd = 1;
112   }
113
114   // Are we reading file.gz to write to file?
115   if (!out_fd) {
116     // "gunzip x.gz" will decompress "x.gz" to "x".
117     if ((len = strlen(arg))<4 || strcmp(arg+len-3, ".gz")) {
118       error_msg("no .gz: %s", arg);
119       return;
120     }
121     if (!stat(arg, &sb)) {
122       perror_msg("%s", arg);
123       return;
124     }
125
126     out_name = xstrdup(arg);
127     out_name[len-3] = 0;
128     out_fd = xcreate(out_name,
129       O_CREAT|O_WRONLY|WARN_ONLY|(O_EXCL*!(toys.optflags&FLAG_f)), sb.st_mode);
130     if (out_fd == -1) return;
131   }
132
133   if (CFG_TOYBOX_LIBZ)
134     if (zlib_inflate(in_fd, out_fd) && out_name) arg = out_name;
135   close(out_fd);
136
137   if (out_name) {
138     fix_time(out_name, &sb);
139     if (!(toys.optflags&FLAG_k)) if (unlink(arg)) perror_msg("unlink %s", arg);
140     free(out_name);
141   }
142 }
143
144 static void do_gzip(int in_fd, char *in_name)
145 {
146   size_t len;
147   char *out_name;
148   FILE *in = xfdopen(in_fd, "r");
149   gzFile out;
150   struct stat sb;
151   int both_files, out_fd;
152
153   out_name = (toys.optflags&FLAG_c) ? strdup("-") : xmprintf("%s.gz", in_name);
154   both_files = strcmp(in_name, "-") && strcmp(out_name, "-");
155   if (both_files) xstat(in_name, &sb);
156
157   if (!strcmp(out_name, "-")) out_fd = dup(1);
158   else {
159     out_fd = open(out_name, O_CREAT|O_WRONLY|((toys.optflags&FLAG_f)?0:O_EXCL),
160       both_files?sb.st_mode:0);
161   }
162   if (out_fd == -1) perror_exit("open %s", out_name);
163
164   snprintf(toybuf, sizeof(toybuf), "w%d", TT.level);
165   out = gzdopen(out_fd, toybuf);
166   if (out == NULL) perror_exit("gzdopen %s", out_name);
167
168   while ((len = fread(toybuf, 1, sizeof(toybuf), in)) > 0) {
169     if (gzwrite(out, toybuf, len) != (int) len) {
170       gzerror_msg(out, "gzwrite");
171       return;
172     }
173   }
174   if (ferror(in)) perror_exit("fread");
175   if (fclose(in)) perror_exit("fclose");
176   if (gzclose(out) != Z_OK) error_exit("gzclose");
177
178   if (both_files) fix_time(out_name, &sb);
179   if (!(toys.optflags&(FLAG_c|FLAG_k))) unlink(in_name);
180   free(out_name);
181 }
182
183 void gzip_main(void)
184 {
185   for (TT.level = 0; TT.level<9; TT.level++)
186     if ((toys.optflags>>TT.level)&1) break;
187   if (!(TT.level = 9-TT.level)) TT.level = 6;
188
189   loopfiles(toys.optargs, (toys.optflags&FLAG_d) ? do_gunzip : do_gzip);
190 }
191
192 void gunzip_main(void)
193 {
194   toys.optflags |= FLAG_d;
195   gzip_main();
196 }
197
198 void zcat_main(void)
199 {
200   toys.optflags |= (FLAG_c|FLAG_d);
201   gzip_main();
202 }