OLD | NEW |
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/files/file_util.h" | 5 #include "base/files/file_util.h" |
6 | 6 |
7 #if defined(OS_WIN) | |
8 #include <io.h> | |
9 #endif | |
10 #include <stdio.h> | 7 #include <stdio.h> |
11 | 8 |
12 #include <fstream> | 9 #include <fstream> |
13 #include <limits> | 10 #include <limits> |
14 | 11 |
15 #include "base/files/file_enumerator.h" | 12 #include "base/files/file_enumerator.h" |
16 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
17 #include "base/logging.h" | 14 #include "base/logging.h" |
18 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
19 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 return false; | 188 return false; |
192 *file_size = info.size; | 189 *file_size = info.size; |
193 return true; | 190 return true; |
194 } | 191 } |
195 | 192 |
196 bool TouchFile(const FilePath& path, | 193 bool TouchFile(const FilePath& path, |
197 const Time& last_accessed, | 194 const Time& last_accessed, |
198 const Time& last_modified) { | 195 const Time& last_modified) { |
199 int flags = File::FLAG_OPEN | File::FLAG_WRITE_ATTRIBUTES; | 196 int flags = File::FLAG_OPEN | File::FLAG_WRITE_ATTRIBUTES; |
200 | 197 |
201 #if defined(OS_WIN) | |
202 // On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory. | |
203 if (DirectoryExists(path)) | |
204 flags |= File::FLAG_BACKUP_SEMANTICS; | |
205 #endif // OS_WIN | |
206 | |
207 File file(path, flags); | 198 File file(path, flags); |
208 if (!file.IsValid()) | 199 if (!file.IsValid()) |
209 return false; | 200 return false; |
210 | 201 |
211 return file.SetTimes(last_accessed, last_modified); | 202 return file.SetTimes(last_accessed, last_modified); |
212 } | 203 } |
213 #endif // !defined(OS_NACL_NONSFI) | 204 #endif // !defined(OS_NACL_NONSFI) |
214 | 205 |
215 bool CloseFile(FILE* file) { | 206 bool CloseFile(FILE* file) { |
216 if (file == NULL) | 207 if (file == NULL) |
217 return true; | 208 return true; |
218 return fclose(file) == 0; | 209 return fclose(file) == 0; |
219 } | 210 } |
220 | 211 |
221 #if !defined(OS_NACL_NONSFI) | 212 #if !defined(OS_NACL_NONSFI) |
222 bool TruncateFile(FILE* file) { | 213 bool TruncateFile(FILE* file) { |
223 if (file == NULL) | 214 if (file == NULL) |
224 return false; | 215 return false; |
225 long current_offset = ftell(file); | 216 long current_offset = ftell(file); |
226 if (current_offset == -1) | 217 if (current_offset == -1) |
227 return false; | 218 return false; |
228 #if defined(OS_WIN) | |
229 int fd = _fileno(file); | |
230 if (_chsize(fd, current_offset) != 0) | |
231 return false; | |
232 #else | |
233 int fd = fileno(file); | 219 int fd = fileno(file); |
234 if (ftruncate(fd, current_offset) != 0) | 220 if (ftruncate(fd, current_offset) != 0) |
235 return false; | 221 return false; |
236 #endif | |
237 return true; | 222 return true; |
238 } | 223 } |
239 | 224 |
240 int GetUniquePathNumber(const FilePath& path, | 225 int GetUniquePathNumber(const FilePath& path, |
241 const FilePath::StringType& suffix) { | 226 const FilePath::StringType& suffix) { |
242 bool have_suffix = !suffix.empty(); | 227 bool have_suffix = !suffix.empty(); |
243 if (!PathExists(path) && | 228 if (!PathExists(path) && |
244 (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) { | 229 (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) { |
245 return 0; | 230 return 0; |
246 } | 231 } |
247 | 232 |
248 FilePath new_path; | 233 FilePath new_path; |
249 for (int count = 1; count <= kMaxUniqueFiles; ++count) { | 234 for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
250 new_path = path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", count)); | 235 new_path = path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", count)); |
251 if (!PathExists(new_path) && | 236 if (!PathExists(new_path) && |
252 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { | 237 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { |
253 return count; | 238 return count; |
254 } | 239 } |
255 } | 240 } |
256 | 241 |
257 return -1; | 242 return -1; |
258 } | 243 } |
259 #endif // !defined(OS_NACL_NONSFI) | 244 #endif // !defined(OS_NACL_NONSFI) |
260 | 245 |
261 } // namespace base | 246 } // namespace base |
OLD | NEW |