? .DS_Store
? .cache
? .git
? .project
? .settings
? 243532-follow-up-1.patch
? empty
? logs
? select_add_fields_1.patch
? test.php
? upload-js-fix_1.diff
? sites/all/modules
? sites/default/files
? sites/default/settings.php
? sites/default/test
Index: modules/system/system.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.module,v
retrieving revision 1.635
diff -u -p -r1.635 system.module
--- modules/system/system.module	31 Oct 2008 02:18:22 -0000	1.635
+++ modules/system/system.module	5 Nov 2008 21:59:34 -0000
@@ -1426,18 +1426,15 @@ function system_cron() {
   db_query('DELETE FROM {batch} WHERE timestamp < %d', REQUEST_TIME - 864000);
 
   // Remove temporary files that are older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
-  $result = db_query('SELECT * FROM {files} WHERE status = %d and timestamp < %d', FILE_STATUS_TEMPORARY, REQUEST_TIME - DRUPAL_MAXIMUM_TEMP_FILE_AGE);
-  while ($file = db_fetch_object($result)) {
-    if (file_exists($file->filepath)) {
-      // If files that exist cannot be deleted, continue so the database remains
-      // consistent.
+  $result = db_query('SELECT fid FROM {files} WHERE status & :status AND timestamp < :timestamp', array(':status' => FILE_STATUS_TEMPORARY, ':timestamp' => REQUEST_TIME - DRUPAL_MAXIMUM_TEMP_FILE_AGE));
+  foreach ($result as $row) {
+    if ($file = file_load($row->fid)) {
       if (!file_delete($file)) {
         watchdog('file system', 'Could not delete temporary file "%path" during garbage collection', array('%path' => $file->filepath), WATCHDOG_ERROR);
-        continue;
       }
     }
-    db_query('DELETE FROM {files} WHERE fid = %d', $file->fid);
   }
+
   $core = array('cache', 'cache_block', 'cache_filter', 'cache_page', 'cache_form', 'cache_menu');
   $cache_tables = array_merge(module_invoke_all('flush_caches'), $core);
   foreach ($cache_tables as $table) {
Index: modules/system/system.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.test,v
retrieving revision 1.23
diff -u -p -r1.23 system.test
--- modules/system/system.test	1 Nov 2008 21:21:35 -0000	1.23
+++ modules/system/system.test	5 Nov 2008 21:59:34 -0000
@@ -252,6 +252,42 @@ class CronRunTestCase extends DrupalWebT
     // Execute cron directly.
     $this->assertTrue(drupal_cron_run(), t('Cron ran successfully.'));
   }
+
+  /**
+   * Ensure that temporary files are removed.
+   */
+  function __testTempFileCleanup() {
+    // Create files for all the possible combinations of age and status.
+
+    // Temporary file that's older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
+    $old_temp = file_save_data('foo');
+    file_set_status($old_temp, FILE_STATUS_TEMPORARY);
+    $old_temp->timestamp = 1;
+    $old_temp = file_save($old_temp);
+    $this->assertTrue(file_exists($old_temp->filepath), t('Old temp file was created correctly.'));
+
+    // Temporary file that's less than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
+    $new_temp = file_save_data('foo');
+    file_set_status($new_temp, FILE_STATUS_TEMPORARY);
+    $this->assertTrue(file_exists($new_temp->filepath), t('New temp file was created correctly.'));
+
+    // Permanent file that's older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
+    $old_perm = file_save_data('foo');
+    $old_perm->timestamp = 1;
+    $old_perm = file_save($old_perm);
+    $this->assertTrue(file_exists($old_temp->filepath), t('Old permanent file was created correctly.'));
+
+    // Permanent file that's newer than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
+    $new_perm = file_save_data('foo');
+    $this->assertTrue(file_exists($new_perm->filepath), t('New permanent file was created correctly.'));
+
+    // Run cron and then ensure that only the old, temp file was deleted.
+    $this->assertTrue(drupal_cron_run(), t('Cron ran successfully.'));
+    $this->assertFalse(file_exists($old_temp->filepath), t('Old temp file was correctly removed.'));
+    $this->assertTrue(file_exists($new_temp->filepath), t('New temp file was correctly ignored.'));
+    $this->assertTrue(file_exists($old_temp->filepath), t('Old permanent file was correctly ignored.'));
+    $this->assertTrue(file_exists($new_perm->filepath), t('New permanent file was correctly ignored.'));
+  }
 }
 
 class AdminOverviewTestCase extends DrupalWebTestCase {
