diff --git a/libs/fs.js b/libs/fs.js index 756d9c23..b7bc2eae 100644 --- a/libs/fs.js +++ b/libs/fs.js @@ -312,6 +312,40 @@ var fs = (function() { }); } + // Callers of this function should make sure + // newPath doesn't exist. + function rename(oldPath, newPath, cb) { + oldPath = normalizePath(oldPath); + newPath = normalizePath(newPath); + + list(oldPath, function(files) { + if (files != null && files.length > 0) { + cb(false); + return; + } + + asyncStorage.getItem(oldPath, function(data) { + if (data == null) { + cb(false); + return; + } + + remove(oldPath, function(removed) { + if (!removed) { + cb(false); + return; + } + + if (data instanceof Blob) { + create(newPath, data, cb); + } else { + mkdir(newPath, cb); + } + }); + }); + }); + } + return { dirname: dirname, init: init, @@ -331,5 +365,6 @@ var fs = (function() { mkdir: mkdir, mkdirp: mkdirp, size: size, + rename: rename, }; })(); diff --git a/midp/midp.js b/midp/midp.js index f6cc5d8e..5ad6090c 100644 --- a/midp/midp.js +++ b/midp/midp.js @@ -1374,11 +1374,34 @@ Native["com/ibm/oti/connection/file/Connection.newFileImpl.([B)I"] = function(ct throw VM.Pause; } +Native["com/ibm/oti/connection/file/Connection.deleteFileImpl.([B)Z"] = +Native["com/ibm/oti/connection/file/Connection.deleteDirImpl.([B)Z"] = function(ctx, stack) { + var path = util.decodeUtf8(stack.pop()); + fs.remove(path, function(removed) { + stack.push(removed ? 1 : 0); + ctx.resume(); + }); + throw VM.Pause; +} + +Native["com/ibm/oti/connection/file/Connection.isReadOnlyImpl.([B)Z"] = function(ctx, stack) { + var path = util.decodeUtf8(stack.pop()), _this = stack.pop(); + stack.push(0); +} + Native["com/ibm/oti/connection/file/Connection.isWriteOnlyImpl.([B)Z"] = function(ctx, stack) { var path = util.decodeUtf8(stack.pop()), _this = stack.pop(); stack.push(0); } +Native["com/ibm/oti/connection/file/Connection.renameImpl.([B[B)V"] = function(ctx, stack) { + var newPath = util.decodeUtf8(stack.pop()), oldPath = util.decodeUtf8(stack.pop()), _this = stack.pop(); + fs.rename(oldPath, newPath, function() { + ctx.resume(); + }); + throw VM.Pause; +} + Native["com/ibm/oti/connection/file/Connection.truncateImpl.([BJ)V"] = function(ctx, stack) { var newLength = stack.pop2().toNumber(), byteArray = stack.pop(), _this = stack.pop(); diff --git a/tests/automation.js b/tests/automation.js index 93593752..fa2a2fa8 100644 --- a/tests/automation.js +++ b/tests/automation.js @@ -35,7 +35,7 @@ casper.test.begin("unit tests", 6, function(test) { casper .thenOpen("http://localhost:8000/tests/fstests.html") .waitForText("DONE", function then() { - test.assertTextExists("DONE: 82 pass, 0 FAIL", "run fs.js unit tests"); + test.assertTextExists("DONE: 95 PASS, 0 FAIL", "run fs.js unit tests"); }); casper diff --git a/tests/fstests.js b/tests/fstests.js index 2b34a873..df3dc61d 100644 --- a/tests/fstests.js +++ b/tests/fstests.js @@ -26,10 +26,9 @@ function ok(a, msg) { var tests = []; function next() { -dump("next: " + tests.length); if (tests.length == 0) { ok(true, "TESTS COMPLETED"); - console.log("DONE: " + passed + " pass, " + failed + " FAIL"); + console.log("DONE: " + passed + " PASS, " + failed + " FAIL"); } else { var test = tests.shift(); test(); @@ -480,6 +479,69 @@ tests.push(function() { }); }); +tests.push(function() { + fs.rename("/tmp/tmp.txt", "/tmp/tmp2.txt", function(renamed) { + ok(renamed, "File renamed"); + next(); + }); +}); + +tests.push(function() { + fs.create("/file", new Blob([1,2,3,4]), function(created) { + ok(created, "File created"); + fs.rename("/file", "/file2", function(renamed) { + ok(renamed, "File renamed"); + fs.size("/file2", function(size) { + is(size, 4, "Renamed file size is correct"); + fs.exists("/file", function(exists) { + ok(!exists, "file doesn't exist anymore"); + next(); + }); + }); + }); + }); +}); + +tests.push(function() { + fs.mkdir("/newdir", function(created) { + ok(created, "Directory created"); + fs.rename("/newdir", "/newdir2", function(renamed) { + ok(renamed, "Directory renamed"); + fs.exists("/newdir", function(exists) { + ok(!exists, "newdir doesn't exist anymore"); + next(); + }); + }); + }); +}); + +tests.push(function() { + fs.rename("/tmp", "/tmp3", function(renamed) { + ok(!renamed, "Can't rename a non-empty directory"); + fs.exists("/tmp", function(exists) { + ok(exists, "Directory still exists after an error while renaming"); + next(); + }); + }); +}); + +tests.push(function() { + fs.rename("/tmp", "/newdir2", function(renamed) { + ok(!renamed, "Can't rename a directory with a path to a directory that already exists"); + fs.exists("/tmp", function(exists) { + ok(exists, "Directory still exists after an error while renaming"); + next(); + }); + }); +}); + +tests.push(function() { + fs.rename("/nonexisting", "/nonexising2", function(renamed) { + ok(!renamed, "Can't rename a non-existing file"); + next(); + }); +}) + asyncStorage.clear(function() { fs.init(function() { next();