module: use UNC paths when loading native addons
authorJustin Chase <justin@evolvelabs.com>
Sat, 19 Sep 2015 22:03:32 +0000 (17:03 -0500)
committerJames M Snell <jasnell@gmail.com>
Thu, 8 Oct 2015 03:39:15 +0000 (20:39 -0700)
When using require to load a native addon the path must be converted
into a long path, otherwise the addon will fail to be loaded on
windows if the path is longer than 260 characters.

PR-URL: https://github.com/nodejs/node/pull/2965
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Benjamin Gruenbaum <inglor@gmail.com>
lib/module.js
test/addons/load-long-path/binding.cc [new file with mode: 0644]
test/addons/load-long-path/binding.gyp [new file with mode: 0644]
test/addons/load-long-path/test.js [new file with mode: 0644]

index b1091ca..90ae605 100644 (file)
@@ -456,7 +456,9 @@ Module._extensions['.json'] = function(module, filename) {
 
 
 //Native extension for .node
-Module._extensions['.node'] = process.dlopen;
+Module._extensions['.node'] = function(module, filename) {
+  return process.dlopen(module, path._makeLong(filename));
+};
 
 
 // bootstrap main module.
diff --git a/test/addons/load-long-path/binding.cc b/test/addons/load-long-path/binding.cc
new file mode 100644 (file)
index 0000000..cdf9904
--- /dev/null
@@ -0,0 +1,13 @@
+#include <node.h>
+#include <v8.h>
+
+void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
+  v8::Isolate* isolate = args.GetIsolate();
+  args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world"));
+}
+
+void init(v8::Local<v8::Object> target) {
+  NODE_SET_METHOD(target, "hello", Method);
+}
+
+NODE_MODULE(binding, init);
diff --git a/test/addons/load-long-path/binding.gyp b/test/addons/load-long-path/binding.gyp
new file mode 100644 (file)
index 0000000..3bfb844
--- /dev/null
@@ -0,0 +1,8 @@
+{
+  'targets': [
+    {
+      'target_name': 'binding',
+      'sources': [ 'binding.cc' ]
+    }
+  ]
+}
diff --git a/test/addons/load-long-path/test.js b/test/addons/load-long-path/test.js
new file mode 100644 (file)
index 0000000..1de2b85
--- /dev/null
@@ -0,0 +1,29 @@
+'use strict';
+const common = require('../../common');
+const fs = require('fs');
+const path = require('path');
+const assert = require('assert');
+
+common.refreshTmpDir();
+
+// make a path that is more than 260 chars long.
+// Any given folder cannot have a name longer than 260 characters,
+// so create 10 nested folders each with 30 character long names.
+var addonDestinationDir = path.resolve(common.tmpDir);
+
+for (var i = 0; i < 10; i++) {
+  addonDestinationDir = path.join(addonDestinationDir, 'x'.repeat(30));
+  fs.mkdirSync(addonDestinationDir);
+}
+
+const addonPath = path.join(__dirname, 'build', 'Release', 'binding.node');
+const addonDestinationPath = path.join(addonDestinationDir, 'binding.node');
+
+// Copy binary to long path destination
+var contents = fs.readFileSync(addonPath);
+fs.writeFileSync(addonDestinationPath, contents);
+
+// Attempt to load at long path destination
+var addon = require(addonDestinationPath);
+assert(addon != null);
+assert(addon.hello() == 'world');