-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtar.cpp
More file actions
144 lines (124 loc) · 3.93 KB
/
tar.cpp
File metadata and controls
144 lines (124 loc) · 3.93 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
#include "tar.h"
#define TARHEADER static_cast<PosixTarHeader*>(header)
Tar::Tar(const char *filename): _finished(false){
this->out = std::fopen(filename, "wb");
if(out == NULL){
throw( "Cannot open output");
}
this->_closeFile = true;
this->_finished = true;
if(sizeof(PosixTarHeader)!=512){
throw(sizeof(PosixTarHeader));
}
}
Tar::Tar(std::FILE *file): _finished(false){
this->out = file;
if(out == NULL){
throw( "Cannot open output");
}
this->_closeFile = false;
this->_finished = true;
if(sizeof(PosixTarHeader)!=512){
throw(sizeof(PosixTarHeader));
}
}
Tar::~Tar(){
if(!_finished){
throw("Tar file was not finished.");
}
}
void Tar::_init(void* header){
std::memset(header,0,sizeof(PosixTarHeader));
std::sprintf(TARHEADER->magic,"ustar ");
std::sprintf(TARHEADER->mtime, "%011lo", time(NULL));
std::sprintf(TARHEADER->mode, "%07o", 0777);
std::sprintf(TARHEADER->gname,"%s","users");
}
void Tar::_checksum(void* header){
unsigned int sum = 0;
char *p = (char *) header;
char *q = p + sizeof(PosixTarHeader);
while (p < TARHEADER->checksum) sum += *p++ & 0xff;
for (int i = 0; i < 8; ++i) {
sum += ' ';
++p;
}
while (p < q) sum += *p++ & 0xff;
std::sprintf(TARHEADER->checksum,"%06o",sum);
}
void Tar::_size(void* header,unsigned long fileSize){
std::sprintf(TARHEADER->size,"%011llo",(long long unsigned int)fileSize);
}
void Tar::_filename(void* header,const char* filename){
if(filename==NULL || filename[0]==0 || std::strlen(filename)>=100){
//throw("invalid archive name \"" << filename << "\"");
throw(20);
}
sprintf(TARHEADER->name,"%s",filename); // max 100 chars !!!
}
void Tar::_endRecord(std::size_t len){
char c='\0';
while((len%sizeof(PosixTarHeader))!=0){
std::fwrite(&c, sizeof(char), sizeof(char), out);
++len;
}
}
void Tar::close(){
if(!_finished){
_finished=true;
PosixTarHeader header;
std::memset((void*)&header, 0, sizeof(PosixTarHeader));
std::fwrite((const char*)&header, sizeof(char), sizeof(PosixTarHeader), out);
std::fwrite((const char*)&header, sizeof(char), sizeof(PosixTarHeader), out);
}
if(this->_closeFile) std::fclose(this->out);
}
void Tar::put(const char* filename, const std::string& s){
put(filename, s.c_str(), s.size());
}
void Tar::put(const char* filename, const char* content){
put(filename, content, std::strlen(content));
}
void Tar::put(const char* filename,const char* content,std::size_t len){
PosixTarHeader header;
_init((void*)&header);
_filename((void*)&header, filename);
header.typeflag[0]=48;
_size((void*)&header, len);
_checksum((void*)&header);
std::fwrite((const char*)&header, sizeof(char), sizeof(PosixTarHeader), out);
std::fwrite(content, sizeof(char), len, out);
_endRecord(len);
}
void Tar::putFile(const char* filename, const char* nameInArchive){
std::FILE* in = std::fopen(filename, "rb");
if(in == NULL){
//throw("Cannot open " << filename << " "<< std::strerror(errno));
throw(10);
}
std::fseek(in, 0L, SEEK_END);
long int len = std::ftell(in);
std::fseek(in, 0L, SEEK_SET);
PosixTarHeader header;
_init((void*)&header);
_filename((void*)&header,nameInArchive);
header.typeflag[0]=0;
_size((void*)&header, len);
_checksum((void*)&header);
std::fwrite((const char*)&header, sizeof(char), sizeof(PosixTarHeader), out);
char buff[BUFSIZ];
unsigned long int total = 0;
std::size_t nRead=0;
while( ( nRead = std::fread(buff ,sizeof(char), BUFSIZ, in) ) > 0) {
std::fwrite(buff, sizeof(char), nRead, out);
total = total + nRead;
}
std::fclose(in);
_endRecord(total);
}
long int Tar::fileLength(std::FILE *file){
std::fseek(file, 0L, SEEK_END);
long int len = std::ftell(file);
std::fseek(file, 0L, SEEK_SET);
return len;
}