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" | |
28 #include "base/file_path.h" | 27 #include "base/file_path.h" |
29 | 28 |
30 namespace file_util { | 29 namespace file_util { |
31 | 30 |
32 //----------------------------------------------------------------------------- | 31 //----------------------------------------------------------------------------- |
33 // Functions that operate purely on a path string w/o touching the filesystem: | 32 // Functions that operate purely on a path string w/o touching the filesystem: |
34 | 33 |
35 // Returns a vector of all of the components of the provided path. | 34 // Returns a vector of all of the components of the provided path. |
36 void PathComponents(const FilePath& path, | 35 void PathComponents(const FilePath& path, |
37 std::vector<FilePath::StringType>* components); | 36 std::vector<FilePath::StringType>* components); |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 | 311 |
313 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success. | 312 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success. |
314 FILE* OpenFile(const FilePath& filename, const char* mode); | 313 FILE* OpenFile(const FilePath& filename, const char* mode); |
315 // Deprecated temporary compatibility functions. | 314 // Deprecated temporary compatibility functions. |
316 FILE* OpenFile(const std::string& filename, const char* mode); | 315 FILE* OpenFile(const std::string& filename, const char* mode); |
317 FILE* OpenFile(const std::wstring& filename, const char* mode); | 316 FILE* OpenFile(const std::wstring& filename, const char* mode); |
318 | 317 |
319 // Closes file opened by OpenFile. Returns true on success. | 318 // Closes file opened by OpenFile. Returns true on success. |
320 bool CloseFile(FILE* file); | 319 bool CloseFile(FILE* file); |
321 | 320 |
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 | |
326 // Reads the given number of bytes from the file into the buffer. Returns | 321 // Reads the given number of bytes from the file into the buffer. Returns |
327 // the number of read bytes, or -1 on error. | 322 // the number of read bytes, or -1 on error. |
328 int ReadFile(const std::wstring& filename, char* data, int size); | 323 int ReadFile(const std::wstring& filename, char* data, int size); |
329 | 324 |
330 // Writes the given buffer into the file, overwriting any data that was | 325 // Writes the given buffer into the file, overwriting any data that was |
331 // previously there. Returns the number of bytes written, or -1 on error. | 326 // previously there. Returns the number of bytes written, or -1 on error. |
332 int WriteFile(const std::wstring& filename, const char* data, int size); | 327 int WriteFile(const std::wstring& filename, const char* data, int size); |
333 | 328 |
334 // Gets the current working directory for the process. | 329 // Gets the current working directory for the process. |
335 bool GetCurrentDirectory(FilePath* path); | 330 bool GetCurrentDirectory(FilePath* path); |
336 // Deprecated temporary compatibility function. | 331 // Deprecated temporary compatibility function. |
337 bool GetCurrentDirectory(std::wstring* path); | 332 bool GetCurrentDirectory(std::wstring* path); |
338 | 333 |
339 // Sets the current working directory for the process. | 334 // Sets the current working directory for the process. |
340 bool SetCurrentDirectory(const FilePath& path); | 335 bool SetCurrentDirectory(const FilePath& path); |
341 // Deprecated temporary compatibility function. | 336 // Deprecated temporary compatibility function. |
342 bool SetCurrentDirectory(const std::wstring& current_directory); | 337 bool SetCurrentDirectory(const std::wstring& current_directory); |
343 | 338 |
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 | |
356 // A class for enumerating the files in a provided path. The order of the | 339 // A class for enumerating the files in a provided path. The order of the |
357 // results is not guaranteed. | 340 // results is not guaranteed. |
358 // | 341 // |
359 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test | 342 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test |
360 // program where latency does not matter. This class is blocking. | 343 // program where latency does not matter. This class is blocking. |
361 class FileEnumerator { | 344 class FileEnumerator { |
362 public: | 345 public: |
363 #if defined(OS_WIN) | 346 #if defined(OS_WIN) |
364 typedef WIN32_FIND_DATA FindInfo; | 347 typedef WIN32_FIND_DATA FindInfo; |
365 #elif defined(OS_POSIX) | 348 #elif defined(OS_POSIX) |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 | 460 |
478 // Renames a file using the SHFileOperation API to ensure that the target file | 461 // Renames a file using the SHFileOperation API to ensure that the target file |
479 // gets the correct default security descriptor in the new path. | 462 // gets the correct default security descriptor in the new path. |
480 bool RenameFileAndResetSecurityDescriptor( | 463 bool RenameFileAndResetSecurityDescriptor( |
481 const FilePath& source_file_path, | 464 const FilePath& source_file_path, |
482 const FilePath& target_file_path); | 465 const FilePath& target_file_path); |
483 | 466 |
484 } // namespace file_util | 467 } // namespace file_util |
485 | 468 |
486 #endif // BASE_FILE_UTIL_H_ | 469 #endif // BASE_FILE_UTIL_H_ |
OLD | NEW |