about summary refs log tree commit diff
path: root/src/hash.cc
blob: 9558d36705f38cbe42fe44937c878c7d4a685d92 (plain) (blame)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <iostream>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>

extern "C" {
#include "md5.h"
}

#include "hash.hh"


Hash::Hash()
{
    memset(hash, 0, sizeof(hash));
}


bool Hash::operator == (Hash & h2)
{
    for (unsigned int i = 0; i < hashSize; i++)
        if (hash[i] != h2.hash[i]) return false;
    return true;
}


bool Hash::operator != (Hash & h2)
{
    return !(*this == h2);
}


Hash::operator string() const
{
    ostringstream str;
    for (unsigned int i = 0; i < hashSize; i++) {
        str.fill('0');
        str.width(2);
        str << hex << (int) hash[i];
    }
    return str.str();
}

    
Hash parseHash(const string & s)
{
    Hash hash;
    if (s.length() != Hash::hashSize * 2)
        throw BadRefError("invalid hash: " + s);
    for (unsigned int i = 0; i < Hash::hashSize; i++) {
        string s2(s, i * 2, 2);
        if (!isxdigit(s2[0]) || !isxdigit(s2[1])) 
            throw BadRefError("invalid hash: " + s);
        istringstream str(s2);
        int n;
        str >> hex >> n;
        hash.hash[i] = n;
    }
    return hash;
}


bool isHash(const string & s)
{
    if (s.length() != 32) return false;
    for (int i = 0; i < 32; i++) {
        char c = s[i];
        if (!((c >= '0' && c <= '9') ||
              (c >= 'a' && c <= 'f')))
            return false;
    }
    return true;
}


Hash hashString(const string & s)
{
    Hash hash;
    md5_buffer(s.c_str(), s.length(), hash.hash);
    return hash;
}


Hash hashFile(const string & fileName)
{
    Hash hash;
    FILE * file = fopen(fileName.c_str(), "rb");
    if (!file)
        throw SysError("file `" + fileName + "' does not exist");
    int err = md5_stream(file, hash.hash);
    fclose(file);
    if (err) throw SysError("cannot hash file " + fileName);
    return hash;
}


struct HashSink : DumpSink
{
    struct md5_ctx ctx;
    virtual void operator ()
        (const unsigned char * data, unsigned int len)
    {
        md5_process_bytes(data, len, &ctx);
    }
};


Hash hashPath(const string & path)
{
    Hash hash;
    HashSink sink;
    md5_init_ctx(&sink.ctx);
    dumpPath(path, sink);
    md5_finish_ctx(&sink.ctx, hash.hash);
    return hash;
}


static void pad(unsigned int len, DumpSink & sink)
{
    if (len % 8) {
        unsigned char zero[8];
        memset(zero, 0, sizeof(zero));
        sink(zero, 8 - (len % 8));
    }
}


static void writeInt(unsigned int n, DumpSink & sink)
{
    unsigned char buf[8];
    memset(buf, 0, sizeof(buf));
    buf[0] = n & 0xff;
    buf[1] = (n >> 8) & 0xff;
    buf[2] = (n >> 16) & 0xff;
    buf[3] = (n >> 24) & 0xff;
    sink(buf, sizeof(buf));
}


static void writeString(const string & s, DumpSink & sink)
{
    unsigned int len = s.length();
    writeInt(len, sink);
    sink((const unsigned char *) s.c_str(), len);
    pad(len, sink);
}


static void dumpEntries(const string & path, DumpSink & sink)
{
    DIR * dir = opendir(path.c_str());
    if (!dir) throw SysError("opening directory " + path);
    
    struct dirent * dirent;

    /* !!! sort entries */

    while (errno = 0, dirent = readdir(dir)) {
        string name = dirent->d_name;
        if (name == "." || name == "..") continue;
        writeString("entry", sink);
        writeString("(", sink);
        writeString("name", sink);
        writeString(name, sink);
        writeString("file", sink);
        dumpPath(path + "/" + name, sink);
        writeString(")", sink);
    }

    if (errno) throw SysError("reading directory " + path);
    
    closedir(dir); /* !!! close on exception */
}


static void dumpContents(const string & path, unsigned int size, 
    DumpSink & sink)
{
    writeString("contents", sink);
    writeInt(size, sink);

    int fd = open(path.c_str(), O_RDONLY);
    if (!fd) throw SysError("opening file " + path);
    
    unsigned char buf[16384];

    unsigned int total = 0;
    ssize_t n;
    while ((n = read(fd, buf, sizeof(buf)))) {
        if (n == -1) throw SysError("reading file " + path);
        total += n;
        sink(buf, n);
    }

    if (total != size)
        throw SysError("file changed while reading it: " + path);

    pad(size, sink);

    close(fd); /* !!! close on exception */
}


void dumpPath(const string & path, DumpSink & sink)
{
    cerr << path << endl;

    struct stat st;
    if (lstat(path.c_str(), &st))
        throw SysError("getting attributes of path " + path);

    writeString("(", sink);

    if (S_ISREG(st.st_mode)) {
        writeString("type", sink);
        writeString("regular", sink);
        dumpContents(path, st.st_size, sink);
    } else if (S_ISDIR(st.st_mode)) {
        writeString("type", sink);
        writeString("directory", sink);
        dumpEntries(path, sink);
    } else throw Error("unknown file type: " + path);

    writeString(")", sink);
}