1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/file_util.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,248 @@ 1.4 +// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "base/file_util.h" 1.9 + 1.10 +#if defined(OS_WIN) 1.11 +#include <io.h> 1.12 +#endif 1.13 +#include <stdio.h> 1.14 +#if defined(ANDROID) || defined(OS_POSIX) 1.15 +#include <unistd.h> 1.16 +#endif 1.17 + 1.18 +#include <fstream> 1.19 + 1.20 +#include "base/file_path.h" 1.21 +#include "base/logging.h" 1.22 +#include "base/string_util.h" 1.23 + 1.24 +#include "base/string_piece.h" 1.25 +#include "base/sys_string_conversions.h" 1.26 + 1.27 +namespace { 1.28 + 1.29 +const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.'); 1.30 + 1.31 +} // namespace 1.32 + 1.33 +namespace file_util { 1.34 + 1.35 +bool EndsWithSeparator(const FilePath& path) { 1.36 + FilePath::StringType value = path.value(); 1.37 + if (value.empty()) 1.38 + return false; 1.39 + 1.40 + return FilePath::IsSeparator(value[value.size() - 1]); 1.41 +} 1.42 + 1.43 +void TrimTrailingSeparator(std::wstring* dir) { 1.44 + while (dir->length() > 1 && EndsWithSeparator(dir)) 1.45 + dir->resize(dir->length() - 1); 1.46 +} 1.47 + 1.48 +FilePath::StringType GetFileExtensionFromPath(const FilePath& path) { 1.49 + FilePath::StringType file_name = path.BaseName().value(); 1.50 + const FilePath::StringType::size_type last_dot = 1.51 + file_name.rfind(kExtensionSeparator); 1.52 + return FilePath::StringType(last_dot == FilePath::StringType::npos ? 1.53 + FILE_PATH_LITERAL("") : 1.54 + file_name, last_dot+1); 1.55 +} 1.56 + 1.57 +void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix) { 1.58 + FilePath::StringType& value = 1.59 + const_cast<FilePath::StringType&>(path->value()); 1.60 + 1.61 + const FilePath::StringType::size_type last_dot = 1.62 + value.rfind(kExtensionSeparator); 1.63 + const FilePath::StringType::size_type last_separator = 1.64 + value.find_last_of(FilePath::StringType(FilePath::kSeparators)); 1.65 + 1.66 + if (last_dot == FilePath::StringType::npos || 1.67 + (last_separator != std::wstring::npos && last_dot < last_separator)) { 1.68 + // The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo". 1.69 + // We should just append the suffix to the entire path. 1.70 + value.append(suffix); 1.71 + return; 1.72 + } 1.73 + 1.74 + value.insert(last_dot, suffix); 1.75 +} 1.76 + 1.77 +void ReplaceExtension(FilePath* path, const FilePath::StringType& extension) { 1.78 + FilePath::StringType clean_extension; 1.79 + // If the new extension is "" or ".", then we will just remove the current 1.80 + // extension. 1.81 + if (!extension.empty() && 1.82 + extension != FilePath::StringType(&kExtensionSeparator, 1)) { 1.83 + if (extension[0] != kExtensionSeparator) 1.84 + clean_extension.append(&kExtensionSeparator, 1); 1.85 + clean_extension.append(extension); 1.86 + } 1.87 + 1.88 + FilePath::StringType& value = 1.89 + const_cast<FilePath::StringType&>(path->value()); 1.90 + const FilePath::StringType::size_type last_dot = 1.91 + value.rfind(kExtensionSeparator); 1.92 + const FilePath::StringType::size_type last_separator = 1.93 + value.find_last_of(FilePath::StringType(FilePath::kSeparators)); 1.94 + 1.95 + // Erase the current extension, if any. 1.96 + if ((last_dot > last_separator || 1.97 + last_separator == FilePath::StringType::npos) && 1.98 + last_dot != FilePath::StringType::npos) 1.99 + value.erase(last_dot); 1.100 + 1.101 + value.append(clean_extension); 1.102 +} 1.103 + 1.104 +FILE* CreateAndOpenTemporaryFile(FilePath* path) { 1.105 + FilePath directory; 1.106 + if (!GetTempDir(&directory)) 1.107 + return NULL; 1.108 + 1.109 + return CreateAndOpenTemporaryFileInDir(directory, path); 1.110 +} 1.111 + 1.112 +bool GetFileSize(const FilePath& file_path, int64_t* file_size) { 1.113 + FileInfo info; 1.114 + if (!GetFileInfo(file_path, &info)) 1.115 + return false; 1.116 + *file_size = info.size; 1.117 + return true; 1.118 +} 1.119 + 1.120 +bool CloseFile(FILE* file) { 1.121 + if (file == NULL) 1.122 + return true; 1.123 + return fclose(file) == 0; 1.124 +} 1.125 + 1.126 +// Deprecated functions ---------------------------------------------------- 1.127 + 1.128 +bool AbsolutePath(std::wstring* path_str) { 1.129 + FilePath path(FilePath::FromWStringHack(*path_str)); 1.130 + if (!AbsolutePath(&path)) 1.131 + return false; 1.132 + *path_str = path.ToWStringHack(); 1.133 + return true; 1.134 +} 1.135 +void AppendToPath(std::wstring* path, const std::wstring& new_ending) { 1.136 + if (!path) { 1.137 + NOTREACHED(); 1.138 + return; // Don't crash in this function in release builds. 1.139 + } 1.140 + 1.141 + if (!EndsWithSeparator(path)) 1.142 + path->push_back(FilePath::kSeparators[0]); 1.143 + path->append(new_ending); 1.144 +} 1.145 +bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path, 1.146 + bool recursive) { 1.147 + return CopyDirectory(FilePath::FromWStringHack(from_path), 1.148 + FilePath::FromWStringHack(to_path), 1.149 + recursive); 1.150 +} 1.151 +bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) { 1.152 + return CopyFile(FilePath::FromWStringHack(from_path), 1.153 + FilePath::FromWStringHack(to_path)); 1.154 +} 1.155 +bool CreateDirectory(const std::wstring& full_path) { 1.156 + return CreateDirectory(FilePath::FromWStringHack(full_path)); 1.157 +} 1.158 +bool CreateNewTempDirectory(const std::wstring& prefix, 1.159 + std::wstring* new_temp_path) { 1.160 +#if defined(OS_WIN) 1.161 + FilePath::StringType dir_prefix(prefix); 1.162 +#elif defined(OS_POSIX) 1.163 + FilePath::StringType dir_prefix = WideToUTF8(prefix); 1.164 +#endif 1.165 + FilePath temp_path; 1.166 + if (!CreateNewTempDirectory(dir_prefix, &temp_path)) 1.167 + return false; 1.168 + *new_temp_path = temp_path.ToWStringHack(); 1.169 + return true; 1.170 +} 1.171 +bool CreateTemporaryFileName(std::wstring* temp_file) { 1.172 + FilePath temp_file_path; 1.173 + if (!CreateTemporaryFileName(&temp_file_path)) 1.174 + return false; 1.175 + *temp_file = temp_file_path.ToWStringHack(); 1.176 + return true; 1.177 +} 1.178 +bool Delete(const std::wstring& path, bool recursive) { 1.179 + return Delete(FilePath::FromWStringHack(path), recursive); 1.180 +} 1.181 +bool DirectoryExists(const std::wstring& path) { 1.182 + return DirectoryExists(FilePath::FromWStringHack(path)); 1.183 +} 1.184 +bool EndsWithSeparator(std::wstring* path) { 1.185 + return EndsWithSeparator(FilePath::FromWStringHack(*path)); 1.186 +} 1.187 +bool EndsWithSeparator(const std::wstring& path) { 1.188 + return EndsWithSeparator(FilePath::FromWStringHack(path)); 1.189 +} 1.190 +bool GetCurrentDirectory(std::wstring* path_str) { 1.191 + FilePath path; 1.192 + if (!GetCurrentDirectory(&path)) 1.193 + return false; 1.194 + *path_str = path.ToWStringHack(); 1.195 + return true; 1.196 +} 1.197 +std::wstring GetFileExtensionFromPath(const std::wstring& path) { 1.198 + FilePath::StringType extension = 1.199 + GetFileExtensionFromPath(FilePath::FromWStringHack(path)); 1.200 +#if defined(OS_WIN) 1.201 + return extension; 1.202 +#elif defined(OS_POSIX) 1.203 + return UTF8ToWide(extension); 1.204 +#endif 1.205 +} 1.206 +bool GetFileInfo(const std::wstring& file_path, FileInfo* results) { 1.207 + return GetFileInfo(FilePath::FromWStringHack(file_path), results); 1.208 +} 1.209 +std::wstring GetFilenameFromPath(const std::wstring& path) { 1.210 + if (path.empty() || EndsWithSeparator(path)) 1.211 + return std::wstring(); 1.212 + 1.213 + return FilePath::FromWStringHack(path).BaseName().ToWStringHack(); 1.214 +} 1.215 +bool GetFileSize(const std::wstring& file_path, int64_t* file_size) { 1.216 + return GetFileSize(FilePath::FromWStringHack(file_path), file_size); 1.217 +} 1.218 +bool GetTempDir(std::wstring* path_str) { 1.219 + FilePath path; 1.220 + if (!GetTempDir(&path)) 1.221 + return false; 1.222 + *path_str = path.ToWStringHack(); 1.223 + return true; 1.224 +} 1.225 +FILE* OpenFile(const std::wstring& filename, const char* mode) { 1.226 + return OpenFile(FilePath::FromWStringHack(filename), mode); 1.227 +} 1.228 +bool PathExists(const std::wstring& path) { 1.229 + return PathExists(FilePath::FromWStringHack(path)); 1.230 +} 1.231 +bool PathIsWritable(const std::wstring& path) { 1.232 + return PathIsWritable(FilePath::FromWStringHack(path)); 1.233 +} 1.234 +int ReadFile(const std::wstring& filename, char* data, int size) { 1.235 + return ReadFile(FilePath::FromWStringHack(filename), data, size); 1.236 +} 1.237 +bool SetCurrentDirectory(const std::wstring& directory) { 1.238 + return SetCurrentDirectory(FilePath::FromWStringHack(directory)); 1.239 +} 1.240 +void UpOneDirectory(std::wstring* dir) { 1.241 + FilePath path = FilePath::FromWStringHack(*dir); 1.242 + FilePath directory = path.DirName(); 1.243 + // If there is no separator, we will get back kCurrentDirectory. 1.244 + // In this case don't change |dir|. 1.245 + if (directory.value() != FilePath::kCurrentDirectory) 1.246 + *dir = directory.ToWStringHack(); 1.247 +} 1.248 +int WriteFile(const std::wstring& filename, const char* data, int size) { 1.249 + return WriteFile(FilePath::FromWStringHack(filename), data, size); 1.250 +} 1.251 +} // namespace