From 44ca94fcc18d9e2cc358ec0e80b79900e9e7e92b Mon Sep 17 00:00:00 2001 From: Mitesh Pathak Date: Fri, 12 Oct 2012 00:19:17 +0530 Subject: [PATCH] Bug 770538 - Call Unix C function |dirfd| from JavaScript. r=dteller --- .../components/osfile/osfile_unix_back.jsm | 5 +++++ .../components/osfile/osfile_unix_front.jsm | 8 +++++++ .../tests/mochi/worker_test_osfile_front.js | 22 +++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/toolkit/components/osfile/osfile_unix_back.jsm b/toolkit/components/osfile/osfile_unix_back.jsm index 9c7ccb54065..7a56ac7ab0d 100644 --- a/toolkit/components/osfile/osfile_unix_back.jsm +++ b/toolkit/components/osfile/osfile_unix_back.jsm @@ -241,6 +241,11 @@ /*return*/ Types.negativeone_or_fd, /*fd*/ Types.fd); + UnixFile.dirfd = + declareFFI("dirfd", ctypes.default_abi, + /*return*/ Types.negativeone_or_fd, + /*dir*/ Types.null_or_DIR_ptr); + UnixFile.chdir = declareFFI("chdir", ctypes.default_abi, /*return*/ Types.negativeone_or_nothing, diff --git a/toolkit/components/osfile/osfile_unix_front.jsm b/toolkit/components/osfile/osfile_unix_front.jsm index 7c852b2c868..8ba43653299 100644 --- a/toolkit/components/osfile/osfile_unix_front.jsm +++ b/toolkit/components/osfile/osfile_unix_front.jsm @@ -650,6 +650,14 @@ this._dir = null; }; + /** + * Return directory as |File| + */ + File.DirectoryIterator.prototype.unixAsFile = function unixAsFile() { + if (!this._dir) throw File.Error.closed(); + return error_or_file(UnixFile.dirfd(this._dir)); + }; + /** * An entry in a directory. */ diff --git a/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js b/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js index 6c9406bbe7a..e745a428b11 100644 --- a/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js +++ b/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js @@ -553,6 +553,28 @@ function test_iter_dir() }); iterator.close(); + //test for prototype |OS.File.DirectoryIterator.unixAsFile| + if ("unixAsFile" in OS.File.DirectoryIterator.prototype) { + ok(true, "testing property unixAsFile"); + let path = OS.Path.join("chrome", "toolkit", "components", "osfile", "tests", "mochi"); + iterator = new OS.File.DirectoryIterator(path); + + let dir_file = iterator.unixAsFile();// return |File| + let stat0 = dir_file.stat(); + let stat1 = OS.File.stat(path); + + let unix_info_to_string = function unix_info_to_string(info) { + return "| " + info.unixMode + " | " + info.unixOwner + " | " + info.unixGroup + " | " + info.creationDate + " | " + info.lastModificationDate + " | " + info.lastAccessDate + " | " + info.size + " |"; + }; + + let s0_string = unix_info_to_string(stat0); + let s1_string = unix_info_to_string(stat1); + + ok(stat0.isDir, "unixAsFile returned a directory"); + is(s0_string, s1_string, "unixAsFile returned the correct file"); + dir_file.close(); + iterator.close(); + } ok(true, "test_iter_dir: Complete"); }