Bug 793682 - [OS.File] getCurrentDirectory/setCurrentDirectory for the main thread. r=Yoric
authorWangJun <w1990ww1990w@gmail.com>
Mon, 15 Oct 2012 23:05:44 +0800
changeset 110869 d0916aa78e099f9600d0b53ddfb3802a04fae00c
parent 110868 75058222ece680a62aad4e708556aa7631e7fc32
child 110870 977d1735104901918e054f035755364662a73d70
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersYoric
bugs793682
milestone19.0a1
Bug 793682 - [OS.File] getCurrentDirectory/setCurrentDirectory for the main thread. r=Yoric
toolkit/components/osfile/osfile_async_worker.js
toolkit/components/osfile/osfile_unix_front.jsm
toolkit/components/osfile/osfile_win_front.jsm
toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
--- a/toolkit/components/osfile/osfile_async_worker.js
+++ b/toolkit/components/osfile/osfile_async_worker.js
@@ -176,20 +176,20 @@ if (this.Components) {
       */
      let Agent = {
        // Functions of OS.File
        stat: function stat(path) {
          return exports.OS.File.Info.toMsg(
            exports.OS.File.stat(Type.path.fromMsg(path)));
        },
        getCurrentDirectory: function getCurrentDirectory() {
-         return exports.OS.Shared.Type.path.toMsg(exports.OS.File.curDir);
+         return exports.OS.Shared.Type.path.toMsg(File.getCurrentDirectory());
        },
        setCurrentDirectory: function setCurrentDirectory(path) {
-         exports.OS.File.curDir = exports.OS.Shared.Type.path.fromMsg(path);
+         File.setCurrentDirectory(exports.OS.Shared.Type.path.fromMsg(path));
        },
        copy: function copy(sourcePath, destPath, options) {
          return File.copy(Type.path.fromMsg(sourcePath),
            Type.path.fromMsg(destPath), options);
        },
        move: function move(sourcePath, destPath, options) {
          return File.move(Type.path.fromMsg(sourcePath),
            Type.path.fromMsg(destPath), options);
--- a/toolkit/components/osfile/osfile_unix_front.jsm
+++ b/toolkit/components/osfile/osfile_unix_front.jsm
@@ -841,29 +841,43 @@
        }
        return new File.Info(gStatData);
      };
 
      File.read = exports.OS.Shared.AbstractFile.read;
      File.writeAtomic = exports.OS.Shared.AbstractFile.writeAtomic;
 
      /**
+      * Get the current directory by getCurrentDirectory.
+      */
+     File.getCurrentDirectory = function getCurrentDirectory() {
+       let path = UnixFile.get_current_dir_name?UnixFile.get_current_dir_name():
+         UnixFile.getwd_auto(null);
+       throw_on_null("getCurrentDirectory",path);
+       return path.readString();
+     };
+
+     /**
+      * Set the current directory by setCurrentDirectory.
+      */
+     File.setCurrentDirectory = function setCurrentDirectory(path) {
+       throw_on_negative("setCurrentDirectory",
+         UnixFile.chdir(path)
+       );
+     };
+
+     /**
       * Get/set the current directory.
       */
      Object.defineProperty(File, "curDir", {
          set: function(path) {
-           throw_on_negative("curDir",
-             UnixFile.chdir(path)
-           );
+           this.setCurrentDirectory(path);
          },
          get: function() {
-           let path = UnixFile.get_current_dir_name?UnixFile.get_current_dir_name():
-             UnixFile.getwd_auto(null);
-           throw_on_null("curDir",path);
-           return path.readString();
+           return this.getCurrentDirectory();
          }
        }
      );
 
      // Utility functions
 
      /**
       * Turn the result of |open| into an Error or a File
--- a/toolkit/components/osfile/osfile_win_front.jsm
+++ b/toolkit/components/osfile/osfile_win_front.jsm
@@ -837,54 +837,67 @@
        winFlags: OS.Constants.Win.FILE_FLAG_BACKUP_SEMANTICS,
        winDisposition: OS.Constants.Win.OPEN_EXISTING
      };
 
      File.read = exports.OS.Shared.AbstractFile.read;
      File.writeAtomic = exports.OS.Shared.AbstractFile.writeAtomic;
 
      /**
-      * Get/set the current directory.
+      * Get the current directory by getCurrentDirectory.
       */
-     Object.defineProperty(File, "curDir", {
-         set: function(path) {
-           throw_on_zero("set curDir",
-             WinFile.SetCurrentDirectory(path));
-         },
-         get: function() {
+     File.getCurrentDirectory = function getCurrentDirectory() {
            // This function is more complicated than one could hope.
            //
            // This is due to two facts:
            // - the maximal length of a path under Windows is not completely
            //  specified (there is a constant MAX_PATH, but it is quite possible
            //  to create paths that are much larger, see bug 744413);
            // - if we attempt to call |GetCurrentDirectory| with a buffer that
            //  is too short, it returns the length of the current directory, but
            //  this length might be insufficient by the time we can call again
            //  the function with a larger buffer, in the (unlikely byt possible)
            //  case in which the process changes directory to a directory with
            //  a longer name between both calls.
-
            let buffer_size = 4096;
            while (true) {
              let array = new (ctypes.ArrayType(ctypes.jschar, buffer_size))();
-             let expected_size = throw_on_zero("get curDir",
+         let expected_size = throw_on_zero("getCurrentDirectory",
                WinFile.GetCurrentDirectory(buffer_size, array)
              );
              if (expected_size <= buffer_size) {
                return array.readString();
              }
              // At this point, we are in a case in which our buffer was not
              // large enough to hold the name of the current directory.
              // Consequently
 
              // Note that, even in crazy scenarios, the loop will eventually
              // converge, as the length of the paths cannot increase infinitely.
              buffer_size = expected_size;
            }
+     };
+
+     /**
+      * Set the current directory by setCurrentDirectory.
+      */
+     File.setCurrentDirectory = function setCurrentDirectory(path) {
+       throw_on_zero("setCurrentDirectory",
+         WinFile.SetCurrentDirectory(path));
+     };
+
+     /**
+      * Get/set the current directory by |curDir|.
+      */
+     Object.defineProperty(File, "curDir", {
+         set: function(path) {
+           this.setCurrentDirectory(path);
+         },
+         get: function() {
+           return this.getCurrentDirectory();
          }
        }
      );
 
      // Utility functions, used for error-handling
      function error_or_file(maybe) {
        if (maybe == exports.OS.Constants.Win.INVALID_HANDLE_VALUE) {
          throw new File.Error("open");
--- a/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
+++ b/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
@@ -436,17 +436,17 @@ function test_iter_dir()
 {
   ok(true, "test_iter_dir: Starting");
 
   // Create a file, to be sure that it exists
   let tmp_file_name = "test_osfile_front.tmp";
   let tmp_file = OS.File.open(tmp_file_name, {write: true, trunc:true});
   tmp_file.close();
 
-  let parent = OS.File.curDir;
+  let parent = OS.File.getCurrentDirectory();
   ok(true, "test_iter_dir: directory " + parent);
   let iterator = new OS.File.DirectoryIterator(parent);
   ok(true, "test_iter_dir: iterator created");
   let encountered_tmp_file = false;
   for (let entry in iterator) {
     // Checking that |name| can be decoded properly
     ok(true, "test_iter_dir: encountering entry " + entry.name);
 
@@ -666,17 +666,17 @@ function test_info() {
      "test_info: file 2 was accessed between the start of the test and now - " + start + ", " + stop + ", " + access);
 
   change = info.lastModificationDate;
   ok(change.getTime() >= startMs
      && change.getTime() <= stopMs,
      "test_info: file 2 has changed between the start of the test and now - " + start + ", " + stop + ", " + change);
 
   // Test OS.File.stat on directory
-  info = OS.File.stat(OS.File.curDir);
+  info = OS.File.stat(OS.File.getCurrentDirectory());
   ok(!!info, "test_info: info on directory acquired");
   ok(info.isDir, "test_info: directory is a directory");
 
   ok(true, "test_info: Complete");
 }
 
 function test_mkdir()
 {