From: Pawel Kaczmarek Date: Tue, 24 Feb 2015 10:49:50 +0000 (+0100) Subject: [Filesystem] File.deleteFile JS part X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~370 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3291659c9dc595a18ec0019f88983d704c594cfa;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Filesystem] File.deleteFile JS part [Verification] function onsuccess(files) { for (var i = 0; i < files.length; i++) { if (!files[i].isDirectory) { documentsDir.deleteFile( files[i].fullPath, function() { console.log("File Deleted"); }, function(e) { console.log("Error" + e.message); }); } } } function onerror(error) { console.log("The error " + error.message + " occurred when listing the files in the selected folder"); } var documentsDir; tizen.filesystem.resolve( 'images', function(dir) { documentsDir = dir; dir.listFiles(onsuccess,onerror); }, function(e) { console.log("Error" + e.message); }, "rw" ); //should remove all files from images directory [Depends on] http://168.219.209.56/gerrit/#/c/20599/ http://168.219.209.56/gerrit/#/c/20616/ - needed for verification Change-Id: Ic35270fe31760b6fbb52ff4ba74e686931d2f39a Signed-off-by: Pawel Kaczmarek --- diff --git a/src/filesystem/js/common.js b/src/filesystem/js/common.js index 294221be..8004bbc9 100644 --- a/src/filesystem/js/common.js +++ b/src/filesystem/js/common.js @@ -113,6 +113,21 @@ CommonFS.prototype.isLocationAllowed = function(aPath) { return true; }; +CommonFS.prototype.f_isSubDir = function(fullPathToCheck, fullPath) { + return (-1 !== fullPathToCheck.indexOf(this.toRealPath(fullPath))); +}; + +CommonFS.prototype.f_isCorrectRelativePath = function(relativePath) { + return ((-1 === relativePath.indexOf('/')) && + (-1 === relativePath.indexOf('\\')) && + (-1 === relativePath.indexOf('?')) && + (-1 === relativePath.indexOf('*')) && + (-1 === relativePath.indexOf(':')) && + (-1 === relativePath.indexOf('"')) && + (-1 === relativePath.indexOf('<')) && + (-1 === relativePath.indexOf('>'))); +}; + CommonFS.prototype.toRealPath = function(aPath) { var _fileRealPath = '', _uriPrefix = 'file://', diff --git a/src/filesystem/js/file.js b/src/filesystem/js/file.js index 9282013c..a60fee62 100644 --- a/src/filesystem/js/file.js +++ b/src/filesystem/js/file.js @@ -44,20 +44,7 @@ function File(data) { fileSize: {enumerable: true, set: function() { }, get: fileSizeGetter}, length: {value: data.length, writable: false, enumerable: true}, - mode: {value: data.mode, writable: false}, - f_isSubDir: {value: function(fullPathToCheck) { - return (-1 !== fullPathToCheck.indexOf(commonFS_.toRealPath(this.fullPath))); - }, writable: false}, - f_isCorrectRelativePath: {value: function(relativePath) { - return ((-1 === relativePath.indexOf('/')) && - (-1 === relativePath.indexOf('\\')) && - (-1 === relativePath.indexOf('?')) && - (-1 === relativePath.indexOf('*')) && - (-1 === relativePath.indexOf(':')) && - (-1 === relativePath.indexOf('"')) && - (-1 === relativePath.indexOf('<')) && - (-1 === relativePath.indexOf('>'))); - }} + mode: {value: data.mode, writable: false} }); } @@ -382,7 +369,7 @@ File.prototype.resolve = function(filePath) { 'File object which call this method is not directory'); } - if (!this.f_isCorrectRelativePath(args.filePath)) { + if (!commonFS_.f_isCorrectRelativePath(args.filePath)) { throw new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR, 'Invalid path'); } @@ -430,8 +417,48 @@ File.prototype.deleteFile = function(filePath, onsuccess, onerror) { {name: 'onerror', type: types_.FUNCTION, optional: true, nullable: true} ]); + if (this.isFile) { + setTimeout(function() { + native_.callIfPossible(args.onerror, + new tizen.WebAPIException(tizen.WebAPIException.IO_ERR, + 'File object which call this method is not directory')); + }, 0); + return; + } + + var _fileRealPath = commonFS_.toRealPath(args.filePath); + + try { + var _result = native_.callSync('File_statSync', {location: _fileRealPath}); + var _statObj = native_.getResultObject(_result); + if (_statObj.isDirectory) { + var message = 'Requested object is a directory.'; + setTimeout(function() { + native_.callIfPossible(args.onerror, + new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR, message)); + }, 0); + return; + } + } catch (err) { + setTimeout(function() { + native_.callIfPossible(args.onerror, + new tizen.WebAPIException(tizen.WebAPIException.NOT_FOUND_ERR, 'File is not avalaible')); + }, 0); + return; + } + + if (!commonFS_.f_isSubDir(_fileRealPath, this.fullPath) || this.mode === 'r') { + var _message = 'Deleted file [' + args.filePath + '] should have write access ' + + 'and should be subdirectory of: [' + this.fullPath + ']'; + setTimeout(function() { + native_.callIfPossible(args.onerror, + new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR, _message)); + }, 0); + return; + } + var data = { - filePath: args.filePath + pathToFile: _fileRealPath }; var callback = function(result) { @@ -442,5 +469,5 @@ File.prototype.deleteFile = function(filePath, onsuccess, onerror) { native_.callIfPossible(args.onsuccess); }; - native_.call('File_deleteFile', data, callback); + native_.call('File_unlinkFile', data, callback); };