Index: components/filesystem/directory_impl.cc |
diff --git a/components/filesystem/directory_impl.cc b/components/filesystem/directory_impl.cc |
index 8c86fc7043bb47dd7f9c2a4b6a260f79277d9e28..979347aea5481b1e3a24a0986864d1461b12161f 100644 |
--- a/components/filesystem/directory_impl.cc |
+++ b/components/filesystem/directory_impl.cc |
@@ -15,6 +15,7 @@ |
#include "build/build_config.h" |
#include "components/filesystem/file_impl.h" |
#include "components/filesystem/util.h" |
+#include "mojo/common/common_type_converters.h" |
namespace filesystem { |
@@ -64,7 +65,7 @@ void DirectoryImpl::OpenFile(const mojo::String& raw_path, |
#if defined(OS_WIN) |
// On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory. |
- if (DirectoryExists(path)) |
+ if (base::DirectoryExists(path)) |
open_flags |= base::File::FLAG_BACKUP_SEMANTICS; |
#endif // OS_WIN |
@@ -215,4 +216,68 @@ void DirectoryImpl::Flush(const FlushCallback& callback) { |
callback.Run(FileError::OK); |
} |
+void DirectoryImpl::ReadEntireFile(const mojo::String& raw_path, |
+ const ReadEntireFileCallback& callback) { |
+ base::FilePath path; |
+ FileError error = ValidatePath(raw_path, directory_path_, &path); |
+ if (error != FileError::OK) { |
+ callback.Run(error, mojo::Array<uint8_t>(0)); |
+ return; |
+ } |
+ |
+ if (base::DirectoryExists(path)) { |
+ callback.Run(FileError::NOT_A_FILE, mojo::Array<uint8_t>(0)); |
+ return; |
+ } |
+ |
+ base::File base_file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
+ if (!base_file.IsValid()) { |
+ callback.Run(GetError(base_file), mojo::Array<uint8_t>(0)); |
+ return; |
+ } |
+ |
+ std::string contents; |
+ const int kBufferSize = 1 << 16; |
+ scoped_ptr<char[]> buf(new char[kBufferSize]); |
+ int len; |
+ while ((len = base_file.ReadAtCurrentPos(buf.get(), kBufferSize)) > 0) |
+ contents.append(buf.get(), len); |
+ |
+ callback.Run(FileError::OK, mojo::Array<uint8_t>::From(contents)); |
+} |
+ |
+void DirectoryImpl::WriteFile(const mojo::String& raw_path, |
+ mojo::Array<uint8_t> data, |
+ const WriteFileCallback& callback) { |
+ base::FilePath path; |
+ FileError error = ValidatePath(raw_path, directory_path_, &path); |
+ if (error != FileError::OK) { |
+ callback.Run(error); |
+ return; |
+ } |
+ |
+ if (base::DirectoryExists(path)) { |
+ callback.Run(FileError::NOT_A_FILE); |
+ return; |
+ } |
+ |
+ base::File base_file(path, |
+ base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
+ if (!base_file.IsValid()) { |
+ callback.Run(GetError(base_file)); |
+ return; |
+ } |
+ |
+ // If we're given empty data, we don't write and just truncate the file. |
+ if (data.size()) { |
+ if (base_file.Write(0, reinterpret_cast<char*>(&data.front()), |
+ data.size()) == -1) { |
+ callback.Run(GetError(base_file)); |
+ return; |
+ } |
+ } |
+ |
+ callback.Run(FileError::OK); |
+} |
+ |
} // namespace filesystem |