From: Arkadiusz Pietraszek Date: Wed, 18 Apr 2018 07:47:53 +0000 (+0200) Subject: [Filesystem] Removed old methods and refactoring of argument. X-Git-Tag: submit/tizen/20180518.121229~11^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fc0f1cd5b8e8ffd8b4c53ea3753245b775c1c665;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Filesystem] Removed old methods and refactoring of argument. Methods removed: string_to_array array_to_string Refactored argument _rewrite to _truncate for consistency. ACR: http://suprem.sec.samsung.net/jira/browse/TWDAPI-121 Change-Id: I6b22bc8b65bc1e967a18c60c2416bb7519eb43b1 Signed-off-by: Szymon Jastrzebski Signed-off-by: Arkadiusz Pietraszek Signed-off-by: Jakub Skowron Signed-off-by: Pawel Wasowski --- diff --git a/src/filesystem/filesystem_instance.cc b/src/filesystem/filesystem_instance.cc index 0747aa97..537d7c21 100644 --- a/src/filesystem/filesystem_instance.cc +++ b/src/filesystem/filesystem_instance.cc @@ -237,10 +237,10 @@ static std::vector read_file(std::string path, std::size_t offset, * On failure throws std::runtime_error */ void write_file(const std::uint8_t* data, std::size_t len, std::string path, std::size_t offset, - bool rewrite) { + bool truncate) { ScopeLogger(); - FILE* file = fopen(path.c_str(), rewrite ? "w" : "r+"); + FILE* file = fopen(path.c_str(), truncate ? "w" : "r+"); if (!file) { throw std::runtime_error("cannot open file to write"); @@ -396,12 +396,12 @@ void FilesystemInstance::FileWriteString(const picojson::value& args, picojson:: CHECK_EXIST(args, "location", out) CHECK_EXIST(args, "data", out) CHECK_EXIST(args, "offset", out) - CHECK_EXIST(args, "rewrite", out) + CHECK_EXIST(args, "truncate", out) const std::string& location = args.get("location").get(); const std::string& str = args.get("data").get(); size_t offset = static_cast(args.get("offset").get()); - bool rewrite = static_cast(args.get("rewrite").get()); + bool truncate = static_cast(args.get("truncate").get()); const std::string& encoding = args.contains("encoding") ? args.get("encoding").get() : "utf-8"; @@ -409,11 +409,11 @@ void FilesystemInstance::FileWriteString(const picojson::value& args, picojson:: if (encoding == "iso-8859-1") { std::vector data; latin1::from_utf8(str, data); - write_file(data.data(), data.size(), location, offset, rewrite); + write_file(data.data(), data.size(), location, offset, truncate); } else { // default: UTF-8 const std::uint8_t* buf = (const std::uint8_t*)str.c_str(); std::size_t len = str.length(); - write_file(buf, len, location, offset, rewrite); + write_file(buf, len, location, offset, truncate); } } catch (std::runtime_error& e) { LoggerE("Cannot write to file %s, cause: %s", location.c_str(), e.what()); @@ -430,17 +430,17 @@ void FilesystemInstance::FileWriteBytes(const picojson::value& args, picojson::o CHECK_EXIST(args, "location", out) CHECK_EXIST(args, "data", out) CHECK_EXIST(args, "offset", out) - CHECK_EXIST(args, "rewrite", out) + CHECK_EXIST(args, "truncate", out) const std::string& location = args.get("location").get(); const std::string& str = args.get("data").get(); size_t offset = static_cast(args.get("offset").get()); - bool rewrite = static_cast(args.get("rewrite").get()); + bool truncate = static_cast(args.get("truncate").get()); try { std::vector data; decode_binary_from_string(str, data); - write_file(data.data(), data.size(), location, offset, rewrite); + write_file(data.data(), data.size(), location, offset, truncate); } catch (std::runtime_error& e) { LoggerE("Cannot write to %s, cause: %s", location.c_str(), e.what()); PrepareError(FilesystemError::Other, out); @@ -456,12 +456,12 @@ void FilesystemInstance::FileWriteBase64(const picojson::value& args, picojson:: CHECK_EXIST(args, "location", out) CHECK_EXIST(args, "data", out) CHECK_EXIST(args, "offset", out) - CHECK_EXIST(args, "rewrite", out) + CHECK_EXIST(args, "truncate", out) const std::string& location = args.get("location").get(); const std::string& str = args.get("data").get(); size_t offset = static_cast(args.get("offset").get()); - bool rewrite = static_cast(args.get("rewrite").get()); + bool truncate = static_cast(args.get("truncate").get()); std::vector data; try { @@ -473,7 +473,7 @@ void FilesystemInstance::FileWriteBase64(const picojson::value& args, picojson:: } try { - write_file(data.data(), data.size(), location, offset, rewrite); + write_file(data.data(), data.size(), location, offset, truncate); ReportSuccess(picojson::value{(double)data.size()}, out); } catch (std::runtime_error& e) { LoggerE("Cannot write to %s, cause: %s", location.c_str(), e.what()); diff --git a/src/filesystem/js/file_stream.js b/src/filesystem/js/file_stream.js index 92194a13..9d391bbd 100644 --- a/src/filesystem/js/file_stream.js +++ b/src/filesystem/js/file_stream.js @@ -25,8 +25,7 @@ function FileStream(data, mode, encoding) { get: function() { return _totalBytes < _position; }, - set: function(v) { - }, + set: function(v) {}, enumerable: true }, position: { @@ -45,32 +44,15 @@ function FileStream(data, mode, encoding) { get: function() { return this.eof ? -1 : Math.max(0, _totalBytes - _position); }, - set: function(v) { - }, + set: function(v) {}, enumerable: true }, - _mode: { - value: mode, - writable: false, - enumerable: false - }, - _encoding: { - value: encoding, - writable: false, - enumerable: false - }, - _file: { - value: data, - writable: false, - enumerable: false - }, - _closed: { - value: false, - writable: true, - enumerable: false - }, - _rewrite: { - value: mode === 'w' ? true : false, + _mode: {value: mode, writable: false, enumerable: false}, + _encoding: {value: encoding, writable: false, enumerable: false}, + _file: {value: data, writable: false, enumerable: false}, + _closed: {value: false, writable: true, enumerable: false}, + _truncate: { + value: mode === 'w', // 'w' truncates file to zero length writable: true, enumerable: false } @@ -85,7 +67,7 @@ function _checkClosed(stream) { function closeFileStream() { this._closed = true; -}; +} FileStream.prototype.close = function() { closeFileStream.apply(this, arguments); @@ -103,51 +85,27 @@ function _checkWriteAccess(mode) { } } -/* returns array of numbers */ -function string_to_array( str ) { - var output = []; - var len = str.length; - for( var i = 0; i < len; i++ ) { - output.push( str.charCodeAt(i) ); - } - return output; -} - -/* receives array of numbers, returns string */ -function array_to_string( data ) { - var output = ""; - var len = data.length; - for( var i = 0; i < len; i++ ) { - output += String.fromCharCode(data[i] & 0xFF); // conversion to octet - } - return output; -} - function read() { - var args = validator_.validateArgs(arguments, [ - { - name: 'charCount', - type: types_.LONG - } - ]); + var args = validator_.validateArgs(arguments, [{name: 'charCount', type: types_.LONG}]); _checkClosed(this); _checkReadAccess(this._mode); if (!arguments.length) { - throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, - 'Argument "charCount" missing'); + throw new WebAPIException( + WebAPIException.INVALID_VALUES_ERR, 'Argument "charCount" missing'); } if (!type_.isNumber(args.charCount)) { - throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR, - 'Argument "charCount" must be a number'); + throw new WebAPIException( + WebAPIException.TYPE_MISMATCH_ERR, 'Argument "charCount" must be a number'); } if (args.charCount <= 0) { - throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, + throw new WebAPIException( + WebAPIException.INVALID_VALUES_ERR, 'Argument "charCount" must be greater than 0'); } - if(this.eof) { - throw new WebAPIException(WebAPIException.IO_ERR, 'Stream is marked as EOF.'); + if (this.eof) { + throw new WebAPIException(WebAPIException.IO_ERR, 'Stream is marked as EOF.'); } var _count = this.bytesAvailable; @@ -170,29 +128,25 @@ function read() { this.position += outData.length; can_change_size = false; } else { - this.position += 1; // Set EOF + this.position += 1; // Set EOF } return outData; -}; +} FileStream.prototype.read = function() { return read.apply(this, arguments); }; function readBytes() { - var args = validator_.validateArgs(arguments, [ - { - name: 'byteCount', - type: types_.LONG - } - ]); + var args = validator_.validateArgs(arguments, [{name: 'byteCount', type: types_.LONG}]); _checkClosed(this); _checkReadAccess(this._mode); if (args.byteCount <= 0) { - throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, + throw new WebAPIException( + WebAPIException.INVALID_VALUES_ERR, 'Argument "byteCount" must be greater than 0'); } @@ -209,18 +163,18 @@ function readBytes() { throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, 'Could not read'); } - var decoded = string_to_array( native_.getResultObject(result) ); + var decoded = StringToArray(native_.getResultObject(result), Array); if (decoded.length) { can_change_size = true; this.position += decoded.length; can_change_size = false; } else { - this.position += 1; // Set EOF + this.position += 1; // Set EOF } return decoded; -}; +} FileStream.prototype.readBytes = function() { return readBytes.apply(this, arguments); @@ -228,31 +182,29 @@ FileStream.prototype.readBytes = function() { FileStream.prototype.readBase64 = function() { return base64_encode(readBytes.apply(this, arguments)); -} +}; -function check_characters_outside_latin1( str ) { +function check_characters_outside_latin1(str) { var len = str.length; - for( var i = 0; i < len; ++i ) { - if( str.charCodeAt(i) > 255 ) { - throw new WebAPIException(WebAPIException.IO_ERR, 'Invalid character at '+i+': '+str.charAt(i)+' (not ISO-8859-1)'); + for (var i = 0; i < len; ++i) { + if (str.charCodeAt(i) > 255) { + throw new WebAPIException( + WebAPIException.IO_ERR, + 'Invalid character at ' + i + ': ' + str.charAt(i) + ' (not ISO-8859-1)'); } } } function write() { - var args = validator_.validateArgs(arguments, [ - { - name: 'stringData', - type: types_.STRING - } - ]); + var args = + validator_.validateArgs(arguments, [{name: 'stringData', type: types_.STRING}]); _checkClosed(this); _checkWriteAccess(this._mode); if (!arguments.length) { - throw new WebAPIException(WebAPIException.NOT_FOUND_ERR, - 'Argument "stringData" missing'); + throw new WebAPIException( + WebAPIException.NOT_FOUND_ERR, 'Argument "stringData" missing'); } var data = { @@ -260,10 +212,10 @@ function write() { encoding: this._encoding, offset: this.position, data: args.stringData, - rewrite: this._rewrite + truncate: this._truncate }; - if( data.encoding == "iso-8859-1") { + if (data.encoding == 'iso-8859-1') { check_characters_outside_latin1(data.data); } @@ -275,35 +227,35 @@ function write() { can_change_size = true; this.position = this.position + args.stringData.length; can_change_size = false; - this._rewrite = false; -}; + this._truncate = false; +} FileStream.prototype.write = function() { write.apply(this, arguments); }; function writeBytes() { - var args = validator_.validateArgs(arguments, [ - { - name: 'byteData', - type: types_.ARRAY, - values: undefined /* was types_.OCTET, but checking moved to array_to_string for performance */ - } - ]); + var args = validator_.validateArgs( + arguments, [{ + name: 'byteData', + type: types_.ARRAY, + values: undefined /* was types_.OCTET, but checking moved to ArrayToString for + performance */ + }]); _checkClosed(this); _checkWriteAccess(this._mode); if (!arguments.length) { - throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR, - 'Argument "byteData" missing'); + throw new WebAPIException( + WebAPIException.TYPE_MISMATCH_ERR, 'Argument "byteData" missing'); } var data = { location: commonFS_.toRealPath(this._file.fullPath), offset: this.position, - data: array_to_string(args.byteData), - rewrite: this._rewrite, + data: ArrayToString(args.byteData), + truncate: this._truncate, }; var result = native_.callSync('File_writeBytes', data); @@ -314,20 +266,16 @@ function writeBytes() { can_change_size = true; this.position = this.position + args.byteData.length; can_change_size = false; - this._rewrite = false; -}; + this._truncate = false; +} FileStream.prototype.writeBytes = function() { writeBytes.apply(this, arguments); }; function writeBase64() { - var args = validator_.validateArgs(arguments, [ - { - name: 'base64Data', - type: types_.STRING - } - ]); + var args = + validator_.validateArgs(arguments, [{name: 'base64Data', type: types_.STRING}]); _checkClosed(this); _checkWriteAccess(this._mode); @@ -336,13 +284,13 @@ function writeBase64() { location: commonFS_.toRealPath(this._file.fullPath), offset: this.position, data: args.base64Data, - rewrite: this._rewrite, + truncate: this._truncate, }; var result = native_.callSync('File_writeBase64', data); if (native_.isFailure(result)) { - throw native_.getErrorObject(result); + throw native_.getErrorObject(result); } var written_bytes = native_.getResultObject(result); @@ -350,8 +298,8 @@ function writeBase64() { can_change_size = true; this.position += written_bytes; can_change_size = false; - this._rewrite = false; -}; + this._truncate = false; +} FileStream.prototype.writeBase64 = function() { writeBase64.apply(this, arguments);