| Index: webkit/fileapi/obfuscated_file_util.cc
|
| ===================================================================
|
| --- webkit/fileapi/obfuscated_file_util.cc (revision 0)
|
| +++ webkit/fileapi/obfuscated_file_util.cc (revision 0)
|
| @@ -0,0 +1,138 @@
|
| +// 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.
|
| +
|
| +// Useful functions: FilePath::CompareIgnoreCase, FilePath::value [returns a
|
| +// StringType].
|
| +
|
| +#include "webkit/fileapi/obfuscated_file_util.h"
|
| +
|
| +#include "base/file_util.h"
|
| +#include "base/logging.h"
|
| +#include "webkit/fileapi/file_system_context.h"
|
| +#include "webkit/fileapi/file_system_operation_context.h"
|
| +#include "webkit/fileapi/file_system_path_manager.h"
|
| +#include "webkit/fileapi/path_obfuscator.h"
|
| +
|
| +namespace fileapi {
|
| +
|
| +ObfuscatedFileUtil* ObfuscatedFileUtil::GetInstance() {
|
| + return Singleton<ObfuscatedFileUtil>::get();
|
| +}
|
| +
|
| +PlatformFileError ObfuscatedFileUtil::CreateOrOpen(
|
| + FileSystemOperationContext* context,
|
| + const FilePath& file_path,
|
| + int file_flags,
|
| + PlatformFile* file_handle,
|
| + bool *created) {
|
| + FilePath root_path;
|
| + FilePath virtual_path;
|
| +
|
| + CHECK(context->file_system_context()->path_manager()->CrackFileSystemPath(
|
| + file_path, NULL, NULL, &root_path, &virtual_path));
|
| +
|
| + fileapi::PathObfuscator obfuscator(root_path);
|
| + FilePath obfuscated_path =
|
| + obfuscator.ObfuscatedPathFromClearPath(virtual_path);
|
| + FilePath full_path = root_path.Append(obfuscated_path);
|
| +
|
| + if (!file_util::DirectoryExists(full_path.DirName())) {
|
| + // If its parent does not exist, should return NOT_FOUND error.
|
| + *file_handle = base::kInvalidPlatformFileValue;
|
| + return base::PLATFORM_FILE_ERROR_NOT_FOUND;
|
| + }
|
| + FilePath temp;
|
| + bool need_commit = false;
|
| +
|
| + // Check whether we have a dictionary entry for the file first, so that we can
|
| + // record that we're going to try to create it if we don't.
|
| + PlatformFileError error =
|
| + obfuscator.ClearPathFromObfuscatedPath(obfuscated_path, &temp);
|
| + if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) {
|
| + error = obfuscator.PrepareAdd(virtual_path, obfuscated_path);
|
| + need_commit = true;
|
| + }
|
| + if (error != base::PLATFORM_FILE_OK) {
|
| + *file_handle = base::kInvalidPlatformFileValue;
|
| + return error;
|
| + }
|
| + *file_handle =
|
| + base::CreatePlatformFile(full_path, file_flags, NULL, &error);
|
| + const int CREATE_EXCLUSIVE =
|
| + base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_EXCLUSIVE_WRITE;
|
| + if (error == base::PLATFORM_FILE_ERROR_EXISTS && need_commit &&
|
| + ((file_flags & CREATE_EXCLUSIVE) == CREATE_EXCLUSIVE)) {
|
| + // The file existed, but wasn't in the dictionary. The user tried an
|
| + // exclusive create, and it failed by collision. We should add the file to
|
| + // the dictionary [recovering from some corruption], but report the
|
| + // collision.
|
| + obfuscator.Commit();
|
| + *file_handle = base::kInvalidPlatformFileValue;
|
| + return error;
|
| + } else if (error != base::PLATFORM_FILE_OK) {
|
| + if (need_commit)
|
| + obfuscator.Rollback();
|
| + *file_handle = base::kInvalidPlatformFileValue;
|
| + return error;
|
| + }
|
| + if (need_commit)
|
| + obfuscator.Commit();
|
| + return base::PLATFORM_FILE_OK;
|
| +}
|
| +
|
| +
|
| +PlatformFileError ObfuscatedFileUtil::CreateDirectory(
|
| + FileSystemOperationContext* context,
|
| + const FilePath& file_path,
|
| + bool exclusive) {
|
| + FilePath root_path;
|
| + FilePath virtual_path;
|
| +
|
| + CHECK(context->file_system_context()->path_manager()->CrackFileSystemPath(
|
| + file_path, NULL, NULL, &root_path, &virtual_path));
|
| +
|
| + fileapi::PathObfuscator obfuscator(root_path);
|
| + FilePath obfuscated_path =
|
| + obfuscator.ObfuscatedPathFromClearPath(virtual_path);
|
| + FilePath full_path = root_path.Append(obfuscated_path);
|
| + if (!file_util::PathExists(full_path.DirName())) {
|
| + return base::PLATFORM_FILE_ERROR_NOT_FOUND;
|
| + }
|
| + bool path_exists = file_util::PathExists(full_path);
|
| + if (exclusive && path_exists) {
|
| + // TODO(ericu): What if it's not in the dictionary?
|
| + return base::PLATFORM_FILE_ERROR_EXISTS;
|
| + }
|
| + if (path_exists && !file_util::DirectoryExists(full_path)) {
|
| + return base::PLATFORM_FILE_ERROR_EXISTS;
|
| + }
|
| + bool inhibit_commit = false;
|
| + PlatformFileError error =
|
| + obfuscator.PrepareAdd(virtual_path, obfuscated_path);
|
| + if (error != base::PLATFORM_FILE_OK) {
|
| + if (path_exists && !exclusive &&
|
| + error == base::PLATFORM_FILE_ERROR_EXISTS) {
|
| + return base::PLATFORM_FILE_OK;
|
| + }
|
| + if (path_exists || error != base::PLATFORM_FILE_ERROR_EXISTS) {
|
| + return error;
|
| + }
|
| + // Else it's in the dictionary, but not on the disk. We recover from this
|
| + // corruption by just creating the directory and leaving the dictionary
|
| + // alone.
|
| + inhibit_commit = true;
|
| + }
|
| +
|
| + // TODO: Replace this call with a call to
|
| + // QuotaFileUtil::GetInstance()->CreateDirectory().
|
| + if (!file_util::CreateDirectory(full_path)) {
|
| + obfuscator.Rollback();
|
| + return base::PLATFORM_FILE_ERROR_FAILED;
|
| + }
|
| + if (!inhibit_commit)
|
| + obfuscator.Commit();
|
| + return base::PLATFORM_FILE_OK;
|
| +}
|
| +
|
| +} // namespace fileapi
|
|
|
| Property changes on: webkit\fileapi\obfuscated_file_util.cc
|
| ___________________________________________________________________
|
| Added: svn:eol-style
|
| + LF
|
|
|
|
|