From 1b847ad9dc65d11a4c1842d98264a0fd332c492a Mon Sep 17 00:00:00 2001 From: WangJun Date: Mon, 15 Oct 2012 23:05:44 +0800 Subject: [PATCH] Bug 793682 - [OS.File] getCurrentDirectory/setCurrentDirectory for the main thread. r=Yoric --- .../components/osfile/osfile_async_worker.js | 4 +-- .../components/osfile/osfile_unix_front.jsm | 28 ++++++++++++----- .../components/osfile/osfile_win_front.jsm | 31 +++++++++++++------ .../tests/mochi/worker_test_osfile_front.js | 4 +-- 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/toolkit/components/osfile/osfile_async_worker.js b/toolkit/components/osfile/osfile_async_worker.js index df7719dc27a..032ada5cbe3 100644 --- a/toolkit/components/osfile/osfile_async_worker.js +++ b/toolkit/components/osfile/osfile_async_worker.js @@ -181,10 +181,10 @@ if (this.Components) { 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), diff --git a/toolkit/components/osfile/osfile_unix_front.jsm b/toolkit/components/osfile/osfile_unix_front.jsm index 5669a6b8a06..7c852b2c868 100644 --- a/toolkit/components/osfile/osfile_unix_front.jsm +++ b/toolkit/components/osfile/osfile_unix_front.jsm @@ -845,20 +845,34 @@ 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(); } } ); diff --git a/toolkit/components/osfile/osfile_win_front.jsm b/toolkit/components/osfile/osfile_win_front.jsm index 5734cad9e89..8fdb20b3094 100644 --- a/toolkit/components/osfile/osfile_win_front.jsm +++ b/toolkit/components/osfile/osfile_win_front.jsm @@ -842,14 +842,9 @@ 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: @@ -862,11 +857,10 @@ // 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) { @@ -880,6 +874,25 @@ // 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(); } } ); 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 3264b68ac84..202504b9080 100644 --- a/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js +++ b/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js @@ -441,7 +441,7 @@ function test_iter_dir() 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"); @@ -671,7 +671,7 @@ function test_info() { "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");