📄 jartool.c
字号:
/* jartool.c - main functions for fastjar utility Copyright (C) 2002, 2004 Free Software Foundation Copyright (C) 1999, 2000, 2001 Bryan Burns This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*//* Revision 1.10 2002/01/03 04:57:56 rodrigc 2001-01-02 Craig Rodrigues <rodrigc@gcc.gnu.org> PR bootstrap/5117 * configure.in (AC_CHECK_HEADERS): Check for stdlib.h. * Makefile.am: Move grepjar to bin_PROGRAMS. * config.h.in: Regenerated. * Makefile.in: Regenerated. * aclocal.m4: Regenerated. * jargrep.c: Eliminate some signed/unsigned and default uninitialized warnings. Use HAVE_STDLIB_H instead of STDC_HEADERS macro. * jartool.c: Likewise. * compress.c: Likewise. Revision 1.9 2001/10/12 00:49:42 bryce * jatool.c (extract_jar): Account for null termination when determining whether to expand "filename". Revision 1.8 2001/08/29 01:35:31 apbianco 2001-08-28 Alexandre Petit-Bianco <apbianco@redhat.com> * jartool.c (add_to_jar): Return 1 if `stat' initialy failed. Fixes PR java/3949. (http://gcc.gnu.org/ml/gcc-patches/2001-08/msg01641.html) Revision 1.7 2001/08/27 23:09:37 tromey * jartool.c (jarfile): Remove length limitation. (main): Use jt_strdup when initializing jarfile. Revision 1.6 2001/07/04 18:33:53 tromey Modified from patch by Julian Hall <jules@acris.co.uk>: * jartool.c (errno): Conditionally declare. (O_BINARY): Conditionally define. (main): Use open, not creat. Use O_BINARY everywhere. (make_manifest): Use O_BINARY. (add_to_jar): Likewise. Revision 1.5 2001/05/03 21:40:47 danglin * jartool.c (jt_strdup): New function. (get_next_arg): Use jt_strdup instead of strdup. Revision 1.4 2000/12/28 21:47:37 robertl 2000-12-28 Robert Lipe <robertl@sco.com> * jartool.c (MAXPATHLEN): Provide if not defined. Revision 1.3 2000/12/14 18:45:35 ghazi Warning fixes: * compress.c: Include stdlib.h and compress.h. (rcsid): Delete. (report_str_error): Make static. (ez_inflate_str): Delete unused variable. Add parens in if-stmt. (hrd_inflate_str): Likewise. * compress.h (init_compression, end_compression, init_inflation, end_inflation): Prototype void arguments. * dostime.c (rcsid): Delete. * jargrep.c: Include ctype.h, stdlib.h, zlib.h and compress.h. Make functions static. Cast ctype function argument to `unsigned char'. Add parens in if-stmts. Constify. (Usage): Change into a macro. (jargrep): Remove unused parameter. * jartool.c: Constify. Add parens in if-stmts. Align signed/unsigned char pointers in functions calls using casts. (rcsid): Delete. (list_jar): Fix printf format specifier. (usage): Chop long string into bits. Reformat. * pushback.c (rcsid): Delete. Revision 1.2 2000/12/13 18:11:57 tromey * jartool.c (extract_jar): Use strchr, not index. Revision 1.1 2000/12/09 03:08:23 apbianco 2000-12-08 Alexandre Petit-Bianco <apbianco@cygnus.com> * fastjar: Imported. Revision 1.5 2000/08/24 15:01:27 cory Made certain that fastjar opened the jar file before trying to update it with the -u option. Revision 1.4 2000/08/24 13:39:21 cory Changed +'s to |'s in jartool.c to insure there was no confusion with sign when byte swapping. Better safe than sorry. Revision 1.3 2000/08/23 19:42:17 cory Added support for more Unix platforms. The following code has been hacked to work on AIX, Solaris, True 64, and HP-UX. Added bigendian check. Probably works on most big and little endian platforms now. Revision 1.2 1999/12/06 07:38:28 toast fixed recursive archiving bug Revision 1.1.1.1 1999/12/06 03:09:34 toast initial checkin.. Revision 1.22 1999/10/12 19:45:13 burnsbr adding patch to fix compat problem Revision 1.21 1999/05/10 09:15:49 burnsbr fixed manifest file version info Revision 1.20 1999/05/10 08:53:16 burnsbr *** empty log message *** Revision 1.19 1999/05/10 08:30:39 burnsbr added extract / listing code Revision 1.18 1999/04/28 04:24:29 burnsbr updated version Revision 1.17 1999/04/28 04:21:23 burnsbr added support for -C dir-changing flag.. Updated total compression display Revision 1.16 1999/04/27 10:28:22 burnsbr updated version string Revision 1.15 1999/04/27 10:04:06 burnsbr configure support Revision 1.14 1999/04/27 08:56:14 burnsbr added -V flag, better error messages Revision 1.13 1999/04/26 02:35:21 burnsbr changed all sorts of stuff.. compression now works 100% Revision 1.12 1999/04/23 12:00:45 burnsbr 90% done with compression code Revision 1.11 1999/04/22 04:12:57 burnsbr finished first round of Manifest file support.. might need to do more, digest etc.. Revision 1.10 1999/04/22 02:35:23 burnsbr added more manifest support, about 75% done now. Replaced all the redundant shifts and bit-logic with a macro or two, making the code easier to read. Revision 1.9 1999/04/21 09:55:16 burnsbr pulled out printfs Revision 1.8 1999/04/21 02:58:01 burnsbr started manifest code Revision 1.7 1999/04/20 23:15:28 burnsbr added patch sent by John Bley <jbb6@acpub.duke.edu> Revision 1.6 1999/04/20 08:56:02 burnsbr added GPL comment Revision 1.5 1999/04/20 08:16:09 burnsbr fixed verbose flag, did some optimization Revision 1.4 1999/04/20 05:09:59 burnsbr added rcsid variable Revision 1.3 1999/04/20 05:08:54 burnsbr fixed Log statement*/#include "config.h"#include <zlib.h>#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#include <stdio.h>#include <sys/stat.h>#include <sys/types.h>#ifdef HAVE_SYS_PARAM_H#include <sys/param.h>#endif#ifndef MAXPATHLEN#define MAXPATHLEN 1024#endif#ifdef HAVE_DIRENT_H#include <dirent.h>#endif#ifdef HAVE_FCNTL_H#include <fcntl.h>#endif#include <string.h>#include <errno.h>#ifdef TM_IN_SYS_TIME#include <sys/time.h>#else#include <time.h>#endif#include <getopt.h>#include "jartool.h"#include "zipfile.h"#include "dostime.h"#include "pushback.h"#include "compress.h"#include "shift.h"/* Some systems have mkdir that takes a single argument. */#ifdef MKDIR_TAKES_ONE_ARG# define mkdir(a,b) mkdir(a)#endif#ifdef WORDS_BIGENDIAN#define L2BI(l) ((l & 0xff000000) >> 24) | \ ((l & 0x00ff0000) >> 8) | \ ((l & 0x0000ff00) << 8) | \ ((l & 0x000000ff) << 24);#define L2BS(l) ((l & 0xff00) >> 8) | ((l & 0x00ff) << 8);#endif#ifndef errnoextern int errno;#endif#ifndef O_BINARY#define O_BINARY 0#endifvoid usage(const char*);void help(const char *);void version(void);void add_entry(struct zipentry *);void init_headers(void);int consume(pb_file *, int);int list_jar(int, char**, int);int extract_jar(int, char**, int);int add_file_to_jar(int, int, const char*, struct stat*, int);int add_to_jar(int, const char*, int);int add_to_jar_with_dir(int, const char*, const char*, int);int create_central_header(int);int make_manifest(int, const char*, int);int read_entries (int);static void init_args(char **, int);static char *get_next_arg (void);static char *jt_strdup (char*);static void expand_options (int *argcp, char ***argvp);static struct zipentry *find_entry (const char *);static int looks_like_dir (const char *);/* global variables */ub1 file_header[30];ub1 data_descriptor[16];int do_compress;int seekable;int verbose;char *jarfile;/* If non zero, then don't recurse in directory. Instead, add the directory entry and relie on an explicit list of files to populate the archive. This option isn't supported by the original jar tool. */int use_explicit_list_only;/* If non zero, then read the entry names from stdin. This option isn't supported by the original jar tool. */int read_names_from_stdin;zipentry *ziplist; /* linked list of entries */zipentry *ziptail; /* tail of the linked list */int number_of_entries; /* number of entries in the linked list *//* What we go by. */const char *progname;/* The offset of the end of the last zip entry. */ub4 end_of_entries;/* This is used to mark options with no short value. */#define LONG_OPT(Num) ((Num) + 128)#define OPT_HELP LONG_OPT (0)/* This holds all options. */#define OPTION_STRING "-ctxuvVf:m:C:0ME@"/* Define the MANIFEST content here to have it easier with calculations below. This is for the case we create an empty MANIFEST.MF. */#define MANIFEST_STR "Manifest-Version: 1.0\nCreated-By: "#define MANIFEST_END "\n\n"static const struct option options[] ={ { "help", no_argument, NULL, OPT_HELP }, { "version", no_argument, NULL, 'V' }, { NULL, no_argument, NULL, 0 }};int main(int argc, char **argv){ char *mfile = NULL; int action = ACTION_NONE; int manifest = TRUE; int opt; int jarfd = -1; /* These are used to collect file names and `-C' options for the second pass through the command line. */ int new_argc; char **new_argv; progname = argv[0]; do_compress = TRUE; verbose = FALSE; ziplist = NULL; number_of_entries = 0; if(argc < 2) usage(argv[0]); new_argc = 0; new_argv = (char **) malloc (argc * sizeof (char *)); expand_options (&argc, &argv); while ((opt = getopt_long (argc, argv, OPTION_STRING, options, NULL)) != -1) { switch(opt){ case 'C': new_argv[new_argc++] = (char *) "-C"; /* ... fall through ... */ case 1: /* File name or unparsed option, due to RETURN_IN_ORDER. */ new_argv[new_argc++] = optarg; break; case 'c': action = ACTION_CREATE; break; case 't': action = ACTION_LIST; break; case 'x': action = ACTION_EXTRACT; break; case 'u': action = ACTION_UPDATE; break; case 'v': verbose = TRUE; break; case 'V': version(); exit(0); case 'f': jarfile = optarg; break; case 'm': mfile = optarg; break; case '0': do_compress = FALSE; break; case 'M': manifest = FALSE; break; case OPT_HELP: help(argv[0]); break; /* The following options aren't supported by the original jar tool. */ case 'E': use_explicit_list_only = TRUE; break; case '@': read_names_from_stdin = TRUE; break; default: usage(argv[0]); } } /* We might have seen `--'. In this case we want to make sure that all following options are handled as file names. */ while (optind < argc) new_argv[new_argc++] = argv[optind++]; new_argv[new_argc] = NULL; if(action == ACTION_NONE){ fprintf(stderr, "%s: one of options -{ctxu} must be specified.\n", progname); usage(argv[0]); } /* Verify unsupported combinations and warn of the use of non standard features */ if(verbose && use_explicit_list_only) fprintf (stderr, "Warning: using non standard '-E' option\n"); if(verbose && read_names_from_stdin) fprintf (stderr, "Warning: using non standard '-@' option\n"); if(read_names_from_stdin && (action != ACTION_CREATE && action != ACTION_UPDATE)){ fprintf(stderr, "%s: option '-@' is supported only with '-c' or '-u'.\n", progname); usage(argv[0]); } /* create the jarfile */ if(action == ACTION_CREATE){ if(jarfile){ jarfd = open(jarfile, O_CREAT | O_BINARY | O_WRONLY | O_TRUNC, 0666); if(jarfd < 0){ fprintf(stderr, "%s: error opening %s for writing: %s\n", progname, jarfile, strerror (errno)); exit(1); } /* We assume that the file is seekable */ seekable = TRUE; } else { jarfd = STDOUT_FILENO; /* jarfd is stdout otherwise */ /* standard out is not seekable */ seekable = FALSE; /* don't want our output to be part of the jar file.. figured this one out the hard way.. =P */ verbose = FALSE; } } else if(action == ACTION_LIST || action == ACTION_EXTRACT){ if(jarfile){ jarfd = open(jarfile, O_RDONLY | O_BINARY); if(jarfd < 0){ fprintf(stderr, "%s: error opening %s for reading: %s\n", progname, jarfile, strerror (errno)); exit(1); } seekable = TRUE; } else { jarfd = STDIN_FILENO; /* jarfd is standard in */ /* we assume that the stream isn't seekable for safety */ seekable = FALSE; } } if (action == ACTION_UPDATE) { if (!jarfile) { fprintf (stderr, "%s: `-u' mode requires a file name\n", argv[0]); exit (1); } if ((jarfd = open (jarfile, O_RDWR | O_BINARY)) < 0) { fprintf (stderr, "Error opening %s for reading!\n", jarfile); perror (jarfile); exit (1); } /* Assert that jarfd is seekable. */ if (lseek (jarfd, 0, SEEK_CUR) == -1) { fprintf (stderr, "%s: %s is not seekable\n", argv[0], jarfile); exit (1); } seekable = TRUE; } if(action == ACTION_CREATE || action == ACTION_UPDATE){ const char *arg; init_headers(); if(do_compress) init_compression(); if (action == ACTION_UPDATE) { if (read_entries (jarfd)) exit (1); } /* Add the META-INF/ directory and the manifest */ if(manifest && mfile) make_manifest(jarfd, mfile, action == ACTION_UPDATE); else if(manifest && action == ACTION_CREATE) make_manifest(jarfd, NULL, FALSE); init_args (new_argv, 0); /* now we add the files to the archive */ while ((arg = get_next_arg ())){ if(!strcmp(arg, "-C")){ const char *dir_to_change = get_next_arg (); const char *file_to_add = get_next_arg (); if (!dir_to_change || !file_to_add) { fprintf(stderr, "%s: error: missing argument for -C.\n", progname); exit(1); } if (add_to_jar_with_dir(jarfd, dir_to_change, file_to_add, action == ACTION_UPDATE)) { fprintf(stderr, "Error adding %s (in directory %s) to jar archive!\n", file_to_add, dir_to_change); exit(1); } } else { if(add_to_jar(jarfd, arg, action == ACTION_UPDATE)){ fprintf(stderr, "Error adding %s to jar archive!\n", arg); exit(1); } } } /* de-initialize the compression DS */ if(do_compress) end_compression(); if (action == ACTION_UPDATE) lseek (jarfd, end_of_entries, SEEK_SET); create_central_header(jarfd);#if ! (HAVE_FTRUNCATE || HAVE__CHSIZE) #error neither ftruncate() or _chsize() available#endif /* Check if the file shrunk when we updated it. */ if (action == ACTION_UPDATE)#if HAVE_FTRUNCATE ftruncate (jarfd, lseek (jarfd, 0, SEEK_CUR));#else _chsize (jarfd, lseek (jarfd, 0, SEEK_CUR));#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -