tfarina@chromium.org | 3482822 | 2010-05-26 10:40:12 +0900 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
| 5 | #include "base/file_util.h" |
| 6 | |
jeremy@chromium.org | a5f404d | 2009-01-28 04:08:39 +0900 | [diff] [blame] | 7 | #if defined(OS_WIN) |
| 8 | #include <io.h> |
| 9 | #endif |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 12 | #include <fstream> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 13 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 14 | #include "base/file_path.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 15 | #include "base/logging.h" |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 16 | #include "base/string_piece.h" |
brettw@chromium.org | 50c9465 | 2009-10-07 11:10:20 +0900 | [diff] [blame] | 17 | #include "base/string_util.h" |
| 18 | #include "base/utf_string_conversions.h" |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 19 | |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.'); |
| 23 | |
willchan@chromium.org | 2f0b26b | 2009-04-25 02:44:39 +0900 | [diff] [blame] | 24 | } // namespace |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 25 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 26 | namespace file_util { |
| 27 | |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 28 | bool EndsWithSeparator(const FilePath& path) { |
| 29 | FilePath::StringType value = path.value(); |
| 30 | if (value.empty()) |
| 31 | return false; |
| 32 | |
| 33 | return FilePath::IsSeparator(value[value.size() - 1]); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 34 | } |
| 35 | |
estade@chromium.org | d778b8f | 2008-11-26 07:04:37 +0900 | [diff] [blame] | 36 | bool EnsureEndsWithSeparator(FilePath* path) { |
| 37 | if (!DirectoryExists(*path)) |
| 38 | return false; |
| 39 | |
| 40 | if (EndsWithSeparator(*path)) |
| 41 | return true; |
| 42 | |
| 43 | FilePath::StringType& path_str = |
| 44 | const_cast<FilePath::StringType&>(path->value()); |
| 45 | path_str.append(&FilePath::kSeparators[0], 1); |
| 46 | |
| 47 | return true; |
| 48 | } |
| 49 | |
estade@chromium.org | ae50894 | 2009-01-07 08:31:41 +0900 | [diff] [blame] | 50 | FilePath::StringType GetFileExtensionFromPath(const FilePath& path) { |
| 51 | FilePath::StringType file_name = path.BaseName().value(); |
| 52 | const FilePath::StringType::size_type last_dot = |
| 53 | file_name.rfind(kExtensionSeparator); |
| 54 | return FilePath::StringType(last_dot == FilePath::StringType::npos ? |
| 55 | FILE_PATH_LITERAL("") : |
| 56 | file_name, last_dot+1); |
glen@chromium.org | 40eb492 | 2008-11-04 01:14:46 +0900 | [diff] [blame] | 57 | } |
| 58 | |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 59 | void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix) { |
| 60 | FilePath::StringType& value = |
| 61 | const_cast<FilePath::StringType&>(path->value()); |
| 62 | |
| 63 | const FilePath::StringType::size_type last_dot = |
| 64 | value.rfind(kExtensionSeparator); |
| 65 | const FilePath::StringType::size_type last_separator = |
| 66 | value.find_last_of(FilePath::StringType(FilePath::kSeparators)); |
| 67 | |
| 68 | if (last_dot == FilePath::StringType::npos || |
| 69 | (last_separator != std::wstring::npos && last_dot < last_separator)) { |
| 70 | // The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo". |
| 71 | // We should just append the suffix to the entire path. |
| 72 | value.append(suffix); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | value.insert(last_dot, suffix); |
| 77 | } |
| 78 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 79 | bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 80 | // We open the file in binary format even if they are text files because |
| 81 | // we are just comparing that bytes are exactly same in both files and not |
| 82 | // doing anything smart with text formatting. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 83 | std::ifstream file1(filename1.value().c_str(), |
erikkay@google.com | 9fc57d0 | 2008-08-09 05:16:08 +0900 | [diff] [blame] | 84 | std::ios::in | std::ios::binary); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 85 | std::ifstream file2(filename2.value().c_str(), |
erikkay@google.com | 9fc57d0 | 2008-08-09 05:16:08 +0900 | [diff] [blame] | 86 | std::ios::in | std::ios::binary); |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 87 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 88 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 89 | // any unusable file yields a result of "false". |
| 90 | if (!file1.is_open() || !file2.is_open()) |
| 91 | return false; |
| 92 | |
| 93 | const int BUFFER_SIZE = 2056; |
| 94 | char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE]; |
| 95 | do { |
| 96 | file1.read(buffer1, BUFFER_SIZE); |
| 97 | file2.read(buffer2, BUFFER_SIZE); |
| 98 | |
mark@chromium.org | 95c9ec9 | 2009-06-27 06:17:24 +0900 | [diff] [blame] | 99 | if ((file1.eof() != file2.eof()) || |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 100 | (file1.gcount() != file2.gcount()) || |
| 101 | (memcmp(buffer1, buffer2, file1.gcount()))) { |
| 102 | file1.close(); |
| 103 | file2.close(); |
| 104 | return false; |
| 105 | } |
mark@chromium.org | 95c9ec9 | 2009-06-27 06:17:24 +0900 | [diff] [blame] | 106 | } while (!file1.eof() || !file2.eof()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 107 | |
| 108 | file1.close(); |
| 109 | file2.close(); |
| 110 | return true; |
| 111 | } |
| 112 | |
mark@chromium.org | 95c9ec9 | 2009-06-27 06:17:24 +0900 | [diff] [blame] | 113 | bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
| 114 | std::ifstream file1(filename1.value().c_str(), std::ios::in); |
| 115 | std::ifstream file2(filename2.value().c_str(), std::ios::in); |
| 116 | |
| 117 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 118 | // any unusable file yields a result of "false". |
| 119 | if (!file1.is_open() || !file2.is_open()) |
| 120 | return false; |
| 121 | |
| 122 | do { |
| 123 | std::string line1, line2; |
| 124 | getline(file1, line1); |
| 125 | getline(file2, line2); |
| 126 | |
| 127 | // Check for mismatched EOF states, or any error state. |
| 128 | if ((file1.eof() != file2.eof()) || |
| 129 | file1.bad() || file2.bad()) { |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | // Trim all '\r' and '\n' characters from the end of the line. |
| 134 | std::string::size_type end1 = line1.find_last_not_of("\r\n"); |
| 135 | if (end1 == std::string::npos) |
| 136 | line1.clear(); |
| 137 | else if (end1 + 1 < line1.length()) |
| 138 | line1.erase(end1 + 1); |
| 139 | |
| 140 | std::string::size_type end2 = line2.find_last_not_of("\r\n"); |
| 141 | if (end2 == std::string::npos) |
| 142 | line2.clear(); |
| 143 | else if (end2 + 1 < line2.length()) |
| 144 | line2.erase(end2 + 1); |
| 145 | |
| 146 | if (line1 != line2) |
| 147 | return false; |
| 148 | } while (!file1.eof() || !file2.eof()); |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
erikkay@google.com | 52460fb | 2009-01-28 09:22:46 +0900 | [diff] [blame] | 153 | bool ReadFileToString(const FilePath& path, std::string* contents) { |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 154 | FILE* file = OpenFile(path, "rb"); |
| 155 | if (!file) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 156 | return false; |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 157 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 158 | |
| 159 | char buf[1 << 16]; |
| 160 | size_t len; |
| 161 | while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { |
evan@chromium.org | a75b2e8 | 2010-05-19 20:07:55 +0900 | [diff] [blame] | 162 | if (contents) |
| 163 | contents->append(buf, len); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 164 | } |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 165 | CloseFile(file); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 166 | |
| 167 | return true; |
| 168 | } |
| 169 | |
tfarina@chromium.org | 3482822 | 2010-05-26 10:40:12 +0900 | [diff] [blame] | 170 | bool IsDirectoryEmpty(const FilePath& dir_path) { |
| 171 | FileEnumerator files(dir_path, false, |
| 172 | static_cast<FileEnumerator::FILE_TYPE>( |
| 173 | FileEnumerator::FILES | FileEnumerator::DIRECTORIES)); |
| 174 | if (files.Next().value().empty()) |
| 175 | return true; |
| 176 | return false; |
| 177 | } |
| 178 | |
phajdan.jr@chromium.org | 8139fe1 | 2009-04-28 15:50:36 +0900 | [diff] [blame] | 179 | FILE* CreateAndOpenTemporaryFile(FilePath* path) { |
| 180 | FilePath directory; |
| 181 | if (!GetTempDir(&directory)) |
evan@chromium.org | 2abe0b4 | 2010-06-11 07:56:23 +0900 | [diff] [blame] | 182 | return NULL; |
phajdan.jr@chromium.org | 8139fe1 | 2009-04-28 15:50:36 +0900 | [diff] [blame] | 183 | |
| 184 | return CreateAndOpenTemporaryFileInDir(directory, path); |
| 185 | } |
| 186 | |
dkegel@google.com | 4498268 | 2008-11-05 06:00:46 +0900 | [diff] [blame] | 187 | bool GetFileSize(const FilePath& file_path, int64* file_size) { |
darin@google.com | 7f479f2 | 2008-09-26 10:04:08 +0900 | [diff] [blame] | 188 | FileInfo info; |
| 189 | if (!GetFileInfo(file_path, &info)) |
| 190 | return false; |
| 191 | *file_size = info.size; |
| 192 | return true; |
| 193 | } |
| 194 | |
estade@chromium.org | a99d0e1 | 2010-02-12 08:27:47 +0900 | [diff] [blame] | 195 | bool IsDot(const FilePath& path) { |
| 196 | return FILE_PATH_LITERAL(".") == path.BaseName().value(); |
| 197 | } |
| 198 | |
| 199 | bool IsDotDot(const FilePath& path) { |
| 200 | return FILE_PATH_LITERAL("..") == path.BaseName().value(); |
| 201 | } |
| 202 | |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 203 | bool CloseFile(FILE* file) { |
sidchat@google.com | d3b2643 | 2008-10-22 02:14:45 +0900 | [diff] [blame] | 204 | if (file == NULL) |
| 205 | return true; |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 206 | return fclose(file) == 0; |
| 207 | } |
| 208 | |
jeremy@chromium.org | a5f404d | 2009-01-28 04:08:39 +0900 | [diff] [blame] | 209 | bool TruncateFile(FILE* file) { |
| 210 | if (file == NULL) |
| 211 | return false; |
| 212 | long current_offset = ftell(file); |
| 213 | if (current_offset == -1) |
| 214 | return false; |
| 215 | #if defined(OS_WIN) |
| 216 | int fd = _fileno(file); |
| 217 | if (_chsize(fd, current_offset) != 0) |
| 218 | return false; |
| 219 | #else |
| 220 | int fd = fileno(file); |
| 221 | if (ftruncate(fd, current_offset) != 0) |
| 222 | return false; |
| 223 | #endif |
| 224 | return true; |
| 225 | } |
| 226 | |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 227 | bool ContainsPath(const FilePath &parent, const FilePath& child) { |
| 228 | FilePath abs_parent = FilePath(parent); |
| 229 | FilePath abs_child = FilePath(child); |
| 230 | |
| 231 | if (!file_util::AbsolutePath(&abs_parent) || |
| 232 | !file_util::AbsolutePath(&abs_child)) |
| 233 | return false; |
| 234 | |
| 235 | #if defined(OS_WIN) |
| 236 | // file_util::AbsolutePath() does not flatten case on Windows, so we must do |
| 237 | // a case-insensitive compare. |
| 238 | if (!StartsWith(abs_child.value(), abs_parent.value(), false)) |
| 239 | #else |
| 240 | if (!StartsWithASCII(abs_child.value(), abs_parent.value(), true)) |
| 241 | #endif |
| 242 | return false; |
| 243 | |
| 244 | // file_util::AbsolutePath() normalizes '/' to '\' on Windows, so we only need |
| 245 | // to check kSeparators[0]. |
| 246 | if (abs_child.value().length() <= abs_parent.value().length() || |
| 247 | abs_child.value()[abs_parent.value().length()] != |
| 248 | FilePath::kSeparators[0]) |
| 249 | return false; |
| 250 | |
| 251 | return true; |
| 252 | } |
| 253 | |
cpu@chromium.org | 83f07be | 2010-03-25 06:56:26 +0900 | [diff] [blame] | 254 | int64 ComputeDirectorySize(const FilePath& root_path) { |
| 255 | int64 running_size = 0; |
| 256 | FileEnumerator file_iter(root_path, true, FileEnumerator::FILES); |
| 257 | for (FilePath current = file_iter.Next(); !current.empty(); |
| 258 | current = file_iter.Next()) { |
| 259 | FileEnumerator::FindInfo info; |
| 260 | file_iter.GetFindInfo(&info); |
| 261 | #if defined(OS_WIN) |
| 262 | LARGE_INTEGER li = { info.nFileSizeLow, info.nFileSizeHigh }; |
| 263 | running_size += li.QuadPart; |
| 264 | #else |
| 265 | running_size += info.stat.st_size; |
| 266 | #endif |
| 267 | } |
| 268 | return running_size; |
| 269 | } |
| 270 | |
rvargas@google.com | aa24e11 | 2010-06-12 07:53:43 +0900 | [diff] [blame^] | 271 | int64 ComputeFilesSize(const FilePath& directory, |
| 272 | const FilePath::StringType& pattern) { |
| 273 | int64 running_size = 0; |
| 274 | FileEnumerator file_iter(directory, false, FileEnumerator::FILES, pattern); |
| 275 | for (FilePath current = file_iter.Next(); !current.empty(); |
| 276 | current = file_iter.Next()) { |
| 277 | FileEnumerator::FindInfo info; |
| 278 | file_iter.GetFindInfo(&info); |
| 279 | #if defined(OS_WIN) |
| 280 | LARGE_INTEGER li = { info.nFileSizeLow, info.nFileSizeHigh }; |
| 281 | running_size += li.QuadPart; |
| 282 | #else |
| 283 | running_size += info.stat.st_size; |
| 284 | #endif |
| 285 | } |
| 286 | return running_size; |
| 287 | } |
| 288 | |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 289 | /////////////////////////////////////////////// |
| 290 | // MemoryMappedFile |
| 291 | |
| 292 | MemoryMappedFile::~MemoryMappedFile() { |
| 293 | CloseHandles(); |
| 294 | } |
| 295 | |
estade@chromium.org | 3ac3206 | 2009-11-17 07:55:17 +0900 | [diff] [blame] | 296 | bool MemoryMappedFile::Initialize(base::PlatformFile file) { |
| 297 | if (IsValid()) |
| 298 | return false; |
| 299 | |
| 300 | file_ = file; |
| 301 | |
| 302 | if (!MapFileToMemoryInternal()) { |
| 303 | CloseHandles(); |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | return true; |
| 308 | } |
| 309 | |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 310 | bool MemoryMappedFile::Initialize(const FilePath& file_name) { |
| 311 | if (IsValid()) |
| 312 | return false; |
| 313 | |
| 314 | if (!MapFileToMemory(file_name)) { |
| 315 | CloseHandles(); |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | return true; |
| 320 | } |
| 321 | |
estade@chromium.org | 3ac3206 | 2009-11-17 07:55:17 +0900 | [diff] [blame] | 322 | bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) { |
| 323 | file_ = base::CreatePlatformFile(file_name, |
| 324 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
| 325 | NULL); |
| 326 | |
| 327 | if (file_ == base::kInvalidPlatformFileValue) { |
| 328 | LOG(ERROR) << "Couldn't open " << file_name.value(); |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | return MapFileToMemoryInternal(); |
| 333 | } |
| 334 | |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 335 | bool MemoryMappedFile::IsValid() { |
| 336 | return data_ != NULL; |
| 337 | } |
| 338 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 339 | // Deprecated functions ---------------------------------------------------- |
| 340 | |
erikkay@google.com | 52460fb | 2009-01-28 09:22:46 +0900 | [diff] [blame] | 341 | bool ReadFileToString(const std::wstring& path, std::string* contents) { |
| 342 | return ReadFileToString(FilePath::FromWStringHack(path), contents); |
| 343 | } |
| 344 | |
evan@chromium.org | 68a7a91 | 2009-10-10 06:03:58 +0900 | [diff] [blame] | 345 | bool AbsolutePath(std::wstring* path_str) { |
| 346 | FilePath path(FilePath::FromWStringHack(*path_str)); |
| 347 | if (!AbsolutePath(&path)) |
| 348 | return false; |
| 349 | *path_str = path.ToWStringHack(); |
| 350 | return true; |
| 351 | } |
evan@chromium.org | 1db7f94 | 2010-02-27 00:11:55 +0900 | [diff] [blame] | 352 | |
| 353 | #if defined(OS_WIN) |
| 354 | // This function is deprecated; see file_util_deprecated.h for details. |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 355 | void AppendToPath(std::wstring* path, const std::wstring& new_ending) { |
| 356 | if (!path) { |
| 357 | NOTREACHED(); |
| 358 | return; // Don't crash in this function in release builds. |
| 359 | } |
| 360 | |
evan@chromium.org | 1db7f94 | 2010-02-27 00:11:55 +0900 | [diff] [blame] | 361 | if (!EndsWithSeparator(*path)) |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 362 | path->push_back(FilePath::kSeparators[0]); |
| 363 | path->append(new_ending); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 364 | } |
evan@chromium.org | 1db7f94 | 2010-02-27 00:11:55 +0900 | [diff] [blame] | 365 | #endif |
| 366 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 367 | bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path, |
| 368 | bool recursive) { |
| 369 | return CopyDirectory(FilePath::FromWStringHack(from_path), |
| 370 | FilePath::FromWStringHack(to_path), |
| 371 | recursive); |
| 372 | } |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 373 | bool Delete(const std::wstring& path, bool recursive) { |
| 374 | return Delete(FilePath::FromWStringHack(path), recursive); |
| 375 | } |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 376 | bool EndsWithSeparator(const std::wstring& path) { |
| 377 | return EndsWithSeparator(FilePath::FromWStringHack(path)); |
| 378 | } |
estade@chromium.org | ae50894 | 2009-01-07 08:31:41 +0900 | [diff] [blame] | 379 | std::wstring GetFileExtensionFromPath(const std::wstring& path) { |
| 380 | FilePath::StringType extension = |
| 381 | GetFileExtensionFromPath(FilePath::FromWStringHack(path)); |
| 382 | #if defined(OS_WIN) |
| 383 | return extension; |
| 384 | #elif defined(OS_POSIX) |
| 385 | return UTF8ToWide(extension); |
| 386 | #endif |
| 387 | } |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 388 | std::wstring GetFilenameFromPath(const std::wstring& path) { |
| 389 | if (path.empty() || EndsWithSeparator(path)) |
| 390 | return std::wstring(); |
| 391 | |
estade@chromium.org | d872d68 | 2009-01-06 08:59:36 +0900 | [diff] [blame] | 392 | return FilePath::FromWStringHack(path).BaseName().ToWStringHack(); |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 393 | } |
estade@chromium.org | b1d358a | 2008-11-18 06:01:19 +0900 | [diff] [blame] | 394 | FILE* OpenFile(const std::wstring& filename, const char* mode) { |
| 395 | return OpenFile(FilePath::FromWStringHack(filename), mode); |
| 396 | } |
estade@chromium.org | 9d32ed8 | 2009-01-28 14:47:15 +0900 | [diff] [blame] | 397 | int ReadFile(const std::wstring& filename, char* data, int size) { |
| 398 | return ReadFile(FilePath::FromWStringHack(filename), data, size); |
| 399 | } |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 400 | void UpOneDirectory(std::wstring* dir) { |
| 401 | FilePath path = FilePath::FromWStringHack(*dir); |
| 402 | FilePath directory = path.DirName(); |
| 403 | // If there is no separator, we will get back kCurrentDirectory. |
| 404 | // In this case don't change |dir|. |
| 405 | if (directory.value() != FilePath::kCurrentDirectory) |
| 406 | *dir = directory.ToWStringHack(); |
| 407 | } |
| 408 | void UpOneDirectoryOrEmpty(std::wstring* dir) { |
| 409 | FilePath path = FilePath::FromWStringHack(*dir); |
| 410 | FilePath directory = path.DirName(); |
| 411 | // If there is no separator, we will get back kCurrentDirectory. |
| 412 | // In this case, clear dir. |
| 413 | if (directory == path || directory.value() == FilePath::kCurrentDirectory) |
| 414 | dir->clear(); |
| 415 | else |
| 416 | *dir = directory.ToWStringHack(); |
| 417 | } |
estade@chromium.org | 9d32ed8 | 2009-01-28 14:47:15 +0900 | [diff] [blame] | 418 | int WriteFile(const std::wstring& filename, const char* data, int size) { |
| 419 | return WriteFile(FilePath::FromWStringHack(filename), data, size); |
| 420 | } |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 421 | |
| 422 | /////////////////////////////////////////////// |
| 423 | // FileEnumerator |
| 424 | // |
| 425 | // Note: the main logic is in file_util_<platform>.cc |
| 426 | |
| 427 | bool FileEnumerator::ShouldSkip(const FilePath& path) { |
| 428 | FilePath::StringType basename = path.BaseName().value(); |
| 429 | return IsDot(path) || (IsDotDot(path) && !(INCLUDE_DOT_DOT & file_type_)); |
| 430 | } |
| 431 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 432 | } // namespace |