From 7a9b4c9697b6a1af7aa658f0307963bf47123bcb Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Fri, 2 Dec 2011 00:24:44 +0100 Subject: [PATCH] Win: make process.cwd and chdir support non-ansi characters Closes GH-2215 --- src/node.cc | 25 +++++++++++++++---------- test/simple/test-chdir.js | 13 ++++++++++++- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/node.cc b/src/node.cc index 9189f8b255c0..44939ade2400 100644 --- a/src/node.cc +++ b/src/node.cc @@ -47,8 +47,6 @@ #include /* setuid, getuid */ #else #include -#define chdir _chdir -#define getcwd _getcwd #include #define getpid _getpid #include @@ -1231,10 +1229,10 @@ static Handle Chdir(const Arguments& args) { String::Utf8Value path(args[0]->ToString()); - int r = chdir(*path); + uv_err_t r = uv_chdir(*path); - if (r != 0) { - return ThrowException(Exception::Error(String::New(strerror(errno)))); + if (r.code != UV_OK) { + return ThrowException(UVException(r.code, "uv_chdir")); } return Undefined(); @@ -1243,18 +1241,25 @@ static Handle Chdir(const Arguments& args) { static Handle Cwd(const Arguments& args) { HandleScope scope; +#ifdef _WIN32 + /* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */ + char buf[MAX_PATH * 4 + 1]; +#else + char buf[PATH_MAX + 1]; +#endif - char *r = getcwd(getbuf, ARRAY_SIZE(getbuf) - 1); - if (r == NULL) { - return ThrowException(Exception::Error(String::New(strerror(errno)))); + uv_err_t r = uv_cwd(buf, ARRAY_SIZE(buf) - 1); + if (r.code != UV_OK) { + return ThrowException(UVException(r.code, "uv_cwd")); } - getbuf[ARRAY_SIZE(getbuf) - 1] = '\0'; - Local cwd = String::New(r); + buf[ARRAY_SIZE(buf) - 1] = '\0'; + Local cwd = String::New(buf); return scope.Close(cwd); } + #ifdef _WIN32 static Handle CwdForDrive(const Arguments& args) { HandleScope scope; diff --git a/test/simple/test-chdir.js b/test/simple/test-chdir.js index 4d0c52136ace..7454bee2d338 100644 --- a/test/simple/test-chdir.js +++ b/test/simple/test-chdir.js @@ -21,9 +21,20 @@ var common = require('../common'); var assert = require('assert'); +var fs = require('fs'); +var path = require('path'); assert.equal(true, process.cwd() !== __dirname); process.chdir(__dirname); - assert.equal(true, process.cwd() === __dirname); + +var dir = path.resolve(common.fixturesDir, + 'weird \uc3a4\uc3ab\uc3af characters \u00e1\u00e2\u00e3'); +fs.mkdirSync(dir); +process.chdir(dir); +assert(process.cwd() == dir); + +process.chdir('..'); +assert(process.cwd() == path.resolve(common.fixturesDir)); +fs.rmdirSync(dir);