Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Unified Diff: webkit/fileapi/file_system_file_util_base.cc

Issue 6471019: Stackable file_util for FileAPIs. Sample code for discussion. Incomplete. Cannot be compiled. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Fixed the Patch Set 3 before reflecting the comments. -UtilBase can be built and works. Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webkit/fileapi/file_system_file_util_base.h ('k') | webkit/fileapi/file_system_file_util_proxy.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webkit/fileapi/file_system_file_util_base.cc
diff --git a/webkit/fileapi/file_system_file_util_base.cc b/webkit/fileapi/file_system_file_util_base.cc
new file mode 100644
index 0000000000000000000000000000000000000000..179fb155f1e6642c092e8e0f934570159d913a66
--- /dev/null
+++ b/webkit/fileapi/file_system_file_util_base.cc
@@ -0,0 +1,337 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "webkit/fileapi/file_system_file_util_base.h"
+
+#include "base/file_util.h"
+
+namespace {
+
+// Performs common checks for move and copy.
+// This also removes the destination directory if it's non-empty and all other
+// checks are passed (so that the copy/move correctly overwrites the
+// destination).
+static base::PlatformFileError PerformCommonCheckAndPreparationForMoveAndCopy(
+ const FilePath& src_file_path,
+ const FilePath& dest_file_path) {
+ // Exits earlier if the source path does not exist.
+ if (!file_util::PathExists(src_file_path))
+ return base::PLATFORM_FILE_ERROR_NOT_FOUND;
+
+ // The parent of the |dest_file_path| does not exist.
+ if (!file_util::DirectoryExists(dest_file_path.DirName()))
+ return base::PLATFORM_FILE_ERROR_NOT_FOUND;
+
+ // It is an error to try to copy/move an entry into its child.
+ if (src_file_path.IsParent(dest_file_path))
+ return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
+
+ // Now it is ok to return if the |dest_file_path| does not exist.
+ if (!file_util::PathExists(dest_file_path))
+ return base::PLATFORM_FILE_OK;
+
+ // |src_file_path| exists and is a directory.
+ // |dest_file_path| exists and is a file.
+ bool src_is_directory = file_util::DirectoryExists(src_file_path);
+ bool dest_is_directory = file_util::DirectoryExists(dest_file_path);
+ if (src_is_directory && !dest_is_directory)
+ return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY;
+
+ // |src_file_path| exists and is a file.
+ // |dest_file_path| exists and is a directory.
+ if (!src_is_directory && dest_is_directory)
+ return base::PLATFORM_FILE_ERROR_NOT_A_FILE;
+
+ // It is an error to copy/move an entry into the same path.
+ if (src_file_path.value() == dest_file_path.value())
+ return base::PLATFORM_FILE_ERROR_EXISTS;
+
+ if (dest_is_directory) {
+ // It is an error to copy/move an entry to a non-empty directory.
+ // Otherwise the copy/move attempt must overwrite the destination, but
+ // the file_util's Copy or Move method doesn't perform overwrite
+ // on all platforms, so we delete the destination directory here.
+ // TODO(kinuko): may be better to change the file_util::{Copy,Move}.
+ if (!file_util::Delete(dest_file_path, false /* recursive */)) {
+ if (!file_util::IsDirectoryEmpty(dest_file_path))
+ return base::PLATFORM_FILE_ERROR_NOT_EMPTY;
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ }
+ }
+ return base::PLATFORM_FILE_OK;
+}
+
+} // namespace
+
+namespace fileapi {
+
+// static
+FileSystemFileUtilBase* FileSystemFileUtilBase::GetInstance() {
+ return Singleton<FileSystemFileUtilBase>::get();
+}
+
+base::PlatformFileError FileSystemFileUtilBase::CreateOrOpen(
+ FileSystemOperationContext* fs_context,
+ const FilePath& file_path,
+ int file_flags,
+ base::PlatformFile& file_handle,
+ bool& created) {
+ if (!file_util::DirectoryExists(file_path.DirName()))
+ // If its parent does not exist, should return NOT_FOUND error.
+ return base::PLATFORM_FILE_ERROR_NOT_FOUND;
+ base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
+ file_handle = base::CreatePlatformFile(file_path, file_flags,
+ &created, &error_code);
+ return error_code;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::CreateTemporary(
+ FileSystemOperationContext* fs_context,
+ FilePath& file_path,
+ base::PlatformFile& file_handle) {
+ // TODO(darin): file_util should have a variant of CreateTemporaryFile
+ // that returns a FilePath and a PlatformFile.
+ file_util::CreateTemporaryFile(&file_path);
+
+ // Use a fixed set of flags that are appropriate for writing to a temporary
+ // file from the IO thread using a net::FileStream.
+ int file_flags =
+ base::PLATFORM_FILE_CREATE_ALWAYS |
+ base::PLATFORM_FILE_WRITE |
+ base::PLATFORM_FILE_ASYNC |
+ base::PLATFORM_FILE_TEMPORARY;
+ base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
+ file_handle = base::CreatePlatformFile(file_path, file_flags,
+ NULL, &error_code);
+ return error_code;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::Close(
+ FileSystemOperationContext* fs_context,
+ base::PlatformFile file_handle) {
+ if (!base::ClosePlatformFile(file_handle))
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ return base::PLATFORM_FILE_OK;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::EnsureFileExists(
+ FileSystemOperationContext* fs_context,
+ const FilePath& file_path,
+ bool *created) {
+ if (!file_util::DirectoryExists(file_path.DirName()))
+ // If its parent does not exist, should return NOT_FOUND error.
+ return base::PLATFORM_FILE_ERROR_NOT_FOUND;
+ base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
+ // Tries to create the |file_path| exclusively. This should fail
+ // with PLATFORM_FILE_ERROR_EXISTS if the path already exists.
+ base::PlatformFile handle = base::CreatePlatformFile(
+ file_path,
+ base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ,
+ created, &error_code);
+ if (error_code == base::PLATFORM_FILE_ERROR_EXISTS) {
+ // Make sure *created is false.
+ *created = false;
+ error_code = base::PLATFORM_FILE_OK;
+ }
+ if (handle != base::kInvalidPlatformFileValue)
+ base::ClosePlatformFile(handle);
+ return error_code;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::Delete(
+ FileSystemOperationContext* fs_context,
+ const FilePath& file_path,
+ bool recursive) {
+ if (!file_util::PathExists(file_path))
+ return base::PLATFORM_FILE_ERROR_NOT_FOUND;
+ if (!file_util::Delete(file_path, recursive)) {
+ if (!recursive && !file_util::IsDirectoryEmpty(file_path))
+ return base::PLATFORM_FILE_ERROR_NOT_EMPTY;
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ }
+ return base::PLATFORM_FILE_OK;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::Copy(
+ FileSystemOperationContext* fs_context,
+ const FilePath& src_file_path,
+ const FilePath& dest_file_path) {
+ // TODO(dmikurube): move the checking function
+ base::PlatformFileError error =
+ PerformCommonCheckAndPreparationForMoveAndCopy(
+ src_file_path, dest_file_path);
+ if (error != base::PLATFORM_FILE_OK)
+ return error;
+ if (!file_util::CopyDirectory(src_file_path, dest_file_path,
+ true /* recursive */))
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ return base::PLATFORM_FILE_OK;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::Move(
+ FileSystemOperationContext* fs_context,
+ const FilePath& src_file_path,
+ const FilePath& dest_file_path) {
+ // TODO(dmikurube): move the checking function
+ base::PlatformFileError error =
+ PerformCommonCheckAndPreparationForMoveAndCopy(
+ src_file_path, dest_file_path);
+ if (error != base::PLATFORM_FILE_OK)
+ return error;
+ if (!file_util::Move(src_file_path, dest_file_path))
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ return base::PLATFORM_FILE_OK;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::CreateDirectory(
+ FileSystemOperationContext* fs_context,
+ const FilePath& file_path,
+ bool exclusive,
+ bool recursive) {
+ bool path_exists = file_util::PathExists(file_path);
+ // If parent dir of file doesn't exist.
+ if (!recursive && !file_util::PathExists(file_path.DirName()))
+ return base::PLATFORM_FILE_ERROR_NOT_FOUND;
+ if (exclusive && path_exists)
+ return base::PLATFORM_FILE_ERROR_EXISTS;
+ // If file exists at the path.
+ if (path_exists && !file_util::DirectoryExists(file_path))
+ return base::PLATFORM_FILE_ERROR_EXISTS;
+ if (!file_util::CreateDirectory(file_path))
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ return base::PLATFORM_FILE_OK;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::ReadDirectory(
+ FileSystemOperationContext* fs_context,
+ const FilePath& file_path,
+ std::vector<base::FileUtilProxy::Entry>& entries) {
+ // TODO(kkanetkar): Implement directory read in multiple chunks.
+ if (!file_util::DirectoryExists(file_path))
+ return base::PLATFORM_FILE_ERROR_NOT_FOUND;
+
+ file_util::FileEnumerator file_enum(
+ file_path, false, static_cast<file_util::FileEnumerator::FILE_TYPE>(
+ file_util::FileEnumerator::FILES |
+ file_util::FileEnumerator::DIRECTORIES));
+ FilePath current;
+ while (!(current = file_enum.Next()).empty()) {
+ base::FileUtilProxy::Entry entry;
+ file_util::FileEnumerator::FindInfo info;
+ file_enum.GetFindInfo(&info);
+ entry.is_directory = file_enum.IsDirectory(info);
+ // This will just give the entry's name instead of entire path
+ // if we use current.value().
+ entry.name = file_util::FileEnumerator::GetFilename(info).value();
+ entries.push_back(entry);
+ }
+ return base::PLATFORM_FILE_OK;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::GetFileInfo(
+ FileSystemOperationContext* fs_context,
+ const FilePath& file_path,
+ base::PlatformFileInfo& file_info) {
+ if (!file_util::PathExists(file_path))
+ return base::PLATFORM_FILE_ERROR_NOT_FOUND;
+ if (!file_util::GetFileInfo(file_path, &file_info))
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ return base::PLATFORM_FILE_OK;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::GetFileInfoFromPlatformFile(
+ FileSystemOperationContext* fs_context,
+ base::PlatformFile file,
+ base::PlatformFileInfo& file_info) {
+ if (!base::GetPlatformFileInfo(file, &file_info))
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ return base::PLATFORM_FILE_OK;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::Read(
+ FileSystemOperationContext* fs_context,
+ base::PlatformFile file,
+ int64 offset,
+ int bytes_to_read,
+ int& bytes_read,
+ char* buffer) {
+ bytes_read = base::ReadPlatformFile(file, offset, buffer, bytes_to_read);
+ if (bytes_read < 0)
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ return base::PLATFORM_FILE_OK;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::Write(
+ FileSystemOperationContext* fs_context,
+ base::PlatformFile file,
+ int64 offset,
+ int bytes_to_write,
+ int& bytes_written,
+ char* buffer) {
+ bytes_written = base::WritePlatformFile(file, offset, buffer,
+ bytes_to_write);
+ if (bytes_written < 0)
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ return base::PLATFORM_FILE_OK;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::Touch(
+ FileSystemOperationContext* fs_context,
+ base::PlatformFile file,
+ const base::Time& last_access_time,
+ const base::Time& last_modified_time) {
+ if (!base::TouchPlatformFile(file, last_access_time, last_modified_time))
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ return base::PLATFORM_FILE_OK;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::TouchFilePath(
+ FileSystemOperationContext* fs_context,
+ const FilePath& file_path,
+ const base::Time& last_access_time,
+ const base::Time& last_modified_time) {
+ if (!file_util::TouchFile(
+ file_path, last_access_time, last_modified_time))
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ return base::PLATFORM_FILE_OK;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::TruncatePlatformFile(
+ FileSystemOperationContext* fs_context,
+ base::PlatformFile file,
+ int64 length) {
+ if (!base::TruncatePlatformFile(file, length))
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ return base::PLATFORM_FILE_OK;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::Truncate(
+ FileSystemOperationContext* fs_context,
+ const FilePath& path,
+ int64 length) {
+ base::PlatformFileError error_code(base::PLATFORM_FILE_ERROR_FAILED);
+ base::PlatformFile file =
+ base::CreatePlatformFile(
+ path,
+ base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
+ NULL,
+ &error_code);
+ if (error_code != base::PLATFORM_FILE_OK)
+ return error_code;
+ error_code = base::PLATFORM_FILE_OK;
+ if (!base::TruncatePlatformFile(file, length))
+ error_code = base::PLATFORM_FILE_ERROR_FAILED;
+ base::ClosePlatformFile(file);
+ return error_code;
+}
+
+base::PlatformFileError FileSystemFileUtilBase::Flush(
+ FileSystemOperationContext* fs_context,
+ base::PlatformFile file) {
+ if (!base::FlushPlatformFile(file))
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ return base::PLATFORM_FILE_OK;
+}
+
+} // namespace fileapi
« no previous file with comments | « webkit/fileapi/file_system_file_util_base.h ('k') | webkit/fileapi/file_system_file_util_proxy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698