This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compress_lrzip.c
107 lines (88 loc) · 1.89 KB
/
compress_lrzip.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
/*
* Copyright (C) 2013 Adam Tkac <[email protected]>
*
* This program can be distributed under the terms of the GNU GPLv3.
* See the file COPYING.
*/
#include "compress_lrzip.h"
#include "log.h"
#include <errno.h>
#include <stdio.h>
#include <Lrzip.h>
static void
lrzip_log(void *data, unsigned int level, unsigned int line, const char *file,
const char *format, va_list args)
{
return; /* TODO: We don't support logging now */
/* We know only two error modes - debug or error */
if (level == LRZIP_LOG_LEVEL_ERROR)
log_errorv(format, args);
else
log_debugv(format, args);
}
static int
lrzip_processfile(int sfd, int dfd, char compress)
{
static char once = 0;
int ret = 0;
FILE *sfp = NULL;
FILE *dfp = NULL;
Lrzip *lr = NULL;
if (!once) {
once = 1;
lrzip_init();
}
sfp = fdopen(sfd, "rb");
if (sfp == NULL) {
ret = -errno;
return ret;
}
dfp = fdopen(dfd, "wb");
if (dfp == NULL) {
ret = -errno;
goto cleanup;
}
lr = lrzip_new(compress ? LRZIP_MODE_COMPRESS_LZMA : LRZIP_MODE_DECOMPRESS);
if (lr == NULL) {
ret = -ENOMEM;
goto cleanup;
}
lrzip_config_env(lr);
lrzip_log_cb_set(lr, &lrzip_log, NULL);
lrzip_log_level_set(lr, debug ? LRZIP_LOG_LEVEL_DEBUG : LRZIP_LOG_LEVEL_ERROR);
if (compress) {
/* Set the best compression */
lrzip_flags_set(lr, lrzip_flags_get(lr) | LRZIP_FLAG_UNLIMITED_RAM);
}
if (lrzip_file_add(lr, sfp) != true) {
ret = -ENOMEM;
goto cleanup;
}
lrzip_outfile_set(lr, dfp);
if (!lrzip_run(lr)) {
ret = -ENOMEM; /* FIXME: Return better error? */
goto cleanup;
}
lrzip_free(lr);
fflush(dfp);
fflush(sfp);
return 0;
cleanup:
if (lr != NULL)
lrzip_free(lr);
if (dfp != NULL)
fclose(dfp);
if (sfp != NULL)
fclose(sfp);
return ret;
}
int
lazfs_lrzip_decompress(int sfd, int dfd)
{
return lrzip_processfile(sfd, dfd, 0);
}
int
lazfs_lrzip_compress(int sfd, int dfd)
{
return lrzip_processfile(sfd, dfd, 1);
}