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