? .INSTALL.sqlite.txt.swp
? .cache
? .git
? .project
? .settings
? 314870_2.patch
? INSTALL.sqlite.txt
? empty
? file_276280_0.patch
? file_330633_1.patch
? logs
? 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.646
diff -u -p -r1.646 system.module
--- modules/system/system.module	24 Nov 2008 10:41:40 -0000	1.646
+++ modules/system/system.module	24 Nov 2008 12:46:06 -0000
@@ -1509,18 +1509,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 & :permanent != :permanent AND timestamp < :timestamp', array(':permanent' => FILE_STATUS_PERMANENT, ':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.25
diff -u -p -r1.25 system.test
--- modules/system/system.test	24 Nov 2008 10:41:40 -0000	1.25
+++ modules/system/system.test	24 Nov 2008 12:46:07 -0000
@@ -252,6 +252,41 @@ 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. We're
+    // using UPDATE statments rather than file_save() because it would set the
+    // timestamp.
+
+    // Temporary file that is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
+    $temp_old = file_save_data('');
+    db_query('UPDATE {files} SET status = :status, timestamp = :timestamp WHERE fid = :fid', array(':status' => FILE_STATUS_TEMPORARY, ':timestamp' => 1, ':fid' => $temp_old->fid));
+    $this->assertTrue(file_exists($temp_old->filepath), t('Old temp file was created correctly.'));
+
+    // Temporary file that is less than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
+    $temp_new = file_save_data('');
+    db_query('UPDATE {files} SET status = :status WHERE fid = :fid', array(':status' => FILE_STATUS_TEMPORARY, ':fid' => $temp_new->fid));
+    $this->assertTrue(file_exists($temp_new->filepath), t('New temp file was created correctly.'));
+
+    // Permanent file that is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
+    $perm_old = file_save_data('');
+    db_query('UPDATE {files} SET timestamp = :timestamp WHERE fid = :fid', array(':timestamp' => 1, ':fid' => $perm_old->fid));
+    $this->assertTrue(file_exists($perm_old->filepath), t('Old permanent file was created correctly.'));
+
+    // Permanent file that is newer than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
+    $perm_new = file_save_data('');
+    $this->assertTrue(file_exists($perm_new->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($temp_old->filepath), t('Old temp file was correctly removed.'));
+    $this->assertTrue(file_exists($temp_new->filepath), t('New temp file was correctly ignored.'));
+    $this->assertTrue(file_exists($perm_old->filepath), t('Old permanent file was correctly ignored.'));
+    $this->assertTrue(file_exists($perm_new->filepath), t('New permanent file was correctly ignored.'));
+  }
 }
 
 class AdminOverviewTestCase extends DrupalWebTestCase {
