int JarFileParser::do_next_entries(const JvmPathChar* jar_file_name,
                                   const char *suffix,
                                   bool should_match, 
                                   JarFileParser::do_entry_proc f,
                                   int entry_id, int max_size JVM_TRAPS)
{
  GUARANTEE(entry_id >= 0 && max_size > 0, "Sanity");
  int result = -1;

  // JAR entry iteration is not compatible with CacheJarEntries. The 
  // easiest way make iteration co-exist with CacheJarEntries (especially
  // in an MVM environment where other tasks may be loading class files
  // at the same time) is to flush the JarFileParser caches before and after.

#if ENABLE_JAR_ENTRY_CACHE
  // Make sure we don't have a JarFileParser that has entry caching enabled.
  flush_caches();
#endif
  {
    UsingFastOops fast_oops;
    JarFileParser::Fast parser = get(jar_file_name, 
                                     /*enable_entry_cache=*/false
                                     JVM_NO_CHECK);
    if (parser.not_null()) {
      SuffixMatchFilterData data = { suffix, should_match, &parser, f };
      result =
        parser().filtered_do_next_entries(&JarFileParser::suffix_match_filter, 
                                          NULL, &data, entry_id, max_size 
                                          JVM_NO_CHECK);
    }
  }
#if ENABLE_JAR_ENTRY_CACHE
  // Make sure we don't leave behind a JarFileParser that has entry caching
  // disabled.
  flush_caches();
#endif

  return result;
}
示例#2
0
extern "C" void
Jvm_read_jar_entry(const JvmPathChar* jar_file_name, const char* entry_name, 
                   jobject entry) {
   SETUP_ERROR_CHECKER_ARG;
   UsingFastOops fast_oops;
   JarFileParser::Fast parser = JarFileParser::get(jar_file_name JVM_NO_CHECK);
   if (parser.not_null()) {
     bool found = parser().find_entry(entry_name JVM_MUST_SUCCEED);
     if (found) {
       *(ReturnOop*)entry = parser().load_entry(JVM_SINGLE_ARG_NO_CHECK);
     }
   }
   if (CURRENT_HAS_PENDING_EXCEPTION) {
     *(ReturnOop*)entry = NULL;
     Thread::clear_current_pending_exception();
   }

   // This file may not be in the classpath and the MIDP code may try to 
   // delete this file, so let's not keep it in cache. See CR 6357720.
   //
   // IMPL_NOTE: we should add an API to flush a specific cached JarFile.
   JarFileParser::flush_caches();
}
// Create or retrieve a JarFileParser for the given JAR file.
// As a side effect, it might invalidate any previously obtained
// JarFileParser objects by closing their OsFile_handles.
ReturnOop JarFileParser::get(const JvmPathChar* jar_file_name1,
                             TypeArray * jar_file_name2,
                             bool enable_entry_cache JVM_TRAPS) {
  GUARANTEE((jar_file_name1 != NULL && jar_file_name2 == NULL) ||
            (jar_file_name1 == NULL && jar_file_name2 != NULL), "sanity");

  UsingFastOops fast_oops;
  JarFileParser::Fast parser = get_parser_from_cache(jar_file_name1,
                                                     jar_file_name2);
  if (parser.not_null()) {
    return parser;
  }

  if (jar_file_name1 && !OsFile_exists(jar_file_name1)) {
    return NULL;
  }
  if (jar_file_name2 && 
      !OsFile_exists((JvmPathChar *)jar_file_name2->byte_base_address())) {
    return NULL;
  }

  parser = Universe::new_mixed_oop(MixedOopDesc::Type_JarFileParser,
                                   JarFileParser::allocation_size(),
                                   JarFileParser::pointer_count()
                                   JVM_CHECK_0);
  TypeArray::Fast stored_name;
  if (jar_file_name1 != NULL) {
    size_t name_bytes = (fn_strlen(jar_file_name1)+1) * sizeof(JvmPathChar);
    stored_name = Universe::new_byte_array_raw(name_bytes JVM_CHECK_0);
    JvmPathChar *data = (JvmPathChar *)stored_name().byte_base_address();
    jvm_memcpy(data, jar_file_name1, name_bytes); // copy trailing NUL as well.
  } else {
    stored_name = jar_file_name2->obj();
  }


  BufferedFile::Fast bf = BufferedFile::allocate(JVM_SINGLE_ARG_CHECK_0);
  FileDescriptor::Fast desc = FileDescriptor::allocate(JVM_SINGLE_ARG_CHECK_0);

  OsFile_Handle fh = NULL;
  for (int pass=0; pass<2; pass++) {
    if (jar_file_name1) {
      fh = OsFile_open(jar_file_name1, "rb");
    } else {
      fh = OsFile_open((JvmPathChar *)jar_file_name2->byte_base_address(),
                       "rb");
    }
    if (fh != NULL) {
      break;
    }
    if (pass == 1 && fh == NULL) {
      // The system is running low on OsFile_Handles. Make sure we flush
      // the cache, and free all currently cached OsFile_Handles that belong
      // to other JAR files.
      flush_caches();
    }
  }
  if (fh == NULL) {
    return NULL;
  }

  desc().set_handle(fh);
  bf().set_file_pointer(fh);
  bf().set_file_size(fh == NULL ? 0 : OsFile_length(fh));
  parser().set_file_descriptor(&desc);
  parser().set_enable_entry_cache(enable_entry_cache);
  parser().set_pathname(&stored_name);
  parser().set_buffered_file(&bf);
  parser().set_timestamp(++_timestamp);

  if (!parser().find_end_of_central_header()) {
    // The jar file is corrupted. Stop parsing it.
    return NULL;
  }

  parser().save_parser_in_cache(JVM_SINGLE_ARG_MUST_SUCCEED);
  return parser;
}