| Index: webkit/fileapi/path_obfuscator.cc
|
| ===================================================================
|
| --- webkit/fileapi/path_obfuscator.cc (revision 0)
|
| +++ webkit/fileapi/path_obfuscator.cc (revision 0)
|
| @@ -0,0 +1,160 @@
|
| +// 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/path_obfuscator.h"
|
| +
|
| +#include <vector>
|
| +#include "base/logging.h"
|
| +
|
| +namespace fileapi {
|
| +
|
| +PathObfuscator::PathObfuscator(const FilePath& root_path) :
|
| + from_map_valid_(false), to_map_valid_(false), root_path_(root_path) {
|
| +}
|
| +
|
| +FilePath PathObfuscator::ObfuscatedPathFromClearPath(
|
| + const FilePath& virtual_path) {
|
| + std::vector<StringType> components;
|
| + virtual_path.GetComponents(&components);
|
| +
|
| + FilePath obfuscated_path;
|
| + std::vector<StringType>::const_iterator iter;
|
| + for (iter = components.begin(); iter != components.end(); ++iter)
|
| + obfuscated_path = obfuscated_path.Append(
|
| + ObfuscatedNameMap::ObfuscatedNameFromClearName(*iter));
|
| + return obfuscated_path;
|
| +}
|
| +
|
| +PlatformFileError PathObfuscator::ClearPathFromObfuscatedPath(
|
| + const FilePath& obfuscated_path,
|
| + FilePath* clear_path) {
|
| + std::vector<StringType> components;
|
| + obfuscated_path.GetComponents(&components);
|
| +
|
| + FilePath dict_path = root_path_;
|
| + FilePath path;
|
| + std::vector<StringType>::const_iterator iter;
|
| + ObfuscatedNameMap map;
|
| + for (iter = components.begin(); iter != components.end(); ++iter) {
|
| + StringType clear_name;
|
| + map.Init(dict_path);
|
| + PlatformFileError error =
|
| + map.ClearNameFromObfuscatedName(*iter, &clear_name);
|
| + if (error != base::PLATFORM_FILE_OK)
|
| + return error;
|
| + path = path.Append(clear_name);
|
| + dict_path.Append(*iter);
|
| + }
|
| + *clear_path = path;
|
| + return base::PLATFORM_FILE_OK;
|
| +}
|
| +
|
| +PlatformFileError PathObfuscator::PrepareAdd(
|
| + const FilePath& clear_path,
|
| + const FilePath& obfuscated_path) {
|
| + CHECK(!from_map_valid_);
|
| +#ifndef NDEBUG
|
| + FilePath test_path(ObfuscatedPathFromClearPath(clear_path));
|
| + CHECK(test_path == obfuscated_path);
|
| +#endif
|
| + FilePath dict_dir = root_path_.Append(obfuscated_path).DirName();
|
| + from_map_.Init(dict_dir);
|
| + PlatformFileError error =
|
| + from_map_.PrepareAdd(clear_path.BaseName().value(),
|
| + obfuscated_path.BaseName().value());
|
| + if (error != base::PLATFORM_FILE_OK)
|
| + return error;
|
| + from_map_valid_ = true;
|
| + return base::PLATFORM_FILE_OK;
|
| +}
|
| +
|
| +PlatformFileError PathObfuscator::PrepareDelete(
|
| + const FilePath& clear_path,
|
| + const FilePath& obfuscated_path) {
|
| + CHECK(!from_map_valid_);
|
| +#ifndef NDEBUG
|
| + FilePath test_path(ObfuscatedPathFromClearPath(clear_path));
|
| + CHECK(test_path == obfuscated_path);
|
| +#endif
|
| + FilePath dict_dir = root_path_.Append(obfuscated_path).DirName();
|
| + from_map_.Init(dict_dir);
|
| + {
|
| + PlatformFileError error =
|
| + from_map_.PrepareDelete(clear_path.BaseName().value(),
|
| + obfuscated_path.BaseName().value());
|
| + if (error != base::PLATFORM_FILE_OK)
|
| + return error;
|
| + }
|
| + from_map_valid_ = true;
|
| + return base::PLATFORM_FILE_OK;
|
| +}
|
| +
|
| +PlatformFileError PathObfuscator::PrepareMove(
|
| + const FilePath& old_clear_path,
|
| + const FilePath& old_obfuscated_path,
|
| + const FilePath& new_clear_path,
|
| + const FilePath& new_obfuscated_path) {
|
| + CHECK(!from_map_valid_);
|
| +#ifndef NDEBUG
|
| + FilePath test_path(ObfuscatedPathFromClearPath(old_clear_path));
|
| + CHECK(test_path == old_obfuscated_path);
|
| + test_path = ObfuscatedPathFromClearPath(new_clear_path);
|
| + CHECK(test_path == new_obfuscated_path);
|
| +#endif
|
| + FilePath dict_dir = root_path_.Append(old_obfuscated_path).DirName();
|
| + from_map_.Init(dict_dir);
|
| + if (old_obfuscated_path.DirName() == new_obfuscated_path.DirName()) {
|
| + PlatformFileError error =
|
| + from_map_.PrepareMove(
|
| + old_clear_path.BaseName().value(),
|
| + old_obfuscated_path.BaseName().value(),
|
| + new_clear_path.BaseName().value(),
|
| + new_obfuscated_path.BaseName().value());
|
| + if (error != base::PLATFORM_FILE_OK)
|
| + return error;
|
| + from_map_valid_ = true;
|
| + } else {
|
| + dict_dir = root_path_.Append(new_obfuscated_path).DirName();
|
| + to_map_.Init(new_obfuscated_path);
|
| + PlatformFileError error =
|
| + from_map_.PrepareDelete(old_clear_path.BaseName().value(),
|
| + old_obfuscated_path.BaseName().value());
|
| + if (error != base::PLATFORM_FILE_OK)
|
| + return error;
|
| + error = to_map_.PrepareAdd(new_clear_path.BaseName().value(),
|
| + new_obfuscated_path.BaseName().value());
|
| + if (error != base::PLATFORM_FILE_OK) {
|
| + from_map_.Rollback();
|
| + return error;
|
| + }
|
| + from_map_valid_ = true;
|
| + to_map_valid_ = true;
|
| + }
|
| + return base::PLATFORM_FILE_OK;
|
| +}
|
| +
|
| +PlatformFileError PathObfuscator::Commit() {
|
| + CHECK(from_map_valid_);
|
| + PlatformFileError error = from_map_.Commit();
|
| + if (error != base::PLATFORM_FILE_OK)
|
| + return error;
|
| + if (to_map_valid_)
|
| + error = to_map_.Commit();
|
| + from_map_valid_ = false;
|
| + to_map_valid_ = false;
|
| + return error;
|
| +}
|
| +
|
| +void PathObfuscator::Rollback() {
|
| + if (from_map_valid_) {
|
| + from_map_.Rollback();
|
| + from_map_valid_ = false;
|
| + }
|
| + if (to_map_valid_) {
|
| + to_map_.Rollback();
|
| + to_map_valid_ = false;
|
| + }
|
| +}
|
| +
|
| +} // namespace fileapi
|
|
|
| Property changes on: webkit\fileapi\path_obfuscator.cc
|
| ___________________________________________________________________
|
| Added: svn:eol-style
|
| + LF
|
|
|
|
|