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.

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

mercurial