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

Side by Side Diff: base/file_util.h

Issue 18332014: Move Copy* into 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 | « no previous file | base/file_util.cc » ('j') | chrome/browser/chromeos/drive/file_cache.cc » ('J')
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 // 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
(...skipping 24 matching lines...) Expand all
35 #include "base/logging.h" 35 #include "base/logging.h"
36 #include "base/posix/eintr_wrapper.h" 36 #include "base/posix/eintr_wrapper.h"
37 #endif 37 #endif
38 38
39 namespace base { 39 namespace base {
40 40
41 class Time; 41 class Time;
42 42
43 extern bool g_bug108724_debug; 43 extern bool g_bug108724_debug;
44 44
45 void DoSomething(const FilePath&);
46
45 //----------------------------------------------------------------------------- 47 //-----------------------------------------------------------------------------
46 // Functions that involve filesystem access or modification: 48 // Functions that involve filesystem access or modification:
47 49
48 // Returns an absolute version of a relative path. Returns an empty path on 50 // Returns an absolute version of a relative path. Returns an empty path on
49 // error. On POSIX, this function fails if the path does not exist. This 51 // error. On POSIX, this function fails if the path does not exist. This
50 // function can result in I/O so it can be slow. 52 // function can result in I/O so it can be slow.
51 BASE_EXPORT FilePath MakeAbsoluteFilePath(const FilePath& input); 53 BASE_EXPORT FilePath MakeAbsoluteFilePath(const FilePath& input);
52 54
53 // Returns the total number of bytes used by all the files under |root_path|. 55 // Returns the total number of bytes used by all the files under |root_path|.
54 // If the path does not exist the function returns 0. 56 // If the path does not exist the function returns 0.
(...skipping 25 matching lines...) Expand all
80 BASE_EXPORT bool DeleteAfterReboot(const FilePath& path); 82 BASE_EXPORT bool DeleteAfterReboot(const FilePath& path);
81 #endif 83 #endif
82 84
83 // Moves the given path, whether it's a file or a directory. 85 // Moves the given path, whether it's a file or a directory.
84 // If a simple rename is not possible, such as in the case where the paths are 86 // If a simple rename is not possible, such as in the case where the paths are
85 // on different volumes, this will attempt to copy and delete. Returns 87 // on different volumes, this will attempt to copy and delete. Returns
86 // true for success. 88 // true for success.
87 // This function fails if either path contains traversal components ('..'). 89 // This function fails if either path contains traversal components ('..').
88 BASE_EXPORT bool Move(const FilePath& from_path, const FilePath& to_path); 90 BASE_EXPORT bool Move(const FilePath& from_path, const FilePath& to_path);
89 91
90 // Same as Move but allows paths with traversal components.
91 // Use only with extreme care.
92 BASE_EXPORT bool MoveUnsafe(const FilePath& from_path,
93 const FilePath& to_path);
94
95 // Renames file |from_path| to |to_path|. Both paths must be on the same 92 // Renames file |from_path| to |to_path|. Both paths must be on the same
96 // volume, or the function will fail. Destination file will be created 93 // volume, or the function will fail. Destination file will be created
97 // if it doesn't exist. Prefer this function over Move when dealing with 94 // if it doesn't exist. Prefer this function over Move when dealing with
98 // temporary files. On Windows it preserves attributes of the target file. 95 // temporary files. On Windows it preserves attributes of the target file.
99 // Returns true on success, leaving *error unchanged. 96 // Returns true on success, leaving *error unchanged.
100 // Returns false on failure and sets *error appropriately, if it is non-NULL. 97 // Returns false on failure and sets *error appropriately, if it is non-NULL.
101 BASE_EXPORT bool ReplaceFile(const FilePath& from_path, 98 BASE_EXPORT bool ReplaceFile(const FilePath& from_path,
102 const FilePath& to_path, 99 const FilePath& to_path,
103 PlatformFileError* error); 100 PlatformFileError* error);
104 101
105 } // namespace base
106
107 // -----------------------------------------------------------------------------
108
109 namespace file_util {
110
111 // Copies a single file. Use CopyDirectory to copy directories. 102 // Copies a single file. Use CopyDirectory to copy directories.
112 // This function fails if either path contains traversal components ('..'). 103 // This function fails if either path contains traversal components ('..').
113 BASE_EXPORT bool CopyFile(const base::FilePath& from_path, 104 BASE_EXPORT bool CopyFile(const FilePath& from_path, const FilePath& to_path);
114 const base::FilePath& to_path);
115
116 // Same as CopyFile but allows paths with traversal components.
117 // Use only with extreme care.
118 BASE_EXPORT bool CopyFileUnsafe(const base::FilePath& from_path,
119 const base::FilePath& to_path);
120 105
121 // Copies the given path, and optionally all subdirectories and their contents 106 // Copies the given path, and optionally all subdirectories and their contents
122 // as well. 107 // as well.
123 // 108 //
124 // If there are files existing under to_path, always overwrite. Returns true 109 // If there are files existing under to_path, always overwrite. Returns true
125 // if successful, false otherwise. Wildcards on the names are not supported. 110 // if successful, false otherwise. Wildcards on the names are not supported.
126 // 111 //
127 // If you only need to copy a file use CopyFile, it's faster. 112 // If you only need to copy a file use CopyFile, it's faster.
128 BASE_EXPORT bool CopyDirectory(const base::FilePath& from_path, 113 BASE_EXPORT bool CopyDirectory(const FilePath& from_path,
129 const base::FilePath& to_path, 114 const FilePath& to_path,
130 bool recursive); 115 bool recursive);
131 116
117 } // namespace base
118
119 // -----------------------------------------------------------------------------
120
121 namespace file_util {
122
132 // Returns true if the given path exists on the local filesystem, 123 // Returns true if the given path exists on the local filesystem,
133 // false otherwise. 124 // false otherwise.
134 BASE_EXPORT bool PathExists(const base::FilePath& path); 125 BASE_EXPORT bool PathExists(const base::FilePath& path);
135 126
136 // Returns true if the given path is writable by the user, false otherwise. 127 // Returns true if the given path is writable by the user, false otherwise.
137 BASE_EXPORT bool PathIsWritable(const base::FilePath& path); 128 BASE_EXPORT bool PathIsWritable(const base::FilePath& path);
138 129
139 // Returns true if the given path exists and is a directory, false otherwise. 130 // Returns true if the given path exists and is a directory, false otherwise.
140 BASE_EXPORT bool DirectoryExists(const base::FilePath& path); 131 BASE_EXPORT bool DirectoryExists(const base::FilePath& path);
141 132
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 // bits in |mode|. If |path| is symbolic link, |mode| is the permission of 186 // bits in |mode|. If |path| is symbolic link, |mode| is the permission of
196 // a file which the symlink points to. 187 // a file which the symlink points to.
197 BASE_EXPORT bool GetPosixFilePermissions(const base::FilePath& path, 188 BASE_EXPORT bool GetPosixFilePermissions(const base::FilePath& path,
198 int* mode); 189 int* mode);
199 // Sets the permission of the given |path|. If |path| is symbolic link, sets 190 // Sets the permission of the given |path|. If |path| is symbolic link, sets
200 // the permission of a file which the symlink points to. 191 // the permission of a file which the symlink points to.
201 BASE_EXPORT bool SetPosixFilePermissions(const base::FilePath& path, 192 BASE_EXPORT bool SetPosixFilePermissions(const base::FilePath& path,
202 int mode); 193 int mode);
203 #endif // defined(OS_POSIX) 194 #endif // defined(OS_POSIX)
204 195
205 #if defined(OS_WIN)
206 // Copy from_path to to_path recursively and then delete from_path recursively.
207 // Returns true if all operations succeed.
208 // This function simulates Move(), but unlike Move() it works across volumes.
209 // This fuction is not transactional.
210 BASE_EXPORT bool CopyAndDeleteDirectory(const base::FilePath& from_path,
211 const base::FilePath& to_path);
212 #endif // defined(OS_WIN)
213
214 // Return true if the given directory is empty 196 // Return true if the given directory is empty
215 BASE_EXPORT bool IsDirectoryEmpty(const base::FilePath& dir_path); 197 BASE_EXPORT bool IsDirectoryEmpty(const base::FilePath& dir_path);
216 198
217 // Get the temporary directory provided by the system. 199 // Get the temporary directory provided by the system.
218 // WARNING: DON'T USE THIS. If you want to create a temporary file, use one of 200 // WARNING: DON'T USE THIS. If you want to create a temporary file, use one of
219 // the functions below. 201 // the functions below.
220 BASE_EXPORT bool GetTempDir(base::FilePath* path); 202 BASE_EXPORT bool GetTempDir(base::FilePath* path);
221 // Get a temporary directory for shared memory files. 203 // Get a temporary directory for shared memory files.
222 // Only useful on POSIX; redirects to GetTempDir() on Windows. 204 // Only useful on POSIX; redirects to GetTempDir() on Windows.
223 BASE_EXPORT bool GetShmemTempDir(base::FilePath* path, bool executable); 205 BASE_EXPORT bool GetShmemTempDir(base::FilePath* path, bool executable);
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 }; 429 };
448 430
449 // Attempts determine the FileSystemType for |path|. 431 // Attempts determine the FileSystemType for |path|.
450 // Returns false if |path| doesn't exist. 432 // Returns false if |path| doesn't exist.
451 BASE_EXPORT bool GetFileSystemType(const base::FilePath& path, 433 BASE_EXPORT bool GetFileSystemType(const base::FilePath& path,
452 FileSystemType* type); 434 FileSystemType* type);
453 #endif 435 #endif
454 436
455 } // namespace file_util 437 } // namespace file_util
456 438
439 // Internal --------------------------------------------------------------------
440
441 namespace base {
442 namespace internal {
443
444 // Same as Move but allows paths with traversal components.
445 // Use only with extreme care.
446 BASE_EXPORT bool MoveUnsafe(const FilePath& from_path,
447 const FilePath& to_path);
448
449 // Same as CopyFile but allows paths with traversal components.
450 // Use only with extreme care.
451 BASE_EXPORT bool CopyFileUnsafe(const FilePath& from_path,
452 const FilePath& to_path);
453
454 #if defined(OS_WIN)
455 // Copy from_path to to_path recursively and then delete from_path recursively.
456 // Returns true if all operations succeed.
457 // This function simulates Move(), but unlike Move() it works across volumes.
458 // This fuction is not transactional.
459 BASE_EXPORT bool CopyAndDeleteDirectory(const base::FilePath& from_path,
460 const base::FilePath& to_path);
461 #endif // defined(OS_WIN)
462
463 } // namespace internal
464 } // namespace base
465
457 #endif // BASE_FILE_UTIL_H_ 466 #endif // BASE_FILE_UTIL_H_
OLDNEW
« no previous file with comments | « no previous file | base/file_util.cc » ('j') | chrome/browser/chromeos/drive/file_cache.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698