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
|
#include <u.h>
#include <libc.h>
#include <mp.h>
#include <libsec.h>
#include "SConn.h"
static long
ls(char *p, Dir **dirbuf)
{
int fd;
long n;
Dir *db;
if((db = dirstat(p)) == nil ||
!(db->qid.type & QTDIR) ||
(fd = open(p, OREAD)) < 0 )
return -1;
free(db);
n = dirreadall(fd, dirbuf);
close(fd);
return n;
}
static uchar*
sha1file(char *pfx, char *nm)
{
int n, fd, len;
char *tmp;
uchar buf[8192];
static uchar digest[SHA1dlen];
DigestState *s;
len = strlen(pfx)+1+strlen(nm)+1;
tmp = emalloc(len);
snprint(tmp, len, "%s/%s", pfx, nm);
if((fd = open(tmp, OREAD)) < 0){
free(tmp);
return nil;
}
free(tmp);
s = nil;
while((n = read(fd, buf, sizeof buf)) > 0)
s = sha1(buf, n, nil, s);
close(fd);
sha1(nil, 0, digest, s);
return digest;
}
static int
compare(Dir *a, Dir *b)
{
return strcmp(a->name, b->name);
}
/* list the (name mtime size sum) of regular, readable files in path */
char *
dirls(char *path)
{
char *list, *date, dig[30], buf[128];
int m, nmwid, lenwid;
long i, n, ndir, len;
Dir *dirbuf;
if(path==nil || (ndir = ls(path, &dirbuf)) < 0)
return nil;
qsort(dirbuf, ndir, sizeof dirbuf[0], (int (*)(void *, void *))compare);
for(nmwid=lenwid=i=0; i<ndir; i++){
if((m = strlen(dirbuf[i].name)) > nmwid)
nmwid = m;
snprint(buf, sizeof(buf), "%ulld", dirbuf[i].length);
if((m = strlen(buf)) > lenwid)
lenwid = m;
}
for(list=nil, len=0, i=0; i<ndir; i++){
date = ctime(dirbuf[i].mtime);
date[28] = 0; // trim newline
n = snprint(buf, sizeof buf, "%*ulld %s", lenwid, dirbuf[i].length, date+4);
n += enc64(dig, sizeof dig, sha1file(path, dirbuf[i].name), SHA1dlen);
n += nmwid+3+strlen(dirbuf[i].name);
list = erealloc(list, len+n+1);
len += snprint(list+len, n+1, "%-*s\t%s %s\n", nmwid, dirbuf[i].name, buf, dig);
}
free(dirbuf);
return list;
}
|