From: Tomasz Marciniak Date: Mon, 13 Mar 2017 12:19:35 +0000 (+0100) Subject: [Filesystem] Fix for writing data with 'w' mode X-Git-Tag: submit/tizen_3.0/20170315.072531~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ef728910b19af6576c7f25f21444ffa923aac854;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Filesystem] Fix for writing data with 'w' mode [Feature] If stream is opened with 'w' mode and file exists its content is discarded before writing data. [Verification] Code compiles. TCT (Auto) pass rate 100% Change-Id: I5579b7f17175c39a8bf57b210a2a1ff29c1e7e2e Signed-off-by: Tomasz Marciniak --- diff --git a/src/filesystem/filesystem_file.cc b/src/filesystem/filesystem_file.cc index fc3d89c6..a12047e7 100644 --- a/src/filesystem/filesystem_file.cc +++ b/src/filesystem/filesystem_file.cc @@ -218,9 +218,16 @@ bool FilesystemFile::Read(FilesystemBuffer* data, return true; } -bool FilesystemFile::Write(const FilesystemBuffer& data, size_t offset) { +bool FilesystemFile::Write(const FilesystemBuffer& data, size_t offset, bool rewrite) { LoggerD("Enter"); - FILE* file = fopen(path.c_str(), "r+"); + + FILE* file = nullptr; + if (rewrite) { + file = fopen(path.c_str(), "w"); + } else { + file = fopen(path.c_str(), "r+"); + } + if (!file) { LoggerE("Cannot open file %s to write!", path.c_str()); return false; diff --git a/src/filesystem/filesystem_file.h b/src/filesystem/filesystem_file.h index 5779eafb..c7f2ece4 100644 --- a/src/filesystem/filesystem_file.h +++ b/src/filesystem/filesystem_file.h @@ -45,7 +45,7 @@ class FilesystemFile { FilesystemFile(const std::string& path_); bool Read(FilesystemBuffer* data, size_t offset, size_t length); - bool Write(const FilesystemBuffer& data, size_t offset); + bool Write(const FilesystemBuffer& data, size_t offset, bool rewrite); }; } // namespace filesystem diff --git a/src/filesystem/filesystem_instance.cc b/src/filesystem/filesystem_instance.cc index 54b0a2e1..47ffc97c 100644 --- a/src/filesystem/filesystem_instance.cc +++ b/src/filesystem/filesystem_instance.cc @@ -216,11 +216,13 @@ void FilesystemInstance::FileWrite(const picojson::value& args, CHECK_EXIST(args, "location", out) CHECK_EXIST(args, "data", out) CHECK_EXIST(args, "offset", out) + CHECK_EXIST(args, "rewrite", out) double callback_id = args.get("callbackId").get(); const std::string& location = args.get("location").get(); const std::string& data = args.get("data").get(); size_t offset = static_cast(args.get("location").get()); + bool rewrite = static_cast(args.get("rewrite").get()); auto onSuccess = [this, callback_id]() { LoggerD("enter"); @@ -247,6 +249,7 @@ void FilesystemInstance::FileWrite(const picojson::value& args, location, data, offset, + rewrite, onSuccess, onError)); } @@ -258,10 +261,12 @@ void FilesystemInstance::FileWriteSync(const picojson::value& args, CHECK_EXIST(args, "location", out) CHECK_EXIST(args, "data", out) CHECK_EXIST(args, "offset", out) + CHECK_EXIST(args, "rewrite", out) const std::string& location = args.get("location").get(); const std::string& data = args.get("data").get(); size_t offset = static_cast(args.get("offset").get()); + bool rewrite = static_cast(args.get("rewrite").get()); auto onSuccess = [this, &out]() { LoggerD("enter"); @@ -274,7 +279,7 @@ void FilesystemInstance::FileWriteSync(const picojson::value& args, }; FilesystemManager::GetInstance().FileWrite( - location, data, offset, onSuccess, onError); + location, data, offset, rewrite, onSuccess, onError); } void FilesystemInstance::FileStat(const picojson::value& args, diff --git a/src/filesystem/filesystem_manager.cc b/src/filesystem/filesystem_manager.cc index 468a095d..dc96e58e 100644 --- a/src/filesystem/filesystem_manager.cc +++ b/src/filesystem/filesystem_manager.cc @@ -412,6 +412,7 @@ void FilesystemManager::FileWrite( const std::string& path, const std::string& data, size_t offset, + bool rewrite, const std::function& success_cb, const std::function& error_cb) { @@ -425,7 +426,7 @@ void FilesystemManager::FileWrite( return; } - if (file.Write(buffer, offset)) { + if (file.Write(buffer, offset, rewrite)) { success_cb(); } else { LoggerE("Cannot write to file %s!", path.c_str()); diff --git a/src/filesystem/filesystem_manager.h b/src/filesystem/filesystem_manager.h index d455ed9a..b2060fb1 100644 --- a/src/filesystem/filesystem_manager.h +++ b/src/filesystem/filesystem_manager.h @@ -101,6 +101,7 @@ class FilesystemManager { void FileWrite(const std::string& path, const std::string& data, size_t offset, + bool rewrite, const std::function& success_cb, const std::function& error_cb); diff --git a/src/filesystem/js/file_stream.js b/src/filesystem/js/file_stream.js index 1d3f708b..221f87e1 100644 --- a/src/filesystem/js/file_stream.js +++ b/src/filesystem/js/file_stream.js @@ -68,6 +68,11 @@ function FileStream(data, mode, encoding) { value: false, writable: true, enumerable: false + }, + _rewrite: { + value: mode === 'w' ? true : false, + writable: true, + enumerable: false } }); } @@ -277,7 +282,8 @@ function write() { var data = { location: commonFS_.toRealPath(this._file.fullPath), offset: this.position, - data: Base64.encodeString(args.stringData) + data: Base64.encodeString(args.stringData), + rewrite: this._rewrite }; var result = native_.callSync('File_writeSync', data); @@ -288,6 +294,7 @@ function write() { can_change_size = true; this.position = this.position + args.stringData.length; can_change_size = false; + this._rewrite = false; }; FileStream.prototype.write = function() { @@ -314,7 +321,8 @@ function writeBytes() { var data = { location: commonFS_.toRealPath(this._file.fullPath), offset: this.position, - data: Base64.encode(args.byteData) + data: Base64.encode(args.byteData), + rewrite: this._rewrite }; var result = native_.callSync('File_writeSync', data); @@ -325,6 +333,7 @@ function writeBytes() { can_change_size = true; this.position = this.position + args.byteData.length; can_change_size = false; + this._rewrite = false; }; FileStream.prototype.writeBytes = function() { @@ -359,7 +368,8 @@ function writeBase64() { var data = { location: commonFS_.toRealPath(this._file.fullPath), offset: this.position, - data: args.base64Data + data: args.base64Data, + rewrite: this._rewrite }; var result = native_.callSync('File_writeSync', data); @@ -373,6 +383,7 @@ function writeBase64() { can_change_size = true; this.position += decoded.length; can_change_size = false; + this._rewrite = false; }; FileStream.prototype.writeBase64 = function() {