-
Notifications
You must be signed in to change notification settings - Fork 4
/
compress_null.c
108 lines (93 loc) · 1.92 KB
/
compress_null.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
/*
FuseCompress
Copyright (C) 2006 Milan Svoboda <milan.svoboda@centrum.cz>
*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "structs.h"
#include "globals.h"
#include "file.h"
#include "log.h"
#include "compress.h"
#define BUF_SIZE 4096
void *nullOpen(int fd, const char *mode)
{
return (void *)(intptr_t) fd;
}
/**
*
*
* @param fd_source Source file descriptor
* @param fd_dest Destination file descriptor
* @return Number of bytes readed from fd_source or (off_t)-1 on error
*/
static off_t nullCompress(void *cancel_cookie, int fd_source, int fd_dest)
{
char buf[BUF_SIZE];
unsigned wr;
int rd;
off_t size = 0;
while ((rd = read(fd_source, buf, sizeof(buf))) > 0)
{
size += rd;
wr = write(fd_dest, buf, rd);
if (wr == 0)
{
return (off_t) FAIL;
}
if (compress_testcancel(cancel_cookie))
{
break;
}
}
if (rd < 0)
{
return (off_t) FAIL;
}
return size;
}
/**
* Decompress data from fd_source into fd_dest.
*
* @param fd_source Source file descriptor
* @param fd_dest Destination file descriptor
* @return Number of bytes written to fd_dest or (off_t)-1 on error
*/
static off_t nullDecompress(int fd_source, int fd_dest)
{
char buf[BUF_SIZE];
int wr;
int rd;
off_t size = 0;
while ((rd = read(fd_source, buf, BUF_SIZE)) > 0)
{
wr = write(fd_dest, buf, rd);
if (wr == -1)
{
return (off_t) FAIL;
}
size += wr;
}
if (rd == -1)
{
return (off_t) FAIL;
}
return size;
}
compressor_t module_null = {
.type = 0x00,
.name = "null",
.compress = nullCompress,
.decompress = nullDecompress,
.open = (void *(*) (int fd, const char *mode)) nullOpen,
.write = (int (*)(void *file, void *buf, unsigned int len)) write,
.read = (int (*)(void *file, void *buf, unsigned int len)) read,
.close = (int (*)(void *file)) close,
};