Bug 770538 - Call Unix C function |dirfd| from JavaScript. r=dteller
authorMitesh Pathak <miteshpathak05@gmail.com>
Fri, 12 Oct 2012 00:19:17 +0530
changeset 111905 ea6edd3749cefe0557fe3854ffc55cf6108aca7d
parent 111904 5d062234521ade91a3414d5040fdcec10b5cff0d
child 111906 354250ed1e72526efa01c9830852152aadd1c58c
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersdteller
bugs770538
milestone19.0a1
Bug 770538 - Call Unix C function |dirfd| from JavaScript. r=dteller
toolkit/components/osfile/osfile_unix_back.jsm
toolkit/components/osfile/osfile_unix_front.jsm
toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
--- a/toolkit/components/osfile/osfile_unix_back.jsm
+++ b/toolkit/components/osfile/osfile_unix_back.jsm
@@ -236,16 +236,21 @@
                     /*state*/  Types.void_t.in_ptr, // Ignored atm
                     /*flags*/  Types.uint32_t);
 
        UnixFile.dup =
          declareFFI("dup", ctypes.default_abi,
                     /*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,
                     /*path*/   Types.path);
 
        UnixFile.fchdir =
          declareFFI("fchdir", ctypes.default_abi,
                     /*return*/ Types.negativeone_or_nothing,
--- a/toolkit/components/osfile/osfile_unix_front.jsm
+++ b/toolkit/components/osfile/osfile_unix_front.jsm
@@ -646,16 +646,24 @@
       */
      File.DirectoryIterator.prototype.close = function close() {
        if (!this._dir) return;
        UnixFile.closedir(this._dir);
        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.
       */
      File.DirectoryIterator.Entry = function Entry(unix_entry, parent) {
        // Copy the relevant part of |unix_entry| to ensure that
        // our data is not overwritten prematurely.
        this._d_type = unix_entry.d_type;
        this._name = unix_entry.d_name.readString();
        this._parent = parent;
--- a/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
+++ b/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
@@ -548,16 +548,38 @@ function test_iter_dir()
         iterator.close();
       } else {
         ok(false, "test_iter_dir: Checking that forEach can be stopped early");
       }
       ++index;
   });
   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");
 }
 
 function test_position() {
   ok(true, "test_position: Starting");
 
   ok("POS_START" in OS.File, "test_position: POS_START exists");
   ok("POS_CURRENT" in OS.File, "test_position: POS_CURRENT exists");