-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTarStream.cpp
More file actions
188 lines (173 loc) · 4.34 KB
/
TarStream.cpp
File metadata and controls
188 lines (173 loc) · 4.34 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
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
#include <string.h>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <fstream>
#include "TarStream.h"
using namespace std;
TarStream::TarStream() : readCursor(0)
{
}
TarStream::~TarStream()
{
}
void TarStream::putFile( const std::string& filePath, const std::string& pathInTar )
{
TarEntry entry(filePath, pathInTar);
this->files.push_back(entry);
}
void TarStream::putDirectory( const std::string& dirPath )
{
TarEntry entry(dirPath, dirPath, true);
this->files.push_back(entry);
}
size_t TarStream::getChunk(char* buf, size_t size)
{
size_t start = readCursor;
char *p = buf;
size_t file_size, orig_size = size;
vector<class TarEntry>::iterator ci;
for (ci = files.begin(); ci != files.end(); ++ci)
{
file_size = ci->getSize();
if (start >= file_size)
start -= file_size;
else
break;
}
while(size > 0 && ci != files.end())
{
file_size = ci->getSize();
if (size > file_size - start)
{
memcpy(p, ci->getChunk(start, file_size - start).c_str(), file_size - start);
p+=file_size - start;
size -= (file_size - start);
start = 0;
}
else
{
memcpy(p, ci->getChunk(start, size).c_str(), size);
p+=size;
size = 0;
break;
}
ci++;
}
memset(p, 0, size);
readCursor += orig_size - size;
if(getSize()-readCursor < size)
return orig_size + getSize() - readCursor;
return orig_size;
}
bool TarStream::seekg (size_t pos)
{
if(pos < 0 || pos >=getSize())
{
return false;
}
readCursor = pos;
return true;
}
size_t TarStream::getSize() const
{
size_t result = 0;
vector<class TarEntry>::const_iterator ci;
for (ci = files.begin(); ci != files.end(); ++ci)
{
result += ci->getSize();
}
return result + 2 * sizeof(TarHeaderBlock);
}
unsigned int TarStream::TarEntry::calculateChkSum(const char *header, const size_t s)
{
unsigned int sum = 0, i;
for (i = 0; i < s; i++) {
sum += header[i] & 0xff;
}
return sum;
}
TarStream::TarEntry::TarEntry(string path, string name, bool dir) : path(path)
{
if ( name == "")
name = path;
this->path = path;
this->name = name;
isDir = dir;
struct stat filestat;
stat(path.c_str(), &filestat);
if(isDir)
size = 0;
else
size = filestat.st_size;
memset(&header, 0, sizeof(header));
snprintf(header.name, sizeof(header.name), "%s", name.c_str());//TODO: long file names
snprintf(header.mode, sizeof(header.mode), "%07o", (unsigned int)filestat.st_mode & (unsigned int)0777);
snprintf(header.uid, sizeof(header.uid), "%07o", filestat.st_uid);
snprintf(header.gid, sizeof(header.gid), "%07o", filestat.st_gid);
snprintf(header.size, sizeof(header.size), "%011llo", (unsigned long long)size);
snprintf(header.magic, sizeof(header.magic), "ustar ");
snprintf(header.uname, sizeof(header.uname), "");
snprintf(header.gname, sizeof(header.gname), "");
snprintf(header.mtime, sizeof(header.mtime), "%011lo", filestat.st_mtime);
if (S_ISDIR(filestat.st_mode))
header.typeflag = '5';
else
header.typeflag = '0'; // regular file
memset(header.chksum, ' ', sizeof(header.chksum));
snprintf(header.chksum, sizeof(header.chksum), "%06o", calculateChkSum((const char *)&header, sizeof(header)));
//fprintf(stderr, "Created file %s, size %llu\n", this->name.c_str(), (unsigned long long)size);
}
TarStream::TarEntry::~TarEntry()
{
}
const size_t TarStream::TarEntry::getSize() const
{
int result = 0;
result += sizeof(header);
result += size;
if ((size % sizeof(header)) > 0)
result += sizeof(header) - (size % sizeof(header));
//fprintf(stderr, "Size of %s is %d\n", path.c_str(), result);
return result;
}
string TarStream::TarEntry::getChunk(size_t start, size_t size)
{
char buf[size];
char *p = buf;
if (!file.is_open())
file.open(path.c_str(), ifstream::binary);
if (!file.is_open())
{
fprintf(stderr, "Cant't read file: %s\n", path.c_str());
}
if (start <= sizeof(header))
{
memcpy(p, (const char *)&header, sizeof(header)-start);
p += sizeof(header)-start;
}
if(start+size > sizeof(header))
{
if(isDir)
{
string result(buf, sizeof(header)-start);
return result;
}
if(start <= sizeof(header))
start = 0;
else
start -= sizeof(header);
file.seekg(start);
file.read(p, size - (p - buf));
streamsize numread = file.gcount();
if (numread < size - (p - buf))
{
memset(p+numread, 0, size - (p - buf) - numread);
}
file.close();
}
string result(buf, size);
return result;
}