| 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,100 @@
|
| +// 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 "webkit/fileapi/path_obfuscator.h"
|
| +
|
| +namespace obfuscated_file_util {
|
| +
|
| +PlatformFile CreateOrOpen(
|
| + const FilePath& root_path,
|
| + const FilePath& virtual_path,
|
| + int file_flags,
|
| + bool *created,
|
| + PlatformFileError* error) {
|
| + 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.
|
| + *error = base::PLATFORM_FILE_ERROR_NOT_FOUND;
|
| + return base::kInvalidPlatformFileValue;
|
| + }
|
| + 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.
|
| + *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)
|
| + return base::kInvalidPlatformFileValue;
|
| + base::PlatformFile file_handle =
|
| + base::CreatePlatformFile(full_path, file_flags, created, 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();
|
| + return base::kInvalidPlatformFileValue;
|
| + } else if (*error != base::PLATFORM_FILE_OK) {
|
| + if (need_commit)
|
| + obfuscator.RollBack();
|
| + return base::kInvalidPlatformFileValue;
|
| + }
|
| + if (need_commit)
|
| + obfuscator.Commit();
|
| + return file_handle;
|
| +}
|
| +
|
| +
|
| +void CreateDirectory(
|
| + const FilePath& root_path,
|
| + const FilePath& virtual_path,
|
| + bool exclusive,
|
| + PlatformFileError* error) {
|
| + 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())) {
|
| + *error = base::PLATFORM_FILE_ERROR_NOT_FOUND;
|
| + return;
|
| + }
|
| + bool path_exists = file_util::PathExists(full_path);
|
| + if (exclusive && path_exists) {
|
| + *error = base::PLATFORM_FILE_ERROR_EXISTS;
|
| + return;
|
| + }
|
| + if (path_exists && !file_util::DirectoryExists(full_path)) {
|
| + *error = base::PLATFORM_FILE_ERROR_EXISTS;
|
| + return;
|
| + }
|
| + *error = obfuscator.PrepareAdd(virtual_path, obfuscated_path);
|
| + if (*error != base::PLATFORM_FILE_OK)
|
| + return;
|
| +
|
| + if (!file_util::CreateDirectory(full_path)) {
|
| + *error = base::PLATFORM_FILE_ERROR_FAILED;
|
| + obfuscator.RollBack();
|
| + return;
|
| + }
|
| + obfuscator.Commit();
|
| +}
|
| +
|
| +} // namespace obfuscated_file_util
|
|
|
| Property changes on: webkit\fileapi\obfuscated_file_util.cc
|
| ___________________________________________________________________
|
| Added: svn:eol-style
|
| + LF
|
|
|
|
|