Skip to content

Commit a34eed0

Browse files
author
Mark Cavage
committed
initial creation
0 parents  commit a34eed0

35 files changed

+19639
-0
lines changed

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2011 Mark Cavage, All rights reserved.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
node-dirsum is a super small library that gives you the node.js equivalent of:
2+
3+
find <dir> -type f | xargs md5
4+
5+
With the added feature that this library will summarize the hashes into one
6+
master hash so you can get a single checksum for a directory treat. Note that
7+
there is no 'standard' way to do this in UNIX, but you basically have two
8+
choices:
9+
10+
* Pipe all the bytes of all the files into one digest
11+
* Compute a digest of all the digests
12+
13+
Since (1) is hard to do if your files are large, I went with (2). Again, this
14+
may or may not be useful to you, since there's not a standard client to validate
15+
this. At a minimum, you can at least get a tree of all the files and their
16+
checksums.
17+
18+
## Usage
19+
20+
var dirsum = require('../lib/dirsum');
21+
22+
dirsum.digest('/your/tree', 'sha1', function(err, hashes) {
23+
if (err) throw err;
24+
console.log(JSON.stringify(hashes, null, 2));
25+
});
26+
27+
There you go. That's the whole API. There's the equivalent of that in the
28+
tst directory, and I copied in the openldap default files, since there's a bunch
29+
of schema files in there. Running that with 'md5' as the method gives this
30+
output:
31+
32+
{
33+
"files": {
34+
"ldap.conf": "a1b26e90628b18bc9cea6c9573a63b7b",
35+
"ldap.conf.default": "a1b26e90628b18bc9cea6c9573a63b7b",
36+
"schema": {
37+
"files": {
38+
"apple.schema": "d74ab4c3522e0154fb409c9a6515ad8f",
39+
"apple_auxillary.schema": "20b1560538fe8cc73bc97b0ce8f8d7a4",
40+
"collective.schema": "139aefbd0541ad2f4198ea53d2689334",
41+
"corba.schema": "72684228a9d9691e458a9850f93231fb",
42+
"core.ldif": "0c9d2c5df2376192e02f11bc6edc96f7",
43+
"core.schema": "fdd7cfc44ca73698f877d64becdc8278",
44+
"cosine.ldif": "82c062506b1dc1786c7b1bdd56c53f1a",
45+
"duaconf.schema": "2503265c236a158ae5be7eb6d81b130a",
46+
"dyngroup.schema": "55b65ea42cec37ad0ba110752e1c20c2",
47+
"fmserver.schema": "928cb73c8f0dd0b5b18d2619a3a0dbc5",
48+
"inetorgperson.ldif": "71db554a56953e03669784270f69942c",
49+
"inetorgperson.schema": "7452ce52fcecad8aa97446fed52b1db2",
50+
"java.schema": "8f8f12b72432388e065e37e19eca0eec",
51+
"krb5-kdc.schema": "04df5507f0a3b1602efd523a42bd1f90",
52+
"microsoft.std.schema": "928686833526a63590d8d56fb15f3602",
53+
"misc.schema": "c2453f83012e97e84a4a2841c62cee88",
54+
"nadf.schema": "687e1d49faa7c0f4990eba6899e3ba7f",
55+
"netinfo.schema": "4ca7cc2b5cf43618cfbd540171f8ffd6",
56+
"nis.ldif": "4322c3f16f38f802bea8fdc0a3524274",
57+
"nis.schema": "fc3255f06a87dd7c360e88772b2d17de",
58+
"openldap.ldif": "f342787e81ac1101cf860dea78e7bb51",
59+
"openldap.schema": "bbf95ea610c9f28c4a7bdc4c867e8961",
60+
"ppolicy.schema": "8fbc0dda95c831a9ed84b2884c56a02b",
61+
"README": "dfe2414dc543da3fdf98b48ab3de1a94",
62+
"samba.schema": "a26fbdabbd97f076fcaa3b3774b7eba8",
63+
"cosine.schema": "0a85a2dbf9729ceb4bb6354c4c865135",
64+
"microsoft.schema": "2ced4df70c8e72f5fa1f857ed3a5c39d",
65+
"microsoft.ext.schema": "65dbf5a721b14de93fa011386025ab72"
66+
},
67+
"hash": "c3dce06943963c28163db6e5164844a2"
68+
}
69+
},
70+
"hash": "440a3933672304d72660526e4fd1463e"
71+
}
72+
73+
Basically, every level in the tree is going to have two attributes in the
74+
returned `Hashes` object, a `hash` which is the hash of hashes, and `files`,
75+
which is a key/value pairing of filename to hash. Each file might be another
76+
object, which indicates there was a directory tree encountered.
77+
78+
## Installation
79+
80+
npm install dirsum
81+
82+
## License
83+
84+
MIT.
85+
86+
## Bugs
87+
88+
See <https://github.com/mcavage/node-dirsum/issues>.

lib/dirsum.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2011 Mark Cavage <[email protected]> All rights reserved.
2+
3+
var crypto = require('crypto');
4+
var fs = require('fs');
5+
6+
function _summarize(method, hashes) {
7+
var keys = Object.keys(hashes);
8+
keys.sort();
9+
10+
var obj = {};
11+
obj.files = hashes;
12+
var hash = crypto.createHash(method);
13+
for (var i = 0; i < keys.length; i++) {
14+
if (typeof(hashes[keys[i]]) === 'string') {
15+
hash.update(hashes[keys[i]]);
16+
} else if (typeof(hashes[keys[i]]) === 'object') {
17+
hash.update(hashes[keys[i]].hash);
18+
} else {
19+
console.error('Unknown type found in hash: ' + typeof(hashes[keys[i]]));
20+
}
21+
}
22+
23+
obj.hash = hash.digest('hex');
24+
return obj;
25+
}
26+
27+
function digest(root, method, callback) {
28+
if (!root || typeof(root) !== 'string') {
29+
throw new TypeError('root is required (string)');
30+
}
31+
if (method) {
32+
if (typeof(method) === 'string') {
33+
// NO-OP
34+
} else if (typeof(method) === 'function') {
35+
callback = method;
36+
method = 'md5';
37+
} else {
38+
throw new TypeError('hash must be a string');
39+
}
40+
} else {
41+
throw new TypeError('callback is required (function)');
42+
}
43+
if (!callback) {
44+
throw new TypeError('callback is required (function)');
45+
}
46+
47+
var hashes = {};
48+
49+
fs.readdir(root, function(err, files) {
50+
if (err) return callback(err);
51+
52+
var hashed = 0;
53+
files.forEach(function(f) {
54+
var path = root + '/' + f;
55+
fs.stat(path, function(err, stats) {
56+
if (err) return callback(err);
57+
58+
if (stats.isDirectory()) {
59+
return digest(path, method, function(err, hash) {
60+
if (err) return hash;
61+
62+
hashes[f] = hash;
63+
if (++hashed >= files.length) {
64+
return callback(undefined, _summarize(method, hashes));
65+
}
66+
});
67+
} else if (stats.isFile()) {
68+
fs.readFile(path, 'utf8', function(err, data) {
69+
if (err) return callback(err);
70+
71+
var hash = crypto.createHash(method);
72+
hash.update(data);
73+
hashes[f] = hash.digest('hex');
74+
75+
if (++hashed >= files.length) {
76+
return callback(undefined, _summarize(method, hashes));
77+
}
78+
});
79+
} else {
80+
console.error('Skipping hash of %s', f);
81+
if (++hashed > files.length) {
82+
return callback(undefined, _summarize(method, hashes));
83+
}
84+
}
85+
});
86+
});
87+
});
88+
}
89+
90+
module.exports = {
91+
digest: digest
92+
};

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "dirsum",
3+
"description": "A small library that computes checksums of directory trees",
4+
"version": "0.1.0",
5+
"repository": {
6+
"type": "git",
7+
"url": "git://github.com/mcavage/node-dirsum.git"
8+
},
9+
"author": "Mark Cavage <[email protected]> (http://www.joyent.com)",
10+
"main": "lib/dirsum.js",
11+
"scripts": {
12+
"test": "jshint lib tst/dirsum.test.js && node tst/dirsum.test.js"
13+
},
14+
"devDependencies": {
15+
"jshint": ">=0.1.9"
16+
},
17+
"engines": {
18+
"node": ">=0.4"
19+
}
20+
}

tst/dirsum.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2011 Mark Cavage <[email protected]> All rights reserved.
2+
3+
var assert = require('assert');
4+
5+
var dirsum = require('../lib/dirsum');
6+
7+
dirsum.digest(process.cwd() + '/tst/openldap', function(err, hashes) {
8+
assert.ok(!err);
9+
assert.ok(hashes);
10+
console.log(JSON.stringify(hashes, null, 2));
11+
});

tst/openldap/ldap.conf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#
2+
# LDAP Defaults
3+
#
4+
5+
# See ldap.conf(5) for details
6+
# This file should be world readable but not world writable.
7+
8+
#BASE dc=example, dc=com
9+
#URI ldap://ldap.example.com ldap://ldap-master.example.com:666
10+
11+
#SIZELIMIT 12
12+
#TIMELIMIT 15
13+
#DEREF never
14+
TLS_REQCERT demand
15+

tst/openldap/ldap.conf.default

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#
2+
# LDAP Defaults
3+
#
4+
5+
# See ldap.conf(5) for details
6+
# This file should be world readable but not world writable.
7+
8+
#BASE dc=example, dc=com
9+
#URI ldap://ldap.example.com ldap://ldap-master.example.com:666
10+
11+
#SIZELIMIT 12
12+
#TIMELIMIT 15
13+
#DEREF never
14+
TLS_REQCERT demand
15+

tst/openldap/schema/README

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
This directory contains user application schema definitions for use
2+
with slapd(8).
3+
4+
File Description
5+
---- -----------
6+
collective.schema Collective attributes (experimental)
7+
corba.schema Corba Object
8+
core.schema OpenLDAP "core"
9+
cosine.schema COSINE Pilot
10+
duaconf.schema Client Configuration (work in progress)
11+
dyngroup.schema Dynamic Group (experimental)
12+
inetorgperson.schema InetOrgPerson
13+
java.schema Java Object
14+
misc.schema Miscellaneous Schema (experimental)
15+
nadf.schema North American Directory Forum (obsolete)
16+
nis.schema Network Information Service (experimental)
17+
openldap.schema OpenLDAP Project (FYI)
18+
ppolicy.schema Password Policy Schema (work in progress)
19+
20+
Additional "generally useful" schema definitions can be submitted
21+
using the OpenLDAP Issue Tracking System <http://www.openldap.org/its/>.
22+
Submissions should include a stable reference to a mature, open
23+
technical specification (e.g., an RFC) for the schema.
24+
25+
The core.ldif and openldap.ldif files are equivalent to their
26+
corresponding .schema files. They have been provided as examples
27+
for use with the dynamic configuration backend. These example files
28+
are not actually necessary since slapd will automatically convert any
29+
included *.schema files into LDIF when converting a slapd.conf file
30+
to a configuration database, but they serve as a model of how to
31+
convert schema files in general.
32+
33+
---
34+
35+
This notice applies to all files in this directory.
36+
37+
Copyright 1998-2008 The OpenLDAP Foundation, Redwood City, California, USA
38+
All rights reserved.
39+
40+
Redistribution and use in source and binary forms, with or without
41+
modification, are permitted only as authorized by the OpenLDAP
42+
Public License. A copy of this license is available at
43+
http://www.OpenLDAP.org/license.html or in file LICENSE in the
44+
top-level directory of the distribution.
45+
46+
---
47+
48+
This notice applies to all schema in this directory which are derived
49+
from RFCs and other IETF documents.
50+
51+
Portions Copyright 1991-2004, The Internet Society. All Rights Reserved.
52+
53+
This document and translations of it may be copied and furnished
54+
to others, and derivative works that comment on or otherwise explain
55+
it or assist in its implementation may be prepared, copied, published
56+
and distributed, in whole or in part, without restriction of any
57+
kind, provided that the above copyright notice and this paragraph
58+
are included on all such copies and derivative works. However,
59+
this document itself may not be modified in any way, such as by
60+
removing the copyright notice or references to the Internet Society
61+
or other Internet organizations, except as needed for the purpose
62+
of developing Internet standards in which case the procedures for
63+
copyrights defined in the Internet Standards process must be
64+
followed, or as required to translate it into languages other than
65+
English.
66+
67+
The limited permissions granted above are perpetual and will not
68+
be revoked by the Internet Society or its successors or assigns.
69+
70+
This document and the information contained herein is provided on
71+
an "AS IS" basis and THE AUTHORS, THE INTERNET SOCIETY, AND THE
72+
INTERNET ENGINEERING TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS
73+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE
74+
OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY
75+
IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
76+
PURPOSE.
77+
78+
79+
---
80+
$OpenLDAP: pkg/ldap/servers/slapd/schema/README,v 1.29.2.3 2008/02/11 23:26:49 kurt Exp $

0 commit comments

Comments
 (0)