aboutsummaryrefslogtreecommitdiffstats
path: root/toj/center/src/pack.cpp
blob: c2206b16608eaefb47f08163969236a4e0ff1563 (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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<libtar.h>
#include<bzlib.h>
#include<map>

#include"pack.h"

static int pack_copenfn(const char *pathname,int flags,...){
    int fd;
    pack_bzinfo *bzinfo;

    if((fd = open(pathname,flags,0644)) == -1){
        return -1;
    }

    bzinfo = new pack_bzinfo;
    bzinfo->len = 0;
    bzinfo->off = 0;
    bzinfo->endflag = false;
    bzinfo->bzs.bzalloc = NULL;
    bzinfo->bzs.bzfree = NULL;
    bzinfo->bzs.opaque = NULL;
    BZ2_bzCompressInit(&bzinfo->bzs,9,0,0);

    pack_fdmap.insert(std::pair<int,pack_bzinfo*>(fd,bzinfo));

    return fd;
}
static int pack_cclosefn(long fd){
    int ret;
    pack_bzinfo *bzinfo;

    bzinfo = pack_fdmap.find(fd)->second;
    pack_fdmap.erase(fd);

    bzinfo->bzs.next_in = NULL;
    bzinfo->bzs.avail_in = 0;
    while(true){
        bzinfo->bzs.next_out = bzinfo->buf;
        bzinfo->bzs.avail_out = PACK_BUFSIZE;
        ret = BZ2_bzCompress(&bzinfo->bzs,BZ_FINISH);

        if(bzinfo->bzs.avail_out != PACK_BUFSIZE){
            write(fd,bzinfo->buf,PACK_BUFSIZE - bzinfo->bzs.avail_out);
        }
        if(ret == BZ_STREAM_END){
            break;
        }
    }

    BZ2_bzCompressEnd(&bzinfo->bzs);
    delete bzinfo;
    return close(fd);
}
static ssize_t pack_cwritefn(long fd,const void *buf,size_t count){
    pack_bzinfo *bzinfo;

    bzinfo = pack_fdmap.find(fd)->second;

    bzinfo->bzs.next_in = (char*)buf;
    bzinfo->bzs.avail_in = count;
    while(bzinfo->bzs.avail_in > 0){
        bzinfo->bzs.next_out = bzinfo->buf;
        bzinfo->bzs.avail_out = PACK_BUFSIZE;
        BZ2_bzCompress(&bzinfo->bzs,BZ_RUN);
        if(bzinfo->bzs.avail_out != PACK_BUFSIZE){
            write(fd,bzinfo->buf,PACK_BUFSIZE - bzinfo->bzs.avail_out);
        }
    }

    return count;
}
static int pack_xopenfn(const char *pathname,int flags,...){
    int fd;
    pack_bzinfo *bzinfo;

    if((fd = open(pathname,flags)) == -1){
        return -1;
    }

    bzinfo = new pack_bzinfo;
    bzinfo->len = 0;
    bzinfo->off = 0;
    bzinfo->endflag = false;
    bzinfo->bzs.bzalloc = NULL;
    bzinfo->bzs.bzfree = NULL;
    bzinfo->bzs.opaque = NULL;
    BZ2_bzDecompressInit(&bzinfo->bzs,0,0);

    pack_fdmap.insert(std::pair<int,pack_bzinfo*>(fd,bzinfo));

    return fd;
}
static int pack_xclosefn(long fd){
    int ret;
    pack_bzinfo *bzinfo;

    bzinfo = pack_fdmap.find(fd)->second;
    pack_fdmap.erase(fd);
    BZ2_bzDecompressEnd(&bzinfo->bzs);
    delete bzinfo;

    return close(fd);
}
static ssize_t pack_xreadfn(long fd,void *buf,size_t count){
    int ret;
    pack_bzinfo *bzinfo;

    bzinfo = pack_fdmap.find(fd)->second;

    bzinfo->bzs.next_out = (char*)buf;
    bzinfo->bzs.avail_out = count;
    while(bzinfo->endflag == false){
        if(bzinfo->len == 0){
            ret = read(fd,bzinfo->buf,PACK_BUFSIZE);
            bzinfo->len = ret;
            bzinfo->off = 0;
        }
        if(bzinfo->len == 0){
            break;
        }

        bzinfo->bzs.next_in = bzinfo->buf + bzinfo->off;
        bzinfo->bzs.avail_in = bzinfo->len;
        while(bzinfo->bzs.avail_in > 0 && bzinfo->bzs.avail_out > 0){
            if(BZ2_bzDecompress(&bzinfo->bzs) != BZ_OK){
                bzinfo->endflag = true;
                break;
            }
        }
        bzinfo->off += bzinfo->len - bzinfo->bzs.avail_in;
        bzinfo->len = bzinfo->bzs.avail_in;

        if(bzinfo->bzs.avail_out == 0){
            break;
        }
    }

    return count - bzinfo->bzs.avail_out;
}

int pack_pack(char *packpath,char *dirpath){
    tartype_t tartype;
    TAR *tarp;

    tartype.openfunc = pack_copenfn;
    tartype.closefunc = pack_cclosefn;
    tartype.readfunc = (readfunc_t)read;
    tartype.writefunc = pack_cwritefn;
    tar_open(&tarp,packpath,&tartype,O_WRONLY | O_CREAT,0644,TAR_GNU);

    tar_append_tree(tarp,dirpath,".");
    tar_close(tarp);

    return 0;
}
int pack_unpack(char *packpath,char *dirpath){
    tartype_t tartype;
    TAR *tarp;

    tartype.openfunc = pack_xopenfn;
    tartype.closefunc = pack_xclosefn;
    tartype.readfunc = pack_xreadfn;
    tartype.writefunc = (writefunc_t)write;
    tar_open(&tarp,packpath,&tartype,O_RDONLY,0644,TAR_GNU);

    tar_extract_all(tarp,dirpath);
    tar_close(tarp);

    return 0;
}