#895308: kill file_directory_path().

From: Damien Tournoud <damien@commerceguys.com>


---
 common.inc                            |    2 -
 file.inc                              |   51 ++++++---------------
 updater.inc                           |    2 -
 file/file.field.inc                   |   10 ----
 image/image.install                   |    7 +--
 image/image.module                    |   13 +++--
 image/image.test                      |    5 +-
 locale/locale.test                    |    4 +-
 simpletest/drupal_web_test_case.php   |   11 ++--
 simpletest/simpletest.install         |    2 -
 simpletest/simpletest.module          |    4 +-
 simpletest/simpletest.test            |   10 ++--
 simpletest/tests/file.test            |   82 ++++++++++++---------------------
 simpletest/tests/filetransfer.test    |    4 +-
 simpletest/tests/image.test           |    2 -
 simpletest/tests/registry.test        |    2 -
 simpletest/tests/session.test         |    2 -
 simpletest/tests/upgrade/upgrade.test |    2 -
 system/system.admin.inc               |    6 +-
 system/system.install                 |    6 +-
 user/user.admin.inc                   |    4 +-
 user/user.module                      |    2 -
 22 files changed, 91 insertions(+), 142 deletions(-)

diff --git includes/common.inc includes/common.inc
index bc44cf8..5d068da 100644
--- includes/common.inc
+++ includes/common.inc
@@ -4587,7 +4587,7 @@ function _drupal_bootstrap_full() {
     // Running inside the simpletest child site, log fatal errors to test
     // specific file directory.
     ini_set('log_errors', 1);
-    ini_set('error_log', file_directory_path() . '/error.log');
+    ini_set('error_log', 'public://error.log');
   }
 
   // Initialize $_GET['q'] prior to invoking hook_init().
diff --git includes/file.inc includes/file.inc
index cd36ebf..486cbd8 100644
--- includes/file.inc
+++ includes/file.inc
@@ -223,6 +223,16 @@ function file_uri_target($uri) {
 }
 
 /**
+ * Get the default file stream implementation.
+ *
+ * @return
+ *   'public', 'private' or any other file scheme defined as the default.
+ */
+function file_default_scheme() {
+  return variable_get('file_default_scheme', 'public');
+}
+
+/**
  * Normalizes a URI by making it syntactically correct.
  *
  * A stream is referenced as "scheme://target".
@@ -317,11 +327,9 @@ function file_stream_wrapper_get_instance_by_scheme($scheme) {
  * @see http://drupal.org/node/515192
  *
  * There are two kinds of local files:
- * - "created files", i.e. those in the files directory (which is stored in
- *   the file_directory_path variable and can be retrieved using
- *   file_directory_path()). These are files that have either been uploaded by
- *   users or were generated automatically (for example through CSS
- *   aggregation).
+ * - "managed files", i.e. those stored by a Drupal-compatible stream wrapper.
+ *   These are files that have either been uploaded by users or were generated
+ *   automatically (for example through CSS aggregation).
  * - "shipped files", i.e. those outside of the files directory, which ship as
  *   part of Drupal core or contributed modules or themes.
  *
@@ -829,7 +837,7 @@ function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXIST
  * Given a relative path, construct a URI into Drupal's default files location.
  */
 function file_build_uri($path) {
-  $uri = variable_get('file_default_scheme', 'public') . '://' . $path;
+  $uri = file_default_scheme() . '://' . $path;
   return file_stream_wrapper_uri_normalize($uri);
 }
 
@@ -1889,33 +1897,6 @@ function file_scan_directory($dir, $mask, $options = array(), $depth = 0) {
 }
 
 /**
- * Determines the local directory path of a given wrapper.
- *
- * This function will return the directory path of a stream wrapper. A stream
- * is referenced as: "scheme://target".  For example, a scheme of "public"
- * might return "sites/default/files" or "temporary" might return "/tmp".
- *
- * @param $scheme
- *   A string representing the scheme of a stream. The default wrapper is
- *   is assumed if this is not provided.
- *
- * @return
- *   A string containing the directory path of a stream. FALSE is returned if
- *   the scheme is invalid or a wrapper could not be instantiated.
- */
-function file_directory_path($scheme = NULL) {
-  if (empty($scheme)) {
-    $scheme = variable_get('file_default_scheme', 'public');
-  }
-  if ($wrapper = file_stream_wrapper_get_instance_by_scheme($scheme)) {
-    return $wrapper->getDirectoryPath();
-  }
-  else {
-    return FALSE;
-  }
-}
-
-/**
  * Determine the maximum file upload size by querying the PHP settings.
  *
  * @return
@@ -2269,9 +2250,9 @@ function file_directory_temp() {
       }
     }
 
-    // if a directory has been found, use it, otherwise default to 'files/tmp' or 'files\\tmp'.
     if (empty($temporary_directory)) {
-      $temporary_directory = file_directory_path() . $path_delimiter . 'tmp';
+      // If no directory has been found default to 'files/tmp' or 'files\\tmp'.
+      $temporary_directory = variable_get('file_public_path', conf_path() . '/files') . $path_delimiter . 'tmp';
     }
     // Save the path of the discovered directory.
     variable_set('file_directory_temp', $temporary_directory);
diff --git includes/updater.inc includes/updater.inc
index ca9825a..7388af9 100644
--- includes/updater.inc
+++ includes/updater.inc
@@ -368,7 +368,7 @@ class Updater {
    * Return the full path to a directory where backups should be written.
    */
   public function getBackupDir() {
-    return file_directory_path('temporary');
+    return file_stream_wrapper_get_instance_by_scheme('temporary')->getDirectoryPath();
   }
 
   /**
diff --git modules/file/file.field.inc modules/file/file.field.inc
index 6f4cce7..4ed8e35 100644
--- modules/file/file.field.inc
+++ modules/file/file.field.inc
@@ -193,15 +193,7 @@ function _file_generic_settings_extensions($element, &$form_state) {
 function _file_generic_settings_file_directory_validate($element, &$form_state) {
   // Strip slashes from the beginning and end of $widget['file_directory'].
   $value = trim($element['#value'], '\\/');
-
-  // Do not allow the file path to be the same as the file_directory_path().
-  // This causes all sorts of problems with things like file_create_url().
-  if (strpos($value, file_directory_path()) === 0) {
-    form_error($element, t('The file directory (@file_directory) cannot start with the system files directory (@files_directory), as this may cause conflicts when building file URLs.', array('@file_directory' => $form_state['values']['file_directory'], '@files_directory' => file_directory_path())));
-  }
-  else {
-    form_set_value($element, $value, $form_state);
-  }
+  form_set_value($element, $value, $form_state);
 }
 
 /**
diff --git modules/image/image.install modules/image/image.install
index 5bdc235..c4c7b2a 100644
--- modules/image/image.install
+++ modules/image/image.install
@@ -11,8 +11,8 @@
  */
 function image_install() {
   // Create the styles directory and ensure it's writable.
-  $path = file_directory_path() . '/styles';
-  file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
+  $directory = file_default_scheme() . '://styles';
+  file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
 }
 
 /**
@@ -20,8 +20,7 @@ function image_install() {
  */
 function image_uninstall() {
   // Remove the styles directory and generated images.
-  $path = file_directory_path() . '/styles';
-  file_unmanaged_delete_recursive($path);
+  file_unmanaged_delete_recursive(file_default_scheme() . '://styles');
 }
 
 /**
diff --git modules/image/image.module modules/image/image.module
index 1dadcb0..fd3651e 100644
--- modules/image/image.module
+++ modules/image/image.module
@@ -75,11 +75,12 @@ function image_menu() {
   // If clean URLs are disabled, image derivatives will always be served 
   // through the menu system.
   // If clean URLs are enabled and the image derivative already exists,
-  // PHP will be bypassed. 
-  $items[file_directory_path('public') . '/styles/%image_style'] = array(
+  // PHP will be bypassed.
+  $directory_path = file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath();
+  $items[$directory_path . '/styles/%image_style'] = array(
     'title' => 'Generate image style',
     'page callback' => 'image_style_deliver',
-    'page arguments' => array(count(explode('/', file_directory_path())) + 1),
+    'page arguments' => array(count(explode('/', $directory_path)) + 1),
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
   );
@@ -763,7 +764,7 @@ function image_style_create_derivative($style, $source, $destination) {
  *   An image style array.
  */
 function image_style_flush($style) {
-  $style_directory = drupal_realpath(variable_get('file_default_scheme', 'public') . '://styles/' . $style['name']);
+  $style_directory = drupal_realpath(file_default_scheme() . '://styles/' . $style['name']);
   if (is_dir($style_directory)) {
     file_unmanaged_delete_recursive($style_directory);
   }
@@ -808,7 +809,7 @@ function image_style_url($style_name, $path) {
   }
   else {
     $destination = image_style_path($style_name, $path);
-    $url = url(file_directory_path($scheme) . '/' . file_uri_target($destination), array('absolute' => TRUE));
+    $url = url(file_stream_wrapper_get_instance_by_scheme($scheme)->getDirectoryPath() . '/' . file_uri_target($destination), array('absolute' => TRUE));
   }
   return $url;
 }
@@ -834,7 +835,7 @@ function image_style_path($style_name, $uri) {
   }
   else {
     $path = $uri;
-    $scheme = variable_get('file_default_scheme', 'public');
+    $scheme = file_default_scheme();
   }
   return $scheme . '://styles/' . $style_name . '/' . $scheme . '/' . $path;
 }
diff --git modules/image/image.test modules/image/image.test
index b1666ad..6cfeea8 100644
--- modules/image/image.test
+++ modules/image/image.test
@@ -334,8 +334,7 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase {
    * Count the number of images currently create for a style.
    */
   function getImageCount($style) {
-    $directory = file_directory_path() . '/styles/' . $style['name'];
-    return count(file_scan_directory($directory, '/.*/'));
+    return count(file_scan_directory('public://styles/' . $style['name'], '/.*/'));
   }
 
   /**
@@ -482,7 +481,7 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase {
     $this->drupalPost('admin/config/media/image-styles/delete/' . $style_name, array(), t('Delete'));
 
     // Confirm the style directory has been removed.
-    $directory = file_directory_path() . '/styles/' . $style_name;
+    $directory = file_default_scheme() . '://styles/' . $style_name;
     $this->assertFalse(is_dir($directory), t('Image style %style directory removed on style deletion.', array('%style' => $style['name'])));
 
     drupal_static_reset('image_styles');
diff --git modules/locale/locale.test modules/locale/locale.test
index 0c50ac7..89787af 100644
--- modules/locale/locale.test
+++ modules/locale/locale.test
@@ -758,7 +758,7 @@ class LocaleImportFunctionalTest extends DrupalWebTestCase {
    *   Additional options to pass to the translation import form.
    */
   function importPoFile($contents, array $options = array()) {
-    $name = tempnam(file_directory_path('temporary'), "po_") . '.po';
+    $name = tempnam('temporary://', "po_") . '.po';
     file_put_contents($name, $contents);
     $options['files[file]'] = $name;
     $this->drupalPost('admin/config/regional/translate/import', $options, t('Import'));
@@ -905,7 +905,7 @@ class LocaleExportFunctionalTest extends DrupalWebTestCase {
   function testExportTranslation() {
     // First import some known translations.
     // This will also automatically enable the 'fr' language.
-    $name = tempnam(file_directory_path('temporary'), "po_") . '.po';
+    $name = tempnam('temporary://', "po_") . '.po';
     file_put_contents($name, $this->getPoFile());
     $this->drupalPost('admin/config/regional/translate/import', array(
       'langcode' => 'fr',
diff --git modules/simpletest/drupal_web_test_case.php modules/simpletest/drupal_web_test_case.php
index 714cc2e..0ebcea4 100644
--- modules/simpletest/drupal_web_test_case.php
+++ modules/simpletest/drupal_web_test_case.php
@@ -416,7 +416,7 @@ abstract class DrupalTestCase {
    */
   public function run() {
     // Initialize verbose debugging.
-    simpletest_verbose(NULL, file_directory_path(), get_class($this));
+    simpletest_verbose(NULL, variable_get('file_public_path', conf_path() . '/files'), get_class($this));
 
     // HTTP auth settings (<username>:<password>) for the simpletest browser
     // when sending requests to the test site.
@@ -568,7 +568,7 @@ class DrupalUnitTestCase extends DrupalTestCase {
     global $conf;
 
     // Store necessary current values before switching to the test environment.
-    $this->originalFileDirectory = file_directory_path();
+    $this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
 
     spl_autoload_register('db_autoload');
 
@@ -903,9 +903,8 @@ class DrupalWebTestCase extends DrupalTestCase {
       // Copy other test files from simpletest.
       $original = drupal_get_path('module', 'simpletest') . '/files';
       $files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/');
-      $destination_path = file_directory_path('public');
       foreach ($files as $file) {
-        file_unmanaged_copy($file->uri, $destination_path);
+        file_unmanaged_copy($file->uri, variable_get('file_public_path', conf_path() . '/files'));
       }
 
       $this->generatedTestFiles = TRUE;
@@ -914,7 +913,7 @@ class DrupalWebTestCase extends DrupalTestCase {
     $files = array();
     // Make sure type is valid.
     if (in_array($type, array('binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'))) {
-      $files = file_scan_directory(file_directory_path('public'), '/' . $type . '\-.*/');
+      $files = file_scan_directory(variable_get('file_public_path', conf_path() . '/files'), '/' . $type . '\-.*/');
 
       // If size is set then remove any files that are not of that size.
       if ($size !== NULL) {
@@ -1152,7 +1151,7 @@ class DrupalWebTestCase extends DrupalTestCase {
     // Store necessary current values before switching to prefixed database.
     $this->originalLanguage = $language;
     $this->originalLanguageDefault = variable_get('language_default');
-    $this->originalFileDirectory = file_directory_path();
+    $this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
     $this->originalProfile = drupal_get_profile();
     $clean_url_original = variable_get('clean_url', 0);
 
diff --git modules/simpletest/simpletest.install modules/simpletest/simpletest.install
index c71925a..ee31d41 100644
--- modules/simpletest/simpletest.install
+++ modules/simpletest/simpletest.install
@@ -20,7 +20,7 @@ function simpletest_uninstall() {
   variable_del('simpletest_verbose');
 
   // Remove generated files.
-  $path = file_directory_path() . '/simpletest';
+  $path = 'public://simpletest';
   $files = file_scan_directory($path, '/.*/');
   foreach ($files as $file) {
     file_unmanaged_delete($file->uri);
diff --git modules/simpletest/simpletest.module modules/simpletest/simpletest.module
index e21bc6f..2144e14 100644
--- modules/simpletest/simpletest.module
+++ modules/simpletest/simpletest.module
@@ -131,7 +131,7 @@ function simpletest_run_tests($test_list, $reporter = 'drupal') {
     ->execute();
 
   // Clear out the previous verbose files.
-  file_unmanaged_delete_recursive(file_directory_path() . '/simpletest/verbose');
+  file_unmanaged_delete_recursive('public://simpletest/verbose');
 
   // Get the info for the first test being run.
   $first_test = array_shift($test_list);
@@ -406,7 +406,7 @@ function simpletest_generate_file($filename, $width, $lines, $type = 'binary-tex
   $text = wordwrap($text, $width - 1, "\n", TRUE) . "\n"; // Add \n for symetrical file.
 
   // Create filename.
-  file_put_contents(file_directory_path() . '/' . $filename . '.txt', $text);
+  file_put_contents('public://' . $filename . '.txt', $text);
   return $filename;
 }
 
diff --git modules/simpletest/simpletest.test modules/simpletest/simpletest.test
index 86cbd60..fe1a501 100644
--- modules/simpletest/simpletest.test
+++ modules/simpletest/simpletest.test
@@ -433,11 +433,13 @@ class SimpleTestFolderTestCase extends DrupalWebTestCase {
     );
   }
 
+  function setUp() {
+    return parent::setUp('image');
+  }
+
   function testFolderSetup() {
-    if (module_exists('image')) {
-      $path = file_directory_path() . '/styles';
-      $this->assertTrue(file_prepare_directory($path, FALSE), "Directory created.");
-    }
+    $directory = file_default_scheme() . '://styles';
+    $this->assertTrue(file_prepare_directory($directory, FALSE), "Directory created.");
   }
 }
 
diff --git modules/simpletest/tests/file.test modules/simpletest/tests/file.test
index fab0355..ad0b915 100644
--- modules/simpletest/tests/file.test
+++ modules/simpletest/tests/file.test
@@ -175,7 +175,7 @@ class FileTestCase extends DrupalWebTestCase {
   function createDirectory($path = NULL) {
     // A directory to operate on.
     if (is_null($path)) {
-      $path = file_directory_path() . '/' . $this->randomName();
+      $path = file_default_scheme() . '://' . $this->randomName();
     }
     $this->assertTrue(drupal_mkdir($path) && is_dir($path), t('Directory was created successfully.'));
     return $path;
@@ -428,9 +428,8 @@ class FileValidatorTest extends DrupalWebTestCase {
     // Maximum size.
     if (image_get_toolkit()) {
       // Copy the image so that the original doesn't get resized.
-      $temp_dir = file_directory_path('temporary');
-      copy(drupal_realpath('misc/druplicon.png'), drupal_realpath($temp_dir) . '/druplicon.png');
-      $this->image->uri = $temp_dir . '/druplicon.png';
+      copy(drupal_realpath('misc/druplicon.png'), 'temporary://druplicon.png');
+      $this->image->uri = 'temporary://druplicon.png';
 
       $errors = file_validate_image_resolution($this->image, '10x5');
       $this->assertEqual(count($errors), 0, t('No errors should be reported when an oversized image can be scaled down.'), 'File');
@@ -439,7 +438,7 @@ class FileValidatorTest extends DrupalWebTestCase {
       $this->assertTrue($info['width'] <= 10, t('Image scaled to correct width.'), 'File');
       $this->assertTrue($info['height'] <= 5, t('Image scaled to correct height.'), 'File');
 
-      drupal_unlink(drupal_realpath($temp_dir . '/druplicon.png'));
+      drupal_unlink(drupal_realpath('temporary://druplicon.png'));
     }
     else {
       // TODO: should check that the error is returned if no toolkit is available.
@@ -532,13 +531,12 @@ class FileUnmanagedSaveDataTest extends FileTestCase {
     // No filename.
     $filepath = file_unmanaged_save_data($contents);
     $this->assertTrue($filepath, t('Unnamed file saved correctly.'));
-    $this->assertEqual(file_directory_path('public'), file_directory_path(file_stream_wrapper_valid_scheme($filepath)), t("File was placed in Drupal's files directory."));
+    $this->assertEqual(file_uri_scheme($filepath), file_default_scheme(), t("File was placed in Drupal's files directory."));
     $this->assertEqual($contents, file_get_contents(drupal_realpath($filepath)), t('Contents of the file are correct.'));
 
     // Provide a filename.
     $filepath = file_unmanaged_save_data($contents, 'public://asdf.txt', FILE_EXISTS_REPLACE);
     $this->assertTrue($filepath, t('Unnamed file saved correctly.'));
-    $this->assertEqual(file_directory_path('public'), file_directory_path(file_uri_scheme($filepath)), t("File was placed in Drupal's files directory."));
     $this->assertEqual('asdf.txt', basename($filepath), t('File was named correctly.'));
     $this->assertEqual($contents, file_get_contents(drupal_realpath($filepath)), t('Contents of the file are correct.'));
     $this->assertFilePermissions($filepath, variable_get('file_chmod_file', 0664));
@@ -876,11 +874,11 @@ class FileDirectoryTest extends FileTestCase {
   }
 
   /**
-   * Test the file_directory_path() function.
+   * Test directory handling functions.
    */
-  function testFileCheckDirectory() {
+  function testFileCheckDirectoryHandling() {
     // A directory to operate on.
-    $directory = file_directory_path() . '/' . $this->randomName() . '/' . $this->randomName();
+    $directory = file_stream_wrapper_get_instance_by_scheme(file_default_scheme())->getDirectoryPath() . '/' . $this->randomName() . '/' . $this->randomName();
     $this->assertFalse(is_dir($directory), t('Directory does not exist prior to testing.'));
 
     // Non-existent directory.
@@ -899,6 +897,7 @@ class FileDirectoryTest extends FileTestCase {
       // in the directory on any recent version of Windows.
 
       // Make directory read only.
+      debug($directory);
       @drupal_chmod($directory, 0444);
       $this->assertFalse(file_prepare_directory($directory, 0), t('Error reported for a non-writeable directory.'), 'File');
 
@@ -910,36 +909,16 @@ class FileDirectoryTest extends FileTestCase {
     $this->assertDirectoryPermissions($directory, variable_get('file_chmod_directory', 0775));
 
     // Remove .htaccess file to then test that it gets re-created.
-    $directory = file_directory_path();
-    @drupal_unlink($directory . '/.htaccess');
-    $this->assertFalse(is_file($directory . '/.htaccess'), t('Successfully removed the .htaccess file in the files directory.'), 'File');
+    @drupal_unlink(file_default_scheme() . '://.htaccess');
+    $this->assertFalse(is_file(file_default_scheme() . '://.htaccess'), t('Successfully removed the .htaccess file in the files directory.'), 'File');
     file_ensure_htaccess();
-    $this->assertTrue(is_file($directory . '/.htaccess'), t('Successfully re-created the .htaccess file in the files directory.'), 'File');
+    $this->assertTrue(is_file(file_default_scheme() . '://.htaccess'), t('Successfully re-created the .htaccess file in the files directory.'), 'File');
     // Verify contents of .htaccess file.
-    $file = file_get_contents(file_directory_path() . '/.htaccess');
+    $file = file_get_contents(file_default_scheme() . '://.htaccess');
     $this->assertEqual($file, "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\nOptions None\nOptions +FollowSymLinks", t('The .htaccess file contains the proper content.'), 'File');
   }
 
   /**
-   * Check file_directory_path('public').
-   */
-  function testFileDirectoryPath() {
-    // Directory path.
-    $path = variable_get('file_public_path', '');
-    $this->assertEqual($path, file_directory_path('public'), t('Properly returns the stored file directory path.'), 'File');
-  }
-
-  /**
-   * Check file_directory_path() and file_directory_path('temporary').
-   */
-  function testFileDirectoryTemp() {
-    // Temporary directory handling.
-    variable_set('file_directory_temp', NULL);
-    $temp = file_directory_temp();
-    $this->assertTrue(!is_null($temp), t('Properly set and retrieved temp directory %directory.', array('%directory' => $temp)), 'File');
-  }
-
-  /**
    * This will take a directory and path, and find a valid filepath that is not
    * taken by another file.
    */
@@ -1154,7 +1133,7 @@ class FileUnmanagedDeleteTest extends FileTestCase {
    */
   function testMissing() {
     // Try to delete a non-existing file
-    $this->assertTrue(file_unmanaged_delete(file_directory_path() . '/' . $this->randomName()), t('Returns true when deleting a non-existent file.'));
+    $this->assertTrue(file_unmanaged_delete(file_default_scheme() . '/' . $this->randomName()), t('Returns true when deleting a non-existent file.'));
   }
 
   /**
@@ -1188,7 +1167,7 @@ class FileUnmanagedDeleteRecursiveTest extends FileTestCase {
    */
   function testSingleFile() {
     // Create a file for testing
-    $filepath = file_directory_path() . '/' . $this->randomName();
+    $filepath = file_default_scheme() . '://' . $this->randomName();
     file_put_contents($filepath, '');
 
     // Delete the file.
@@ -2073,7 +2052,7 @@ class FileSaveDataTest extends FileHookTestCase {
     $result = file_save_data($contents);
     $this->assertTrue($result, t('Unnamed file saved correctly.'));
 
-    $this->assertEqual(file_directory_path('public'), file_directory_path(file_stream_wrapper_valid_scheme($result->uri)), t("File was placed in Drupal's files directory."));
+    $this->assertEqual(file_default_scheme(), file_uri_scheme($result->uri), t("File was placed in Drupal's files directory."));
     $this->assertEqual($result->filename, basename($result->uri), t("Filename was set to the file's basename."));
     $this->assertEqual($contents, file_get_contents($result->uri), t('Contents of the file are correct.'));
     $this->assertEqual($result->filemime, 'application/octet-stream', t('A MIME type was set.'));
@@ -2095,7 +2074,7 @@ class FileSaveDataTest extends FileHookTestCase {
     $result = file_save_data($contents, 'public://' . 'asdf.txt');
     $this->assertTrue($result, t('Unnamed file saved correctly.'));
 
-    $this->assertEqual(file_directory_path('public'), file_directory_path(file_stream_wrapper_valid_scheme($result->uri)), t("File was placed in Drupal's files directory."));
+    $this->assertEqual('public', file_uri_scheme($result->uri), t("File was placed in Drupal's files directory."));
     $this->assertEqual('asdf.txt', basename($result->uri), t('File was named correctly.'));
     $this->assertEqual($contents, file_get_contents($result->uri), t('Contents of the file are correct.'));
     $this->assertEqual($result->filemime, 'text/plain', t('A MIME type was set.'));
@@ -2119,7 +2098,7 @@ class FileSaveDataTest extends FileHookTestCase {
     $result = file_save_data($contents, $existing->uri, FILE_EXISTS_RENAME);
     $this->assertTrue($result, t("File saved sucessfully."));
 
-    $this->assertEqual(file_directory_path('public'), file_directory_path(file_stream_wrapper_valid_scheme($result->uri)), t("File was placed in Drupal's files directory."));
+    $this->assertEqual('public', file_uri_scheme($result->uri), t("File was placed in Drupal's files directory."));
     $this->assertEqual($result->filename, $existing->filename, t("Filename was set to the basename of the source, rather than that of the renamed file."));
     $this->assertEqual($contents, file_get_contents($result->uri), t("Contents of the file are correct."));
     $this->assertEqual($result->filemime, 'application/octet-stream', t("A MIME type was set."));
@@ -2147,7 +2126,7 @@ class FileSaveDataTest extends FileHookTestCase {
     $result = file_save_data($contents, $existing->uri, FILE_EXISTS_REPLACE);
     $this->assertTrue($result, t('File saved sucessfully.'));
 
-    $this->assertEqual(file_directory_path('public'), file_directory_path(file_stream_wrapper_valid_scheme($result->uri)), t("File was placed in Drupal's files directory."));
+    $this->assertEqual('public', file_uri_scheme($result->uri), t("File was placed in Drupal's files directory."));
     $this->assertEqual($result->filename, $existing->filename, t('Filename was set to the basename of the existing file, rather than preserving the original name.'));
     $this->assertEqual($contents, file_get_contents($result->uri), t('Contents of the file are correct.'));
     $this->assertEqual($result->filemime, 'application/octet-stream', t('A MIME type was set.'));
@@ -2208,7 +2187,7 @@ class FileDownloadTest extends FileTestCase {
     // Test generating an URL to a created file.
     $file = $this->createFile();
     $url = file_create_url($file->uri);
-    $this->assertEqual($GLOBALS['base_url'] . '/' . file_directory_path() . '/' . $file->filename, $url, t('Correctly generated a URL for a created file.'));
+    $this->assertEqual($GLOBALS['base_url'] . '/' . file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath() . '/' . $file->filename, $url, t('Correctly generated a URL for a created file.'));
     $this->drupalHead($url);
     $this->assertResponse(200, t('Confirmed that the generated URL is correct by downloading the created file.'));
 
@@ -2265,7 +2244,7 @@ class FileDownloadTest extends FileTestCase {
       '%2523%2525%2526%252B%252F%253F' .
       '%C3%A9%C3%B8%C3%AF%D0%B2%CE%B2%E4%B8%AD%E5%9C%8B%E6%9B%B8%DB%9E';
 
-    $this->checkUrl('public', '', $basename, $base_url . '/' . file_directory_path() . '/' . $basename_encoded);
+    $this->checkUrl('public', '', $basename, $base_url . '/' . file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath() . '/' . $basename_encoded);
     $this->checkUrl('private', '', $basename, $base_url . '/system/files/' . $basename_encoded);
     $this->checkUrl('private', '', $basename, $base_url . '/?q=system/files/' . $basename_encoded, '0');
   }
@@ -2378,19 +2357,20 @@ class FileURLRewritingTest extends FileTestCase {
     variable_set('file_test_hook_file_url_alter', 'cdn');
     $file = $this->createFile();
     $url = file_create_url($file->uri);
-    $this->assertEqual(FILE_URL_TEST_CDN_2 . '/' . file_directory_path() . '/' . $file->filename, $url, t('Correctly generated a CDN URL for a created file.'));
+    $public_directory_path = file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath();
+    $this->assertEqual(FILE_URL_TEST_CDN_2 . '/' . $public_directory_path . '/' . $file->filename, $url, t('Correctly generated a CDN URL for a created file.'));
 
     // Test alteration of file URLs to use root-relative URLs.
     variable_set('file_test_hook_file_url_alter', 'root-relative');
     $file = $this->createFile();
     $url = file_create_url($file->uri);
-    $this->assertEqual(base_path() . '/' . file_directory_path() . '/' . $file->filename, $url, t('Correctly generated a root-relative URL for a created file.'));
+    $this->assertEqual(base_path() . '/' . $public_directory_path . '/' . $file->filename, $url, t('Correctly generated a root-relative URL for a created file.'));
 
     // Test alteration of file URLs to use a protocol-relative URLs.
     variable_set('file_test_hook_file_url_alter', 'protocol-relative');
     $file = $this->createFile();
     $url = file_create_url($file->uri);
-    $this->assertEqual('/' . base_path() . '/' . file_directory_path() . '/' . $file->filename, $url, t('Correctly generated a protocol-relative URL for a created file.'));
+    $this->assertEqual('/' . base_path() . '/' . $public_directory_path . '/' . $file->filename, $url, t('Correctly generated a protocol-relative URL for a created file.'));
   }
 }
 
@@ -2599,17 +2579,13 @@ class StreamWrapperTest extends DrupalWebTestCase {
     $this->assertEqual(file_uri_target('public://foo/bar.txt'), 'foo/bar.txt', t('Got a valid stream target from public://foo/bar.txt.'));
     $this->assertFalse(file_uri_target('foo/bar.txt'), t('foo/bar.txt is not a valid stream.'));
 
-    // Test file_build_uri() and file_directory_path().
+    // Test file_build_uri() and DrupalLocalStreamWrapper::getDirectoryPath().
     $this->assertEqual(file_build_uri('foo/bar.txt'), 'public://foo/bar.txt', t('Expected scheme was added.'));
-    $this->assertEqual(file_directory_path(), variable_get('file_public_path'), t('Expected default directory path was returned.'));
-    $this->assertEqual(file_directory_path('public'), variable_get('file_public_path'), t('Expected public directory path was returned.'));
-    $this->assertEqual(file_directory_path('temporary'), variable_get('file_temporary_path'), t('Expected temporary directory path was returned.'));
-    $this->assertEqual(file_directory_path($this->scheme), variable_get('stream_public_path', 'sites/default/files'), t('Expected dummy directory path was returned.'));
-    $this->assertFalse(file_directory_path('non-existent'), t('No directory path returned for invalid scheme.'));
+    $this->assertEqual(file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath(), variable_get('file_public_path'), t('Expected default directory path was returned.'));
+    $this->assertEqual(file_stream_wrapper_get_instance_by_scheme('temporary')->getDirectoryPath(), variable_get('file_temporary_path'), t('Expected temporary directory path was returned.'));
+
     variable_set('file_default_scheme', 'private');
     $this->assertEqual(file_build_uri('foo/bar.txt'), 'private://foo/bar.txt', t('Got a valid URI from foo/bar.txt.'));
-    $this->assertEqual(file_directory_path(), variable_get('file_private_path'), t('Expected default directory path was returned.'));
-    $this->assertEqual(file_directory_path('private'), variable_get('file_private_path'), t('Expected private directory path was returned.'));
   }
 
   /**
diff --git modules/simpletest/tests/filetransfer.test modules/simpletest/tests/filetransfer.test
index 7dbef48..c52b503 100644
--- modules/simpletest/tests/filetransfer.test
+++ modules/simpletest/tests/filetransfer.test
@@ -36,7 +36,7 @@ class FileTranferTest extends DrupalWebTestCase {
   }
 
   function _buildFakeModule() {
-    $location = file_directory_path('temporary') . '/fake';
+    $location = 'temporary://fake';
     if (is_dir($location)) {
       $ret = 0;
       $output = array();
@@ -80,7 +80,7 @@ class FileTranferTest extends DrupalWebTestCase {
 
     $gotit = TRUE;
     try {
-      $this->testConnection->copyDirectory($source, DRUPAL_ROOT . '/'. file_directory_path());
+      $this->testConnection->copyDirectory($source, DRUPAL_ROOT . '/'. variable_get('file_public_path', conf_path() . '/files'));
     }
     catch (FileTransferException $e) {
       $gotit = FALSE;
diff --git modules/simpletest/tests/image.test modules/simpletest/tests/image.test
index fbd4835..7296bc4 100644
--- modules/simpletest/tests/image.test
+++ modules/simpletest/tests/image.test
@@ -444,7 +444,7 @@ class ImageToolkitGdTestCase extends DrupalWebTestCase {
           $correct_colors = $this->colorsAreEqual($color, $corner);
         }
 
-        $directory = file_directory_path() . '/imagetests';
+        $directory = file_default_scheme() . '://imagetests';
         file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
         image_save($image, $directory . '/' . $op . '.' . $image->info['extension']);
 
diff --git modules/simpletest/tests/registry.test modules/simpletest/tests/registry.test
index e771968..a65e9cc 100644
--- modules/simpletest/tests/registry.test
+++ modules/simpletest/tests/registry.test
@@ -64,7 +64,7 @@ class RegistryParseFilesTestCase extends DrupalWebTestCase {
     foreach ($this->fileTypes as $fileType) {
       $chrs = hash('sha256', microtime() . mt_rand());
       $this->$fileType = new stdClass();
-      $this->$fileType->fileName = file_directory_path() . '/registry_test_' . substr($chrs, 0, 16);
+      $this->$fileType->fileName = 'public://registry_test_' . substr($chrs, 0, 16);
       $this->$fileType->className = 'registry_test_class' . substr($chrs, 16, 16);
       $this->$fileType->interfaceName = 'registry_test_interface' . substr($chrs, 32, 16);
       $this->$fileType->contents = $this->getFileContents($fileType);
diff --git modules/simpletest/tests/session.test modules/simpletest/tests/session.test
index 110b3c8..7d636fc 100644
--- modules/simpletest/tests/session.test
+++ modules/simpletest/tests/session.test
@@ -191,7 +191,7 @@ class SessionTestCase extends DrupalWebTestCase {
     $this->loggedInUser = FALSE;
 
     // Change cookie file for user.
-    $this->cookieFile = file_directory_path('temporary') . '/cookie.' . $uid . '.txt';
+    $this->cookieFile = 'temporary://cookie.' . $uid . '.txt';
     $this->additionalCurlOptions[CURLOPT_COOKIEFILE] = $this->cookieFile;
     $this->additionalCurlOptions[CURLOPT_COOKIESESSION] = TRUE;
     $this->drupalGet('session-test/get');
diff --git modules/simpletest/tests/upgrade/upgrade.test modules/simpletest/tests/upgrade/upgrade.test
index c90cf8e..6db261c 100644
--- modules/simpletest/tests/upgrade/upgrade.test
+++ modules/simpletest/tests/upgrade/upgrade.test
@@ -58,7 +58,7 @@ abstract class UpgradePathTestCase extends DrupalWebTestCase {
     // Store necessary current values before switching to prefixed database.
     $this->originalLanguage = $language;
     $this->originalLanguageDefault = variable_get('language_default');
-    $this->originalFileDirectory = file_directory_path();
+    $this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
     $this->originalProfile = drupal_get_profile();
     $clean_url_original = variable_get('clean_url', 0);
 
diff --git modules/system/system.admin.inc modules/system/system.admin.inc
index 03f8a44..15786a1 100644
--- modules/system/system.admin.inc
+++ modules/system/system.admin.inc
@@ -1804,7 +1804,7 @@ function system_file_system_settings() {
   $form['file_public_path'] = array(
     '#type' => 'textfield',
     '#title' => t('Public file system path'),
-    '#default_value' => variable_get('file_public_path', file_directory_path()),
+    '#default_value' => variable_get('file_public_path', conf_path() . '/files'),
     '#maxlength' => 255,
     '#description' => t('A local file system path where public files will be stored. This directory must exist and be writable by Drupal. This directory must be relative to the Drupal installation directory and be accessible over the web.'),
     '#after_build' => array('system_check_directory'),
@@ -1813,7 +1813,7 @@ function system_file_system_settings() {
   $form['file_private_path'] = array(
     '#type' => 'textfield',
     '#title' => t('Private file system path'),
-    '#default_value' => variable_get('file_private_path', file_directory_path('private')),
+    '#default_value' => variable_get('file_private_path', ''),
     '#maxlength' => 255,
     '#description' => t('A local file system path where private files will be stored. This directory must exist and be writable by Drupal. This directory should not be accessible over the web.'),
     '#after_build' => array('system_check_directory'),
@@ -1822,7 +1822,7 @@ function system_file_system_settings() {
   $form['file_temporary_path'] = array(
     '#type' => 'textfield',
     '#title' => t('Temporary directory'),
-    '#default_value' => variable_get('file_temporary_path', file_directory_path('temporary')),
+    '#default_value' => variable_get('file_temporary_path', file_directory_temp()),
     '#maxlength' => 255,
     '#description' => t('A local file system path where temporary files will be stored. This directory should not be accessible over the web.'),
     '#after_build' => array('system_check_directory'),
diff --git modules/system/system.install modules/system/system.install
index 62571df..4319aca 100644
--- modules/system/system.install
+++ modules/system/system.install
@@ -323,7 +323,7 @@ function system_requirements($phase) {
       }
     }
     else {
-      if (variable_get('file_default_scheme', 'public') == 'public') {
+      if (file_default_scheme() == 'public') {
         $requirements['file system']['value'] = $t('Writable (<em>public</em> download method)');
       }
       else {
@@ -2674,7 +2674,7 @@ function system_update_7059(&$sandbox) {
         'settings' => array(
           'display_field' => 1,
           'display_default' => variable_get('upload_list_default', 1),
-          'uri_scheme' => variable_get('file_default_scheme', 'public'),
+          'uri_scheme' => file_default_scheme(),
           'default_file' => 0,
         ),
       );
@@ -2778,7 +2778,7 @@ function system_update_7059(&$sandbox) {
   }
 
   $basename = variable_get('file_directory_path', conf_path() . '/files');
-  $scheme = variable_get('file_default_scheme', 'public') . '://';
+  $scheme = file_default_scheme() . '://';
   foreach ($node_revisions as $vid => $revision) {
     // We will convert filepaths to uri using the default scheme
     // and stripping off the existing file directory path.
diff --git modules/user/user.admin.inc modules/user/user.admin.inc
index 8fd62fa..675377b 100644
--- modules/user/user.admin.inc
+++ modules/user/user.admin.inc
@@ -341,7 +341,7 @@ function user_admin_settings() {
   );
   // If picture support is enabled, check whether the picture directory exists.
   if (variable_get('user_pictures', 0)) {
-    $picture_path =  variable_get('file_default_scheme', 'public') . '://' . variable_get('user_picture_path', 'pictures');
+    $picture_path =  file_default_scheme() . '://' . variable_get('user_picture_path', 'pictures');
     if (!file_prepare_directory($picture_path, FILE_CREATE_DIRECTORY)) {
       form_set_error('user_picture_path', t('The directory %directory does not exist or is not writable.', array('%directory' => $picture_path)));
       watchdog('file system', 'The directory %directory does not exist or is not writable.', array('%directory' => $picture_path), WATCHDOG_ERROR);
@@ -369,7 +369,7 @@ function user_admin_settings() {
     '#default_value' => variable_get('user_picture_path', 'pictures'),
     '#size' => 30,
     '#maxlength' => 255,
-    '#description' => t('Subdirectory in the directory %dir where pictures will be stored.', array('%dir' => file_directory_path() . '/')),
+    '#description' => t('Subdirectory in the file upload directory where pictures will be stored.'),
   );
   $form['personalization']['pictures']['user_picture_default'] = array(
     '#type' => 'textfield',
diff --git modules/user/user.module modules/user/user.module
index b81c860..6911766 100644
--- modules/user/user.module
+++ modules/user/user.module
@@ -433,7 +433,7 @@ function user_save($account, $edit = array(), $category = 'account') {
         // make it permanent.
         if (!$picture->status) {
           $info = image_get_info($picture->uri);
-          $picture_directory =  variable_get('file_default_scheme', 'public') . '://' . variable_get('user_picture_path', 'pictures');
+          $picture_directory =  file_default_scheme() . '://' . variable_get('user_picture_path', 'pictures');
 
           // Prepare the pictures directory.
           file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY);
