Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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
3 // found in the LICENSE file.
5 // This file contains utility functions for dealing with the local
6 // filesystem.
8 #ifndef BASE_FILE_UTIL_H_
9 #define BASE_FILE_UTIL_H_
11 #include "build/build_config.h"
13 #if defined(OS_WIN)
14 #include <windows.h>
15 #elif defined(ANDROID)
16 #include <sys/stat.h>
17 #elif defined(OS_POSIX)
18 #include <sys/types.h>
19 #include <fts.h>
20 #include <sys/stat.h>
21 #endif
23 #include <stdio.h>
25 #include <stack>
26 #include <string>
27 #include <vector>
29 #include "base/basictypes.h"
30 #include "base/scoped_ptr.h"
31 #include "base/file_path.h"
33 namespace file_util {
35 //-----------------------------------------------------------------------------
36 // Functions that operate purely on a path string w/o touching the filesystem:
38 // Returns true if the given path ends with a path separator character.
39 bool EndsWithSeparator(const FilePath& path);
40 // These two versions are both deprecated. TODO(estade): remove them.
41 bool EndsWithSeparator(std::wstring* path);
42 bool EndsWithSeparator(const std::wstring& path);
44 // Modifies a string by trimming all trailing separators from the end.
45 // Deprecated. FilePath does this automatically, and if it's constructed from a
46 // path with a trailing separator, StripTrailingSeparators() may be used.
47 void TrimTrailingSeparator(std::wstring* dir);
49 // Strips the topmost directory from the end of 'dir'. Assumes 'dir' does not
50 // refer to a file.
51 // If 'dir' is a root directory, return without change.
52 // Deprecated. Use FilePath::DirName instead.
53 void UpOneDirectory(std::wstring* dir);
55 // Returns the filename portion of 'path', without any leading \'s or /'s.
56 // Deprecated. Use FilePath::BaseName instead.
57 std::wstring GetFilenameFromPath(const std::wstring& path);
59 // Deprecated compatibility function. Use FilePath::Extension.
60 FilePath::StringType GetFileExtensionFromPath(const FilePath& path);
61 // Deprecated temporary compatibility function.
62 std::wstring GetFileExtensionFromPath(const std::wstring& path);
64 // Appends new_ending to path, adding a separator between the two if necessary.
65 void AppendToPath(std::wstring* path, const std::wstring& new_ending);
67 // Convert provided relative path into an absolute path. Returns false on
68 // error. On POSIX, this function fails if the path does not exist.
69 bool AbsolutePath(FilePath* path);
70 // Deprecated temporary compatibility function.
71 bool AbsolutePath(std::wstring* path);
73 // Deprecated compatibility function. Use FilePath::InsertBeforeExtension.
74 void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix);
76 // Deprecated compatibility function. Use FilePath::ReplaceExtension.
77 void ReplaceExtension(FilePath* file_name,
78 const FilePath::StringType& extension);
80 #if defined(OS_WIN)
81 // Deprecated temporary compatibility functions.
82 void InsertBeforeExtension(std::wstring* path, const std::wstring& suffix);
83 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension);
84 #endif
86 //-----------------------------------------------------------------------------
87 // Functions that involve filesystem access or modification:
89 // Deletes the given path, whether it's a file or a directory.
90 // If it's a directory, it's perfectly happy to delete all of the
91 // directory's contents. Passing true to recursive deletes
92 // subdirectories and their contents as well.
93 // Returns true if successful, false otherwise.
94 //
95 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT
96 // TO "rm -rf", SO USE WITH CAUTION.
97 bool Delete(const FilePath& path, bool recursive);
98 // Deprecated temporary compatibility function.
99 bool Delete(const std::wstring& path, bool recursive);
101 // Copies a single file. Use CopyDirectory to copy directories.
102 bool CopyFile(const FilePath& from_path, const FilePath& to_path);
103 // Deprecated temporary compatibility function.
104 bool CopyFile(const std::wstring& from_path, const std::wstring& to_path);
106 // Copies the given path, and optionally all subdirectories and their contents
107 // as well.
108 // If there are files existing under to_path, always overwrite.
109 // Returns true if successful, false otherwise.
110 // Dont't use wildcards on the names, it may stop working without notice.
111 //
112 // If you only need to copy a file use CopyFile, it's faster.
113 bool CopyDirectory(const FilePath& from_path, const FilePath& to_path,
114 bool recursive);
115 // Deprecated temporary compatibility function.
116 bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path,
117 bool recursive);
119 // Returns true if the given path exists on the local filesystem,
120 // false otherwise.
121 bool PathExists(const FilePath& path);
122 // Deprecated temporary compatibility function.
123 bool PathExists(const std::wstring& path);
125 // Returns true if the given path is writable by the user, false otherwise.
126 bool PathIsWritable(const FilePath& path);
127 // Deprecated temporary compatibility function.
128 bool PathIsWritable(const std::wstring& path);
130 // Returns true if the given path exists and is a directory, false otherwise.
131 bool DirectoryExists(const FilePath& path);
132 // Deprecated temporary compatibility function.
133 bool DirectoryExists(const std::wstring& path);
135 #if defined(OS_POSIX)
136 // Read exactly |bytes| bytes from file descriptor |fd|, storing the result
137 // in |buffer|. This function is protected against EINTR and partial reads.
138 // Returns true iff |bytes| bytes have been successfuly read from |fd|.
139 bool ReadFromFD(int fd, char* buffer, size_t bytes);
140 #endif // defined(OS_POSIX)
142 // Get the temporary directory provided by the system.
143 bool GetTempDir(FilePath* path);
144 // Deprecated temporary compatibility function.
145 bool GetTempDir(std::wstring* path);
146 // Get a temporary directory for shared memory files.
147 // Only useful on POSIX; redirects to GetTempDir() on Windows.
148 bool GetShmemTempDir(FilePath* path);
150 // Creates a temporary file. The full path is placed in |path|, and the
151 // function returns true if was successful in creating the file. The file will
152 // be empty and all handles closed after this function returns.
153 // TODO(erikkay): rename this function and track down all of the callers.
154 // (Clarification of erik's comment: the intent is to rename the BlahFileName()
155 // calls into BlahFile(), since they create temp files (not temp filenames).)
156 bool CreateTemporaryFileName(FilePath* path);
157 // Deprecated temporary compatibility function.
158 bool CreateTemporaryFileName(std::wstring* temp_file);
160 // Create and open a temporary file. File is opened for read/write.
161 // The full path is placed in |path|, and the function returns true if
162 // was successful in creating and opening the file.
163 FILE* CreateAndOpenTemporaryFile(FilePath* path);
164 // Like above but for shmem files. Only useful for POSIX.
165 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path);
167 // Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|.
168 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path);
170 // Same as CreateTemporaryFileName but the file is created in |dir|.
171 bool CreateTemporaryFileNameInDir(const std::wstring& dir,
172 std::wstring* temp_file);
174 // Create a new directory under TempPath. If prefix is provided, the new
175 // directory name is in the format of prefixyyyy.
176 // NOTE: prefix is ignored in the POSIX implementation.
177 // TODO(erikkay): is this OK?
178 // If success, return true and output the full path of the directory created.
179 bool CreateNewTempDirectory(const FilePath::StringType& prefix,
180 FilePath* new_temp_path);
181 // Deprecated temporary compatibility function.
182 bool CreateNewTempDirectory(const std::wstring& prefix,
183 std::wstring* new_temp_path);
185 // Creates a directory, as well as creating any parent directories, if they
186 // don't exist. Returns 'true' on successful creation, or if the directory
187 // already exists.
188 bool CreateDirectory(const FilePath& full_path);
189 // Deprecated temporary compatibility function.
190 bool CreateDirectory(const std::wstring& full_path);
192 // Returns the file size. Returns true on success.
193 bool GetFileSize(const FilePath& file_path, int64_t* file_size);
194 // Deprecated temporary compatibility function.
195 bool GetFileSize(const std::wstring& file_path, int64_t* file_size);
197 // Used to hold information about a given file path. See GetFileInfo below.
198 struct FileInfo {
199 // The size of the file in bytes. Undefined when is_directory is true.
200 int64_t size;
202 // True if the file corresponds to a directory.
203 bool is_directory;
205 // Add additional fields here as needed.
206 };
208 // Returns information about the given file path.
209 bool GetFileInfo(const FilePath& file_path, FileInfo* info);
210 // Deprecated temporary compatibility function.
211 bool GetFileInfo(const std::wstring& file_path, FileInfo* info);
213 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success.
214 FILE* OpenFile(const FilePath& filename, const char* mode);
215 // Deprecated temporary compatibility functions.
216 FILE* OpenFile(const std::string& filename, const char* mode);
217 FILE* OpenFile(const std::wstring& filename, const char* mode);
219 // Closes file opened by OpenFile. Returns true on success.
220 bool CloseFile(FILE* file);
222 // Reads the given number of bytes from the file into the buffer. Returns
223 // the number of read bytes, or -1 on error.
224 int ReadFile(const FilePath& filename, char* data, int size);
225 // Deprecated temporary compatibility function.
226 int ReadFile(const std::wstring& filename, char* data, int size);
228 // Writes the given buffer into the file, overwriting any data that was
229 // previously there. Returns the number of bytes written, or -1 on error.
230 int WriteFile(const FilePath& filename, const char* data, int size);
231 // Deprecated temporary compatibility function.
232 int WriteFile(const std::wstring& filename, const char* data, int size);
234 // Gets the current working directory for the process.
235 bool GetCurrentDirectory(FilePath* path);
236 // Deprecated temporary compatibility function.
237 bool GetCurrentDirectory(std::wstring* path);
239 // Sets the current working directory for the process.
240 bool SetCurrentDirectory(const FilePath& path);
241 // Deprecated temporary compatibility function.
242 bool SetCurrentDirectory(const std::wstring& current_directory);
244 } // namespace file_util
246 #endif // BASE_FILE_UTIL_H_