📄 jar_support.c
字号:
sprintf(tmppkgbuf, "%s%c", pkgbuf, (char)LOCAL_DIR_SEPARATOR); } if (JAR_DEBUG && verbose) jio_fprintf(stderr, "remove_dir: Reading filename [%s] from directory [%s]\n", name, dirname); /* we just have a class file that needs to be removed */ len = strlen(name); /* append the dirname and name */ strcpy(buf, dirname); strcat(buf, name); status = stat(buf, &stat_buf); if ((status == 0) && !(stat_buf.st_mode & S_IFDIR)) { /* remove if this is a file and not a directory */ if (JAR_DEBUG && verbose) jio_fprintf(stderr, "remove_dir: Removing file [%s] \n", buf); err = remove(buf); if (JAR_DEBUG && verbose) { jio_fprintf(stderr, "remove_dir: remove() returned error %d\n", err); } } else { strcat(tmppkgbuf, name); stat(buf, &stat_buf); len = strlen(buf); if (stat_buf.st_mode & S_IFDIR) { /* handle the recursive directory found */ sprintf(tmpbuf, "%s%c", buf, (char)LOCAL_DIR_SEPARATOR); if (JAR_DEBUG && verbose) jio_fprintf(stderr, "remove_dir: Recursive dir, calling remove_dir [%s,%s]\n", tmpbuf, tmppkgbuf); remove_dir(tmpbuf, tmppkgbuf); continue; } } } /* close the directory to free the dirp first */ closedir(dir);#ifdef WIN32 /* remove the trailing '\' from the directory */ { int tmppkglen = strlen(dirname); dirname[tmppkglen-1] = '\0'; }#endif /* Remove the directory by calling rmdir() or remove() API as appropriate */ sprintf(tmppkg,"%s", dirname); if (JAR_DEBUG && verbose) jio_fprintf(stderr, "remove_dir: Removing [%s]\n", tmppkg);#ifdef WIN32 err = rmdir(tmppkg); if (JAR_DEBUG && verbose) { if (err != 0) { jio_fprintf(stderr, "remove_dir: rmdir(%s) failed with error %d\n", tmppkg, err); } }#endif#ifdef UNIX err = remove (tmppkg); if (JAR_DEBUG && verbose) { if (err != 0) { jio_fprintf(stderr, "remove_dir: remove(%s) failed with error %d\n", tmppkg, err); } }#endif}/*========================================================================= * FUNCTION: ProcessJARfile() * TYPE: Processes ZIP file entries * OVERVIEW: Internal function called by ProcessInputs(). * * This function processes a JAR file by first creating a zip entry for * reading JAR directories, then calls ReadFromZip() to read the Zip * class names and verifies them. It finally creates a new JAR file and * places all the verified classes into it. * It returns a boolean type to indicate if a valid JAR file was found * and the contents of the JAR file were processed without errors. * * INTERFACE: * parameters: buf: JAR file name. * len: size of the file * returns: boolean type *=======================================================================*/bool_t ProcessJARfile(char *buf, int len) { zip_t *zipEntry; struct stat stat_buf; char *fname = NULL; char *buffer = NULL; char *jarName = NULL; char dirdelim = '\0'; /* directory delimiter */ int err = 0; int tmpdir_len = 0; int statcode; char tmpdir_buf[MAXPACKAGENAME]; if (JAR_DEBUG && verbose) jio_fprintf(stderr, "ProcessJARfile: JAR file [%s] Size [%d]\n", buf, len); statcode = stat(buf, &stat_buf); /* Create the zip entry for searching the JAR directories. * If the zip entry is NULL, it would indicate that we ran * out of memory and would have exited already. */ zipEntry = getZipEntry (buf, len); if (JAR_DEBUG && verbose) jio_fprintf(stderr, "Searching for JAR directories...\n"); /* search for the JAR directories */ if (findJARDirectories(zipEntry, &stat_buf)) { /* the JAR directories were found */ JARfile = TRUE; zipFileName = buf; if (JAR_DEBUG && verbose) { jio_fprintf(stderr, "ProcessJARfile: JAR directory [%s] found \n", zipEntry->name); } zipEntry->type = 'j'; /* Read and Verify the classes from the ZIP file */ if (JAR_DEBUG && verbose) jio_fprintf(stderr, "ProcessJARfile: Verifying Zip class names...\n"); pushJarFileOntoClassPath(zipEntry); ReadFromZip(zipEntry); popClassPath(); /* Ensure that the output_dir also exists or create it if it * does not already exist */ if (output_dir != NULL) { char *dir = strdup(output_dir); if (JAR_DEBUG && verbose) jio_fprintf(stderr, "ProcessJARfile: Checking if output [%s] exists\n", output_dir); ensure_dir_exists(dir); ensure_dir_writable(dir); free(dir); } /* Create a JAR file only if the input parameter was a JAR file, * the tmp_dir was created with classes verified and an output * dir also exists. */ if (JARfile && tmpDirExists && tmp_dir && output_dir) { const char *p; /* Allocate enough space to hold the JAR name */ jarName = (char *)sysCalloc(len+32, len); if (jarName == NULL) { fprintf(stderr, "ProcessJARfile: Out of memory"); exit(1); } if (JAR_DEBUG && verbose) jio_fprintf(stderr, "ProcessJARfile: Creating JAR file of verified classes...\n"); /* search for the last '/' to get the actual JAR file name */ for (p = buf+len; ;) { --p; if (*p == '/' || *p == '\\') { dirdelim = *p; memcpy(jarName, p+1, (len-1)-(p-buf)); if (JAR_DEBUG && verbose) jio_fprintf(stderr, "ProcessJARfile: JAR file [%s]\n", jarName); break; } if (p == buf) { /* no directories in path, get the individual JAR name */ strncpy(jarName, buf, len); if (JAR_DEBUG && verbose) jio_fprintf(stderr, "ProcessJARfile: JAR filename [%s]\n", jarName); break; } } /* move the verified classes into a JAR file */ /* Be sure to allocate enough space to hold the sprintfs below */ fname = (char *)malloc(strlen(output_dir)+ len+32); if (fname == NULL) { fprintf(stderr, "ProcessJARfile: Out of memory"); exit(1); } if (dirdelim != '\0') { sprintf(fname, "%s%c%s", output_dir, dirdelim, jarName); } else { sprintf(fname, "%s%c%s", output_dir, (char)LOCAL_DIR_SEPARATOR, jarName); } /* Be sure to allocate enough space to hold the sprintfs below */ /* The size here must be adjusted anytime the buffer used in the * sprintfs is extended. */ buffer = (char *)malloc(strlen(output_dir)+strlen(fname) + strlen(tmp_dir) + strlen(manifestfile) + strlen(tmp_dir) + 51); if (buffer == NULL) { fprintf(stderr, "ProcessJARfile: Out of memory"); exit(1); } if (verbose) { if (isManifestfile(manifestfile, strlen(manifestfile))) { /* use existing manifest if one exists */ sprintf(buffer, "jar -cvfm \"%s\" %s%c%s -C %s .", fname, tmp_dir, (char)LOCAL_DIR_SEPARATOR, manifestfile, tmp_dir); } else { sprintf(buffer, "jar -cvfM \"%s\" -C %s .", fname, tmp_dir); } } else { /* Run jar in non-verbose mode, and log errors in a file */ if (isManifestfile(manifestfile, strlen(manifestfile))) { /* use existing manifest if one exists */#ifdef UNIX /* create JAR with existing manifest file */ /* Redirect errors and stdout to jarlog.txt */ sprintf(buffer, "sh -c \"jar -cfm \\\"%s\\\" %s%c%s -C %s .\" > \"%s%c\"%s", fname, tmp_dir, (char)LOCAL_DIR_SEPARATOR, manifestfile, tmp_dir, output_dir, (char)LOCAL_DIR_SEPARATOR,"jarlog.txt 2>&1");#else sprintf(buffer, "jar -cfm \"%s\" %s%c%s -C %s . ", fname, tmp_dir, (char)LOCAL_DIR_SEPARATOR, manifestfile, tmp_dir);#endif } else {#ifdef UNIX /* create JAR with no manifest since none exists */ /* Redirect errors and stdout to jarlog.txt */ sprintf(buffer, "sh -c \"jar -cfM \\\"%s\\\" -C %s .\" > \"%s%c\"%s", fname, tmp_dir, output_dir, (char)LOCAL_DIR_SEPARATOR,"jarlog.txt 2>&1");#else sprintf(buffer, "jar -cfM \"%s\" -C %s . ", fname, tmp_dir);#endif } } if (verbose) { jio_fprintf(stderr, "Executing command [%s]\n", buffer); }#ifdef WIN32 /* system() function does not return the exit code of the child * process under Windows98. * The documentation states: * "If command is not NULL, system returns the value that is * returned by the command interpreter.". * Thus it is probably a bug within 'command.com'. * Note that _spawnlp correctly returns the exit status of the * new process. */ if (verbose) { err = _spawnlp(_P_WAIT, "jar", "jar", buffer+4, NULL); if (err != 0) { fprintf(stderr, "%s\n", buffer); perror("Error"); } } else { int cstderr; int cstdout; FILE *logfile; /* Save stderr and stdout*/ if ((cstderr = dup(_fileno(stderr))) == -1) { fprintf(stderr, "Cannot copy dup stderr\n"); } if ((cstdout = dup(_fileno(stdout))) == -1) { fprintf(stderr, "Cannot copy dup stdout\n"); } sprintf(tmpdir_buf, "%s\\%s", output_dir, "jarlog.txt"); if ((logfile = fopen(tmpdir_buf, "w")) == NULL) { fprintf(stderr, "Cannot create output file\n"); exit(1); } if (_dup2(_fileno(logfile), _fileno(stderr)) == -1) { fprintf(stderr, "dup2 failed for stderr\n"); exit(1); } if (_dup2(_fileno(logfile), _fileno(stdout)) == -1) { fprintf(stderr, "dup2 failed for stdout\n"); exit(1); } err = _spawnlp(_P_WAIT, "jar", "jar", buffer+4, NULL); if (err != 0) { fprintf(stderr, "%s\n", buffer); perror("Error"); } fflush(stderr); fflush(stdout); fclose(logfile); /* Restore stderr and stdout */ _dup2(cstderr, _fileno(stderr)); _dup2(cstdout, _fileno(stdout)); memset(tmpdir_buf,0,sizeof(tmpdir_buf)); }#else err = system(buffer);#endif if (err != 0) { /* jar file creation failed - return back the error */ fprintf(stderr, "JAR file creation failed with error %d\n", err); if (!verbose) fprintf(stderr,"The preverified classes if any are in %s. See jar log of errors in %s%c%s \n", tmp_dir, output_dir, (char)LOCAL_DIR_SEPARATOR, "jarlog.txt"); } else { if (!verbose) { /* remove the jar log file if no error occurred */ char *jarfn = NULL; int error; jarfn = (char *)malloc(strlen(output_dir)+10+1+1); if (jarfn == NULL) { fprintf(stderr, "ProcessJARfile: Out of memory"); exit(1); } sprintf(jarfn, "%s%c%s", output_dir, (char)LOCAL_DIR_SEPARATOR, "jarlog.txt"); error = remove(jarfn); /* remove the file */ if (jarfn != NULL) sysFree(jarfn); } /* prepare to remove the tmp directory */ /* copy the tmp directory name to a buffer */ strcpy(tmpdir_buf, tmp_dir); tmpdir_len = strlen(tmp_dir); /* Append dir separator if it does not yet exist */ if (tmpdir_buf[tmpdir_len - 1] != LOCAL_DIR_SEPARATOR && tmpdir_buf[tmpdir_len - 1] != DIR_SEPARATOR) { tmpdir_buf[tmpdir_len] = LOCAL_DIR_SEPARATOR; tmpdir_buf[tmpdir_len + 1] = 0; } /* remove the tmp_dir and all its contents recursively */ remove_dir(tmpdir_buf, ""); } /* jar creation returned no error */ } } else { if (JAR_DEBUG && verbose) jio_fprintf(stderr, "JAR directories not found for JAR file [%s]\n", buf); if (fname != NULL) sysFree(fname); if (zipEntry != NULL) sysFree(zipEntry); return FALSE; /* could not locate JAR directories - invalid JAR file */ } if (fname != NULL) sysFree(fname); if (zipEntry != NULL) sysFree(zipEntry); if (buffer != NULL) sysFree(buffer); if (jarName != NULL) sysFree(jarName); if ( err!=0 ) { exit(errorCode = 1); } return JARfile;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -