Problem/Motivation

Hi,

file_copy() D7 docs say:
"If this value is omitted, Drupal's default files scheme will be used, usually "public://"."

From my experience this is not true.

file_valid_uri() will call file_uri_scheme() with the NULL value and they just return false and you get all those warnigs displayed.

My patch adds an empty check so we can just use file_unmanaged_copy() as this will handle a NULL destination correctly.

Steps to reproduce

Proposed resolution

TBD

Remaining tasks

TBD

User interface changes

API changes

Data model changes

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Kars-T’s picture

Renamed patch for autotesting.

trobey’s picture

Status: Needs review » Reviewed & tested by the community

The function signature for file_copy and file_unmanaged_copy:

function file_copy(stdClass $source, $destination = NULL, $replace = FILE_EXISTS_RENAME)

function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME)

If NULL is used as the default argument for $destination then the two functions have to work with this default; otherwise, NULL should not be specified as the default and the function should require two arguments. The first thing that is called in file_copy() is file_valid_uri(). file_valid_uri() calls file_uri_scheme(). file_uri_scheme returns FALSE for a NULL argument. The fix in the patch corrects this problem.

Once that is fixed, NULL is passed to file_unmanaged_copy(). After checking the $source, we have this code:

// Build a destination URI if necessary.
if (!isset($destination)) {
$destination = file_build_uri(basename($source));
}

So the code in the patch should work fine.

Testing

I installed the examples module and used the file_example to create a file. I added a file_copy to the end of the submit function to test this. A second argument was not supplied. The result was the error:

The specified file public://test/test could not be copied, because the destination is invalid. More information is available in the system log.

After the patch the file was copied with no error.

catch’s picture

Version: 7.x-dev » 8.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs backport to D7

Why can't file_valid_uri() check for empty() instead?

Moving to 8.x and adding backport tag.

Kars-T’s picture

Why can't file_valid_uri() check for empty() instead?

I not 100% sure about if there is any agreement for core development. But as it is an API function maybe it should require some string or it wouldn't make sense?

trobey’s picture

I do not think it is a good idea for file_valid_uri() to check for empty. It is called by functions such as file_delete(). File_delete() would either have to handle the empty case or it would try to remove a null file and delete it from the database. Furthermore, the documentation requires that a scheme such as private, public or temporary be supplied. Since NULL is often used to return a failure I do not think passing it to field_valid_uri() should pass. If it does than code will have to check for both empty() and field_valid_uri().

catch’s picture

I meant file_valid_uri() could check for empty and immediately return false. Null isn't a valid file uri. If we think it's a silly check to do and it should just happen in file_copy() then fine but I wan't suggesting return true from it.

trobey’s picture

The problem is that the default value of $destination is null. file_copy() must be able to handle a default argument. You stated that null is not a valid file uri. If file_valid_uri returns false for a null URI then file_copy() fails. So it makes sense to check for null before generating error messages and exiting just like this patch does.

jhedstrom’s picture

Issue summary: View changes
Issue tags: +Needs reroll

Needs a reroll.

AjitS’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
861 bytes

Rerolled manually for 8.0.x

mgifford’s picture

Re-submitting the prior patch for the bots to review.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

20th’s picture

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

quietone’s picture

Version: 8.9.x-dev » 9.3.x-dev
Category: Bug report » Task
Issue summary: View changes
Issue tags: +Bug Smash Initiative
FileSize
2.02 KB

Triaging for Bug Smash Initiative.

This issue is from 2011 and while the file system has had many changes since then the $destination argument is not tested for NULL in file_copy. I looked for tests of that case and could not find any, so I wrote a test for when the destination is null. The tests pass without changing any code so functionally there is no bug. I suppose an argument could be made for some optimization, to test $destination early in the function, log a message and exit. If that is done this is still not a bug, so changing to a task.

quietone’s picture

Ignore that patch.

kim.pepper’s picture

#3223209: deprecate file_save_data, file_copy and file_move and replace with a service makes $destination a required parameter, so this should resolve the issue.

andypost’s picture

phenaproxima’s picture

Status: Needs review » Closed (duplicate)

Per #1048114: file_copy(), $destination cannot be NULL, I've transferred credit to that issue and am closing this one out as a duplicate.