view backends/directory.js @ 57:addbf6bbfaa8

Various fixes for the migration to ltx.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 12 Sep 2011 23:45:00 +0200
parents 99bd1d1ac071
children
line wrap: on
line source

/*
 *  Copyright (C) 2010  Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 *
 *  This file is part of PSĜS, a PubSub server written in JavaScript.
 *
 *  PSĜS is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License.
 *
 *  PSĜS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with PSĜS.  If not, see <http://www.gnu.org/licenses/>.
 */

'use strict';

var fs = require('fs');
var Node = require('../nodes').Node;
var backend = exports;

backend.save = function(dir) {
	if (!dir)
		dir = 'data';

	fs.stat(dir, function(err, stats) {
		if (err)
			fs.mkdir(dir, 509); // 0755 in decimal
		else if (!stats.isDirectory()) {
			console.log('\x1b[1;41mThe file “' + dir + '” isn’t a directory. Exiting.\x1b[0m');
			process.exit(1);
		}

		fs.readdir(dir, function(err, files) {
			function sanitize(o) {
				var n = {};
				for (var i in o) {
					if (i == 'content' || o[i].setFromISO8601)
						n[i] = o[i].toString();
					else if (o[i] instanceof Array)
						n[i] = o[i];
					else if (typeof o[i] == 'object')
						n[i] = sanitize(o[i]);
					else if (typeof o[i] == 'function')
						;
					else
						n[i] = o[i];
				}
				return n;
			}
			var data = sanitize(backend.list);

			var inspect = require('sys').inspect;

			for (var nodeID in data)
				fs.writeFile(dir+'/'+encodeURI(nodeID).replace('/', '%2f')+'.json', inspect(data[nodeID], false, null));

			var toDelete = [];

			for (var i in files) {
				var file = files[i];
				if (!(file.replace(/\.json$/, '') in data))
					toDelete.push(file);
			}

			for (var nodeID in toDelete)
				fs.unlink(dir+'/'+encodeURI(toDelete[nodeID]).replace('/', '%2f'));
		});
	});
}

backend.load = function(dir) {
	var parse = require('ltx').parse;
	if (!dir)
		dir = 'data';

	fs.stat(dir, function(err, stats) {
		if (err)
			fs.mkdir(dir, 509); // 0755 in decimal
		else if (!stats.isDirectory()) {
			console.log('\x1b[1;41mThe file “' + dir + '” isn’t a directory. Exiting.\x1b[0m');
			process.exit(1);
		}

		fs.readdir(dir, function(err, files) {
			function endParsing(o) {
				var regexp = /\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ/;
				for (var i in o) {
					if (typeof o[i] == 'string' && i == 'content')
						o[i] = parse(o[i]);
					else if (typeof o[i] == 'string' && regexp(o[i])) {
						var today = new Date();
						today.setFromISO8601(o[i]);
						o[i] = today;
					} else if (typeof o[i] == 'object')
						endParsing(o[i]);
				}
				return o;
			}

			backend.list = {};

			for (var i in files) {
				var file = files[i];
				if (/^\./.test(file))
					continue;

				if (!/\.json$/.test(file))
					continue;

				var nodeID = decodeURI(file.replace(/\.json$/, '').replace('%2f', '/'));

				var data = fs.readFileSync(dir+'/'+file).toString();

				var obj = eval('('+data+')');
				backend.list[nodeID] = endParsing(obj);

				for (var j in Node.prototype)
					backend.list[nodeID][j] = Node.prototype[j];
			}
		});
	});
}