-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzstdfuse.c
268 lines (222 loc) · 6.56 KB
/
zstdfuse.c
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "common.h"
#include <zstd.h>
#define FUSE_USE_VERSION 31
#include <fuse3/fuse.h>
static const char* zststr = NULL;
static off_t rawsize = 0;
void ShowUsage(int outtype)
{
if(outtype == 0)
{
printf("Loop mount a ZSTD compressed ZST_FILE as the uncompressed file at the designated MOUNT_PATH.\n\n");
printf("Usage :\n");
printf("\tzstdmount ZST_FILE MOUNT_PATH\n\n");
printf("ZST_FILE\t: ZSTD compressed full path file name.\n");
printf("MOUNT_PATH\t: Mount point to mount the loop mounted ZSTD compressed file as uncompressed file.\n");
printf("Arguments :\n");
printf("-v\t: Prints Version information\n");
printf("-h\t: Prints help information\n\n");
printf("Example Usage :\n");
printf("zstdmount /mnt/item1.zst /mnt/zst/\n");
}
else if(outtype == 1)
{
printf("zstdmount v0.1\n");
printf("License CC0-1.0: Creative Commons Zero v1.0 Universal\n");
printf("This software is in the public domain\n");
printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
printf("Written by Pasquale Rinaldi\n");
}
};
static int zstd_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
{
//(void) fi;
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0)
{
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
}
else if(strcmp(path, "/zst") == 0)
{
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = rawsize;
}
//else
// res = -ENOENT;
return res;
}
static int zstd_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi, enum fuse_readdir_flags flags)
{
(void) offset;
//(void) fi;
(void) flags;
if (strcmp(path, "/") != 0)
return -ENOENT;
filler(buf, ".", NULL, 0, 0);
filler(buf, "..", NULL, 0, 0);
filler(buf, "zst", NULL, 0, 0);
return 0;
}
static int zstd_open(const char *path, struct fuse_file_info *fi)
{
if(strcmp(path, "/zst") != 0)
return -ENOENT;
return 0;
}
static int zstd_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
(void)fi;
FILE* fout = NULL;
fout = fopen_orDie(zststr, "rb");
size_t bufinsize = ZSTD_DStreamInSize();
void* bufin = malloc_orDie(bufinsize);
size_t bufoutsize = ZSTD_DStreamOutSize();
void* bufout = malloc_orDie(bufoutsize);
ZSTD_DCtx* dctx = ZSTD_createDCtx();
CHECK(dctx != NULL, "ZSTD_createDCtx() failed");
size_t toread = bufinsize;
size_t read;
size_t lastret = 0;
int isempty = 1;
uint64_t indxstart = offset / bufoutsize;
uint64_t relpos = offset - (indxstart * bufoutsize);
uint64_t startingblock = offset / bufoutsize;
uint64_t endingblock = (offset + size) / bufoutsize;
int posodd = (offset + size) % bufoutsize;
if(posodd > 0)
endingblock++;
size_t tmpbufsize = bufoutsize * (endingblock - startingblock + 1);
char* tmpbuffer = malloc_orDie(tmpbufsize);
int blkcnt = 0;
int bufblkoff = 0;
size_t readcount = 0;
size_t outcount = 0;
while( (read = fread_orDie(bufin, toread, fout)) )
{
readcount = readcount + read;
isempty = 0;
ZSTD_inBuffer input = { bufin, read, 0 };
while(input.pos < input.size)
{
ZSTD_outBuffer output = { bufout, bufoutsize, 0 };
size_t ret = ZSTD_decompressStream(dctx, &output, &input);
if(blkcnt >= startingblock && blkcnt <= endingblock)
{
memcpy(tmpbuffer+(bufblkoff*bufoutsize), bufout, bufoutsize);
bufblkoff++;
}
outcount = outcount + output.pos;
blkcnt++;
CHECK_ZSTD(ret);
lastret = ret;
}
}
memcpy(buf, tmpbuffer+relpos, size);
if(isempty)
{
return 0;
}
if(lastret != 0)
{
return 0;
}
ZSTD_freeDCtx(dctx);
fclose_orDie(fout);
free(bufin);
free(bufout);
free(tmpbuffer);
return size;
}
static const struct fuse_operations zstd_oper = {
.getattr = zstd_getattr,
.open = zstd_open,
.read = zstd_read,
.readdir = zstd_readdir,
};
int main(int argc, char* argv[])
{
if(argc == 1 || (argc == 2 && strcmp(argv[1], "-h") == 0))
{
ShowUsage(0);
return 1;
}
else if(strcmp(argv[1], "-v") == 0)
{
ShowUsage(1);
return 1;
}
else if(argc == 3)
{
char zststr2[256];
realpath(argv[1], zststr2);
zststr = malloc(sizeof(char)*strlen(zststr2));
zststr = zststr2;
//zststr = argv[1];
// GET UNCOMPRESSED FILE SIZE
FILE* fout = NULL;
fout = fopen_orDie(zststr, "rb");
size_t bufinsize = ZSTD_DStreamInSize();
void* bufin = malloc_orDie(bufinsize);
size_t bufoutsize = ZSTD_DStreamOutSize();
void* bufout = malloc_orDie(bufoutsize);
ZSTD_DCtx* dctx = ZSTD_createDCtx();
CHECK(dctx != NULL, "ZSTD_createDCtx() failed");
size_t toread = bufinsize;
size_t read;
size_t lastret = 0;
int isempty = 1;
size_t outcount = 0;
while( (read = fread_orDie(bufin, toread, fout)) )
{
isempty = 0;
ZSTD_inBuffer input = { bufin, read, 0 };
while(input.pos < input.size)
{
ZSTD_outBuffer output = { bufout, bufoutsize, 0 };
size_t ret = ZSTD_decompressStream(dctx, &output, &input);
outcount = outcount + output.pos;
CHECK_ZSTD(ret);
lastret = ret;
}
}
if(isempty)
{
return 0;
}
if(lastret != 0)
{
return 0;
}
ZSTD_freeDCtx(dctx);
fclose_orDie(fout);
free(bufin);
free(bufout);
rawsize = outcount;
char** fargv = NULL;
fargv = (char**)calloc(2, sizeof(char*));
int fargc = 2;
fargv[0] = argv[1];
fargv[1] = argv[2];
struct fuse_args fuseargs = FUSE_ARGS_INIT(fargc, fargv);
int ret;
ret = fuse_main(fuseargs.argc, fuseargs.argv, &zstd_oper, NULL);
fuse_opt_free_args(&fuseargs);
return ret;
return 0;
}
else
{
ShowUsage(0);
return 1;
}
return 0;
}