Index: install.php
===================================================================
--- install.php	(revision 96)
+++ install.php	(working copy)
@@ -645,7 +645,7 @@
   // Install profile modules.
   if ($task == 'profile-install') {
     $modules = variable_get('install_profile_modules', array());
-    $files = module_rebuild_cache();
+    $files = system_get_module_data();
     variable_del('install_profile_modules');
     $operations = array();
     foreach ($modules as $module) {
Index: includes/theme.maintenance.inc
===================================================================
--- includes/theme.maintenance.inc	(revision 96)
+++ includes/theme.maintenance.inc	(working copy)
@@ -210,7 +210,7 @@
 
   global $theme;
   // Retrieve the theme data to list all available regions.
-  $theme_data = _system_theme_data();
+  $theme_data = _system_get_theme_data();
   $regions = $theme_data[$theme]->info['regions'];
 
   // Get all region content set with drupal_set_content().
Index: includes/module.inc
===================================================================
--- includes/module.inc	(revision 96)
+++ includes/module.inc	(working copy)
@@ -94,61 +94,22 @@
  *   The array of filesystem objects used to rebuild the cache.
  */
 function module_rebuild_cache() {
-  // Get current list of modules
-  $files = drupal_system_listing('\.module$', 'modules', 'name', 0);
+  return system_get_module_data();
+}
 
-  // Extract current files from database.
-  system_get_files_database($files, 'module');
-
-  ksort($files);
-
-  // Set defaults for module info
-  $defaults = array(
-    'dependencies' => array(),
-    'dependents' => array(),
-    'description' => '',
-    'version' => NULL,
-    'php' => DRUPAL_MINIMUM_PHP,
-  );
-
-  foreach ($files as $filename => $file) {
-    // Look for the info file.
-    $file->info = drupal_parse_info_file(dirname($file->filename) .'/'. $file->name .'.info');
-
-    // Skip modules that don't provide info.
-    if (empty($file->info)) {
-      unset($files[$filename]);
-      continue;
-    }
-    // Merge in defaults and save.
-    $files[$filename]->info = $file->info + $defaults;
-
-    // Invoke hook_system_info_alter() to give installed modules a chance to
-    // modify the data in the .info files if necessary.
-    drupal_alter('system_info', $files[$filename]->info, $files[$filename]);
-
-    // Log the critical hooks implemented by this module.
-    $bootstrap = 0;
-    foreach (bootstrap_hooks() as $hook) {
-      if (module_hook($file->name, $hook)) {
-        $bootstrap = 1;
-        break;
-      }
-    }
-
-    // Update the contents of the system table:
-    if (isset($file->status) || (isset($file->old_filename) && $file->old_filename != $file->filename)) {
-      db_query("UPDATE {system} SET info = '%s', name = '%s', filename = '%s', bootstrap = %d WHERE filename = '%s'", serialize($files[$filename]->info), $file->name, $file->filename, $bootstrap, $file->old_filename);
-    }
-    else {
-      // This is a new module.
-      $files[$filename]->status = 0;
-      $files[$filename]->throttle = 0;
-      db_query("INSERT INTO {system} (name, info, type, filename, status, throttle, bootstrap) VALUES ('%s', '%s', '%s', '%s', %d, %d, %d)", $file->name, serialize($files[$filename]->info), 'module', $file->filename, 0, 0, $bootstrap);
-    }
-  }
-  $files = _module_build_dependencies($files);
-  return $files;
+/**
+ * Collect data about all currently available modules.
+ *
+ * @return
+ *   Array of all available modules and their data.
+ */
+function system_get_module_data() {
+  $modules = _system_get_module_data();
+  ksort($modules);
+  system_get_files_database($modules, 'module');
+  system_update_files_database($modules, 'module');
+  $modules = _module_build_dependencies($modules);
+  return $modules;
 }
 
 /**
Index: includes/common.inc
===================================================================
--- includes/common.inc	(revision 96)
+++ includes/common.inc	(working copy)
@@ -3653,10 +3653,10 @@
   // If invoked from update.php, we must not update the theme information in the
   // database, or this will result in all themes being disabled.
   if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'update') {
-    _system_theme_data();
+    _system_get_theme_data();
   }
   else {
-    system_theme_data();
+    system_get_theme_data();
   }
 
   drupal_rebuild_theme_registry();
Index: includes/theme.inc
===================================================================
--- includes/theme.inc	(revision 96)
+++ includes/theme.inc	(working copy)
@@ -456,7 +456,7 @@
     }
     else {
       // Scan the installation when the database should not be read.
-      $themes = _system_theme_data();
+      $themes = _system_get_theme_data();
     }
 
     foreach ($themes as $theme) {
Index: includes/install.inc
===================================================================
--- includes/install.inc	(revision 96)
+++ includes/install.inc	(working copy)
@@ -309,7 +309,7 @@
  *   The modules to install.
  */
 function drupal_install_modules($module_list = array()) {
-  $files = module_rebuild_cache();
+  $files = system_get_module_data();
   $module_list = array_flip(array_values($module_list));
   do {
     $moved = FALSE;
@@ -363,7 +363,7 @@
   db_query("INSERT INTO {system} (filename, name, type, owner, status, throttle, bootstrap, schema_version) VALUES('%s', '%s', '%s', '%s', %d, %d, %d, %d)", $system_path .'/system.module', 'system', 'module', '', 1, 0, 0, $system_version);
   // Now that we've installed things properly, bootstrap the full Drupal environment
   drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
-  module_rebuild_cache();
+  system_get_module_data();
 }
 
 
Index: modules/system/system.admin.inc
===================================================================
--- modules/system/system.admin.inc	(revision 96)
+++ modules/system/system.admin.inc	(working copy)
@@ -98,7 +98,7 @@
  */
 function system_admin_by_module() {
 
-  $modules = module_rebuild_cache();
+  $modules = system_get_module_data();
   $menu_items = array();
   $help_arg = module_exists('help') ? drupal_help_arg() : FALSE;
 
@@ -151,7 +151,7 @@
  * @see system_settings_form()
  */
 function system_admin_theme_settings() {
-  $themes = system_theme_data();
+  $themes = system_get_theme_data();
 
   uasort($themes, 'system_sort_modules_by_info_name');
 
@@ -188,7 +188,7 @@
 function system_themes_form() {
 
   drupal_clear_css_cache();
-  $themes = system_theme_data();
+  $themes = system_get_theme_data();
 
   uasort($themes, 'system_sort_modules_by_info_name');
 
@@ -332,7 +332,7 @@
   if ($key) {
     $settings = theme_get_settings($key);
     $var = str_replace('/', '_', 'theme_'. $key .'_settings');
-    $themes = system_theme_data();
+    $themes = system_get_theme_data();
     $features = $themes[$key]->info['features'];
   }
   else {
@@ -623,7 +623,7 @@
   menu_rebuild();
   cache_clear_all('schema', 'cache');
   // Get current list of modules.
-  $files = module_rebuild_cache();
+  $files = system_get_module_data();
 
   uasort($files, 'system_sort_modules_by_info_name');
 
@@ -801,7 +801,7 @@
  * Display confirmation form for dependencies.
  *
  * @param $modules
- *   Array of module file objects as returned from module_rebuild_cache().
+ *   Array of module file objects as returned from system_get_module_data().
  * @param $storage
  *   The contents of $form_state['storage']; an array with two
  *   elements: the list of dependencies and the list of status
Index: modules/system/system.install
===================================================================
--- modules/system/system.install	(revision 96)
+++ modules/system/system.install	(working copy)
@@ -359,7 +359,7 @@
   }
 
   // Load system theme data appropriately.
-  system_theme_data();
+  system_get_theme_data();
 
   // Inserting uid 0 here confuses MySQL -- the next user might be created as
   // uid 2 which is not what we want. So we insert the first user here, the
@@ -1292,8 +1292,8 @@
   db_drop_field($ret, 'system', 'description');
 
   // Rebuild system table contents.
-  module_rebuild_cache();
-  system_theme_data();
+  system_get_module_data();
+  system_get_theme_data();
 
   return $ret;
 }
@@ -1364,8 +1364,8 @@
  */
 function system_update_6013() {
   // Rebuild system table contents.
-  module_rebuild_cache();
-  system_theme_data();
+  system_get_module_data();
+  system_get_theme_data();
 
   return array(array('success' => TRUE, 'query' => 'Cache rebuilt.'));
 }
Index: modules/system/system.module
===================================================================
--- modules/system/system.module	(revision 96)
+++ modules/system/system.module	(working copy)
@@ -805,23 +805,86 @@
  *   Array of all available themes and their data.
  */
 function system_theme_data() {
-  // Scan the installation theme .info files and their engines.
-  $themes = _system_theme_data();
+  return system_get_theme_data();
+}
 
-  // Extract current files from database.
+/**
+ * Collect data about all currently available themes.
+ *
+ * @return
+ *   Array of all available themes and their data.
+ */
+function system_get_theme_data() {
+  $themes = _system_get_theme_data();
+  ksort($themes);
   system_get_files_database($themes, 'theme');
+  system_update_files_database($themes, 'theme');
+  return $themes;
+}
 
-  db_query("DELETE FROM {system} WHERE type = 'theme'");
+/**
+ * Updates the records in the system table based on the files array.
+ *
+ * @param $files_updated
+ *   An array of files.
+ * @param $type
+ *   The type of the files.
+ */
+function system_update_files_database(&$files_updated, $type) {
+  // Need to make a safe, modifiable copy of the $files array since PHP
+  // automatically makes references to objects instead of copies.
+  $files = array();
+  foreach ($files_updated as $key => $file) {
+    $files[$key] = clone $file;
+    $files[$key]->info = serialize($file->info);
+  }
 
-  foreach ($themes as $theme) {
-    if (!isset($theme->owner)) {
-      $theme->owner = '';
+  $result = db_query("SELECT * FROM {system} WHERE type = '%s'", $type);
+  while ($file = db_fetch_object($result)) {
+    if (isset($files[$file->name]) && is_object($files[$file->name])) {
+      // Keep the old filename from the database in case the file has moved.
+      $file->old_filename = $file->filename;
+
+      // Scan fields to find only the updated values.
+      $file->updated_fields = array();
+      $sql_sets = array();
+      $sql_args = array();
+      foreach ($file as $key => $value) {
+        if (isset($files[$file->name]->$key) && $files[$file->name]->$key != $value) {
+          $file->updated_fields[$key] = $files[$file->name]->$key;
+          $sql_sets[] = "$key = '%s'";
+          $sql_args[] = $files[$file->name]->$key;
+        }
+      }
+
+      // Update the record.
+      if (count($file->updated_fields)) {
+       $sql_args[] = $file->old_filename;
+       db_query("UPDATE {system} SET " . implode(',',$sql_sets) . " WHERE filename='%s'",$sql_args);
+      }
+
+      // Remove the file so that we don't try to insert it later.
+      unset($files[$file->name]);
     }
+    else {
+      // File is not found in filesystem, so delete record from the system table.
+      db_query("DELETE FROM {system} WHERE filename='" . $file->filename . "'");
+    }
+  }
 
-    db_query("INSERT INTO {system} (name, owner, info, type, filename, status, throttle, bootstrap) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, %d)", $theme->name, $theme->owner, serialize($theme->info), 'theme', $theme->filename, isset($theme->status) ? $theme->status : 0, 0, 0);
+  // All remaining files are not in the system table, so we need to add them.
+  foreach ($files as $file) {
+    db_query("INSERT INTO {system} (filename,name,type,owner,bootstrap,info) VALUES ('%s','%s','%s','%s',%d,'%s')",
+        $file->filename,
+        $file->name,
+        $type,
+        isset($file->owner) ? $file->owner : '',
+        isset($file->bootstrap) ? $file->bootstrap : 0,
+        $file->info);
+    $files_updated[$file->name]->type = $type;
+    $files_updated[$file->name]->status = 0;
+    $files_updated[$file->name]->schema_version = -1;
   }
-
-  return $themes;
 }
 
 /**
@@ -830,7 +893,7 @@
  * @return
  *   An associative array of themes information.
  */
-function _system_theme_data() {
+function _system_get_theme_data() {
   static $themes_info = array();
 
   if (empty($themes_info)) {
@@ -918,6 +981,56 @@
 }
 
 /**
+ * Helper function to scan and collect module .info data.
+ *
+ * @return
+ *   An associative array of module information.
+ */
+function _system_get_module_data() {
+  // Find modules.
+  $modules = drupal_system_listing('\.module$', 'modules', 'name', 0);
+
+  // Set defaults for module info.
+  $defaults = array(
+    'dependencies' => array(),
+    'dependents' => array(),
+    'description' => '',
+    'version' => NULL,
+    'php' => DRUPAL_MINIMUM_PHP,
+  );
+
+  // Read info files for each theme.
+  foreach ($modules as $key => $module) {
+    // Look for the info file.
+    $module->info = drupal_parse_info_file(dirname($module->filename) . '/' . $module->name . '.info');
+
+    // Skip modules that don't provide info.
+    if (empty($module->info)) {
+      unset($modules[$key]);
+      continue;
+    }
+
+    // Merge in defaults and save.
+    $modules[$key]->info = $module->info + $defaults;
+
+    // Invoke hook_system_info_alter() to give installed modules a chance to
+    // modify the data in the .info files if necessary.
+    drupal_alter('system_info', $modules[$key]->info, $modules[$key]);
+
+    // Log the critical hooks implemented by this module.
+    $modules[$key]->bootstrap = 0;
+    foreach (bootstrap_hooks() as $hook) {
+      if (module_hook($modules[$key]->name, $hook)) {
+        $modules[$key]->bootstrap = 1;
+        break;
+      }
+    }
+  }
+
+  return $modules;
+}
+
+/**
  * Recursive function to find the top level base theme. Themes can inherit
  * templates and function implementations from earlier themes.
  *
Index: modules/update/update.compare.inc
===================================================================
--- modules/update/update.compare.inc	(revision 96)
+++ modules/update/update.compare.inc	(working copy)
@@ -36,8 +36,8 @@
     $projects = update_project_cache('update_project_projects');
     if (empty($projects)) {
       // Still empty, so we have to rebuild the cache.
-      _update_process_info_list($projects, module_rebuild_cache(), 'module');
-      _update_process_info_list($projects, system_theme_data(), 'theme');
+      _update_process_info_list($projects, system_get_module_data(), 'module');
+      _update_process_info_list($projects, system_get_theme_data(), 'theme');
       // Allow other modules to alter projects before fetching and comparing.
       drupal_alter('update_projects', $projects);
       // Cache the site's project data for at most 1 hour.
Index: modules/help/help.admin.inc
===================================================================
--- modules/help/help.admin.inc	(revision 96)
+++ modules/help/help.admin.inc	(working copy)
@@ -47,7 +47,7 @@
 
 function help_links_as_list() {
   $empty_arg = drupal_help_arg();
-  $module_info = module_rebuild_cache();
+  $module_info = system_get_module_data();
 
   $modules = array();
   foreach (module_implements('help', TRUE) as $module) {
Index: update.php
===================================================================
--- update.php	(revision 96)
+++ update.php	(working copy)
@@ -448,8 +448,8 @@
 
   // Store values of expensive functions for future use.
   if (empty($themes) || empty($modules)) {
-    $themes = _system_theme_data();
-    $modules = module_rebuild_cache();
+    $themes = system_get_theme_data();
+    $modules = system_get_module_data();
   }
 
   if ($type == 'module' && isset($modules[$name])) {
