From 42ccb0040b211ad302ab6e05e250e3b2024e626d Mon Sep 17 00:00:00 2001 From: elsehow Date: Sun, 27 Nov 2016 15:47:58 -0800 Subject: [PATCH] Handle directories with odd names Previously, adding a directory with square brackets resulted in a strange, silent error [1]. This happened because directory names were being passed to a file- matching glob designed to recursively stream files from the directory. When filenames contained meaningful glob symbols (like [ or *), the glob matching would fail. This commit resolves this problem by escaping glob characters on filenames, before the filename is inserted into the main glob in src/get-file-streams.js L48. We add a dependency to glob-escape in the process, and a new test in test/ipfs-api/util.spec.js. --- package.json | 5 +++-- src/get-files-stream.js | 3 ++- test/fixtures/weird name folder [v0]/add | 22 +++++++++++++++++++ test/fixtures/weird name folder [v0]/cat | 18 +++++++++++++++ .../weird name folder [v0]/files/hello.txt | 1 + .../weird name folder [v0]/files/ipfs.txt | 1 + .../weird name folder [v0]/hello-link | 1 + test/fixtures/weird name folder [v0]/ipfs-add | 14 ++++++++++++ test/fixtures/weird name folder [v0]/ls | 18 +++++++++++++++ test/fixtures/weird name folder [v0]/version | 8 +++++++ test/ipfs-api/util.spec.js | 9 ++++++++ 11 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/weird name folder [v0]/add create mode 100644 test/fixtures/weird name folder [v0]/cat create mode 100644 test/fixtures/weird name folder [v0]/files/hello.txt create mode 100644 test/fixtures/weird name folder [v0]/files/ipfs.txt create mode 120000 test/fixtures/weird name folder [v0]/hello-link create mode 100755 test/fixtures/weird name folder [v0]/ipfs-add create mode 100644 test/fixtures/weird name folder [v0]/ls create mode 100644 test/fixtures/weird name folder [v0]/version diff --git a/package.json b/package.json index 84ce43b3f..5a29900f1 100644 --- a/package.json +++ b/package.json @@ -29,12 +29,13 @@ "detect-node": "^2.0.3", "flatmap": "0.0.3", "glob": "^7.1.1", + "glob-escape": "0.0.2", "ipfs-block": "^0.5.0", "ipld-dag-pb": "^0.9.0", "is-ipfs": "^0.2.1", "isstream": "^0.1.2", - "multiaddr": "^2.0.3", "lru-cache": "^4.0.1", + "multiaddr": "^2.0.3", "multipart-stream": "^2.0.1", "ndjson": "^1.4.3", "once": "^1.4.0", @@ -113,4 +114,4 @@ "url": "https://github.com/ipfs/js-ipfs-api/issues" }, "homepage": "https://github.com/ipfs/js-ipfs-api" -} \ No newline at end of file +} diff --git a/src/get-files-stream.js b/src/get-files-stream.js index 485e06cce..609c6e842 100644 --- a/src/get-files-stream.js +++ b/src/get-files-stream.js @@ -3,6 +3,7 @@ const isNode = require('detect-node') const Multipart = require('multipart-stream') const flatmap = require('flatmap') +const escape = require('glob-escape') function headers (file) { const name = file.path || '' @@ -44,7 +45,7 @@ function loadPaths (opts, file) { } if (stats.isDirectory() && opts.recursive) { - const mg = new glob.sync.GlobSync(`${file}/**/*`, { + const mg = new glob.sync.GlobSync(`${escape(file)}/**/*`, { follow: followSymlinks }) diff --git a/test/fixtures/weird name folder [v0]/add b/test/fixtures/weird name folder [v0]/add new file mode 100644 index 000000000..ce7fb7cc8 --- /dev/null +++ b/test/fixtures/weird name folder [v0]/add @@ -0,0 +1,22 @@ +'use strict' + +const ipfs = require('../src')('localhost', 5001) + +const f1 = 'Hello' +const f2 = 'World' + +ipfs.add([new Buffer(f1), new Buffer(f2)], function (err, res) { + if (err || !res) return console.log(err) + + for (let i = 0; i < res.length; i++) { + console.log(res[i]) + } +}) + +ipfs.add(['./files/hello.txt', './files/ipfs.txt'], function (err, res) { + if (err || !res) return console.log(err) + + for (let i = 0; i < res.length; i++) { + console.log(res[i]) + } +}) diff --git a/test/fixtures/weird name folder [v0]/cat b/test/fixtures/weird name folder [v0]/cat new file mode 100644 index 000000000..9bfc46638 --- /dev/null +++ b/test/fixtures/weird name folder [v0]/cat @@ -0,0 +1,18 @@ +'use strict' + +const ipfs = require('../src')('localhost', 5001) + +const hash = [ + 'QmdFyxZXsFiP4csgfM5uPu99AvFiKH62CSPDw5TP92nr7w', + 'QmY9cxiHqTFoWamkQVkpmmqzBrY3hCBEL2XNu3NtX74Fuu' +] + +ipfs.cat(hash, function (err, res) { + if (err || !res) return console.log(err) + + if (res.readable) { + res.pipe(process.stdout) + } else { + console.log(res) + } +}) diff --git a/test/fixtures/weird name folder [v0]/files/hello.txt b/test/fixtures/weird name folder [v0]/files/hello.txt new file mode 100644 index 000000000..e965047ad --- /dev/null +++ b/test/fixtures/weird name folder [v0]/files/hello.txt @@ -0,0 +1 @@ +Hello diff --git a/test/fixtures/weird name folder [v0]/files/ipfs.txt b/test/fixtures/weird name folder [v0]/files/ipfs.txt new file mode 100644 index 000000000..95a311652 --- /dev/null +++ b/test/fixtures/weird name folder [v0]/files/ipfs.txt @@ -0,0 +1 @@ +IPFS diff --git a/test/fixtures/weird name folder [v0]/hello-link b/test/fixtures/weird name folder [v0]/hello-link new file mode 120000 index 000000000..50b07e9e7 --- /dev/null +++ b/test/fixtures/weird name folder [v0]/hello-link @@ -0,0 +1 @@ +files/hello.txt \ No newline at end of file diff --git a/test/fixtures/weird name folder [v0]/ipfs-add b/test/fixtures/weird name folder [v0]/ipfs-add new file mode 100755 index 000000000..962aa11fe --- /dev/null +++ b/test/fixtures/weird name folder [v0]/ipfs-add @@ -0,0 +1,14 @@ +#!/usr/bin/env node + +'use strict' + +const ipfs = require('../src')('localhost', 5001) +const files = process.argv.slice(2) + +ipfs.add(files, {recursive: true}, function (err, res) { + if (err || !res) return console.log(err) + + for (let i = 0; i < res.length; i++) { + console.log('added', res[i].Hash, res[i].Name) + } +}) diff --git a/test/fixtures/weird name folder [v0]/ls b/test/fixtures/weird name folder [v0]/ls new file mode 100644 index 000000000..c810fbead --- /dev/null +++ b/test/fixtures/weird name folder [v0]/ls @@ -0,0 +1,18 @@ +'use strict' + +const ipfs = require('../src')('localhost', 5001) + +const hash = ['QmdbHK6gMiecyjjSoPnfJg6iKMF7v6E2NkoBgGpmyCoevh'] + +ipfs.ls(hash, function (err, res) { + if (err || !res) return console.log(err) + + res.Objects.forEach(function (node) { + console.log(node.Hash) + + console.log('Links [%d]', node.Links.length) + node.Links.forEach(function (link, i) { + console.log('[%d]', i, link) + }) + }) +}) diff --git a/test/fixtures/weird name folder [v0]/version b/test/fixtures/weird name folder [v0]/version new file mode 100644 index 000000000..a792af011 --- /dev/null +++ b/test/fixtures/weird name folder [v0]/version @@ -0,0 +1,8 @@ +'use strict' + +const ipfs = require('../src')('localhost', 5001) + +ipfs.commands(function (err, res) { + if (err) throw err + console.log(res) +}) diff --git a/test/ipfs-api/util.spec.js b/test/ipfs-api/util.spec.js index c9820492f..e2e9dede2 100644 --- a/test/ipfs-api/util.spec.js +++ b/test/ipfs-api/util.spec.js @@ -51,6 +51,15 @@ describe('.util', () => { }) }) + it('.fsAdd a directory with an odd name', (done) => { + const filesPath = path.join(__dirname, '../fixtures/weird name folder [v0]') + ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => { + expect(err).to.not.exist + expect(result.length).to.be.above(8) + done() + }) + }) + it('.fsAdd a file', (done) => { const filePath = path.join(__dirname, '../fixtures/testfile.txt') ipfs.util.addFromFs(filePath, (err, result) => {