Skip to content

Commit

Permalink
Applied files_table_rename_329301.patch with fuzzing from http://drup…
Browse files Browse the repository at this point in the history
…al.org/node/329301 and corrected failed hunks caused by new database abstraction layer calls.
  • Loading branch information
jmstacey committed Jun 16, 2009
1 parent 8f1ed72 commit 4b0d6e7
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 36 deletions.
12 changes: 6 additions & 6 deletions includes/file.inc
Expand Up @@ -296,7 +296,7 @@ function file_check_location($source, $directory = '') {
* @param $fids
* An array of file IDs.
* @param $conditions
* An array of conditions to match against the {files} table. These
* An array of conditions to match against the {file} table. These
* should be supplied in the form array('field_name' => 'field_value').
* @return
* An array of file objects, indexed by fid.
Expand All @@ -305,7 +305,7 @@ function file_check_location($source, $directory = '') {
* @see file_load()
*/
function file_load_multiple($fids = array(), $conditions = array()) {
$query = db_select('files', 'f')->fields('f');
$query = db_select('file', 'f')->fields('f');

// If the $fids array is populated, add those to the query.
if ($fids) {
Expand Down Expand Up @@ -367,12 +367,12 @@ function file_save($file) {
$file->filesize = filesize($file->filepath);

if (empty($file->fid)) {
drupal_write_record('files', $file);
drupal_write_record('file', $file);
// Inform modules about the newly added file.
module_invoke_all('file_insert', $file);
}
else {
drupal_write_record('files', $file, 'fid');
drupal_write_record('file', $file, 'fid');
// Inform modules that the file has been updated.
module_invoke_all('file_update', $file);
}
Expand Down Expand Up @@ -791,7 +791,7 @@ function file_delete($file, $force = FALSE) {
// Make sure the file is deleted before removing its row from the
// database, so UIs can still find the file in the database.
if (file_unmanaged_delete($file->filepath)) {
db_delete('files')->condition('fid', $file->fid)->execute();
db_delete('file')->condition('fid', $file->fid)->execute();
return TRUE;
}
return FALSE;
Expand Down Expand Up @@ -882,7 +882,7 @@ function file_unmanaged_delete_recursive($path) {
* An integer containing the number of bytes used.
*/
function file_space_used($uid = NULL, $status = FILE_STATUS_PERMANENT) {
$query = db_select('files', 'f');
$query = db_select('file', 'f');
// Use separate placeholders for the status to avoid a bug in some versions
// of PHP. @see http://drupal.org/node/352956
$query->where('f.status & :status1 = :status2', array(':status1' => $status, ':status2' => $status));
Expand Down
26 changes: 13 additions & 13 deletions modules/simpletest/tests/file.test
Expand Up @@ -218,7 +218,7 @@ class FileTestCase extends DrupalWebTestCase {
$file->status = 0;
// Write the record directly rather than calling file_save() so we don't
// invoke the hooks.
$this->assertNotIdentical(drupal_write_record('files', $file), FALSE, t('The file was added to the database.'), 'Create test file');
$this->assertNotIdentical(drupal_write_record('file', $file), FALSE, t('The file was added to the database.'), 'Create test file');

return $file;
}
Expand Down Expand Up @@ -312,16 +312,16 @@ class FileSpaceUsedTest extends FileTestCase {
parent::setUp();

// Create records for a couple of users with different sizes.
drupal_write_record('files', $file = array('uid' => 2, 'filesize' => 50, 'status' => FILE_STATUS_PERMANENT));
drupal_write_record('files', $file = array('uid' => 2, 'filesize' => 20, 'status' => FILE_STATUS_PERMANENT));
drupal_write_record('files', $file = array('uid' => 3, 'filesize' => 100, 'status' => FILE_STATUS_PERMANENT));
drupal_write_record('files', $file = array('uid' => 3, 'filesize' => 200, 'status' => FILE_STATUS_PERMANENT));
drupal_write_record('file', $file = array('uid' => 2, 'filesize' => 50, 'status' => FILE_STATUS_PERMANENT));
drupal_write_record('file', $file = array('uid' => 2, 'filesize' => 20, 'status' => FILE_STATUS_PERMANENT));
drupal_write_record('file', $file = array('uid' => 3, 'filesize' => 100, 'status' => FILE_STATUS_PERMANENT));
drupal_write_record('file', $file = array('uid' => 3, 'filesize' => 200, 'status' => FILE_STATUS_PERMANENT));

// Now create some with other statuses. These values were chosen arbitrarily
// for the sole purpose of testing that bitwise operators were used
// correctly on the field.
drupal_write_record('files', $file = array('uid' => 2, 'filesize' => 1, 'status' => 2 | 8));
drupal_write_record('files', $file = array('uid' => 3, 'filesize' => 3, 'status' => 2 | 4));
drupal_write_record('file', $file = array('uid' => 2, 'filesize' => 1, 'status' => 2 | 8));
drupal_write_record('file', $file = array('uid' => 3, 'filesize' => 3, 'status' => 2 | 4));
}

/**
Expand Down Expand Up @@ -578,7 +578,7 @@ class FileSaveUploadTest extends FileHookTestCase {
$this->image = current($this->drupalGetTestFiles('image'));
$this->assertTrue(is_file($this->image->filepath), t("The file we're going to upload exists."));

$this->maxFidBefore = db_query('SELECT MAX(fid) AS fid FROM {files}')->fetchField();
$this->maxFidBefore = db_query('SELECT MAX(fid) AS fid FROM {file}')->fetchField();

// Upload with replace to gurantee there's something there.
$edit = array(
Expand All @@ -599,7 +599,7 @@ class FileSaveUploadTest extends FileHookTestCase {
* Test the file_save_upload() function.
*/
function testNormal() {
$max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {files}')->fetchField();
$max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file}')->fetchField();
$this->assertTrue($max_fid_after > $this->maxFidBefore, t('A new file was created.'));
$file1 = file_load($max_fid_after);
$this->assertTrue($file1, t('Loaded the file.'));
Expand All @@ -608,13 +608,13 @@ class FileSaveUploadTest extends FileHookTestCase {
file_test_reset();

// Upload a second file.
$max_fid_before = db_query('SELECT MAX(fid) AS fid FROM {files}')->fetchField();
$max_fid_before = db_query('SELECT MAX(fid) AS fid FROM {file}')->fetchField();
$image2 = current($this->drupalGetTestFiles('image'));
$edit = array('files[file_test_upload]' => drupal_realpath($image2->filepath));
$this->drupalPost('file-test/upload', $edit, t('Submit'));
$this->assertResponse(200, t('Received a 200 response for posted test file.'));
$this->assertRaw(t('You WIN!'));
$max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {files}')->fetchField();
$max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file}')->fetchField();

// Check that the correct hooks were called.
$this->assertFileHooksCalled(array('validate', 'insert'));
Expand Down Expand Up @@ -1728,7 +1728,7 @@ class FileSaveTest extends FileHookTestCase {

$this->assertNotNull($saved_file, t("Saving the file should give us back a file object."), 'File');
$this->assertTrue($saved_file->fid > 0, t("A new file ID is set when saving a new file to the database."), 'File');
$loaded_file = db_query('SELECT * FROM {files} f WHERE f.fid = :fid', array(':fid' => $saved_file->fid))->fetch(PDO::FETCH_OBJ);
$loaded_file = db_query('SELECT * FROM {file} f WHERE f.fid = :fid', array(':fid' => $saved_file->fid))->fetch(PDO::FETCH_OBJ);
$this->assertNotNull($loaded_file, t("Record exists in the database."));
$this->assertEqual($loaded_file->status, $file->status, t("Status was saved correctly."));
$this->assertEqual($saved_file->filesize, filesize($file->filepath), t("File size was set correctly."), 'File');
Expand All @@ -1745,7 +1745,7 @@ class FileSaveTest extends FileHookTestCase {

$this->assertEqual($resaved_file->fid, $saved_file->fid, t("The file ID of an existing file is not changed when updating the database."), 'File');
$this->assertTrue($resaved_file->timestamp >= $saved_file->timestamp, t("Timestamp didn't go backwards."), 'File');
$loaded_file = db_query('SELECT * FROM {files} f WHERE f.fid = :fid', array(':fid' => $saved_file->fid))->fetch(PDO::FETCH_OBJ);
$loaded_file = db_query('SELECT * FROM {file} f WHERE f.fid = :fid', array(':fid' => $saved_file->fid))->fetch(PDO::FETCH_OBJ);
$this->assertNotNull($loaded_file, t("Record still exists in the database."), 'File');
$this->assertEqual($loaded_file->status, $saved_file->status, t("Status was saved correctly."));
}
Expand Down
2 changes: 1 addition & 1 deletion modules/system/system.api.php
Expand Up @@ -1250,7 +1250,7 @@ function hook_file_delete($file) {
function hook_file_download($filepath) {
// Check if the file is controlled by the current module.
$filepath = file_create_path($filepath);
$result = db_query("SELECT f.* FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid WHERE filepath = :filepath", array('filepath' => $filepath));
$result = db_query("SELECT f.* FROM {file} f INNER JOIN {upload} u ON f.fid = u.fid WHERE filepath = :filepath", array('filepath' => $filepath));
foreach ($result as $file) {
if (!user_access('view uploaded files')) {
return -1;
Expand Down
84 changes: 83 additions & 1 deletion modules/system/system.install
Expand Up @@ -617,7 +617,7 @@ function system_schema() {
$schema['cache_registry'] = $schema['cache'];
$schema['cache_registry']['description'] = 'Cache table for the code registry system to remember what code files need to be loaded on any given page.';

$schema['files'] = array(
$schema['file'] = array(
'description' => 'Stores information for uploaded files.',
'fields' => array(
'fid' => array(
Expand Down Expand Up @@ -680,6 +680,9 @@ function system_schema() {
'status' => array('status'),
'timestamp' => array('timestamp'),
),
'unique keys' => array(
'filepath' => array('filepath'),
),
'primary key' => array('fid'),
'foreign keys' => array(
'uid' => array('users' => 'uid'),
Expand Down Expand Up @@ -3559,6 +3562,85 @@ function system_update_7027() {
return $ret;
}

/**
* Create the {file} with the unique key on the filepath.
*/
function system_update_7021() {
$ret = array();

$schema['file'] = array(
'description' => 'Stores information for uploaded files.',
'fields' => array(
'fid' => array(
'description' => 'File ID.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'uid' => array(
'description' => 'The {user}.uid of the user who is associated with the file.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'filename' => array(
'description' => 'Name of the file with no path components. This may differ from the basename of the filepath if the file is renamed to avoid overwriting an existing file.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'filepath' => array(
'description' => 'Path of the file relative to Drupal root.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'filemime' => array(
'description' => "The file's MIME type.",
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'filesize' => array(
'description' => 'The size of the file in bytes.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'status' => array(
'description' => 'A bitmapped field indicating the status of the file the least sigifigant bit indicates temporary (1) or permanent (0). Temporary files older than DRUPAL_MAXIMUM_TEMP_FILE_AGE will be removed during a cron run.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'timestamp' => array(
'description' => 'UNIX timestamp for when the file was added.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'indexes' => array(
'uid' => array('uid'),
'status' => array('status'),
'timestamp' => array('timestamp'),
),
'unique keys' => array(
'filepath' => array('filepath'),
),
'primary key' => array('fid'),
);
db_create_table($ret, 'file', $schema['file']);

return $ret;
}

/**
* @} End of "defgroup updates-6.x-to-7.x"
* The next series of updates should start at 8000.
Expand Down
2 changes: 1 addition & 1 deletion modules/system/system.module
Expand Up @@ -1752,7 +1752,7 @@ function system_cron() {
// Remove temporary files that are older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
// Use separate placeholders for the status to avoid a bug in some versions
// of PHP. See http://drupal.org/node/352956
$result = db_query('SELECT fid FROM {files} WHERE status & :permanent1 <> :permanent2 AND timestamp < :timestamp', array(
$result = db_query('SELECT fid FROM {file} WHERE status & :permanent1 <> :permanent2 AND timestamp < :timestamp', array(
':permanent1' => FILE_STATUS_PERMANENT,
':permanent2' => FILE_STATUS_PERMANENT,
':timestamp' => REQUEST_TIME - DRUPAL_MAXIMUM_TEMP_FILE_AGE
Expand Down
6 changes: 3 additions & 3 deletions modules/system/system.test
Expand Up @@ -332,7 +332,7 @@ class CronRunTestCase extends DrupalWebTestCase {

// Temporary file that is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
$temp_old = file_save_data('');
db_update('files')
db_update('file')
->fields(array(
'status' => 0,
'timestamp' => 1,
Expand All @@ -343,15 +343,15 @@ class CronRunTestCase extends DrupalWebTestCase {

// Temporary file that is less than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
$temp_new = file_save_data('');
db_update('files')
db_update('file')
->fields(array('status' => 0))
->condition('fid', $temp_new->fid)
->execute();
$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_update('files')
db_update('file')
->fields(array('timestamp' => 1))
->condition('fid', $temp_old->fid)
->execute();
Expand Down
52 changes: 51 additions & 1 deletion modules/upload/upload.install
Expand Up @@ -43,7 +43,7 @@ function upload_schema() {
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Primary Key: The {files}.fid.',
'description' => 'Primary Key: The {file}.fid.',
),
'nid' => array(
'type' => 'int',
Expand Down Expand Up @@ -98,3 +98,53 @@ function upload_schema() {
}


/**
* Migrate upload module files from {files} to {file}.
*/
function upload_update_7000(&$sandbox) {
$ret = array();

if (!isset($sandbox['progress'])) {
// Initialize batch update information.
$sandbox['progress'] = 0;
$sandbox['last_fid_processed'] = -1;
$sandbox['max'] = db_query("SELECT COUNT(DISTINCT u.fid) FROM {upload} u")->fetchField();
}

// As a batch operation move records from {files} into the {file} table.
$limit = 500;
$result = db_query_range("SELECT DISTINCT u.fid FROM {upload} u ORDER BY u.vid", array(), 0, $limit);
foreach ($result as $record) {
$old_file = db_query('SELECT f.* FROM {files} f WHERE f.fid = :fid', array(':fid' => $record->fid))->fetch(PDO::FETCH_OBJ);
if (!$old_file) {
continue;
}

$new_file = db_query('SELECT f.* FROM {files} f WHERE f.filepath = :filepath', array(':filepath' => $old_file->filepath))->fetch(PDO::FETCH_OBJ);
if (!$new_file) {
// Re-save the file into the new {file} table.
$new_file = clone $old_file;
drupal_write_record('file', $new_file);
}

// If the fid has changed we need to update the {upload} record to use the
// new id.
if (!empty($new_file->fid) && ($new_file->fid != $old_file->fid)) {
db_update('upload')
->fields(array('fid' => $new_file->fid))
->condition('fid', $old_file->fid)
->execute();
}

// Update our progress information for the batch update.
$sandbox['progress']++;
$sandbox['last_fid_processed'] = $old_file->fid;
}

// Indicate our current progress to the batch update system. If there's no
// max value then there's nothing to update and we're finished.
$ret['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);

return $ret;
}

4 changes: 2 additions & 2 deletions modules/upload/upload.module
Expand Up @@ -150,7 +150,7 @@ function _upload_file_limits($user) {
*/
function upload_file_download($filepath) {
$filepath = file_create_path($filepath);
$file = db_query("SELECT f.*, u.nid FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid WHERE filepath = :path", array(':path' => $filepath))->fetchObject();
$file = db_query("SELECT f.*, u.nid FROM {file} f INNER JOIN {upload} u ON f.fid = u.fid WHERE filepath = :path", array(':path' => $filepath))->fetchObject();

if ($file && user_access('view uploaded files') && ($node = node_load($file->nid)) && node_access('view', $node)) {
return array(
Expand Down Expand Up @@ -475,7 +475,7 @@ function upload_space_used($uid) {
* The amount of disk space used by uploaded files in bytes.
*/
function upload_total_space_used() {
return db_query('SELECT SUM(f.filesize) FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid')->fetchField();
return db_query('SELECT SUM(f.filesize) FROM {file} f INNER JOIN {upload} u ON f.fid = u.fid')->fetchField();
}

function upload_save($node) {
Expand Down

0 comments on commit 4b0d6e7

Please sign in to comment.