Ho una funzione che scarica un file e lo salva in una struttura di directory annidata sulla base di parametri inviati ad esso (es: ./somedir/a/b/c o ./ somedir2/a/d/b). Non riesco a credere che qualsiasi directory lungo il percorso è stato realizzato in modo da ho bisogno di avere ogni directory lungo il percorso file controllato e creato se non esiste. Ora, ho qualche codice che funziona perfettamente per Node 0.4.x, ma qualcosa si è rotto in almeno la versione per Windows del Nodo 0.5.x (testato su 0.5.10 specifico).ricorsiva creazione di directory per Node.js 0.5.x
Sono terribile con la comprensione dei filesystem, quindi se qualcuno riuscisse a capire come potrei fare questo lavoro o sostituirlo con qualcos'altro che funziona allo stesso modo, lo apprezzerei molto. L'obiettivo è quello di avere un codice che funzioni correttamente sia su Unix che su Windows, come pure sul nodo 0.4.xe 0.5.x.
// automatically create directories if they do not exist at a path
function mkdirs(_path, mode, callback) {
var dirs = _path.split("/");
var walker = [dirs.shift()];
var walk = function (ds, acc, m, cb) {
if (ds.length > 0) {
var d = ds.shift();
acc.push(d);
var dir = acc.join("/");
fs.stat(dir, function (err, stat) {
if (err) {
// file does not exist
if (err.errno == 2) {
fs.mkdir(dir, m, function (erro) {
if (erro && erro.errno != 17) {
terminal.error(erro, "Failed to make " + dir);
return cb(new Error("Failed to make " + dir + "\n" + erro));
} else {
return walk(ds, acc, m, cb);
}
});
} else {
return cb(err);
}
} else {
if (stat.isDirectory()) {
return walk(ds, acc, m, cb);
} else {
return cb(new Error("Failed to mkdir " + dir + ": File exists\n"));
}
}
});
} else {
return cb();
}
};
return walk(dirs, walker, mode, callback);
};
Esempio di utilizzo:
mkdirs('/path/to/file/directory/', 0777, function(err){
EDIT: Aggiornamento per Nodo 0.8.x (in CoffeeScript):
#
# Function mkdirs
# Ensures all directories in a path exist by creating those that don't
# @params
# path: string of the path to create (directories only, no files!)
# mode: the integer permission level
# callback: the callback to be used when complete
# @callback
# an error object or false
#
mkdirs = (path, mode, callback) ->
tryDirectory = (dir, cb) ->
fs.stat dir, (err, stat) ->
if err #the file doesn't exist, try one stage earlier then create
if err.errno is 2 or err.errno is 32 or err.errno is 34
if dir.lastIndexOf("/") is dir.indexOf("/") #only slash remaining is initial slash
#should only be triggered when path is '/' in Unix, or 'C:/' in Windows
cb new Error("notfound")
else
tryDirectory dir.substr(0, dir.lastIndexOf("/")), (err) ->
if err #error, return
cb err
else #make this directory
fs.mkdir dir, mode, (error) ->
if error and error.errno isnt 17
cb new Error("failed")
else
cb()
else #unkown error
cb err
else
if stat.isDirectory() #directory exists, no need to check previous directories
cb()
else #file exists at location, cannot make folder
cb new Error("exists")
path = (if path.indexOf("\\") >= 0 then path.replace("\\", "/") else path) #change windows slashes to unix
path = path.substr(0, path.length - 1) if path.substr(path.length - 1) is "/" #remove trailing slash
tryDirectory path, callback
Ho provato con logica diversa suddivisione determinato se il sistema operativo è Windows 'finestre var = _path.indexOf ('\\')> = 0; ', ma sto ancora ottenendo errori ENOENT. Da quello che posso dire, fs.stat restituisce un errore con errno 32, che causa il fallimento del codice. – geoffreak
Dopo aver fatto ulteriori ricerche, sembra che qualcosa non funzioni su fs.stat, quindi ho inviato un [problema su github] (https://github.com/joyent/node/issues/1927). – geoffreak