Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

new file system API (read, apply, create, delete) #144

Merged
merged 6 commits into from

2 participants

@jankeromnes
Owner

good times bro

Makefile
@@ -72,6 +72,16 @@ backup:
@cp -r meta/ web$(DATE)/
@echo "Copied web/ and meta/ to new web$(DATE)/ backup folder."
+gc:
+ @# WARNING: If web/ doesn't exist, meta/ will be deleted entirely.
@espadrine Owner

Could you please put a comment about what that does?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/fsapi.js
((30 lines not shown))
+ });
+ } else {
+ // get the folder's subfiles
+ file.files(function (err, files) {
+ if (err) { data.err = err; end(data); return; }
+ var counter = files.length;
+ if (counter <= 0) { end(data); return; }
+ for (var i = 0; i < files.length; i++) {
+ // only recurse on subfolders
+ var subdepth = files[i].meta.type === 'dir' ? depth - 1 : -1;
+ read(files[i].path, subdepth, function(d) {
+ if (d.err) { data.err = d.err; }
+ data.files.push.apply(data.files, d.files);
+ counter--;
+ if (counter <= 0) end(data);
+}); } }); } }); };
@espadrine Owner

erk wtf?

Edit: I assume this is an artful attempt at imitating line noise? :)

@jankeromnes Owner

An attempt at something a little original :-) I can remove it if you don't like it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@espadrine espadrine commented on the diff
lib/profiler.js
((7 lines not shown))
{doc: "Uptime", data: uptime, unit: "seconds"},
- {doc: "Uptime", data: uptime / (3600 * 24), unit: "days"},
@espadrine Owner

Having days can be nice, can't it?

@jankeromnes Owner

Go to the profiler and be horrified that days is an information shown twice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jankeromnes
Owner

@espadrine looking all good now! Also look at garden/plugs pull request please :)

@espadrine espadrine merged commit db0086b into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
12 Makefile
@@ -72,6 +72,18 @@ backup:
@cp -r meta/ web$(DATE)/
@echo "Copied web/ and meta/ to new web$(DATE)/ backup folder."
+# When files move around in web/, some dead metadata entries stay in meta/.
+# They need to be garbage collected from time to time.
+gc:
+ @# WARNING: If web/ doesn't exist, meta/ will be deleted entirely.
+ @for file in `cd meta && find . -name '*' -print`; do \
+ if [ -d "meta/$$file" ] && ! [ -d "web/$$file" ] || \
+ [ -f "meta/$$file" ] && ! [ -f "web/$$file" ] && \
+ [ "$${file##*/}" != ".DS-Store" ]; then \
+ echo "rm -rf meta/$$file"; rm -rf "meta/$$file"; \
+ fi; \
+ done
+
test:
node lib/test.js
View
2  lib/fs.js
@@ -38,7 +38,7 @@ function profile (param) {
{doc:'Core Size (warning -- overstating)', data:bench.size, unit:'bytes'},
{doc:'Max frenzy for a single file', data:bench.maxops, unit:'op/s'},
{doc:'Max activity for a single file', data:bench.maxop, unit:'operations'},
- {doc:'Latest modified file', data:bench.latestFile},
+ {doc:'Last modified file', data:bench.latestFile},
];
if (param && param.path) {
data.push({
View
135 lib/fsapi.js
@@ -1,15 +1,106 @@
-/* Ajax hooks to the file system.
- * Copyright © 2011 Jan Keromnes, Thaddee Tyl. All rights reserved.
+/* The file system API.
+ * Copyright © 2012 Jan Keromnes, Thaddee Tyl. All rights reserved.
* The following code is covered by the GPLv2 license. */
+
var fs = require('./fs'),
- path = require('path');
+ nodepath = require('path');
+
+
+/* read(1) - Read information about a file or folder, represented by a `path`.
+ * If `depth` is non negative, read file content or folder subfiles
+ * recursively.
+ */
+function read(path, depth, end) {
+ var data = { files: [] }, depth = depth || 0;
+ fs.file(path, function(err, file) {
+ if (err) { data.err = err; end(data); return; }
+ data.files.push({ path: file.path, date: +file.date, meta: file.meta });
+ // negative depth: return only file info, not content or subfiles
+ if (depth < 0) { end(data); return; }
+ if (file.meta.type !== 'dir') {
+ // get the file's content
+ file.open(function(err, content) {
+ if (err) { data.err = err; end(data); return; }
+ data.files[0].content = content;
+ end(data);
+ });
+ } else {
+ // get the folder's subfiles
+ file.files(function (err, files) {
+ if (err) { data.err = err; end(data); return; }
+ var counter = files.length;
+ if (counter <= 0) { end(data); return; }
+ for (var i = 0; i < files.length; i++) {
+ // only recurse on subfolders
+ var subdepth = files[i].meta.type === 'dir' ? depth - 1 : -1;
+ read(files[i].path, subdepth, function(d) {
+ if (d.err) { data.err = d.err; }
+ data.files.push.apply(data.files, d.files);
+ counter--;
+ if (counter <= 0) end(data);
+ });
+ }
+ });
+ }
+ });
+};
+
+
+/* apply(1) - Apply a set of operations to a file's content.
+ * See Operational Transformation.
+ */
+function apply(path, operations, end) {
+ // TODO implement if needed.
+};
+
+
+/* create(1) - Create a new file or folder, represented by a `path`.
+ * If `type` is set to "dir", a folder will be created -- default is file.
+ */
+function create(path, type, end) {
+ console.log('called create', arguments);
+ var data = {}, parent = nodepath.dirname(path), name = nodepath.basename(path);;
+ fs.file(parent, function (err, dir) {
+ if (err) { data.err = err; end(data); return; }
+ // file or folder?
+ (type === "dir" ? dir.mkdir : dir.mkfile).call(dir, name, function (err) {
+ if (err) { data.err = err; end(data); return; }
+ data.path = path;
+ end(data);
+ });
+ });
+};
+
+
+/* delete(1) - Delete a file or folder, represented by a `path`.
+ */
+function rm(path, end) {
+ fs.file(path, function (err, file) {
+ if (err) { end({err: err}); return; }
+ file.rm(function (err) {
+ if (err) { end({err: err}); return; }
+ end({path: path});
+ });
+ });
+};
+
exports.fs = function fsOperation(query, end) {
// `query` must have an `op` field, which is a String.
// It must also have a `path` field, which is a String.
var data = {};
switch (query.op) {
+ case 'read': case 'cat': // case 'ls':
+ read(query.path, query.depth || 0, end);
+ return;
+ case 'create': case 'new':
+ create(query.path, query.type, end);
+ return;
+ case 'delete': case 'rm':
+ rm(query.path, end);
+ return;
+ /** @deprecated FIXME */
case 'ls':
fs.file(query.path, function (err, dir) {
if (err) {
@@ -38,44 +129,8 @@ exports.fs = function fsOperation(query, end) {
}
});
break;
- case 'cat':
- fs.file(query.path, function (err, file) {
- if (err) { data.err = err; end(data); return; }
- data.meta = file.meta;
- data.path = query.path;
- file.open (function (err, content) {
- if (err) { data.err = err; end(data); }
- data.content = file.content;
- end(data);
- });
- });
- break;
- case 'create':
- fs.file(query.path, function (err, file) {
- // file or folder?
- if (err !== null) {
- console.error('server: file %s asked for. %s', query.path, err);
- end({err:err});
- return;
- }
- (query.type === "folder"? file.mkdir: file.mkfile).bind(file)
- (query.name, function (err) {
- data.err = err;
- data.path = path.join(query.path, query.name);
- end(data);
- });
- });
- break;
- case 'rm':
- fs.file(query.path, function (err, file) {
- file.rm(function (err) {
- data.err = err;
- end(data);
- });
- });
- break;
default:
- end({err: 'Unknown File System Operation.'})
+ end({err: 'Unknown file system operation ' + query.op + '.'})
return;
}
}
View
2  lib/plug.js
@@ -158,7 +158,7 @@ exports.resolve = function (query, path, endres, ask) {
for (var i = 0; i < files.length; i++) {
var filepath = driver.normalize(
files[i].path + (files[i].isOfType('dir') ? '/' : ''));
- data.files.push({name: nodepath.basename(filepath), path: filepath});
+ data.files.push({name: nodepath.basename(filepath), path: filepath, type: files[i].meta.type});
}
///console.log('server:root: data sent from dir is', data);
end(err, plug, data);
View
7 lib/profiler.js
@@ -20,11 +20,10 @@ var sources = {
{doc: "Node", data: process.version},
{doc: "Platform", data: process.platform},
{doc: "Architecture", data: process.arch},
- {doc: "Memory Heap", data: mem.heapTotal, unit: "bytes"},
- {doc: "Memory Heap Used", data: mem.heapUsed, unit: "bytes"},
- {doc: "Memory Resident Set Size", data: mem.rss, unit: "bytes"},
{doc: "Uptime", data: uptime, unit: "seconds"},
- {doc: "Uptime", data: uptime / (3600 * 24), unit: "days"},
@espadrine Owner

Having days can be nice, can't it?

@jankeromnes Owner

Go to the profiler and be horrified that days is an information shown twice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {doc: "Heap used", data: mem.heapUsed, unit: "bytes"},
+ {doc: "Heap total", data: mem.heapTotal, unit: "bytes"},
+ {doc: "Resident Set Size", data: mem.rss, unit: "bytes"},
{doc: "Load Average (1, 5, 15 min.)", data: loadAvg, unit: "# of proc."},
];
},
Something went wrong with that request. Please try again.