gzip.c 4.3 KB

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