-
Notifications
You must be signed in to change notification settings - Fork 16
/
fsDriver.js
193 lines (167 loc) · 4.25 KB
/
fsDriver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// fsDriver
// use fs engine to server files
var fs = require('fs');
var findit = require('findit');
var path = require('path');
var mv = require('mv');
var rm = require('rimraf');
var error = require('debug')('rest-fs:fsDriver');
// returns array of files and dir. trailing slash determines type.
var listAll = function(args, cb) {
var dirPath = args.dirPath;
var finder = findit(dirPath);
var files = [];
finder.on('directory', function (dir, stat, stop) {
files.push(path.join(dir, '/'));
});
finder.on('file', function (file, stat) {
files.push(file);
});
finder.on('end', function () {
cb(null, files);
});
};
// returns array of files and dir. trailing slash determines type.
var list = function(args, cb) {
var dirPath = args.dirPath;
var filesList = [];
var cnt = 0;
fs.readdir(dirPath, function (err, files) {
if (err) { return cb(err); }
if (files.length === 0) {
return cb(null, []);
}
var formatFileList = function(index) {
return function (err, stat) {
// here we do something special. if stat failes we know that there is something here
// but we might not have permissons. show it as a file.
if (err) {
error('lstat error', err.stack);
}
else {
var isDir = stat.isDirectory() ? '/' : '';
var file = path.join(dirPath, files[index], isDir);
filesList.push(file);
}
cnt++;
if (cnt === files.length) {
return cb(null, filesList);
}
};
};
for (var i = 0; i < files.length; i++) {
fs.lstat(path.join(dirPath, files[i]), formatFileList(i));
}
});
};
/*
read file from filepath
*/
var readFile = function(args, cb) {
var filePath = args.filePath;
var encoding = args.encoding;
fs.readFile(filePath, encoding, cb);
};
/*
mkdir
*/
var mkdir = function(args, cb) {
var dirPath = args.dirPath;
var mode = args.mode;
fs.mkdir(dirPath, mode, cb);
};
/*
delete directory
*/
var rmdir = function(args, cb) {
var dirPath = args.dirPath;
var clobber = args.clobber;
if (clobber) {
return rm(dirPath, cb);
}
return fs.rmdir(dirPath, cb);
};
/*
writeFile
*/
var writeFile = function(args, cb) {
var dirPath = args.dirPath;
var data = args.data;
var options = args.options;
fs.writeFile(dirPath, data, options, cb);
};
/*
write file with stream
*/
var writeFileStream = function(args, cb) {
var dirPath = args.dirPath;
var stream = args.stream;
var options = args.options;
var file = fs.createWriteStream(dirPath, options);
file.on('error', cb);
file.on('finish', function() {
cb();
});
stream.pipe(file);
};
/*
delete file
*/
var unlink = function(args, cb) {
var dirPath = args.dirPath;
fs.unlink(dirPath, cb);
};
/*
move file
*/
var move = function (args, cb) {
var oldPath = args.dirPath;
var newPath = args.newPath;
var opts = args.options;
// have to remove trailing slaches
if(oldPath.substr(-1) == '/') {
oldPath = oldPath.substr(0, oldPath.length - 1);
}
if(newPath.substr(-1) == '/') {
newPath = newPath.substr(0, newPath.length - 1);
}
// workaround for ncp for dirs. should error if we trying to mv into own dir
fs.stat(oldPath, function(err, stats) {
if (err) { return cb(err); }
if (stats.isDirectory() &&
~newPath.indexOf(oldPath) &&
newPath.split("/").length > oldPath.split("/").length) {
err = new Error('cannot move inside itself');
err.code = 'EPERM';
return cb(err);
}
if (opts.clobber) {
// also work around bug for clobber in dir
return rm(newPath, function(err) {
if (err) { return cb(err); }
mv(oldPath, newPath, opts, cb);
});
}
return mv(oldPath, newPath, opts, cb);
});
};
/*
stat a file
*/
var stat = function (args, cb) {
var path = args.filePath;
fs.stat(path, function(err, stats) {
if (err) { return cb(err); }
cb(null, stats);
});
};
module.exports.listAll = listAll;
module.exports.list = list;
module.exports.readFile = readFile;
module.exports.mkdir = mkdir;
module.exports.rmdir = rmdir;
module.exports.writeFile = writeFile;
module.exports.unlink = unlink;
module.exports.move = move;
module.exports.writeFileStream = writeFileStream;
module.exports.stat = stat;