license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | #include "base/file_util.h" |
| 6 | |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | #include <fstream> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 10 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 11 | #include "base/file_path.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | #include "base/logging.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | #include "base/string_util.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | #include "unicode/uniset.h" |
| 15 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | namespace file_util { |
| 17 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 18 | const wchar_t kExtensionSeparator = L'.'; |
| 19 | |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 20 | void PathComponents(const std::wstring& path, |
| 21 | std::vector<std::wstring>* components) { |
| 22 | DCHECK(components != NULL); |
| 23 | if (components == NULL) |
| 24 | return; |
| 25 | std::wstring::size_type start = 0; |
| 26 | std::wstring::size_type end = path.find(kPathSeparator, start); |
| 27 | |
| 28 | // Special case the "/" or "\" directory. On Windows with a drive letter, |
| 29 | // this code path won't hit, but the right thing should still happen. |
| 30 | // "E:\foo" will turn into "E:","foo". |
| 31 | if (end == start) { |
| 32 | components->push_back(std::wstring(path, 0, 1)); |
| 33 | start = end + 1; |
| 34 | end = path.find(kPathSeparator, start); |
| 35 | } |
| 36 | while (end != std::wstring::npos) { |
| 37 | std::wstring component = std::wstring(path, start, end - start); |
| 38 | components->push_back(component); |
| 39 | start = end + 1; |
| 40 | end = path.find(kPathSeparator, start); |
| 41 | } |
| 42 | std::wstring component = std::wstring(path, start); |
| 43 | components->push_back(component); |
| 44 | } |
| 45 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 46 | bool EndsWithSeparator(std::wstring* path) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 47 | return EndsWithSeparator(*path); |
| 48 | } |
| 49 | |
| 50 | bool EndsWithSeparator(const std::wstring& path) { |
[email protected] | e662113 | 2008-08-15 18:00:48 | [diff] [blame] | 51 | bool is_sep = (path.length() > 0 && |
| 52 | (path)[path.length() - 1] == kPathSeparator); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 53 | return is_sep; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void TrimTrailingSeparator(std::wstring* dir) { |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 57 | while (dir->length() > 1 && EndsWithSeparator(dir)) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 58 | dir->resize(dir->length() - 1); |
| 59 | } |
| 60 | |
| 61 | void UpOneDirectory(std::wstring* dir) { |
| 62 | TrimTrailingSeparator(dir); |
| 63 | |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 64 | std::wstring::size_type last_sep = dir->find_last_of(kPathSeparator); |
| 65 | if (last_sep != std::wstring::npos) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 66 | dir->resize(last_sep); |
| 67 | } |
| 68 | |
| 69 | void UpOneDirectoryOrEmpty(std::wstring* dir) { |
| 70 | TrimTrailingSeparator(dir); |
| 71 | |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 72 | std::wstring::size_type last_sep = dir->find_last_of(kPathSeparator); |
| 73 | if (last_sep != std::wstring::npos) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 74 | dir->resize(last_sep); |
| 75 | else |
| 76 | dir->clear(); |
| 77 | } |
| 78 | |
| 79 | void TrimFilename(std::wstring* path) { |
| 80 | if (EndsWithSeparator(path)) { |
| 81 | TrimTrailingSeparator(path); |
| 82 | } else { |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 83 | std::wstring::size_type last_sep = path->find_last_of(kPathSeparator); |
| 84 | if (last_sep != std::wstring::npos) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 85 | path->resize(last_sep); |
| 86 | } |
| 87 | } |
| 88 | |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 89 | std::wstring GetFilenameFromPath(const std::wstring& path) { |
[email protected] | e662113 | 2008-08-15 18:00:48 | [diff] [blame] | 90 | // TODO(erikkay): fix this - it's not using kPathSeparator, but win unit test |
| 91 | // are exercising '/' as a path separator as well. |
| 92 | std::wstring::size_type pos = path.find_last_of(L"\\/"); |
[email protected] | 52ab8f90 | 2008-11-03 16:14:46 | [diff] [blame^] | 93 | return std::wstring(path, pos == std::wstring::npos ? 0 : pos + 1); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 96 | std::wstring GetFileExtensionFromPath(const std::wstring& path) { |
| 97 | std::wstring file_name = GetFilenameFromPath(path); |
| 98 | std::wstring::size_type last_dot = file_name.rfind(L'.'); |
[email protected] | 52ab8f90 | 2008-11-03 16:14:46 | [diff] [blame^] | 99 | return std::wstring(last_dot == std::wstring::npos ? |
| 100 | L"" : |
| 101 | file_name, last_dot+1); |
| 102 | } |
| 103 | |
| 104 | std::wstring GetFilenameWithoutExtensionFromPath(const std::wstring& path) { |
| 105 | std::wstring file_name = GetFilenameFromPath(path); |
| 106 | std::wstring::size_type last_dot = file_name.rfind(L'.'); |
| 107 | return file_name.substr(0, last_dot); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void AppendToPath(std::wstring* path, const std::wstring& new_ending) { |
| 111 | if (!path) { |
| 112 | NOTREACHED(); |
| 113 | return; // Don't crash in this function in release builds. |
| 114 | } |
| 115 | |
| 116 | if (!EndsWithSeparator(path)) |
| 117 | path->push_back(kPathSeparator); |
| 118 | path->append(new_ending); |
| 119 | } |
| 120 | |
| 121 | void InsertBeforeExtension(std::wstring* path, const std::wstring& suffix) { |
| 122 | DCHECK(path); |
| 123 | |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 124 | const std::wstring::size_type last_dot = path->rfind(kExtensionSeparator); |
| 125 | const std::wstring::size_type last_sep = path->rfind(kPathSeparator); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 126 | |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 127 | if (last_dot == std::wstring::npos || |
| 128 | (last_sep != std::wstring::npos && last_dot < last_sep)) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 129 | // The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo". |
| 130 | // We should just append the suffix to the entire path. |
| 131 | path->append(suffix); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | path->insert(last_dot, suffix); |
| 136 | } |
| 137 | |
| 138 | void ReplaceIllegalCharacters(std::wstring* file_name, int replace_char) { |
| 139 | DCHECK(file_name); |
| 140 | |
[email protected] | d324ab33 | 2008-08-18 16:00:38 | [diff] [blame] | 141 | // Control characters, formatting characters, non-characters, and |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 142 | // some printable ASCII characters regarded as dangerous ('"*/:<>?\\'). |
| 143 | // See http://blogs.msdn.com/michkap/archive/2006/11/03/941420.aspx |
| 144 | // and http://msdn2.microsoft.com/en-us/library/Aa365247.aspx |
| 145 | // TODO(jungshik): Revisit the set. ZWJ and ZWNJ are excluded because they |
| 146 | // are legitimate in Arabic and some S/SE Asian scripts. However, when used |
| 147 | // elsewhere, they can be confusing/problematic. |
| 148 | // Also, consider wrapping the set with our Singleton class to create and |
| 149 | // freeze it only once. Note that there's a trade-off between memory and |
| 150 | // speed. |
| 151 | |
| 152 | UErrorCode status = U_ZERO_ERROR; |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 153 | #if defined(WCHAR_T_IS_UTF16) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 154 | UnicodeSet illegal_characters(UnicodeString( |
| 155 | L"[[\"*/:<>?\\\\|][:Cc:][:Cf:] - [\u200c\u200d]]"), status); |
| 156 | #else |
| 157 | UnicodeSet illegal_characters(UNICODE_STRING_SIMPLE( |
| 158 | "[[\"*/:<>?\\\\|][:Cc:][:Cf:] - [\\u200c\\u200d]]").unescape(), status); |
| 159 | #endif |
| 160 | DCHECK(U_SUCCESS(status)); |
| 161 | // Add non-characters. If this becomes a performance bottleneck by |
| 162 | // any chance, check |ucs4 & 0xFFFEu == 0xFFFEu|, instead. |
| 163 | illegal_characters.add(0xFDD0, 0xFDEF); |
| 164 | for (int i = 0; i <= 0x10; ++i) { |
| 165 | int plane_base = 0x10000 * i; |
| 166 | illegal_characters.add(plane_base + 0xFFFE, plane_base + 0xFFFF); |
| 167 | } |
| 168 | illegal_characters.freeze(); |
| 169 | DCHECK(!illegal_characters.contains(replace_char) && replace_char < 0x10000); |
| 170 | |
| 171 | // Remove leading and trailing whitespace. |
| 172 | TrimWhitespace(*file_name, TRIM_ALL, file_name); |
| 173 | |
| 174 | std::wstring::size_type i = 0; |
| 175 | std::wstring::size_type length = file_name->size(); |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 176 | const wchar_t* wstr = file_name->data(); |
| 177 | #if defined(WCHAR_T_IS_UTF16) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 178 | // Using |span| method of UnicodeSet might speed things up a bit, but |
| 179 | // it's not likely to matter here. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 180 | std::wstring temp; |
| 181 | temp.reserve(length); |
| 182 | while (i < length) { |
| 183 | UChar32 ucs4; |
| 184 | std::wstring::size_type prev = i; |
| 185 | U16_NEXT(wstr, i, length, ucs4); |
| 186 | if (illegal_characters.contains(ucs4)) { |
| 187 | temp.push_back(replace_char); |
| 188 | } else if (ucs4 < 0x10000) { |
| 189 | temp.push_back(ucs4); |
| 190 | } else { |
| 191 | temp.push_back(wstr[prev]); |
| 192 | temp.push_back(wstr[prev + 1]); |
| 193 | } |
| 194 | } |
| 195 | file_name->swap(temp); |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 196 | #elif defined(WCHAR_T_IS_UTF32) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 197 | while (i < length) { |
| 198 | if (illegal_characters.contains(wstr[i])) { |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 199 | (*file_name)[i] = replace_char; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 200 | } |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 201 | ++i; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 202 | } |
| 203 | #else |
| 204 | #error wchar_t* should be either UTF-16 or UTF-32 |
| 205 | #endif |
| 206 | } |
| 207 | |
[email protected] | b988fe4d | 2008-09-19 17:32:18 | [diff] [blame] | 208 | // Appends the extension to file adding a '.' if extension doesn't contain one. |
| 209 | // This does nothing if extension is empty or '.'. This is used internally by |
| 210 | // ReplaceExtension. |
| 211 | static void AppendExtension(const std::wstring& extension, |
| 212 | std::wstring* file) { |
| 213 | if (!extension.empty() && extension != L".") { |
| 214 | if (extension[0] != L'.') |
| 215 | file->append(L"."); |
| 216 | file->append(extension); |
| 217 | } |
| 218 | } |
| 219 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 220 | void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 221 | const std::wstring::size_type last_dot = file_name->rfind(L'.'); |
[email protected] | b988fe4d | 2008-09-19 17:32:18 | [diff] [blame] | 222 | if (last_dot == std::wstring::npos) { |
| 223 | // No extension, just append the supplied extension. |
| 224 | AppendExtension(extension, file_name); |
| 225 | return; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 226 | } |
[email protected] | b988fe4d | 2008-09-19 17:32:18 | [diff] [blame] | 227 | const std::wstring::size_type last_separator = |
| 228 | file_name->rfind(kPathSeparator); |
| 229 | if (last_separator != std::wstring::npos && last_dot < last_separator) { |
| 230 | // File name doesn't have extension, but one of the directories does; don't |
| 231 | // replace it, just append the supplied extension. For example |
| 232 | // 'c:\tmp.bar\foo'. |
| 233 | AppendExtension(extension, file_name); |
| 234 | return; |
| 235 | } |
| 236 | std::wstring result = file_name->substr(0, last_dot); |
| 237 | AppendExtension(extension, &result); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 238 | file_name->swap(result); |
| 239 | } |
| 240 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 241 | bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 242 | // We open the file in binary format even if they are text files because |
| 243 | // we are just comparing that bytes are exactly same in both files and not |
| 244 | // doing anything smart with text formatting. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 245 | std::ifstream file1(filename1.value().c_str(), |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 246 | std::ios::in | std::ios::binary); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 247 | std::ifstream file2(filename2.value().c_str(), |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 248 | std::ios::in | std::ios::binary); |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 249 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 250 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 251 | // any unusable file yields a result of "false". |
| 252 | if (!file1.is_open() || !file2.is_open()) |
| 253 | return false; |
| 254 | |
| 255 | const int BUFFER_SIZE = 2056; |
| 256 | char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE]; |
| 257 | do { |
| 258 | file1.read(buffer1, BUFFER_SIZE); |
| 259 | file2.read(buffer2, BUFFER_SIZE); |
| 260 | |
| 261 | if ((file1.eof() && !file2.eof()) || |
| 262 | (!file1.eof() && file2.eof()) || |
| 263 | (file1.gcount() != file2.gcount()) || |
| 264 | (memcmp(buffer1, buffer2, file1.gcount()))) { |
| 265 | file1.close(); |
| 266 | file2.close(); |
| 267 | return false; |
| 268 | } |
| 269 | } while (!file1.eof() && !file2.eof()); |
| 270 | |
| 271 | file1.close(); |
| 272 | file2.close(); |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | bool ReadFileToString(const std::wstring& path, std::string* contents) { |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 277 | FILE* file = OpenFile(path, "rb"); |
| 278 | if (!file) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 279 | return false; |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 280 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 281 | |
| 282 | char buf[1 << 16]; |
| 283 | size_t len; |
| 284 | while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { |
| 285 | contents->append(buf, len); |
| 286 | } |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 287 | CloseFile(file); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 288 | |
| 289 | return true; |
| 290 | } |
| 291 | |
[email protected] | f5e3da4d | 2008-09-26 01:04:08 | [diff] [blame] | 292 | bool GetFileSize(const std::wstring& file_path, int64* file_size) { |
| 293 | FileInfo info; |
| 294 | if (!GetFileInfo(file_path, &info)) |
| 295 | return false; |
| 296 | *file_size = info.size; |
| 297 | return true; |
| 298 | } |
| 299 | |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 300 | bool CloseFile(FILE* file) { |
[email protected] | a1a1950 | 2008-10-21 17:14:45 | [diff] [blame] | 301 | if (file == NULL) |
| 302 | return true; |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 303 | return fclose(file) == 0; |
| 304 | } |
| 305 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 306 | // Deprecated functions ---------------------------------------------------- |
| 307 | |
| 308 | bool AbsolutePath(std::wstring* path_str) { |
[email protected] | 4a2952f | 2008-10-31 02:03:07 | [diff] [blame] | 309 | FilePath path(FilePath::FromWStringHack(*path_str)); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 310 | if (!AbsolutePath(&path)) |
| 311 | return false; |
| 312 | *path_str = path.ToWStringHack(); |
| 313 | return true; |
| 314 | } |
| 315 | bool Delete(const std::wstring& path, bool recursive) { |
| 316 | return Delete(FilePath::FromWStringHack(path), recursive); |
| 317 | } |
| 318 | bool Move(const std::wstring& from_path, const std::wstring& to_path) { |
| 319 | return Move(FilePath::FromWStringHack(from_path), |
| 320 | FilePath::FromWStringHack(to_path)); |
| 321 | } |
| 322 | bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) { |
| 323 | return CopyFile(FilePath::FromWStringHack(from_path), |
| 324 | FilePath::FromWStringHack(to_path)); |
| 325 | } |
| 326 | bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path, |
| 327 | bool recursive) { |
| 328 | return CopyDirectory(FilePath::FromWStringHack(from_path), |
| 329 | FilePath::FromWStringHack(to_path), |
| 330 | recursive); |
| 331 | } |
| 332 | bool PathExists(const std::wstring& path) { |
| 333 | return PathExists(FilePath::FromWStringHack(path)); |
| 334 | } |
| 335 | bool DirectoryExists(const std::wstring& path) { |
| 336 | return DirectoryExists(FilePath::FromWStringHack(path)); |
| 337 | } |
| 338 | bool ContentsEqual(const std::wstring& filename1, |
| 339 | const std::wstring& filename2) { |
| 340 | return ContentsEqual(FilePath::FromWStringHack(filename1), |
| 341 | FilePath::FromWStringHack(filename2)); |
| 342 | } |
| 343 | bool CreateDirectory(const std::wstring& full_path) { |
| 344 | return CreateDirectory(FilePath::FromWStringHack(full_path)); |
| 345 | } |
| 346 | bool GetCurrentDirectory(std::wstring* path_str) { |
| 347 | FilePath path; |
| 348 | if (!GetCurrentDirectory(&path)) |
| 349 | return false; |
| 350 | *path_str = path.ToWStringHack(); |
| 351 | return true; |
| 352 | } |
| 353 | bool GetTempDir(std::wstring* path_str) { |
| 354 | FilePath path; |
| 355 | if (!GetTempDir(&path)) |
| 356 | return false; |
| 357 | *path_str = path.ToWStringHack(); |
| 358 | return true; |
| 359 | } |
| 360 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 361 | } // namespace |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 362 | |