[Filesystem] File.deleteFile JS part
authorPawel Kaczmarek <p.kaczmarek3@samsung.com>
Tue, 24 Feb 2015 10:49:50 +0000 (11:49 +0100)
committerPawel Sikorski <p.sikorski@samsung.com>
Tue, 24 Feb 2015 14:17:52 +0000 (23:17 +0900)
[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 <p.kaczmarek3@samsung.com>
src/filesystem/js/common.js
src/filesystem/js/file.js

index 294221be4b11a1600ec6df07588c2a81708f68cf..8004bbc9b57eab0ed0bde1433255987f5e34a985 100644 (file)
@@ -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://',
index 9282013c216cf537193df93548177a513c400f79..a60fee6298727f29ea10c757ff3f1b23b71fed8f 100644 (file)
@@ -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);
 };