| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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 // Useful functions: FilePath::CompareIgnoreCase, FilePath::value [returns a |
| 6 // StringType]. |
| 7 |
| 8 #include "webkit/fileapi/obfuscated_file_util.h" |
| 9 |
| 10 #include "base/file_util.h" |
| 11 #include "webkit/fileapi/path_obfuscator.h" |
| 12 |
| 13 namespace obfuscated_file_util { |
| 14 |
| 15 PlatformFile CreateOrOpen( |
| 16 const FilePath& root_path, |
| 17 const FilePath& virtual_path, |
| 18 int file_flags, |
| 19 bool *created, |
| 20 PlatformFileError* error) { |
| 21 PathObfuscator obfuscator(root_path); |
| 22 FilePath obfuscated_path = |
| 23 obfuscator.ObfuscatedPathFromClearPath(virtual_path); |
| 24 FilePath full_path = root_path.Append(obfuscated_path); |
| 25 |
| 26 if (!file_util::DirectoryExists(full_path.DirName())) { |
| 27 // If its parent does not exist, should return NOT_FOUND error. |
| 28 *error = base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 29 return base::kInvalidPlatformFileValue; |
| 30 } |
| 31 FilePath temp; |
| 32 bool need_commit = false; |
| 33 |
| 34 // Check whether we have a dictionary entry for the file first, so that we can |
| 35 // record that we're going to try to create it if we don't. |
| 36 *error = obfuscator.ClearPathFromObfuscatedPath(obfuscated_path, &temp); |
| 37 if (*error == base::PLATFORM_FILE_ERROR_NOT_FOUND) { |
| 38 *error = obfuscator.PrepareAdd(virtual_path, obfuscated_path); |
| 39 need_commit = true; |
| 40 } |
| 41 if (*error != base::PLATFORM_FILE_OK) |
| 42 return base::kInvalidPlatformFileValue; |
| 43 base::PlatformFile file_handle = |
| 44 base::CreatePlatformFile(full_path, file_flags, created, error); |
| 45 const int CREATE_EXCLUSIVE = |
| 46 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_EXCLUSIVE_WRITE; |
| 47 if (*error == base::PLATFORM_FILE_ERROR_EXISTS && need_commit && |
| 48 ((file_flags & CREATE_EXCLUSIVE) == CREATE_EXCLUSIVE)) { |
| 49 // The file existed, but wasn't in the dictionary. The user tried an |
| 50 // exclusive create, and it failed by collision. We should add the file to |
| 51 // the dictionary [recovering from some corruption], but report the |
| 52 // collision. |
| 53 obfuscator.Commit(); |
| 54 return base::kInvalidPlatformFileValue; |
| 55 } else if (*error != base::PLATFORM_FILE_OK) { |
| 56 if (need_commit) |
| 57 obfuscator.RollBack(); |
| 58 return base::kInvalidPlatformFileValue; |
| 59 } |
| 60 if (need_commit) |
| 61 obfuscator.Commit(); |
| 62 return file_handle; |
| 63 } |
| 64 |
| 65 |
| 66 void CreateDirectory( |
| 67 const FilePath& root_path, |
| 68 const FilePath& virtual_path, |
| 69 bool exclusive, |
| 70 PlatformFileError* error) { |
| 71 PathObfuscator obfuscator(root_path); |
| 72 FilePath obfuscated_path = |
| 73 obfuscator.ObfuscatedPathFromClearPath(virtual_path); |
| 74 FilePath full_path = root_path.Append(obfuscated_path); |
| 75 if (!file_util::PathExists(full_path.DirName())) { |
| 76 *error = base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 77 return; |
| 78 } |
| 79 bool path_exists = file_util::PathExists(full_path); |
| 80 if (exclusive && path_exists) { |
| 81 *error = base::PLATFORM_FILE_ERROR_EXISTS; |
| 82 return; |
| 83 } |
| 84 if (path_exists && !file_util::DirectoryExists(full_path)) { |
| 85 *error = base::PLATFORM_FILE_ERROR_EXISTS; |
| 86 return; |
| 87 } |
| 88 *error = obfuscator.PrepareAdd(virtual_path, obfuscated_path); |
| 89 if (*error != base::PLATFORM_FILE_OK) |
| 90 return; |
| 91 |
| 92 if (!file_util::CreateDirectory(full_path)) { |
| 93 *error = base::PLATFORM_FILE_ERROR_FAILED; |
| 94 obfuscator.RollBack(); |
| 95 return; |
| 96 } |
| 97 obfuscator.Commit(); |
| 98 } |
| 99 |
| 100 } // namespace obfuscated_file_util |
| OLD | NEW |