Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(539)

Unified Diff: components/filesystem/directory_impl.cc

Issue 1634293002: mojo filesystem: Simplify full file reading/writing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Further cleanup of pref store. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/filesystem/directory_impl.cc
diff --git a/components/filesystem/directory_impl.cc b/components/filesystem/directory_impl.cc
index 8c86fc7043bb47dd7f9c2a4b6a260f79277d9e28..4d2dc730b2b86d01ae22e0177c31220438ab3885 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]);
+ size_t len;
+ while ((len = base_file.ReadAtCurrentPos(buf.get(), kBufferSize)) > 0)
sky 2016/01/27 01:08:40 ReadAtCurrentPos returns an int.
+ 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

Powered by Google App Engine
This is Rietveld 408576698