1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/file_util.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,246 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +// This file contains utility functions for dealing with the local 1.9 +// filesystem. 1.10 + 1.11 +#ifndef BASE_FILE_UTIL_H_ 1.12 +#define BASE_FILE_UTIL_H_ 1.13 + 1.14 +#include "build/build_config.h" 1.15 + 1.16 +#if defined(OS_WIN) 1.17 +#include <windows.h> 1.18 +#elif defined(ANDROID) 1.19 +#include <sys/stat.h> 1.20 +#elif defined(OS_POSIX) 1.21 +#include <sys/types.h> 1.22 +#include <fts.h> 1.23 +#include <sys/stat.h> 1.24 +#endif 1.25 + 1.26 +#include <stdio.h> 1.27 + 1.28 +#include <stack> 1.29 +#include <string> 1.30 +#include <vector> 1.31 + 1.32 +#include "base/basictypes.h" 1.33 +#include "base/scoped_ptr.h" 1.34 +#include "base/file_path.h" 1.35 + 1.36 +namespace file_util { 1.37 + 1.38 +//----------------------------------------------------------------------------- 1.39 +// Functions that operate purely on a path string w/o touching the filesystem: 1.40 + 1.41 +// Returns true if the given path ends with a path separator character. 1.42 +bool EndsWithSeparator(const FilePath& path); 1.43 +// These two versions are both deprecated. TODO(estade): remove them. 1.44 +bool EndsWithSeparator(std::wstring* path); 1.45 +bool EndsWithSeparator(const std::wstring& path); 1.46 + 1.47 +// Modifies a string by trimming all trailing separators from the end. 1.48 +// Deprecated. FilePath does this automatically, and if it's constructed from a 1.49 +// path with a trailing separator, StripTrailingSeparators() may be used. 1.50 +void TrimTrailingSeparator(std::wstring* dir); 1.51 + 1.52 +// Strips the topmost directory from the end of 'dir'. Assumes 'dir' does not 1.53 +// refer to a file. 1.54 +// If 'dir' is a root directory, return without change. 1.55 +// Deprecated. Use FilePath::DirName instead. 1.56 +void UpOneDirectory(std::wstring* dir); 1.57 + 1.58 +// Returns the filename portion of 'path', without any leading \'s or /'s. 1.59 +// Deprecated. Use FilePath::BaseName instead. 1.60 +std::wstring GetFilenameFromPath(const std::wstring& path); 1.61 + 1.62 +// Deprecated compatibility function. Use FilePath::Extension. 1.63 +FilePath::StringType GetFileExtensionFromPath(const FilePath& path); 1.64 +// Deprecated temporary compatibility function. 1.65 +std::wstring GetFileExtensionFromPath(const std::wstring& path); 1.66 + 1.67 +// Appends new_ending to path, adding a separator between the two if necessary. 1.68 +void AppendToPath(std::wstring* path, const std::wstring& new_ending); 1.69 + 1.70 +// Convert provided relative path into an absolute path. Returns false on 1.71 +// error. On POSIX, this function fails if the path does not exist. 1.72 +bool AbsolutePath(FilePath* path); 1.73 +// Deprecated temporary compatibility function. 1.74 +bool AbsolutePath(std::wstring* path); 1.75 + 1.76 +// Deprecated compatibility function. Use FilePath::InsertBeforeExtension. 1.77 +void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix); 1.78 + 1.79 +// Deprecated compatibility function. Use FilePath::ReplaceExtension. 1.80 +void ReplaceExtension(FilePath* file_name, 1.81 + const FilePath::StringType& extension); 1.82 + 1.83 +#if defined(OS_WIN) 1.84 +// Deprecated temporary compatibility functions. 1.85 +void InsertBeforeExtension(std::wstring* path, const std::wstring& suffix); 1.86 +void ReplaceExtension(std::wstring* file_name, const std::wstring& extension); 1.87 +#endif 1.88 + 1.89 +//----------------------------------------------------------------------------- 1.90 +// Functions that involve filesystem access or modification: 1.91 + 1.92 +// Deletes the given path, whether it's a file or a directory. 1.93 +// If it's a directory, it's perfectly happy to delete all of the 1.94 +// directory's contents. Passing true to recursive deletes 1.95 +// subdirectories and their contents as well. 1.96 +// Returns true if successful, false otherwise. 1.97 +// 1.98 +// WARNING: USING THIS WITH recursive==true IS EQUIVALENT 1.99 +// TO "rm -rf", SO USE WITH CAUTION. 1.100 +bool Delete(const FilePath& path, bool recursive); 1.101 +// Deprecated temporary compatibility function. 1.102 +bool Delete(const std::wstring& path, bool recursive); 1.103 + 1.104 +// Copies a single file. Use CopyDirectory to copy directories. 1.105 +bool CopyFile(const FilePath& from_path, const FilePath& to_path); 1.106 +// Deprecated temporary compatibility function. 1.107 +bool CopyFile(const std::wstring& from_path, const std::wstring& to_path); 1.108 + 1.109 +// Copies the given path, and optionally all subdirectories and their contents 1.110 +// as well. 1.111 +// If there are files existing under to_path, always overwrite. 1.112 +// Returns true if successful, false otherwise. 1.113 +// Dont't use wildcards on the names, it may stop working without notice. 1.114 +// 1.115 +// If you only need to copy a file use CopyFile, it's faster. 1.116 +bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, 1.117 + bool recursive); 1.118 +// Deprecated temporary compatibility function. 1.119 +bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path, 1.120 + bool recursive); 1.121 + 1.122 +// Returns true if the given path exists on the local filesystem, 1.123 +// false otherwise. 1.124 +bool PathExists(const FilePath& path); 1.125 +// Deprecated temporary compatibility function. 1.126 +bool PathExists(const std::wstring& path); 1.127 + 1.128 +// Returns true if the given path is writable by the user, false otherwise. 1.129 +bool PathIsWritable(const FilePath& path); 1.130 +// Deprecated temporary compatibility function. 1.131 +bool PathIsWritable(const std::wstring& path); 1.132 + 1.133 +// Returns true if the given path exists and is a directory, false otherwise. 1.134 +bool DirectoryExists(const FilePath& path); 1.135 +// Deprecated temporary compatibility function. 1.136 +bool DirectoryExists(const std::wstring& path); 1.137 + 1.138 +#if defined(OS_POSIX) 1.139 +// Read exactly |bytes| bytes from file descriptor |fd|, storing the result 1.140 +// in |buffer|. This function is protected against EINTR and partial reads. 1.141 +// Returns true iff |bytes| bytes have been successfuly read from |fd|. 1.142 +bool ReadFromFD(int fd, char* buffer, size_t bytes); 1.143 +#endif // defined(OS_POSIX) 1.144 + 1.145 +// Get the temporary directory provided by the system. 1.146 +bool GetTempDir(FilePath* path); 1.147 +// Deprecated temporary compatibility function. 1.148 +bool GetTempDir(std::wstring* path); 1.149 +// Get a temporary directory for shared memory files. 1.150 +// Only useful on POSIX; redirects to GetTempDir() on Windows. 1.151 +bool GetShmemTempDir(FilePath* path); 1.152 + 1.153 +// Creates a temporary file. The full path is placed in |path|, and the 1.154 +// function returns true if was successful in creating the file. The file will 1.155 +// be empty and all handles closed after this function returns. 1.156 +// TODO(erikkay): rename this function and track down all of the callers. 1.157 +// (Clarification of erik's comment: the intent is to rename the BlahFileName() 1.158 +// calls into BlahFile(), since they create temp files (not temp filenames).) 1.159 +bool CreateTemporaryFileName(FilePath* path); 1.160 +// Deprecated temporary compatibility function. 1.161 +bool CreateTemporaryFileName(std::wstring* temp_file); 1.162 + 1.163 +// Create and open a temporary file. File is opened for read/write. 1.164 +// The full path is placed in |path|, and the function returns true if 1.165 +// was successful in creating and opening the file. 1.166 +FILE* CreateAndOpenTemporaryFile(FilePath* path); 1.167 +// Like above but for shmem files. Only useful for POSIX. 1.168 +FILE* CreateAndOpenTemporaryShmemFile(FilePath* path); 1.169 + 1.170 +// Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|. 1.171 +FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path); 1.172 + 1.173 +// Same as CreateTemporaryFileName but the file is created in |dir|. 1.174 +bool CreateTemporaryFileNameInDir(const std::wstring& dir, 1.175 + std::wstring* temp_file); 1.176 + 1.177 +// Create a new directory under TempPath. If prefix is provided, the new 1.178 +// directory name is in the format of prefixyyyy. 1.179 +// NOTE: prefix is ignored in the POSIX implementation. 1.180 +// TODO(erikkay): is this OK? 1.181 +// If success, return true and output the full path of the directory created. 1.182 +bool CreateNewTempDirectory(const FilePath::StringType& prefix, 1.183 + FilePath* new_temp_path); 1.184 +// Deprecated temporary compatibility function. 1.185 +bool CreateNewTempDirectory(const std::wstring& prefix, 1.186 + std::wstring* new_temp_path); 1.187 + 1.188 +// Creates a directory, as well as creating any parent directories, if they 1.189 +// don't exist. Returns 'true' on successful creation, or if the directory 1.190 +// already exists. 1.191 +bool CreateDirectory(const FilePath& full_path); 1.192 +// Deprecated temporary compatibility function. 1.193 +bool CreateDirectory(const std::wstring& full_path); 1.194 + 1.195 +// Returns the file size. Returns true on success. 1.196 +bool GetFileSize(const FilePath& file_path, int64_t* file_size); 1.197 +// Deprecated temporary compatibility function. 1.198 +bool GetFileSize(const std::wstring& file_path, int64_t* file_size); 1.199 + 1.200 +// Used to hold information about a given file path. See GetFileInfo below. 1.201 +struct FileInfo { 1.202 + // The size of the file in bytes. Undefined when is_directory is true. 1.203 + int64_t size; 1.204 + 1.205 + // True if the file corresponds to a directory. 1.206 + bool is_directory; 1.207 + 1.208 + // Add additional fields here as needed. 1.209 +}; 1.210 + 1.211 +// Returns information about the given file path. 1.212 +bool GetFileInfo(const FilePath& file_path, FileInfo* info); 1.213 +// Deprecated temporary compatibility function. 1.214 +bool GetFileInfo(const std::wstring& file_path, FileInfo* info); 1.215 + 1.216 +// Wrapper for fopen-like calls. Returns non-NULL FILE* on success. 1.217 +FILE* OpenFile(const FilePath& filename, const char* mode); 1.218 +// Deprecated temporary compatibility functions. 1.219 +FILE* OpenFile(const std::string& filename, const char* mode); 1.220 +FILE* OpenFile(const std::wstring& filename, const char* mode); 1.221 + 1.222 +// Closes file opened by OpenFile. Returns true on success. 1.223 +bool CloseFile(FILE* file); 1.224 + 1.225 +// Reads the given number of bytes from the file into the buffer. Returns 1.226 +// the number of read bytes, or -1 on error. 1.227 +int ReadFile(const FilePath& filename, char* data, int size); 1.228 +// Deprecated temporary compatibility function. 1.229 +int ReadFile(const std::wstring& filename, char* data, int size); 1.230 + 1.231 +// Writes the given buffer into the file, overwriting any data that was 1.232 +// previously there. Returns the number of bytes written, or -1 on error. 1.233 +int WriteFile(const FilePath& filename, const char* data, int size); 1.234 +// Deprecated temporary compatibility function. 1.235 +int WriteFile(const std::wstring& filename, const char* data, int size); 1.236 + 1.237 +// Gets the current working directory for the process. 1.238 +bool GetCurrentDirectory(FilePath* path); 1.239 +// Deprecated temporary compatibility function. 1.240 +bool GetCurrentDirectory(std::wstring* path); 1.241 + 1.242 +// Sets the current working directory for the process. 1.243 +bool SetCurrentDirectory(const FilePath& path); 1.244 +// Deprecated temporary compatibility function. 1.245 +bool SetCurrentDirectory(const std::wstring& current_directory); 1.246 + 1.247 +} // namespace file_util 1.248 + 1.249 +#endif // BASE_FILE_UTIL_H_