Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: base/file_util.cc

Issue 19052005: Move PathIsWritable, DirectoryExists, ContentsEqual, and TextContentsEqual to the base namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/file_util.h ('k') | base/file_util_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h" 5 #include "base/file_util.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <io.h> 8 #include <io.h>
9 #endif 9 #endif
10 #include <stdio.h> 10 #include <stdio.h>
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 return false; 49 return false;
50 return internal::MoveUnsafe(from_path, to_path); 50 return internal::MoveUnsafe(from_path, to_path);
51 } 51 }
52 52
53 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { 53 bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
54 if (from_path.ReferencesParent() || to_path.ReferencesParent()) 54 if (from_path.ReferencesParent() || to_path.ReferencesParent())
55 return false; 55 return false;
56 return internal::CopyFileUnsafe(from_path, to_path); 56 return internal::CopyFileUnsafe(from_path, to_path);
57 } 57 }
58 58
59 } // namespace base
60
61 // -----------------------------------------------------------------------------
62
63 namespace file_util {
64
65 using base::FileEnumerator;
66 using base::FilePath;
67 using base::kExtensionSeparator;
68 using base::kMaxUniqueFiles;
69
70 bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) { 59 bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) {
71 // We open the file in binary format even if they are text files because 60 // We open the file in binary format even if they are text files because
72 // we are just comparing that bytes are exactly same in both files and not 61 // we are just comparing that bytes are exactly same in both files and not
73 // doing anything smart with text formatting. 62 // doing anything smart with text formatting.
74 std::ifstream file1(filename1.value().c_str(), 63 std::ifstream file1(filename1.value().c_str(),
75 std::ios::in | std::ios::binary); 64 std::ios::in | std::ios::binary);
76 std::ifstream file2(filename2.value().c_str(), 65 std::ifstream file2(filename2.value().c_str(),
77 std::ios::in | std::ios::binary); 66 std::ios::in | std::ios::binary);
78 67
79 // Even if both files aren't openable (and thus, in some sense, "equal"), 68 // Even if both files aren't openable (and thus, in some sense, "equal"),
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 else if (end2 + 1 < line2.length()) 123 else if (end2 + 1 < line2.length())
135 line2.erase(end2 + 1); 124 line2.erase(end2 + 1);
136 125
137 if (line1 != line2) 126 if (line1 != line2)
138 return false; 127 return false;
139 } while (!file1.eof() || !file2.eof()); 128 } while (!file1.eof() || !file2.eof());
140 129
141 return true; 130 return true;
142 } 131 }
143 132
133 } // namespace base
134
135 // -----------------------------------------------------------------------------
136
137 namespace file_util {
138
139 using base::FileEnumerator;
140 using base::FilePath;
141 using base::kExtensionSeparator;
142 using base::kMaxUniqueFiles;
143
144 bool ReadFileToString(const FilePath& path, std::string* contents) { 144 bool ReadFileToString(const FilePath& path, std::string* contents) {
145 if (path.ReferencesParent()) 145 if (path.ReferencesParent())
146 return false; 146 return false;
147 FILE* file = OpenFile(path, "rb"); 147 FILE* file = OpenFile(path, "rb");
148 if (!file) { 148 if (!file) {
149 return false; 149 return false;
150 } 150 }
151 151
152 char buf[1 << 16]; 152 char buf[1 << 16];
153 size_t len; 153 size_t len;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 if (!PathExists(new_path) && 255 if (!PathExists(new_path) &&
256 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { 256 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) {
257 return count; 257 return count;
258 } 258 }
259 } 259 }
260 260
261 return -1; 261 return -1;
262 } 262 }
263 263
264 } // namespace file_util 264 } // namespace file_util
OLDNEW
« no previous file with comments | « base/file_util.h ('k') | base/file_util_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698