aboutsummaryrefslogtreecommitdiff
path: root/src/archive.cc
blob: 591939bb6ad29455ff1cf15986317a2b4dc2b67a (plain)
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
#include <vector>

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

#include "archive.hh"
#include "util.hh"


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);

    vector<string> names;

    struct dirent * dirent;
    while (errno = 0, dirent = readdir(dir)) {
        string name = dirent->d_name;
        if (name == "." || name == "..") continue;
        names.push_back(name);
    }
    if (errno) throw SysError("reading directory " + path);

    sort(names.begin(), names.end());

    for (vector<string>::iterator it = names.begin();
         it != names.end(); it++)
    {
        writeString("entry", sink);
        writeString("(", sink);
        writeString("name", sink);
        writeString(*it, sink);
        writeString("file", sink);
        dumpPath(path + "/" + *it, sink);
        writeString(")", sink);
    }
    
    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[65536];

    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)
{
    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 if (S_ISLNK(st.st_mode)) {
        writeString("type", sink);
        writeString("symlink", sink);
        char buf[st.st_size];
        if (readlink(path.c_str(), buf, st.st_size) != st.st_size)
            throw SysError("reading symbolic link " + path);
        writeString("target", sink);
        writeString(string(buf, st.st_size), sink);
    }

    else throw Error("unknown file type: " + path);

    writeString(")", sink);
}


void restorePath(const string & path, ReadSource & source)
{
}