OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 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 // This file contains utility functions for dealing with the local | 5 // This file contains utility functions for dealing with the local |
6 // filesystem. | 6 // filesystem. |
7 | 7 |
8 #ifndef BASE_FILE_UTIL_H_ | 8 #ifndef BASE_FILE_UTIL_H_ |
9 #define BASE_FILE_UTIL_H_ | 9 #define BASE_FILE_UTIL_H_ |
10 | 10 |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 | 12 |
13 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
14 #include <windows.h> | 14 #include <windows.h> |
15 #elif defined(OS_POSIX) | 15 #elif defined(OS_POSIX) |
16 #include <fts.h> | 16 #include <fts.h> |
17 #include <sys/stat.h> | 17 #include <sys/stat.h> |
18 #endif | 18 #endif |
19 | 19 |
20 #include <stdio.h> | 20 #include <stdio.h> |
21 | 21 |
22 #include <stack> | 22 #include <stack> |
23 #include <string> | 23 #include <string> |
24 #include <vector> | 24 #include <vector> |
25 | 25 |
26 #include "base/basictypes.h" | 26 #include "base/basictypes.h" |
| 27 #include "base/scoped_ptr.h" |
27 #include "base/file_path.h" | 28 #include "base/file_path.h" |
28 | 29 |
29 namespace file_util { | 30 namespace file_util { |
30 | 31 |
31 //----------------------------------------------------------------------------- | 32 //----------------------------------------------------------------------------- |
32 // Functions that operate purely on a path string w/o touching the filesystem: | 33 // Functions that operate purely on a path string w/o touching the filesystem: |
33 | 34 |
34 // Returns a vector of all of the components of the provided path. | 35 // Returns a vector of all of the components of the provided path. |
35 void PathComponents(const FilePath& path, | 36 void PathComponents(const FilePath& path, |
36 std::vector<FilePath::StringType>* components); | 37 std::vector<FilePath::StringType>* components); |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 | 312 |
312 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success. | 313 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success. |
313 FILE* OpenFile(const FilePath& filename, const char* mode); | 314 FILE* OpenFile(const FilePath& filename, const char* mode); |
314 // Deprecated temporary compatibility functions. | 315 // Deprecated temporary compatibility functions. |
315 FILE* OpenFile(const std::string& filename, const char* mode); | 316 FILE* OpenFile(const std::string& filename, const char* mode); |
316 FILE* OpenFile(const std::wstring& filename, const char* mode); | 317 FILE* OpenFile(const std::wstring& filename, const char* mode); |
317 | 318 |
318 // Closes file opened by OpenFile. Returns true on success. | 319 // Closes file opened by OpenFile. Returns true on success. |
319 bool CloseFile(FILE* file); | 320 bool CloseFile(FILE* file); |
320 | 321 |
| 322 // Truncates an open file to end at the location of the current file pointer. |
| 323 // This is a cross-platform analog to Windows' SetEndOfFile() function. |
| 324 bool TruncateFile(FILE* file); |
| 325 |
321 // Reads the given number of bytes from the file into the buffer. Returns | 326 // Reads the given number of bytes from the file into the buffer. Returns |
322 // the number of read bytes, or -1 on error. | 327 // the number of read bytes, or -1 on error. |
323 int ReadFile(const std::wstring& filename, char* data, int size); | 328 int ReadFile(const std::wstring& filename, char* data, int size); |
324 | 329 |
325 // Writes the given buffer into the file, overwriting any data that was | 330 // Writes the given buffer into the file, overwriting any data that was |
326 // previously there. Returns the number of bytes written, or -1 on error. | 331 // previously there. Returns the number of bytes written, or -1 on error. |
327 int WriteFile(const std::wstring& filename, const char* data, int size); | 332 int WriteFile(const std::wstring& filename, const char* data, int size); |
328 | 333 |
329 // Gets the current working directory for the process. | 334 // Gets the current working directory for the process. |
330 bool GetCurrentDirectory(FilePath* path); | 335 bool GetCurrentDirectory(FilePath* path); |
331 // Deprecated temporary compatibility function. | 336 // Deprecated temporary compatibility function. |
332 bool GetCurrentDirectory(std::wstring* path); | 337 bool GetCurrentDirectory(std::wstring* path); |
333 | 338 |
334 // Sets the current working directory for the process. | 339 // Sets the current working directory for the process. |
335 bool SetCurrentDirectory(const FilePath& path); | 340 bool SetCurrentDirectory(const FilePath& path); |
336 // Deprecated temporary compatibility function. | 341 // Deprecated temporary compatibility function. |
337 bool SetCurrentDirectory(const std::wstring& current_directory); | 342 bool SetCurrentDirectory(const std::wstring& current_directory); |
338 | 343 |
| 344 // A class to handle auto-closing of FILE*'s. |
| 345 class ScopedFILEClose { |
| 346 public: |
| 347 inline void operator()(FILE* x) const { |
| 348 if (x) { |
| 349 fclose(x); |
| 350 } |
| 351 } |
| 352 }; |
| 353 |
| 354 typedef scoped_ptr_malloc<FILE, ScopedFILEClose> ScopedFILE; |
| 355 |
339 // A class for enumerating the files in a provided path. The order of the | 356 // A class for enumerating the files in a provided path. The order of the |
340 // results is not guaranteed. | 357 // results is not guaranteed. |
341 // | 358 // |
342 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test | 359 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test |
343 // program where latency does not matter. This class is blocking. | 360 // program where latency does not matter. This class is blocking. |
344 class FileEnumerator { | 361 class FileEnumerator { |
345 public: | 362 public: |
346 #if defined(OS_WIN) | 363 #if defined(OS_WIN) |
347 typedef WIN32_FIND_DATA FindInfo; | 364 typedef WIN32_FIND_DATA FindInfo; |
348 #elif defined(OS_POSIX) | 365 #elif defined(OS_POSIX) |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 | 477 |
461 // Renames a file using the SHFileOperation API to ensure that the target file | 478 // Renames a file using the SHFileOperation API to ensure that the target file |
462 // gets the correct default security descriptor in the new path. | 479 // gets the correct default security descriptor in the new path. |
463 bool RenameFileAndResetSecurityDescriptor( | 480 bool RenameFileAndResetSecurityDescriptor( |
464 const FilePath& source_file_path, | 481 const FilePath& source_file_path, |
465 const FilePath& target_file_path); | 482 const FilePath& target_file_path); |
466 | 483 |
467 } // namespace file_util | 484 } // namespace file_util |
468 | 485 |
469 #endif // BASE_FILE_UTIL_H_ | 486 #endif // BASE_FILE_UTIL_H_ |
OLD | NEW |