Skip to content

Commit

Permalink
Merge pull request mozilla#355 from mykmelez/create-truncate-file
Browse files Browse the repository at this point in the history
truncate files that exist but are empty; create files with Blob
  • Loading branch information
Marco committed Sep 27, 2014
2 parents 12d4c38 + a3e1a2a commit 182171c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
24 changes: 16 additions & 8 deletions libs/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,14 @@ var fs = (function() {
function truncate(path, cb) {
path = normalizePath(path);

asyncStorage.getItem(path, function(data) {
if (data == null || !(data instanceof Blob)) {
cb(false);
} else {
stat(path, function(stat) {
if (stat && !stat.isDir) {
asyncStorage.setItem(path, new Blob(), function() {
setStat(path, { mtime: Date.now(), isDir: false });
cb(true);
});
} else {
cb(false);
}
});
}
Expand Down Expand Up @@ -281,17 +281,25 @@ var fs = (function() {

function create(path, blob, cb) {
createInternal(path, blob, function(created) {
setStat(path, { mtime: Date.now(), isDir: false }, function() {
if (created) {
setStat(path, { mtime: Date.now(), isDir: false }, function() {
cb(created);
});
} else {
cb(created);
});
}
});
}

function mkdir(path, cb) {
createInternal(path, [], function(created) {
setStat(path, { mtime: Date.now(), isDir: true }, function() {
if (created) {
setStat(path, { mtime: Date.now(), isDir: true }, function() {
cb(created);
});
} else {
cb(created);
});
}
});
}

Expand Down
4 changes: 2 additions & 2 deletions midp/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ Native["com/ibm/oti/connection/file/FCOutputStream.openImpl.([B)I"] = function(c
}
});
} else {
fs.create(path, function(created) {
fs.create(path, new Blob(), function(created) {
if (created) {
open();
} else {
Expand Down Expand Up @@ -661,7 +661,7 @@ Native["com/ibm/oti/connection/file/FCOutputStream.openOffsetImpl.([BJ)I"] = fun
if (exists) {
open();
} else {
fs.create(path, function(created) {
fs.create(path, new Blob(), function(created) {
if (created) {
open();
} else {
Expand Down
4 changes: 2 additions & 2 deletions tests/automation.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ casper.test.begin("unit tests", 5 + gfxTests.length, function(test) {
casper
.start("http://localhost:8000/index.html")
.waitForText("DONE", function() {
test.assertTextExists("DONE: 4901 pass, 0 fail, 168 known fail, 0 unknown pass", "run unit tests");
test.assertTextExists("DONE: 4904 pass, 0 fail, 168 known fail, 0 unknown pass", "run unit tests");
});

casper
Expand All @@ -54,7 +54,7 @@ casper.test.begin("unit tests", 5 + gfxTests.length, function(test) {
casper
.thenOpen("http://localhost:8000/tests/fstests.html")
.waitForText("DONE", function() {
test.assertTextExists("DONE: 121 PASS, 0 FAIL", "run fs.js unit tests");
test.assertTextExists("DONE: 125 PASS, 0 FAIL", "run fs.js unit tests");
});

casper
Expand Down
12 changes: 12 additions & 0 deletions tests/com/ibm/oti/connection/file/TestFileConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,18 @@ public void test(TestHarness th) {
file.delete();
file.close();

// Opening an output stream to a nonexistent file should create it.
file = (FileConnection)Connector.open(dirPath + "provaDir/nonexistent.txt");
th.check(!file.exists());
// Create the stream ourselves because Connection.openOutputStream
// raises an exception when the file doesn't exist.
out = new FCOutputStream((file.getPath() + file.getName()).getBytes("UTF-8"), null);
th.check(file.exists());
th.check(file.fileSize(), 0, "Check file size");
out.close();
file.delete();
file.close();

dir = (FileConnection)Connector.open(dirPath + "provaDir");
dir.delete();
th.check(!dir.exists());
Expand Down
28 changes: 28 additions & 0 deletions tests/fstests.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,27 @@ tests.push(function() {
});
});

tests.push(function() {
fs.stat("/tmp", function(stat) {
ok(stat.isDir, "/tmp is a directory");
next();
});
});

tests.push(function() {
fs.create("/tmp", new Blob(), function(created) {
is(created, false, "can't create a file with the same path of an already existing directory");
next();
});
});

tests.push(function() {
fs.stat("/tmp", function(stat) {
ok(stat.isDir, "/tmp is still a directory");
next();
});
});

tests.push(function() {
fs.mkdir("/tmp", function(created) {
is(created, false, "can't create a directory with the same path of an already existing directory");
Expand All @@ -128,13 +142,27 @@ tests.push(function() {
});
});

tests.push(function() {
fs.stat("/tmp/tmp.txt", function(stat) {
ok(!stat.isDir, "/tmp/tmp.txt is not a directory");
next();
});
});

tests.push(function() {
fs.mkdir("/tmp/tmp.txt", function(created) {
is(created, false, "can't create a directory with the same path of an already existing file");
next();
});
});

tests.push(function() {
fs.stat("/tmp/tmp.txt", function(stat) {
ok(!stat.isDir, "/tmp/tmp.txt is still not a directory");
next();
});
});

tests.push(function() {
fs.size("/tmp/tmp.txt", function(size) {
is(size, 0, "newly created file's size is 0");
Expand Down

0 comments on commit 182171c

Please sign in to comment.