ipc/chromium/src/base/file_util.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial