ipc/chromium/src/base/file_util.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
     2 // Use of this source code is governed by a BSD-style license that can be
     3 // found in the LICENSE file.
     5 #include "base/file_util.h"
     7 #if defined(OS_WIN)
     8 #include <io.h>
     9 #endif
    10 #include <stdio.h>
    11 #if defined(ANDROID) || defined(OS_POSIX)
    12 #include <unistd.h>
    13 #endif
    15 #include <fstream>
    17 #include "base/file_path.h"
    18 #include "base/logging.h"
    19 #include "base/string_util.h"
    21 #include "base/string_piece.h"
    22 #include "base/sys_string_conversions.h"
    24 namespace {
    26 const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.');
    28 }  // namespace
    30 namespace file_util {
    32 bool EndsWithSeparator(const FilePath& path) {
    33   FilePath::StringType value = path.value();
    34   if (value.empty())
    35     return false;
    37   return FilePath::IsSeparator(value[value.size() - 1]);
    38 }
    40 void TrimTrailingSeparator(std::wstring* dir) {
    41   while (dir->length() > 1 && EndsWithSeparator(dir))
    42     dir->resize(dir->length() - 1);
    43 }
    45 FilePath::StringType GetFileExtensionFromPath(const FilePath& path) {
    46   FilePath::StringType file_name = path.BaseName().value();
    47   const FilePath::StringType::size_type last_dot =
    48       file_name.rfind(kExtensionSeparator);
    49   return FilePath::StringType(last_dot == FilePath::StringType::npos ?
    50                               FILE_PATH_LITERAL("") :
    51                               file_name, last_dot+1);
    52 }
    54 void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix) {
    55   FilePath::StringType& value =
    56       const_cast<FilePath::StringType&>(path->value());
    58   const FilePath::StringType::size_type last_dot =
    59       value.rfind(kExtensionSeparator);
    60   const FilePath::StringType::size_type last_separator =
    61       value.find_last_of(FilePath::StringType(FilePath::kSeparators));
    63   if (last_dot == FilePath::StringType::npos ||
    64       (last_separator != std::wstring::npos && last_dot < last_separator)) {
    65     // The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo".
    66     // We should just append the suffix to the entire path.
    67     value.append(suffix);
    68     return;
    69   }
    71   value.insert(last_dot, suffix);
    72 }
    74 void ReplaceExtension(FilePath* path, const FilePath::StringType& extension) {
    75   FilePath::StringType clean_extension;
    76   // If the new extension is "" or ".", then we will just remove the current
    77   // extension.
    78   if (!extension.empty() &&
    79       extension != FilePath::StringType(&kExtensionSeparator, 1)) {
    80     if (extension[0] != kExtensionSeparator)
    81       clean_extension.append(&kExtensionSeparator, 1);
    82     clean_extension.append(extension);
    83   }
    85   FilePath::StringType& value =
    86       const_cast<FilePath::StringType&>(path->value());
    87   const FilePath::StringType::size_type last_dot =
    88       value.rfind(kExtensionSeparator);
    89   const FilePath::StringType::size_type last_separator =
    90       value.find_last_of(FilePath::StringType(FilePath::kSeparators));
    92   // Erase the current extension, if any.
    93   if ((last_dot > last_separator ||
    94       last_separator == FilePath::StringType::npos) &&
    95       last_dot != FilePath::StringType::npos)
    96     value.erase(last_dot);
    98   value.append(clean_extension);
    99 }
   101 FILE* CreateAndOpenTemporaryFile(FilePath* path) {
   102   FilePath directory;
   103   if (!GetTempDir(&directory))
   104     return NULL;
   106   return CreateAndOpenTemporaryFileInDir(directory, path);
   107 }
   109 bool GetFileSize(const FilePath& file_path, int64_t* file_size) {
   110   FileInfo info;
   111   if (!GetFileInfo(file_path, &info))
   112     return false;
   113   *file_size = info.size;
   114   return true;
   115 }
   117 bool CloseFile(FILE* file) {
   118   if (file == NULL)
   119     return true;
   120   return fclose(file) == 0;
   121 }
   123 // Deprecated functions ----------------------------------------------------
   125 bool AbsolutePath(std::wstring* path_str) {
   126   FilePath path(FilePath::FromWStringHack(*path_str));
   127   if (!AbsolutePath(&path))
   128     return false;
   129   *path_str = path.ToWStringHack();
   130   return true;
   131 }
   132 void AppendToPath(std::wstring* path, const std::wstring& new_ending) {
   133   if (!path) {
   134     NOTREACHED();
   135     return;  // Don't crash in this function in release builds.
   136   }
   138   if (!EndsWithSeparator(path))
   139     path->push_back(FilePath::kSeparators[0]);
   140   path->append(new_ending);
   141 }
   142 bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path,
   143                    bool recursive) {
   144   return CopyDirectory(FilePath::FromWStringHack(from_path),
   145                        FilePath::FromWStringHack(to_path),
   146                        recursive);
   147 }
   148 bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) {
   149   return CopyFile(FilePath::FromWStringHack(from_path),
   150                   FilePath::FromWStringHack(to_path));
   151 }
   152 bool CreateDirectory(const std::wstring& full_path) {
   153   return CreateDirectory(FilePath::FromWStringHack(full_path));
   154 }
   155 bool CreateNewTempDirectory(const std::wstring& prefix,
   156                             std::wstring* new_temp_path) {
   157 #if defined(OS_WIN)
   158   FilePath::StringType dir_prefix(prefix);
   159 #elif defined(OS_POSIX)
   160   FilePath::StringType dir_prefix = WideToUTF8(prefix);
   161 #endif
   162   FilePath temp_path;
   163   if (!CreateNewTempDirectory(dir_prefix, &temp_path))
   164     return false;
   165   *new_temp_path = temp_path.ToWStringHack();
   166   return true;
   167 }
   168 bool CreateTemporaryFileName(std::wstring* temp_file) {
   169   FilePath temp_file_path;
   170   if (!CreateTemporaryFileName(&temp_file_path))
   171     return false;
   172   *temp_file = temp_file_path.ToWStringHack();
   173   return true;
   174 }
   175 bool Delete(const std::wstring& path, bool recursive) {
   176   return Delete(FilePath::FromWStringHack(path), recursive);
   177 }
   178 bool DirectoryExists(const std::wstring& path) {
   179   return DirectoryExists(FilePath::FromWStringHack(path));
   180 }
   181 bool EndsWithSeparator(std::wstring* path) {
   182   return EndsWithSeparator(FilePath::FromWStringHack(*path));
   183 }
   184 bool EndsWithSeparator(const std::wstring& path) {
   185   return EndsWithSeparator(FilePath::FromWStringHack(path));
   186 }
   187 bool GetCurrentDirectory(std::wstring* path_str) {
   188   FilePath path;
   189   if (!GetCurrentDirectory(&path))
   190     return false;
   191   *path_str = path.ToWStringHack();
   192   return true;
   193 }
   194 std::wstring GetFileExtensionFromPath(const std::wstring& path) {
   195   FilePath::StringType extension =
   196       GetFileExtensionFromPath(FilePath::FromWStringHack(path));
   197 #if defined(OS_WIN)
   198   return extension;
   199 #elif defined(OS_POSIX)
   200   return UTF8ToWide(extension);
   201 #endif
   202 }
   203 bool GetFileInfo(const std::wstring& file_path, FileInfo* results) {
   204   return GetFileInfo(FilePath::FromWStringHack(file_path), results);
   205 }
   206 std::wstring GetFilenameFromPath(const std::wstring& path) {
   207   if (path.empty() || EndsWithSeparator(path))
   208     return std::wstring();
   210   return FilePath::FromWStringHack(path).BaseName().ToWStringHack();
   211 }
   212 bool GetFileSize(const std::wstring& file_path, int64_t* file_size) {
   213   return GetFileSize(FilePath::FromWStringHack(file_path), file_size);
   214 }
   215 bool GetTempDir(std::wstring* path_str) {
   216   FilePath path;
   217   if (!GetTempDir(&path))
   218     return false;
   219   *path_str = path.ToWStringHack();
   220   return true;
   221 }
   222 FILE* OpenFile(const std::wstring& filename, const char* mode) {
   223   return OpenFile(FilePath::FromWStringHack(filename), mode);
   224 }
   225 bool PathExists(const std::wstring& path) {
   226   return PathExists(FilePath::FromWStringHack(path));
   227 }
   228 bool PathIsWritable(const std::wstring& path) {
   229   return PathIsWritable(FilePath::FromWStringHack(path));
   230 }
   231 int ReadFile(const std::wstring& filename, char* data, int size) {
   232   return ReadFile(FilePath::FromWStringHack(filename), data, size);
   233 }
   234 bool SetCurrentDirectory(const std::wstring& directory) {
   235   return SetCurrentDirectory(FilePath::FromWStringHack(directory));
   236 }
   237 void UpOneDirectory(std::wstring* dir) {
   238   FilePath path = FilePath::FromWStringHack(*dir);
   239   FilePath directory = path.DirName();
   240   // If there is no separator, we will get back kCurrentDirectory.
   241   // In this case don't change |dir|.
   242   if (directory.value() != FilePath::kCurrentDirectory)
   243     *dir = directory.ToWStringHack();
   244 }
   245 int WriteFile(const std::wstring& filename, const char* data, int size) {
   246   return WriteFile(FilePath::FromWStringHack(filename), data, size);
   247 }
   248 }  // namespace

mercurial