| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "webkit/fileapi/native_file_util.h" | |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "webkit/fileapi/file_system_operation_context.h" | |
| 10 | |
| 11 namespace fileapi { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // Sets permissions on directory at |dir_path| based on the target platform. | |
| 16 // Returns true on success, or false otherwise. | |
| 17 // | |
| 18 // TODO(benchan): Find a better place outside webkit to host this function. | |
| 19 bool SetPlatformSpecificDirectoryPermissions(const base::FilePath& dir_path) { | |
| 20 #if defined(OS_CHROMEOS) | |
| 21 // System daemons on Chrome OS may run as a user different than the Chrome | |
| 22 // process but need to access files under the directories created here. | |
| 23 // Because of that, grant the execute permission on the created directory | |
| 24 // to group and other users. | |
| 25 if (HANDLE_EINTR(chmod(dir_path.value().c_str(), | |
| 26 S_IRWXU | S_IXGRP | S_IXOTH)) != 0) { | |
| 27 return false; | |
| 28 } | |
| 29 #endif | |
| 30 // Keep the directory permissions unchanged on non-Chrome OS platforms. | |
| 31 return true; | |
| 32 } | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 using base::PlatformFile; | |
| 37 using base::PlatformFileError; | |
| 38 | |
| 39 class NativeFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator { | |
| 40 public: | |
| 41 NativeFileEnumerator(const base::FilePath& root_path, | |
| 42 bool recursive, | |
| 43 int file_type) | |
| 44 : file_enum_(root_path, recursive, file_type) { | |
| 45 #if defined(OS_WIN) | |
| 46 memset(&file_util_info_, 0, sizeof(file_util_info_)); | |
| 47 #endif // defined(OS_WIN) | |
| 48 } | |
| 49 | |
| 50 virtual ~NativeFileEnumerator() {} | |
| 51 | |
| 52 virtual base::FilePath Next() OVERRIDE; | |
| 53 virtual int64 Size() OVERRIDE; | |
| 54 virtual base::Time LastModifiedTime() OVERRIDE; | |
| 55 virtual bool IsDirectory() OVERRIDE; | |
| 56 | |
| 57 private: | |
| 58 file_util::FileEnumerator file_enum_; | |
| 59 file_util::FileEnumerator::FindInfo file_util_info_; | |
| 60 }; | |
| 61 | |
| 62 base::FilePath NativeFileEnumerator::Next() { | |
| 63 base::FilePath rv = file_enum_.Next(); | |
| 64 if (!rv.empty()) | |
| 65 file_enum_.GetFindInfo(&file_util_info_); | |
| 66 return rv; | |
| 67 } | |
| 68 | |
| 69 int64 NativeFileEnumerator::Size() { | |
| 70 return file_util::FileEnumerator::GetFilesize(file_util_info_); | |
| 71 } | |
| 72 | |
| 73 base::Time NativeFileEnumerator::LastModifiedTime() { | |
| 74 return file_util::FileEnumerator::GetLastModifiedTime(file_util_info_); | |
| 75 } | |
| 76 | |
| 77 bool NativeFileEnumerator::IsDirectory() { | |
| 78 return file_util::FileEnumerator::IsDirectory(file_util_info_); | |
| 79 } | |
| 80 | |
| 81 PlatformFileError NativeFileUtil::CreateOrOpen( | |
| 82 const base::FilePath& path, int file_flags, | |
| 83 PlatformFile* file_handle, bool* created) { | |
| 84 if (!file_util::DirectoryExists(path.DirName())) { | |
| 85 // If its parent does not exist, should return NOT_FOUND error. | |
| 86 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 87 } | |
| 88 if (file_util::DirectoryExists(path)) | |
| 89 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | |
| 90 PlatformFileError error_code = base::PLATFORM_FILE_OK; | |
| 91 *file_handle = base::CreatePlatformFile(path, file_flags, | |
| 92 created, &error_code); | |
| 93 return error_code; | |
| 94 } | |
| 95 | |
| 96 PlatformFileError NativeFileUtil::Close(PlatformFile file_handle) { | |
| 97 if (!base::ClosePlatformFile(file_handle)) | |
| 98 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 99 return base::PLATFORM_FILE_OK; | |
| 100 } | |
| 101 | |
| 102 PlatformFileError NativeFileUtil::EnsureFileExists( | |
| 103 const base::FilePath& path, | |
| 104 bool* created) { | |
| 105 if (!file_util::DirectoryExists(path.DirName())) | |
| 106 // If its parent does not exist, should return NOT_FOUND error. | |
| 107 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 108 PlatformFileError error_code = base::PLATFORM_FILE_OK; | |
| 109 // Tries to create the |path| exclusively. This should fail | |
| 110 // with base::PLATFORM_FILE_ERROR_EXISTS if the path already exists. | |
| 111 PlatformFile handle = base::CreatePlatformFile( | |
| 112 path, | |
| 113 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, | |
| 114 created, &error_code); | |
| 115 if (error_code == base::PLATFORM_FILE_ERROR_EXISTS) { | |
| 116 // Make sure created_ is false. | |
| 117 if (created) | |
| 118 *created = false; | |
| 119 error_code = base::PLATFORM_FILE_OK; | |
| 120 } | |
| 121 if (handle != base::kInvalidPlatformFileValue) | |
| 122 base::ClosePlatformFile(handle); | |
| 123 return error_code; | |
| 124 } | |
| 125 | |
| 126 PlatformFileError NativeFileUtil::CreateDirectory( | |
| 127 const base::FilePath& path, | |
| 128 bool exclusive, | |
| 129 bool recursive) { | |
| 130 // If parent dir of file doesn't exist. | |
| 131 if (!recursive && !file_util::PathExists(path.DirName())) | |
| 132 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 133 | |
| 134 bool path_exists = file_util::PathExists(path); | |
| 135 if (exclusive && path_exists) | |
| 136 return base::PLATFORM_FILE_ERROR_EXISTS; | |
| 137 | |
| 138 // If file exists at the path. | |
| 139 if (path_exists && !file_util::DirectoryExists(path)) | |
| 140 return base::PLATFORM_FILE_ERROR_EXISTS; | |
| 141 | |
| 142 if (!file_util::CreateDirectory(path)) | |
| 143 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 144 | |
| 145 if (!SetPlatformSpecificDirectoryPermissions(path)) | |
| 146 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 147 | |
| 148 return base::PLATFORM_FILE_OK; | |
| 149 } | |
| 150 | |
| 151 PlatformFileError NativeFileUtil::GetFileInfo( | |
| 152 const base::FilePath& path, | |
| 153 base::PlatformFileInfo* file_info) { | |
| 154 if (!file_util::PathExists(path)) | |
| 155 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 156 if (!file_util::GetFileInfo(path, file_info)) | |
| 157 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 158 return base::PLATFORM_FILE_OK; | |
| 159 } | |
| 160 | |
| 161 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> | |
| 162 NativeFileUtil::CreateFileEnumerator(const base::FilePath& root_path, | |
| 163 bool recursive) { | |
| 164 return make_scoped_ptr(new NativeFileEnumerator( | |
| 165 root_path, recursive, | |
| 166 file_util::FileEnumerator::FILES | | |
| 167 file_util::FileEnumerator::DIRECTORIES)) | |
| 168 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); | |
| 169 } | |
| 170 | |
| 171 PlatformFileError NativeFileUtil::Touch( | |
| 172 const base::FilePath& path, | |
| 173 const base::Time& last_access_time, | |
| 174 const base::Time& last_modified_time) { | |
| 175 if (!file_util::TouchFile( | |
| 176 path, last_access_time, last_modified_time)) | |
| 177 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 178 return base::PLATFORM_FILE_OK; | |
| 179 } | |
| 180 | |
| 181 PlatformFileError NativeFileUtil::Truncate(const base::FilePath& path, int64 len
gth) { | |
| 182 PlatformFileError error_code(base::PLATFORM_FILE_ERROR_FAILED); | |
| 183 PlatformFile file = | |
| 184 base::CreatePlatformFile( | |
| 185 path, | |
| 186 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, | |
| 187 NULL, | |
| 188 &error_code); | |
| 189 if (error_code != base::PLATFORM_FILE_OK) { | |
| 190 return error_code; | |
| 191 } | |
| 192 DCHECK_NE(base::kInvalidPlatformFileValue, file); | |
| 193 if (!base::TruncatePlatformFile(file, length)) | |
| 194 error_code = base::PLATFORM_FILE_ERROR_FAILED; | |
| 195 base::ClosePlatformFile(file); | |
| 196 return error_code; | |
| 197 } | |
| 198 | |
| 199 bool NativeFileUtil::PathExists(const base::FilePath& path) { | |
| 200 return file_util::PathExists(path); | |
| 201 } | |
| 202 | |
| 203 bool NativeFileUtil::DirectoryExists(const base::FilePath& path) { | |
| 204 return file_util::DirectoryExists(path); | |
| 205 } | |
| 206 | |
| 207 PlatformFileError NativeFileUtil::CopyOrMoveFile( | |
| 208 const base::FilePath& src_path, | |
| 209 const base::FilePath& dest_path, | |
| 210 bool copy) { | |
| 211 base::PlatformFileInfo info; | |
| 212 base::PlatformFileError error = NativeFileUtil::GetFileInfo(src_path, &info); | |
| 213 if (error != base::PLATFORM_FILE_OK) | |
| 214 return error; | |
| 215 if (info.is_directory) | |
| 216 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | |
| 217 | |
| 218 error = NativeFileUtil::GetFileInfo(dest_path, &info); | |
| 219 if (error != base::PLATFORM_FILE_OK && | |
| 220 error != base::PLATFORM_FILE_ERROR_NOT_FOUND) | |
| 221 return error; | |
| 222 if (info.is_directory) | |
| 223 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 224 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) { | |
| 225 error = NativeFileUtil::GetFileInfo(dest_path.DirName(), &info); | |
| 226 if (error != base::PLATFORM_FILE_OK) | |
| 227 return error; | |
| 228 if (!info.is_directory) | |
| 229 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 230 } | |
| 231 | |
| 232 if (copy) { | |
| 233 if (file_util::CopyFile(src_path, dest_path)) | |
| 234 return base::PLATFORM_FILE_OK; | |
| 235 } else { | |
| 236 if (file_util::Move(src_path, dest_path)) | |
| 237 return base::PLATFORM_FILE_OK; | |
| 238 } | |
| 239 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 240 } | |
| 241 | |
| 242 PlatformFileError NativeFileUtil::DeleteFile(const base::FilePath& path) { | |
| 243 if (!file_util::PathExists(path)) | |
| 244 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 245 if (file_util::DirectoryExists(path)) | |
| 246 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | |
| 247 if (!file_util::Delete(path, false)) | |
| 248 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 249 return base::PLATFORM_FILE_OK; | |
| 250 } | |
| 251 | |
| 252 PlatformFileError NativeFileUtil::DeleteDirectory(const base::FilePath& path) { | |
| 253 if (!file_util::PathExists(path)) | |
| 254 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 255 if (!file_util::DirectoryExists(path)) | |
| 256 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | |
| 257 if (!file_util::IsDirectoryEmpty(path)) | |
| 258 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; | |
| 259 if (!file_util::Delete(path, false)) | |
| 260 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 261 return base::PLATFORM_FILE_OK; | |
| 262 } | |
| 263 | |
| 264 } // namespace fileapi | |
| OLD | NEW |