-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFile.cpp
More file actions
127 lines (93 loc) · 2.24 KB
/
Copy pathFile.cpp
File metadata and controls
127 lines (93 loc) · 2.24 KB
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
#include "common.h"
#include "File.h"
#include <stdio.h>
#include <cstring>
File::File()
{
fd = INVALID_FD;
is_multipart = index = 0;
for(int i = 0; i < 64; i++) fp[i] = INVALID_FD;
}
File::~File()
{
DPRINTF("File destructor.\n");
if (FD_OK(fd))
this->close();
}
int File::open(const char *path, int flags)
{
if (FD_OK(fd))
this->close();
fd = open_file(path, flags);
if (!FD_OK(fd))
return -1;
// multi part
int flen = strlen(path)-6;
if(flen < 0)
is_multipart = 0;
else
is_multipart = (strstr(path + flen, ".iso.0") != NULL || strstr(path + flen, ".ISO.0") != NULL);
if(!is_multipart) return 0;
char *filepath = (char *)malloc(strlen(path)+2); strcpy(filepath, path);
file_stat_t st;
fstat_file(fd, &st); part_size = st.file_size;
is_multipart = 1; // count parts
for(int i = 1; i < 64; i++)
{
filepath[flen+4] = 0; sprintf(filepath, "%s.%i", filepath, i);
fp[i] = open_file(filepath, flags);
if (!FD_OK(fp[i])) break;
is_multipart++;
}
fp[0] = fd; free(filepath);
return 0;
}
int File::close(void)
{
if(!is_multipart)
return close_file(fd);
int ret = close_file(fd); fd = INVALID_FD;
for(int i = 1; i < 64; i++) close_file(fp[i]);
is_multipart = index = 0;
for(int i = 0; i < 64; i++) fp[i] = INVALID_FD;
return ret;
}
ssize_t File::read(void *buf, size_t nbyte)
{
if(!is_multipart)
return read_file(fd, buf, nbyte);
ssize_t ret2 = 0, ret = read_file(fp[index], buf, nbyte);
if(ret < nbyte && index < (is_multipart-1))
{
void *buf2 = (int8_t*)buf + ret;
ret2 = read_file(fp[index+1], buf2, nbyte - ret);
}
return (ret + ret2);
}
ssize_t File::write(void *buf, size_t nbyte)
{
if(!is_multipart)
return write_file(fd, buf, nbyte);
return write_file(fp[index], buf, nbyte);
}
int64_t File::seek(int64_t offset, int whence)
{
if(!is_multipart)
return seek_file(fd, offset, whence);
index = (int)(offset / part_size);
return seek_file(fp[index], (offset % part_size), whence);
}
int File::fstat(file_stat_t *fs)
{
if(!is_multipart)
return fstat_file(fd, fs);
int64_t size = 0;
file_stat_t statbuf;
for(int i = 0; i < is_multipart; i++)
{
fstat_file(fp[i], &statbuf);
size += statbuf.file_size;
}
int ret = fstat_file(fd, fs); statbuf.file_size = size; *fs = statbuf;
return ret;
}