-
Notifications
You must be signed in to change notification settings - Fork 2
/
virtual_file.c
165 lines (127 loc) · 3.01 KB
/
virtual_file.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
/*
* create a new virtual file from part of a file
* qianfan Zhao <qianfanguijin@163.com>
*/
#include <string.h>
#include "imgeditor.h"
#include "gd_private.h"
static struct virtual_file *virtual_file_get(int fd)
{
struct global_data *gd = imgeditor_get_gd();
struct virtual_file *vfps = gd->vfps;
for (int i = 0; i < MAX_VIRTUAL_FILE; i++) {
struct virtual_file *vfp = &vfps[i];
if (vfp->used && vfp->fd == fd)
return vfp;
}
return NULL;
}
int fileopen(const char *file, int flags, mode_t mode)
{
int fd = open(file, flags, mode);
if (fd < 0)
fprintf(stderr, "Error: open %s failed(%m)\n", file);
return fd;
}
int64_t filelength(int fd)
{
struct virtual_file *vf = virtual_file_get(fd);
if (vf)
return vf->total_length;
/* lseek can't work on 32bit ARM platform if the file is larger than
* 2GB, it will report EOVERFLOW.
* let's use lseek64 instead of lseek.
*/
int64_t sz = lseek64(fd, 0, SEEK_END);
lseek64(fd, 0, SEEK_SET);
return sz;
}
off64_t filestart(int fd)
{
struct virtual_file *vf = virtual_file_get(fd);
if (!vf)
return 0;
return vf->start_offset;
}
off64_t fileseek(int fd, off64_t offset)
{
return lseek64(fd, filestart(fd) + offset, SEEK_SET);
}
/* Return zero on successful */
int fileread(int fd, void *buf, size_t sz)
{
size_t n = 0;
while (n < sz) {
int ret = read(fd, buf + n, sz - n);
if (ret < 0)
return ret;
if (ret == 0) /* end of file */
break;
n += ret;
}
return n == sz ? 0 : -1;
}
static struct virtual_file *virtual_file_get_unused()
{
struct global_data *gd = imgeditor_get_gd();
struct virtual_file *vfps = gd->vfps;
for (int i = 0; i < MAX_VIRTUAL_FILE; i++) {
struct virtual_file *vfp = &vfps[i];
if (!vfp->used) {
vfp->used = 1;
return vfp;
}
}
return NULL;
}
static int64_t filelength_no_cache(int fd)
{
/* lseek can't work on 32bit ARM platform if the file is larger than
* 2GB, it will report EOVERFLOW.
* let's use lseek64 instead of lseek.
*/
return lseek64(fd, 0, SEEK_END);
}
int virtual_file_dup(int ref_fd, off64_t offset)
{
struct virtual_file *vfp = virtual_file_get_unused();
vfp->fd = dup(ref_fd);
if (vfp->fd < 0) {
fprintf(stderr, "Error: dup %d failed\n", ref_fd);
vfp->used = 0;
return vfp->fd;
}
vfp->start_offset = offset + filestart(ref_fd);
vfp->total_length =
filelength_no_cache(ref_fd) - vfp->start_offset;
lseek64(vfp->fd, vfp->start_offset, SEEK_SET);
return vfp->fd;
}
int virtual_file_open(const char *filename, int flags, mode_t t, off64_t offset)
{
int fd = fileopen(filename, flags, t);
struct virtual_file *vfp;
if (fd < 0)
return fd;
vfp = virtual_file_get_unused();
if (!vfp) {
close(fd);
return -1;
}
vfp->fd = fd;
vfp->start_offset = offset;
vfp->total_length =
filelength_no_cache(fd) - vfp->start_offset;
lseek64(vfp->fd, vfp->start_offset, SEEK_SET);
return vfp->fd;
}
int virtual_file_close(int fd)
{
struct virtual_file *vfp = virtual_file_get(fd);
if (!vfp)
return -1;
close(vfp->fd);
memset(vfp, 0, sizeof(*vfp));
vfp->fd = -1;
return 0;
}