Always set *error when CreateDirectoryAndGetError fails.
[chromium-blink-merge.git] / webkit / glue / webfileutilities_impl.cc
blobe5aaae9844dcc9e5967e85b84e5a5538de2388f8
1 // Copyright (c) 2012 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.
5 #include "webkit/glue/webfileutilities_impl.h"
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "net/base/file_stream.h"
11 #include "net/base/net_util.h"
12 #include "third_party/WebKit/public/platform/WebFileInfo.h"
13 #include "third_party/WebKit/public/platform/WebString.h"
14 #include "third_party/WebKit/public/platform/WebURL.h"
15 #include "webkit/glue/webkit_glue.h"
17 using WebKit::WebString;
19 namespace webkit_glue {
21 WebFileUtilitiesImpl::WebFileUtilitiesImpl()
22 : sandbox_enabled_(true) {
25 WebFileUtilitiesImpl::~WebFileUtilitiesImpl() {
28 bool WebFileUtilitiesImpl::fileExists(const WebString& path) {
29 return file_util::PathExists(base::FilePath::FromUTF16Unsafe(path));
32 bool WebFileUtilitiesImpl::deleteFile(const WebString& path) {
33 NOTREACHED();
34 return false;
37 bool WebFileUtilitiesImpl::deleteEmptyDirectory(const WebString& path) {
38 NOTREACHED();
39 return false;
42 bool WebFileUtilitiesImpl::getFileInfo(const WebString& path,
43 WebKit::WebFileInfo& web_file_info) {
44 if (sandbox_enabled_) {
45 NOTREACHED();
46 return false;
48 base::PlatformFileInfo file_info;
49 if (!file_util::GetFileInfo(base::FilePath::FromUTF16Unsafe(path),
50 &file_info))
51 return false;
53 webkit_glue::PlatformFileInfoToWebFileInfo(file_info, &web_file_info);
54 web_file_info.platformPath = path;
55 return true;
58 WebString WebFileUtilitiesImpl::directoryName(const WebString& path) {
59 return base::FilePath::FromUTF16Unsafe(path).DirName().AsUTF16Unsafe();
62 WebString WebFileUtilitiesImpl::pathByAppendingComponent(
63 const WebString& webkit_path,
64 const WebString& webkit_component) {
65 base::FilePath path(base::FilePath::FromUTF16Unsafe(webkit_path));
66 base::FilePath component(base::FilePath::FromUTF16Unsafe(webkit_component));
67 base::FilePath combined_path = path.Append(component);
68 return combined_path.AsUTF16Unsafe();
71 bool WebFileUtilitiesImpl::makeAllDirectories(const WebString& path) {
72 DCHECK(!sandbox_enabled_);
73 return file_util::CreateDirectory(base::FilePath::FromUTF16Unsafe(path));
76 bool WebFileUtilitiesImpl::isDirectory(const WebString& path) {
77 return file_util::DirectoryExists(base::FilePath::FromUTF16Unsafe(path));
80 WebKit::WebURL WebFileUtilitiesImpl::filePathToURL(const WebString& path) {
81 return net::FilePathToFileURL(base::FilePath::FromUTF16Unsafe(path));
84 base::PlatformFile WebFileUtilitiesImpl::openFile(const WebString& path,
85 int mode) {
86 if (sandbox_enabled_) {
87 NOTREACHED();
88 return base::kInvalidPlatformFileValue;
90 return base::CreatePlatformFile(
91 base::FilePath::FromUTF16Unsafe(path),
92 (mode == 0) ? (base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ)
93 : (base::PLATFORM_FILE_CREATE_ALWAYS |
94 base::PLATFORM_FILE_WRITE),
95 NULL, NULL);
98 void WebFileUtilitiesImpl::closeFile(base::PlatformFile& handle) {
99 if (handle == base::kInvalidPlatformFileValue)
100 return;
101 if (base::ClosePlatformFile(handle))
102 handle = base::kInvalidPlatformFileValue;
105 long long WebFileUtilitiesImpl::seekFile(base::PlatformFile handle,
106 long long offset,
107 int origin) {
108 if (handle == base::kInvalidPlatformFileValue)
109 return -1;
110 return base::SeekPlatformFile(handle,
111 static_cast<base::PlatformFileWhence>(origin),
112 offset);
115 bool WebFileUtilitiesImpl::truncateFile(base::PlatformFile handle,
116 long long offset) {
117 if (handle == base::kInvalidPlatformFileValue || offset < 0)
118 return false;
119 return base::TruncatePlatformFile(handle, offset);
122 int WebFileUtilitiesImpl::readFromFile(base::PlatformFile handle,
123 char* data,
124 int length) {
125 if (handle == base::kInvalidPlatformFileValue || !data || length <= 0)
126 return -1;
127 return base::ReadPlatformFileCurPosNoBestEffort(handle, data, length);
130 int WebFileUtilitiesImpl::writeToFile(base::PlatformFile handle,
131 const char* data,
132 int length) {
133 if (handle == base::kInvalidPlatformFileValue || !data || length <= 0)
134 return -1;
135 return base::WritePlatformFileCurPosNoBestEffort(handle, data, length);
138 } // namespace webkit_glue