| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 10 #pragma once |
| 11 | 11 |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 | 13 |
| 14 #if defined(OS_WIN) | 14 #if defined(OS_WIN) |
| 15 #include <windows.h> | 15 #include <windows.h> |
| 16 #if defined(UNIT_TEST) | 16 #if defined(UNIT_TEST) |
| 17 #include <aclapi.h> | 17 #include <aclapi.h> |
| 18 #endif | 18 #endif |
| 19 #elif defined(OS_POSIX) | 19 #elif defined(OS_POSIX) |
| 20 #include <sys/stat.h> | 20 #include <sys/stat.h> |
| 21 #include <unistd.h> | 21 #include <unistd.h> |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 #include <stdio.h> | 24 #include <stdio.h> |
| 25 | 25 |
| 26 #include <stack> | 26 #include <stack> |
| 27 #include <string> | 27 #include <string> |
| 28 #include <vector> | 28 #include <vector> |
| 29 | 29 |
| 30 #include "base/base_api.h" | 30 #include "base/base_export.h" |
| 31 #include "base/basictypes.h" | 31 #include "base/basictypes.h" |
| 32 #include "base/file_path.h" | 32 #include "base/file_path.h" |
| 33 #include "base/memory/scoped_ptr.h" | 33 #include "base/memory/scoped_ptr.h" |
| 34 #include "base/platform_file.h" | 34 #include "base/platform_file.h" |
| 35 #include "base/string16.h" | 35 #include "base/string16.h" |
| 36 | 36 |
| 37 #if defined(OS_POSIX) | 37 #if defined(OS_POSIX) |
| 38 #include "base/eintr_wrapper.h" | 38 #include "base/eintr_wrapper.h" |
| 39 #include "base/file_descriptor_posix.h" | 39 #include "base/file_descriptor_posix.h" |
| 40 #include "base/logging.h" | 40 #include "base/logging.h" |
| 41 #endif | 41 #endif |
| 42 | 42 |
| 43 namespace base { | 43 namespace base { |
| 44 class Time; | 44 class Time; |
| 45 } | 45 } |
| 46 | 46 |
| 47 namespace file_util { | 47 namespace file_util { |
| 48 | 48 |
| 49 //----------------------------------------------------------------------------- | 49 //----------------------------------------------------------------------------- |
| 50 // Functions that operate purely on a path string w/o touching the filesystem: | 50 // Functions that operate purely on a path string w/o touching the filesystem: |
| 51 | 51 |
| 52 // Returns true if the given path ends with a path separator character. | 52 // Returns true if the given path ends with a path separator character. |
| 53 BASE_API bool EndsWithSeparator(const FilePath& path); | 53 BASE_EXPORT bool EndsWithSeparator(const FilePath& path); |
| 54 | 54 |
| 55 // Makes sure that |path| ends with a separator IFF path is a directory that | 55 // Makes sure that |path| ends with a separator IFF path is a directory that |
| 56 // exists. Returns true if |path| is an existing directory, false otherwise. | 56 // exists. Returns true if |path| is an existing directory, false otherwise. |
| 57 BASE_API bool EnsureEndsWithSeparator(FilePath* path); | 57 BASE_EXPORT bool EnsureEndsWithSeparator(FilePath* path); |
| 58 | 58 |
| 59 // Convert provided relative path into an absolute path. Returns false on | 59 // Convert provided relative path into an absolute path. Returns false on |
| 60 // error. On POSIX, this function fails if the path does not exist. | 60 // error. On POSIX, this function fails if the path does not exist. |
| 61 BASE_API bool AbsolutePath(FilePath* path); | 61 BASE_EXPORT bool AbsolutePath(FilePath* path); |
| 62 | 62 |
| 63 // Returns true if |parent| contains |child|. Both paths are converted to | 63 // Returns true if |parent| contains |child|. Both paths are converted to |
| 64 // absolute paths before doing the comparison. | 64 // absolute paths before doing the comparison. |
| 65 BASE_API bool ContainsPath(const FilePath& parent, const FilePath& child); | 65 BASE_EXPORT bool ContainsPath(const FilePath& parent, const FilePath& child); |
| 66 | 66 |
| 67 //----------------------------------------------------------------------------- | 67 //----------------------------------------------------------------------------- |
| 68 // Functions that involve filesystem access or modification: | 68 // Functions that involve filesystem access or modification: |
| 69 | 69 |
| 70 // Returns the number of files matching the current path that were | 70 // Returns the number of files matching the current path that were |
| 71 // created on or after the given |file_time|. Doesn't count ".." or ".". | 71 // created on or after the given |file_time|. Doesn't count ".." or ".". |
| 72 // | 72 // |
| 73 // Note for POSIX environments: a file created before |file_time| | 73 // Note for POSIX environments: a file created before |file_time| |
| 74 // can be mis-detected as a newer file due to low precision of | 74 // can be mis-detected as a newer file due to low precision of |
| 75 // timestmap of file creation time. If you need to avoid such | 75 // timestmap of file creation time. If you need to avoid such |
| 76 // mis-detection perfectly, you should wait one second before | 76 // mis-detection perfectly, you should wait one second before |
| 77 // obtaining |file_time|. | 77 // obtaining |file_time|. |
| 78 BASE_API int CountFilesCreatedAfter(const FilePath& path, | 78 BASE_EXPORT int CountFilesCreatedAfter(const FilePath& path, |
| 79 const base::Time& file_time); | 79 const base::Time& file_time); |
| 80 | 80 |
| 81 // Returns the total number of bytes used by all the files under |root_path|. | 81 // Returns the total number of bytes used by all the files under |root_path|. |
| 82 // If the path does not exist the function returns 0. | 82 // If the path does not exist the function returns 0. |
| 83 // | 83 // |
| 84 // This function is implemented using the FileEnumerator class so it is not | 84 // This function is implemented using the FileEnumerator class so it is not |
| 85 // particularly speedy in any platform. | 85 // particularly speedy in any platform. |
| 86 BASE_API int64 ComputeDirectorySize(const FilePath& root_path); | 86 BASE_EXPORT int64 ComputeDirectorySize(const FilePath& root_path); |
| 87 | 87 |
| 88 // Returns the total number of bytes used by all files matching the provided | 88 // Returns the total number of bytes used by all files matching the provided |
| 89 // |pattern|, on this |directory| (without recursion). If the path does not | 89 // |pattern|, on this |directory| (without recursion). If the path does not |
| 90 // exist the function returns 0. | 90 // exist the function returns 0. |
| 91 // | 91 // |
| 92 // This function is implemented using the FileEnumerator class so it is not | 92 // This function is implemented using the FileEnumerator class so it is not |
| 93 // particularly speedy in any platform. | 93 // particularly speedy in any platform. |
| 94 BASE_API int64 ComputeFilesSize(const FilePath& directory, | 94 BASE_EXPORT int64 ComputeFilesSize(const FilePath& directory, |
| 95 const FilePath::StringType& pattern); | 95 const FilePath::StringType& pattern); |
| 96 | 96 |
| 97 // Deletes the given path, whether it's a file or a directory. | 97 // Deletes the given path, whether it's a file or a directory. |
| 98 // If it's a directory, it's perfectly happy to delete all of the | 98 // If it's a directory, it's perfectly happy to delete all of the |
| 99 // directory's contents. Passing true to recursive deletes | 99 // directory's contents. Passing true to recursive deletes |
| 100 // subdirectories and their contents as well. | 100 // subdirectories and their contents as well. |
| 101 // Returns true if successful, false otherwise. | 101 // Returns true if successful, false otherwise. |
| 102 // | 102 // |
| 103 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT | 103 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT |
| 104 // TO "rm -rf", SO USE WITH CAUTION. | 104 // TO "rm -rf", SO USE WITH CAUTION. |
| 105 BASE_API bool Delete(const FilePath& path, bool recursive); | 105 BASE_EXPORT bool Delete(const FilePath& path, bool recursive); |
| 106 | 106 |
| 107 #if defined(OS_WIN) | 107 #if defined(OS_WIN) |
| 108 // Schedules to delete the given path, whether it's a file or a directory, until | 108 // Schedules to delete the given path, whether it's a file or a directory, until |
| 109 // the operating system is restarted. | 109 // the operating system is restarted. |
| 110 // Note: | 110 // Note: |
| 111 // 1) The file/directory to be deleted should exist in a temp folder. | 111 // 1) The file/directory to be deleted should exist in a temp folder. |
| 112 // 2) The directory to be deleted must be empty. | 112 // 2) The directory to be deleted must be empty. |
| 113 BASE_API bool DeleteAfterReboot(const FilePath& path); | 113 BASE_EXPORT bool DeleteAfterReboot(const FilePath& path); |
| 114 #endif | 114 #endif |
| 115 | 115 |
| 116 // Moves the given path, whether it's a file or a directory. | 116 // Moves the given path, whether it's a file or a directory. |
| 117 // If a simple rename is not possible, such as in the case where the paths are | 117 // If a simple rename is not possible, such as in the case where the paths are |
| 118 // on different volumes, this will attempt to copy and delete. Returns | 118 // on different volumes, this will attempt to copy and delete. Returns |
| 119 // true for success. | 119 // true for success. |
| 120 BASE_API bool Move(const FilePath& from_path, const FilePath& to_path); | 120 BASE_EXPORT bool Move(const FilePath& from_path, const FilePath& to_path); |
| 121 | 121 |
| 122 // Renames file |from_path| to |to_path|. Both paths must be on the same | 122 // Renames file |from_path| to |to_path|. Both paths must be on the same |
| 123 // volume, or the function will fail. Destination file will be created | 123 // volume, or the function will fail. Destination file will be created |
| 124 // if it doesn't exist. Prefer this function over Move when dealing with | 124 // if it doesn't exist. Prefer this function over Move when dealing with |
| 125 // temporary files. On Windows it preserves attributes of the target file. | 125 // temporary files. On Windows it preserves attributes of the target file. |
| 126 // Returns true on success. | 126 // Returns true on success. |
| 127 BASE_API bool ReplaceFile(const FilePath& from_path, const FilePath& to_path); | 127 BASE_EXPORT bool ReplaceFile(const FilePath& from_path, |
| 128 const FilePath& to_path); |
| 128 | 129 |
| 129 // Copies a single file. Use CopyDirectory to copy directories. | 130 // Copies a single file. Use CopyDirectory to copy directories. |
| 130 BASE_API bool CopyFile(const FilePath& from_path, const FilePath& to_path); | 131 BASE_EXPORT bool CopyFile(const FilePath& from_path, const FilePath& to_path); |
| 131 | 132 |
| 132 // Copies the given path, and optionally all subdirectories and their contents | 133 // Copies the given path, and optionally all subdirectories and their contents |
| 133 // as well. | 134 // as well. |
| 134 // If there are files existing under to_path, always overwrite. | 135 // If there are files existing under to_path, always overwrite. |
| 135 // Returns true if successful, false otherwise. | 136 // Returns true if successful, false otherwise. |
| 136 // Don't use wildcards on the names, it may stop working without notice. | 137 // Don't use wildcards on the names, it may stop working without notice. |
| 137 // | 138 // |
| 138 // If you only need to copy a file use CopyFile, it's faster. | 139 // If you only need to copy a file use CopyFile, it's faster. |
| 139 BASE_API bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, | 140 BASE_EXPORT bool CopyDirectory(const FilePath& from_path, |
| 140 bool recursive); | 141 const FilePath& to_path, |
| 142 bool recursive); |
| 141 | 143 |
| 142 // Returns true if the given path exists on the local filesystem, | 144 // Returns true if the given path exists on the local filesystem, |
| 143 // false otherwise. | 145 // false otherwise. |
| 144 BASE_API bool PathExists(const FilePath& path); | 146 BASE_EXPORT bool PathExists(const FilePath& path); |
| 145 | 147 |
| 146 // Returns true if the given path is writable by the user, false otherwise. | 148 // Returns true if the given path is writable by the user, false otherwise. |
| 147 BASE_API bool PathIsWritable(const FilePath& path); | 149 BASE_EXPORT bool PathIsWritable(const FilePath& path); |
| 148 | 150 |
| 149 // Returns true if the given path exists and is a directory, false otherwise. | 151 // Returns true if the given path exists and is a directory, false otherwise. |
| 150 BASE_API bool DirectoryExists(const FilePath& path); | 152 BASE_EXPORT bool DirectoryExists(const FilePath& path); |
| 151 | 153 |
| 152 #if defined(OS_WIN) | 154 #if defined(OS_WIN) |
| 153 // Gets the creation time of the given file (expressed in the local timezone), | 155 // Gets the creation time of the given file (expressed in the local timezone), |
| 154 // and returns it via the creation_time parameter. Returns true if successful, | 156 // and returns it via the creation_time parameter. Returns true if successful, |
| 155 // false otherwise. | 157 // false otherwise. |
| 156 BASE_API bool GetFileCreationLocalTime(const std::wstring& filename, | 158 BASE_EXPORT bool GetFileCreationLocalTime(const std::wstring& filename, |
| 157 LPSYSTEMTIME creation_time); | 159 LPSYSTEMTIME creation_time); |
| 158 | 160 |
| 159 // Same as above, but takes a previously-opened file handle instead of a name. | 161 // Same as above, but takes a previously-opened file handle instead of a name. |
| 160 BASE_API bool GetFileCreationLocalTimeFromHandle(HANDLE file_handle, | 162 BASE_EXPORT bool GetFileCreationLocalTimeFromHandle(HANDLE file_handle, |
| 161 LPSYSTEMTIME creation_time); | 163 LPSYSTEMTIME creation_time); |
| 162 #endif // defined(OS_WIN) | 164 #endif // defined(OS_WIN) |
| 163 | 165 |
| 164 // Returns true if the contents of the two files given are equal, false | 166 // Returns true if the contents of the two files given are equal, false |
| 165 // otherwise. If either file can't be read, returns false. | 167 // otherwise. If either file can't be read, returns false. |
| 166 BASE_API bool ContentsEqual(const FilePath& filename1, | 168 BASE_EXPORT bool ContentsEqual(const FilePath& filename1, |
| 167 const FilePath& filename2); | 169 const FilePath& filename2); |
| 168 | 170 |
| 169 // Returns true if the contents of the two text files given are equal, false | 171 // Returns true if the contents of the two text files given are equal, false |
| 170 // otherwise. This routine treats "\r\n" and "\n" as equivalent. | 172 // otherwise. This routine treats "\r\n" and "\n" as equivalent. |
| 171 BASE_API bool TextContentsEqual(const FilePath& filename1, | 173 BASE_EXPORT bool TextContentsEqual(const FilePath& filename1, |
| 172 const FilePath& filename2); | 174 const FilePath& filename2); |
| 173 | 175 |
| 174 // Read the file at |path| into |contents|, returning true on success. | 176 // Read the file at |path| into |contents|, returning true on success. |
| 175 // |contents| may be NULL, in which case this function is useful for its | 177 // |contents| may be NULL, in which case this function is useful for its |
| 176 // side effect of priming the disk cache. | 178 // side effect of priming the disk cache. |
| 177 // Useful for unit tests. | 179 // Useful for unit tests. |
| 178 BASE_API bool ReadFileToString(const FilePath& path, std::string* contents); | 180 BASE_EXPORT bool ReadFileToString(const FilePath& path, std::string* contents); |
| 179 | 181 |
| 180 #if defined(OS_POSIX) | 182 #if defined(OS_POSIX) |
| 181 // Read exactly |bytes| bytes from file descriptor |fd|, storing the result | 183 // Read exactly |bytes| bytes from file descriptor |fd|, storing the result |
| 182 // in |buffer|. This function is protected against EINTR and partial reads. | 184 // in |buffer|. This function is protected against EINTR and partial reads. |
| 183 // Returns true iff |bytes| bytes have been successfuly read from |fd|. | 185 // Returns true iff |bytes| bytes have been successfuly read from |fd|. |
| 184 BASE_API bool ReadFromFD(int fd, char* buffer, size_t bytes); | 186 BASE_EXPORT bool ReadFromFD(int fd, char* buffer, size_t bytes); |
| 185 | 187 |
| 186 // Creates a symbolic link at |symlink| pointing to |target|. Returns | 188 // Creates a symbolic link at |symlink| pointing to |target|. Returns |
| 187 // false on failure. | 189 // false on failure. |
| 188 BASE_API bool CreateSymbolicLink(const FilePath& target, | 190 BASE_EXPORT bool CreateSymbolicLink(const FilePath& target, |
| 189 const FilePath& symlink); | 191 const FilePath& symlink); |
| 190 | 192 |
| 191 // Reads the given |symlink| and returns where it points to in |target|. | 193 // Reads the given |symlink| and returns where it points to in |target|. |
| 192 // Returns false upon failure. | 194 // Returns false upon failure. |
| 193 BASE_API bool ReadSymbolicLink(const FilePath& symlink, FilePath* target); | 195 BASE_EXPORT bool ReadSymbolicLink(const FilePath& symlink, FilePath* target); |
| 194 #endif // defined(OS_POSIX) | 196 #endif // defined(OS_POSIX) |
| 195 | 197 |
| 196 #if defined(OS_WIN) | 198 #if defined(OS_WIN) |
| 197 // Resolve Windows shortcut (.LNK file) | 199 // Resolve Windows shortcut (.LNK file) |
| 198 // This methods tries to resolve a shortcut .LNK file. If the |path| is valid | 200 // This methods tries to resolve a shortcut .LNK file. If the |path| is valid |
| 199 // returns true and puts the target into the |path|, otherwise returns | 201 // returns true and puts the target into the |path|, otherwise returns |
| 200 // false leaving the path as it is. | 202 // false leaving the path as it is. |
| 201 BASE_API bool ResolveShortcut(FilePath* path); | 203 BASE_EXPORT bool ResolveShortcut(FilePath* path); |
| 202 | 204 |
| 203 // Create a Windows shortcut (.LNK file) | 205 // Create a Windows shortcut (.LNK file) |
| 204 // This method creates a shortcut link using the information given. Ensure | 206 // This method creates a shortcut link using the information given. Ensure |
| 205 // you have initialized COM before calling into this function. 'source' | 207 // you have initialized COM before calling into this function. 'source' |
| 206 // and 'destination' parameters are required, everything else can be NULL. | 208 // and 'destination' parameters are required, everything else can be NULL. |
| 207 // 'source' is the existing file, 'destination' is the new link file to be | 209 // 'source' is the existing file, 'destination' is the new link file to be |
| 208 // created; for best results pass the filename with the .lnk extension. | 210 // created; for best results pass the filename with the .lnk extension. |
| 209 // The 'icon' can specify a dll or exe in which case the icon index is the | 211 // The 'icon' can specify a dll or exe in which case the icon index is the |
| 210 // resource id. 'app_id' is the app model id for the shortcut on Win7. | 212 // resource id. 'app_id' is the app model id for the shortcut on Win7. |
| 211 // Note that if the shortcut exists it will overwrite it. | 213 // Note that if the shortcut exists it will overwrite it. |
| 212 BASE_API bool CreateShortcutLink(const wchar_t *source, | 214 BASE_EXPORT bool CreateShortcutLink(const wchar_t *source, |
| 213 const wchar_t *destination, | 215 const wchar_t *destination, |
| 214 const wchar_t *working_dir, | 216 const wchar_t *working_dir, |
| 215 const wchar_t *arguments, | 217 const wchar_t *arguments, |
| 216 const wchar_t *description, | 218 const wchar_t *description, |
| 217 const wchar_t *icon, | 219 const wchar_t *icon, |
| 218 int icon_index, | 220 int icon_index, |
| 219 const wchar_t* app_id); | 221 const wchar_t* app_id); |
| 220 | 222 |
| 221 // Update a Windows shortcut (.LNK file). This method assumes the shortcut | 223 // Update a Windows shortcut (.LNK file). This method assumes the shortcut |
| 222 // link already exists (otherwise false is returned). Ensure you have | 224 // link already exists (otherwise false is returned). Ensure you have |
| 223 // initialized COM before calling into this function. Only 'destination' | 225 // initialized COM before calling into this function. Only 'destination' |
| 224 // parameter is required, everything else can be NULL (but if everything else | 226 // parameter is required, everything else can be NULL (but if everything else |
| 225 // is NULL no changes are made to the shortcut). 'destination' is the link | 227 // is NULL no changes are made to the shortcut). 'destination' is the link |
| 226 // file to be updated. 'app_id' is the app model id for the shortcut on Win7. | 228 // file to be updated. 'app_id' is the app model id for the shortcut on Win7. |
| 227 // For best results pass the filename with the .lnk extension. | 229 // For best results pass the filename with the .lnk extension. |
| 228 BASE_API bool UpdateShortcutLink(const wchar_t *source, | 230 BASE_EXPORT bool UpdateShortcutLink(const wchar_t *source, |
| 229 const wchar_t *destination, | 231 const wchar_t *destination, |
| 230 const wchar_t *working_dir, | 232 const wchar_t *working_dir, |
| 231 const wchar_t *arguments, | 233 const wchar_t *arguments, |
| 232 const wchar_t *description, | 234 const wchar_t *description, |
| 233 const wchar_t *icon, | 235 const wchar_t *icon, |
| 234 int icon_index, | 236 int icon_index, |
| 235 const wchar_t* app_id); | 237 const wchar_t* app_id); |
| 236 | 238 |
| 237 // Pins a shortcut to the Windows 7 taskbar. The shortcut file must already | 239 // Pins a shortcut to the Windows 7 taskbar. The shortcut file must already |
| 238 // exist and be a shortcut that points to an executable. | 240 // exist and be a shortcut that points to an executable. |
| 239 BASE_API bool TaskbarPinShortcutLink(const wchar_t* shortcut); | 241 BASE_EXPORT bool TaskbarPinShortcutLink(const wchar_t* shortcut); |
| 240 | 242 |
| 241 // Unpins a shortcut from the Windows 7 taskbar. The shortcut must exist and | 243 // Unpins a shortcut from the Windows 7 taskbar. The shortcut must exist and |
| 242 // already be pinned to the taskbar. | 244 // already be pinned to the taskbar. |
| 243 BASE_API bool TaskbarUnpinShortcutLink(const wchar_t* shortcut); | 245 BASE_EXPORT bool TaskbarUnpinShortcutLink(const wchar_t* shortcut); |
| 244 | 246 |
| 245 // Copy from_path to to_path recursively and then delete from_path recursively. | 247 // Copy from_path to to_path recursively and then delete from_path recursively. |
| 246 // Returns true if all operations succeed. | 248 // Returns true if all operations succeed. |
| 247 // This function simulates Move(), but unlike Move() it works across volumes. | 249 // This function simulates Move(), but unlike Move() it works across volumes. |
| 248 // This fuction is not transactional. | 250 // This fuction is not transactional. |
| 249 BASE_API bool CopyAndDeleteDirectory(const FilePath& from_path, | 251 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, |
| 250 const FilePath& to_path); | 252 const FilePath& to_path); |
| 251 #endif // defined(OS_WIN) | 253 #endif // defined(OS_WIN) |
| 252 | 254 |
| 253 // Return true if the given directory is empty | 255 // Return true if the given directory is empty |
| 254 BASE_API bool IsDirectoryEmpty(const FilePath& dir_path); | 256 BASE_EXPORT bool IsDirectoryEmpty(const FilePath& dir_path); |
| 255 | 257 |
| 256 // Get the temporary directory provided by the system. | 258 // Get the temporary directory provided by the system. |
| 257 // WARNING: DON'T USE THIS. If you want to create a temporary file, use one of | 259 // WARNING: DON'T USE THIS. If you want to create a temporary file, use one of |
| 258 // the functions below. | 260 // the functions below. |
| 259 BASE_API bool GetTempDir(FilePath* path); | 261 BASE_EXPORT bool GetTempDir(FilePath* path); |
| 260 // Get a temporary directory for shared memory files. | 262 // Get a temporary directory for shared memory files. |
| 261 // Only useful on POSIX; redirects to GetTempDir() on Windows. | 263 // Only useful on POSIX; redirects to GetTempDir() on Windows. |
| 262 BASE_API bool GetShmemTempDir(FilePath* path); | 264 BASE_EXPORT bool GetShmemTempDir(FilePath* path); |
| 263 | 265 |
| 264 // Get the home directory. This is more complicated than just getenv("HOME") | 266 // Get the home directory. This is more complicated than just getenv("HOME") |
| 265 // as it knows to fall back on getpwent() etc. | 267 // as it knows to fall back on getpwent() etc. |
| 266 BASE_API FilePath GetHomeDir(); | 268 BASE_EXPORT FilePath GetHomeDir(); |
| 267 | 269 |
| 268 // Creates a temporary file. The full path is placed in |path|, and the | 270 // Creates a temporary file. The full path is placed in |path|, and the |
| 269 // function returns true if was successful in creating the file. The file will | 271 // function returns true if was successful in creating the file. The file will |
| 270 // be empty and all handles closed after this function returns. | 272 // be empty and all handles closed after this function returns. |
| 271 BASE_API bool CreateTemporaryFile(FilePath* path); | 273 BASE_EXPORT bool CreateTemporaryFile(FilePath* path); |
| 272 | 274 |
| 273 // Same as CreateTemporaryFile but the file is created in |dir|. | 275 // Same as CreateTemporaryFile but the file is created in |dir|. |
| 274 BASE_API bool CreateTemporaryFileInDir(const FilePath& dir, | 276 BASE_EXPORT bool CreateTemporaryFileInDir(const FilePath& dir, |
| 275 FilePath* temp_file); | 277 FilePath* temp_file); |
| 276 | 278 |
| 277 // Create and open a temporary file. File is opened for read/write. | 279 // Create and open a temporary file. File is opened for read/write. |
| 278 // The full path is placed in |path|. | 280 // The full path is placed in |path|. |
| 279 // Returns a handle to the opened file or NULL if an error occured. | 281 // Returns a handle to the opened file or NULL if an error occured. |
| 280 BASE_API FILE* CreateAndOpenTemporaryFile(FilePath* path); | 282 BASE_EXPORT FILE* CreateAndOpenTemporaryFile(FilePath* path); |
| 281 // Like above but for shmem files. Only useful for POSIX. | 283 // Like above but for shmem files. Only useful for POSIX. |
| 282 BASE_API FILE* CreateAndOpenTemporaryShmemFile(FilePath* path); | 284 BASE_EXPORT FILE* CreateAndOpenTemporaryShmemFile(FilePath* path); |
| 283 // Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|. | 285 // Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|. |
| 284 BASE_API FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, | 286 BASE_EXPORT FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, |
| 285 FilePath* path); | 287 FilePath* path); |
| 286 | 288 |
| 287 // Create a new directory. If prefix is provided, the new directory name is in | 289 // Create a new directory. If prefix is provided, the new directory name is in |
| 288 // the format of prefixyyyy. | 290 // the format of prefixyyyy. |
| 289 // NOTE: prefix is ignored in the POSIX implementation. | 291 // NOTE: prefix is ignored in the POSIX implementation. |
| 290 // If success, return true and output the full path of the directory created. | 292 // If success, return true and output the full path of the directory created. |
| 291 BASE_API bool CreateNewTempDirectory(const FilePath::StringType& prefix, | 293 BASE_EXPORT bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
| 292 FilePath* new_temp_path); | 294 FilePath* new_temp_path); |
| 293 | 295 |
| 294 // Create a directory within another directory. | 296 // Create a directory within another directory. |
| 295 // Extra characters will be appended to |prefix| to ensure that the | 297 // Extra characters will be appended to |prefix| to ensure that the |
| 296 // new directory does not have the same name as an existing directory. | 298 // new directory does not have the same name as an existing directory. |
| 297 BASE_API bool CreateTemporaryDirInDir(const FilePath& base_dir, | 299 BASE_EXPORT bool CreateTemporaryDirInDir(const FilePath& base_dir, |
| 298 const FilePath::StringType& prefix, | 300 const FilePath::StringType& prefix, |
| 299 FilePath* new_dir); | 301 FilePath* new_dir); |
| 300 | 302 |
| 301 // Creates a directory, as well as creating any parent directories, if they | 303 // Creates a directory, as well as creating any parent directories, if they |
| 302 // don't exist. Returns 'true' on successful creation, or if the directory | 304 // don't exist. Returns 'true' on successful creation, or if the directory |
| 303 // already exists. The directory is only readable by the current user. | 305 // already exists. The directory is only readable by the current user. |
| 304 BASE_API bool CreateDirectory(const FilePath& full_path); | 306 BASE_EXPORT bool CreateDirectory(const FilePath& full_path); |
| 305 | 307 |
| 306 // Returns the file size. Returns true on success. | 308 // Returns the file size. Returns true on success. |
| 307 BASE_API bool GetFileSize(const FilePath& file_path, int64* file_size); | 309 BASE_EXPORT bool GetFileSize(const FilePath& file_path, int64* file_size); |
| 308 | 310 |
| 309 // Returns true if the given path's base name is ".". | 311 // Returns true if the given path's base name is ".". |
| 310 BASE_API bool IsDot(const FilePath& path); | 312 BASE_EXPORT bool IsDot(const FilePath& path); |
| 311 | 313 |
| 312 // Returns true if the given path's base name is "..". | 314 // Returns true if the given path's base name is "..". |
| 313 BASE_API bool IsDotDot(const FilePath& path); | 315 BASE_EXPORT bool IsDotDot(const FilePath& path); |
| 314 | 316 |
| 315 // Sets |real_path| to |path| with symbolic links and junctions expanded. | 317 // Sets |real_path| to |path| with symbolic links and junctions expanded. |
| 316 // On windows, make sure the path starts with a lettered drive. | 318 // On windows, make sure the path starts with a lettered drive. |
| 317 // |path| must reference a file. Function will fail if |path| points to | 319 // |path| must reference a file. Function will fail if |path| points to |
| 318 // a directory or to a nonexistent path. On windows, this function will | 320 // a directory or to a nonexistent path. On windows, this function will |
| 319 // fail if |path| is a junction or symlink that points to an empty file, | 321 // fail if |path| is a junction or symlink that points to an empty file, |
| 320 // or if |real_path| would be longer than MAX_PATH characters. | 322 // or if |real_path| would be longer than MAX_PATH characters. |
| 321 BASE_API bool NormalizeFilePath(const FilePath& path, FilePath* real_path); | 323 BASE_EXPORT bool NormalizeFilePath(const FilePath& path, FilePath* real_path); |
| 322 | 324 |
| 323 #if defined(OS_WIN) | 325 #if defined(OS_WIN) |
| 324 // Given an existing file in |path|, it returns in |real_path| the path | 326 // Given an existing file in |path|, it returns in |real_path| the path |
| 325 // in the native NT format, of the form "\Device\HarddiskVolumeXX\..". | 327 // in the native NT format, of the form "\Device\HarddiskVolumeXX\..". |
| 326 // Returns false it it fails. Empty files cannot be resolved with this | 328 // Returns false it it fails. Empty files cannot be resolved with this |
| 327 // function. | 329 // function. |
| 328 BASE_API bool NormalizeToNativeFilePath(const FilePath& path, | 330 BASE_EXPORT bool NormalizeToNativeFilePath(const FilePath& path, |
| 329 FilePath* nt_path); | 331 FilePath* nt_path); |
| 330 #endif | 332 #endif |
| 331 | 333 |
| 332 // This function will return if the given file is a symlink or not. | 334 // This function will return if the given file is a symlink or not. |
| 333 BASE_API bool IsLink(const FilePath& file_path); | 335 BASE_EXPORT bool IsLink(const FilePath& file_path); |
| 334 | 336 |
| 335 // Returns information about the given file path. | 337 // Returns information about the given file path. |
| 336 BASE_API bool GetFileInfo(const FilePath& file_path, | 338 BASE_EXPORT bool GetFileInfo(const FilePath& file_path, |
| 337 base::PlatformFileInfo* info); | 339 base::PlatformFileInfo* info); |
| 338 | 340 |
| 339 // Sets the time of the last access and the time of the last modification. | 341 // Sets the time of the last access and the time of the last modification. |
| 340 BASE_API bool TouchFile(const FilePath& path, | 342 BASE_EXPORT bool TouchFile(const FilePath& path, |
| 341 const base::Time& last_accessed, | 343 const base::Time& last_accessed, |
| 342 const base::Time& last_modified); | 344 const base::Time& last_modified); |
| 343 | 345 |
| 344 // Set the time of the last modification. Useful for unit tests. | 346 // Set the time of the last modification. Useful for unit tests. |
| 345 BASE_API bool SetLastModifiedTime(const FilePath& path, | 347 BASE_EXPORT bool SetLastModifiedTime(const FilePath& path, |
| 346 const base::Time& last_modified); | 348 const base::Time& last_modified); |
| 347 | 349 |
| 348 #if defined(OS_POSIX) | 350 #if defined(OS_POSIX) |
| 349 // Store inode number of |path| in |inode|. Return true on success. | 351 // Store inode number of |path| in |inode|. Return true on success. |
| 350 BASE_API bool GetInode(const FilePath& path, ino_t* inode); | 352 BASE_EXPORT bool GetInode(const FilePath& path, ino_t* inode); |
| 351 #endif | 353 #endif |
| 352 | 354 |
| 353 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success. | 355 // Wrapper for fopen-like calls. Returns non-NULL FILE* on success. |
| 354 BASE_API FILE* OpenFile(const FilePath& filename, const char* mode); | 356 BASE_EXPORT FILE* OpenFile(const FilePath& filename, const char* mode); |
| 355 | 357 |
| 356 // Closes file opened by OpenFile. Returns true on success. | 358 // Closes file opened by OpenFile. Returns true on success. |
| 357 BASE_API bool CloseFile(FILE* file); | 359 BASE_EXPORT bool CloseFile(FILE* file); |
| 358 | 360 |
| 359 // Truncates an open file to end at the location of the current file pointer. | 361 // Truncates an open file to end at the location of the current file pointer. |
| 360 // This is a cross-platform analog to Windows' SetEndOfFile() function. | 362 // This is a cross-platform analog to Windows' SetEndOfFile() function. |
| 361 BASE_API bool TruncateFile(FILE* file); | 363 BASE_EXPORT bool TruncateFile(FILE* file); |
| 362 | 364 |
| 363 // Reads the given number of bytes from the file into the buffer. Returns | 365 // Reads the given number of bytes from the file into the buffer. Returns |
| 364 // the number of read bytes, or -1 on error. | 366 // the number of read bytes, or -1 on error. |
| 365 BASE_API int ReadFile(const FilePath& filename, char* data, int size); | 367 BASE_EXPORT int ReadFile(const FilePath& filename, char* data, int size); |
| 366 | 368 |
| 367 // Writes the given buffer into the file, overwriting any data that was | 369 // Writes the given buffer into the file, overwriting any data that was |
| 368 // previously there. Returns the number of bytes written, or -1 on error. | 370 // previously there. Returns the number of bytes written, or -1 on error. |
| 369 BASE_API int WriteFile(const FilePath& filename, const char* data, int size); | 371 BASE_EXPORT int WriteFile(const FilePath& filename, const char* data, int size); |
| 370 #if defined(OS_POSIX) | 372 #if defined(OS_POSIX) |
| 371 // Append the data to |fd|. Does not close |fd| when done. | 373 // Append the data to |fd|. Does not close |fd| when done. |
| 372 BASE_API int WriteFileDescriptor(const int fd, const char* data, int size); | 374 BASE_EXPORT int WriteFileDescriptor(const int fd, const char* data, int size); |
| 373 #endif | 375 #endif |
| 374 | 376 |
| 375 // Gets the current working directory for the process. | 377 // Gets the current working directory for the process. |
| 376 BASE_API bool GetCurrentDirectory(FilePath* path); | 378 BASE_EXPORT bool GetCurrentDirectory(FilePath* path); |
| 377 | 379 |
| 378 // Sets the current working directory for the process. | 380 // Sets the current working directory for the process. |
| 379 BASE_API bool SetCurrentDirectory(const FilePath& path); | 381 BASE_EXPORT bool SetCurrentDirectory(const FilePath& path); |
| 380 | 382 |
| 381 // A class to handle auto-closing of FILE*'s. | 383 // A class to handle auto-closing of FILE*'s. |
| 382 class ScopedFILEClose { | 384 class ScopedFILEClose { |
| 383 public: | 385 public: |
| 384 inline void operator()(FILE* x) const { | 386 inline void operator()(FILE* x) const { |
| 385 if (x) { | 387 if (x) { |
| 386 fclose(x); | 388 fclose(x); |
| 387 } | 389 } |
| 388 } | 390 } |
| 389 }; | 391 }; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 403 }; | 405 }; |
| 404 | 406 |
| 405 typedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD; | 407 typedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD; |
| 406 #endif // OS_POSIX | 408 #endif // OS_POSIX |
| 407 | 409 |
| 408 // A class for enumerating the files in a provided path. The order of the | 410 // A class for enumerating the files in a provided path. The order of the |
| 409 // results is not guaranteed. | 411 // results is not guaranteed. |
| 410 // | 412 // |
| 411 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test | 413 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test |
| 412 // program where latency does not matter. This class is blocking. | 414 // program where latency does not matter. This class is blocking. |
| 413 class BASE_API FileEnumerator { | 415 class BASE_EXPORT FileEnumerator { |
| 414 public: | 416 public: |
| 415 #if defined(OS_WIN) | 417 #if defined(OS_WIN) |
| 416 typedef WIN32_FIND_DATA FindInfo; | 418 typedef WIN32_FIND_DATA FindInfo; |
| 417 #elif defined(OS_POSIX) | 419 #elif defined(OS_POSIX) |
| 418 typedef struct { | 420 typedef struct { |
| 419 struct stat stat; | 421 struct stat stat; |
| 420 std::string filename; | 422 std::string filename; |
| 421 } FindInfo; | 423 } FindInfo; |
| 422 #endif | 424 #endif |
| 423 | 425 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 FILE_TYPE file_type_; | 506 FILE_TYPE file_type_; |
| 505 FilePath::StringType pattern_; // Empty when we want to find everything. | 507 FilePath::StringType pattern_; // Empty when we want to find everything. |
| 506 | 508 |
| 507 // A stack that keeps track of which subdirectories we still need to | 509 // A stack that keeps track of which subdirectories we still need to |
| 508 // enumerate in the breadth-first search. | 510 // enumerate in the breadth-first search. |
| 509 std::stack<FilePath> pending_paths_; | 511 std::stack<FilePath> pending_paths_; |
| 510 | 512 |
| 511 DISALLOW_COPY_AND_ASSIGN(FileEnumerator); | 513 DISALLOW_COPY_AND_ASSIGN(FileEnumerator); |
| 512 }; | 514 }; |
| 513 | 515 |
| 514 class BASE_API MemoryMappedFile { | 516 class BASE_EXPORT MemoryMappedFile { |
| 515 public: | 517 public: |
| 516 // The default constructor sets all members to invalid/null values. | 518 // The default constructor sets all members to invalid/null values. |
| 517 MemoryMappedFile(); | 519 MemoryMappedFile(); |
| 518 ~MemoryMappedFile(); | 520 ~MemoryMappedFile(); |
| 519 | 521 |
| 520 // Opens an existing file and maps it into memory. Access is restricted to | 522 // Opens an existing file and maps it into memory. Access is restricted to |
| 521 // read only. If this object already points to a valid memory mapped file | 523 // read only. If this object already points to a valid memory mapped file |
| 522 // then this method will fail and return false. If it cannot open the file, | 524 // then this method will fail and return false. If it cannot open the file, |
| 523 // the file does not exist, or the memory mapping fails, it will return false. | 525 // the file does not exist, or the memory mapping fails, it will return false. |
| 524 // Later we may want to allow the user to specify access. | 526 // Later we may want to allow the user to specify access. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 #endif | 561 #endif |
| 560 base::PlatformFile file_; | 562 base::PlatformFile file_; |
| 561 uint8* data_; | 563 uint8* data_; |
| 562 size_t length_; | 564 size_t length_; |
| 563 | 565 |
| 564 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); | 566 DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile); |
| 565 }; | 567 }; |
| 566 | 568 |
| 567 // Renames a file using the SHFileOperation API to ensure that the target file | 569 // Renames a file using the SHFileOperation API to ensure that the target file |
| 568 // gets the correct default security descriptor in the new path. | 570 // gets the correct default security descriptor in the new path. |
| 569 BASE_API bool RenameFileAndResetSecurityDescriptor( | 571 BASE_EXPORT bool RenameFileAndResetSecurityDescriptor( |
| 570 const FilePath& source_file_path, | 572 const FilePath& source_file_path, |
| 571 const FilePath& target_file_path); | 573 const FilePath& target_file_path); |
| 572 | 574 |
| 573 // Returns whether the file has been modified since a particular date. | 575 // Returns whether the file has been modified since a particular date. |
| 574 BASE_API bool HasFileBeenModifiedSince( | 576 BASE_EXPORT bool HasFileBeenModifiedSince( |
| 575 const FileEnumerator::FindInfo& find_info, | 577 const FileEnumerator::FindInfo& find_info, |
| 576 const base::Time& cutoff_time); | 578 const base::Time& cutoff_time); |
| 577 | 579 |
| 578 #ifdef UNIT_TEST | 580 #ifdef UNIT_TEST |
| 579 | 581 |
| 580 inline bool MakeFileUnreadable(const FilePath& path) { | 582 inline bool MakeFileUnreadable(const FilePath& path) { |
| 581 #if defined(OS_POSIX) | 583 #if defined(OS_POSIX) |
| 582 struct stat stat_buf; | 584 struct stat stat_buf; |
| 583 if (stat(path.value().c_str(), &stat_buf) != 0) | 585 if (stat(path.value().c_str(), &stat_buf) != 0) |
| 584 return false; | 586 return false; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 626 } | 628 } |
| 627 | 629 |
| 628 #endif // UNIT_TEST | 630 #endif // UNIT_TEST |
| 629 | 631 |
| 630 #if defined(OS_WIN) | 632 #if defined(OS_WIN) |
| 631 // Loads the file passed in as an image section and touches pages to avoid | 633 // Loads the file passed in as an image section and touches pages to avoid |
| 632 // subsequent hard page faults during LoadLibrary. The size to be pre read | 634 // subsequent hard page faults during LoadLibrary. The size to be pre read |
| 633 // is passed in. If it is 0 then the whole file is paged in. The step size | 635 // is passed in. If it is 0 then the whole file is paged in. The step size |
| 634 // which indicates the number of bytes to skip after every page touched is | 636 // which indicates the number of bytes to skip after every page touched is |
| 635 // also passed in. | 637 // also passed in. |
| 636 bool BASE_API PreReadImage(const wchar_t* file_path, size_t size_to_read, | 638 bool BASE_EXPORT PreReadImage(const wchar_t* file_path, size_t size_to_read, |
| 637 size_t step_size); | 639 size_t step_size); |
| 638 #endif // OS_WIN | 640 #endif // OS_WIN |
| 639 | 641 |
| 640 #if defined(OS_LINUX) | 642 #if defined(OS_LINUX) |
| 641 // Broad categories of file systems as returned by statfs() on Linux. | 643 // Broad categories of file systems as returned by statfs() on Linux. |
| 642 enum FileSystemType { | 644 enum FileSystemType { |
| 643 FILE_SYSTEM_UNKNOWN, // statfs failed. | 645 FILE_SYSTEM_UNKNOWN, // statfs failed. |
| 644 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS. | 646 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS. |
| 645 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2 | 647 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2 |
| 646 FILE_SYSTEM_NFS, | 648 FILE_SYSTEM_NFS, |
| 647 FILE_SYSTEM_SMB, | 649 FILE_SYSTEM_SMB, |
| 648 FILE_SYSTEM_CODA, | 650 FILE_SYSTEM_CODA, |
| 649 FILE_SYSTEM_MEMORY, // in-memory file system | 651 FILE_SYSTEM_MEMORY, // in-memory file system |
| 650 FILE_SYSTEM_CGROUP, // cgroup control. | 652 FILE_SYSTEM_CGROUP, // cgroup control. |
| 651 FILE_SYSTEM_OTHER, // any other value. | 653 FILE_SYSTEM_OTHER, // any other value. |
| 652 FILE_SYSTEM_TYPE_COUNT | 654 FILE_SYSTEM_TYPE_COUNT |
| 653 }; | 655 }; |
| 654 | 656 |
| 655 // Attempts determine the FileSystemType for |path|. | 657 // Attempts determine the FileSystemType for |path|. |
| 656 // Returns false if |path| doesn't exist. | 658 // Returns false if |path| doesn't exist. |
| 657 BASE_API bool GetFileSystemType(const FilePath& path, FileSystemType* type); | 659 BASE_EXPORT bool GetFileSystemType(const FilePath& path, FileSystemType* type); |
| 658 #endif | 660 #endif |
| 659 | 661 |
| 660 } // namespace file_util | 662 } // namespace file_util |
| 661 | 663 |
| 662 // Deprecated functions have been moved to this separate header file, | 664 // Deprecated functions have been moved to this separate header file, |
| 663 // which must be included last after all the above definitions. | 665 // which must be included last after all the above definitions. |
| 664 #include "base/file_util_deprecated.h" | 666 #include "base/file_util_deprecated.h" |
| 665 | 667 |
| 666 #endif // BASE_FILE_UTIL_H_ | 668 #endif // BASE_FILE_UTIL_H_ |
| OLD | NEW |