]> git.tdb.fi Git - builder.git/blob - source/tar.cpp
Replace per-file copyright notices with a single file
[builder.git] / source / tar.cpp
1 #include <cstring>
2 #include <msp/fs/stat.h>
3 #include <msp/fs/utils.h>
4 #include <msp/io/file.h>
5 #include <msp/io/print.h>
6 #include "builder.h"
7 #include "sourcepackage.h"
8 #include "tar.h"
9 #include "tarball.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 Tar::Tar(Builder &b, const TarBall &t):
15         InternalAction(b),
16         tarball(t)
17 {
18         string basename = FS::basename(tarball.get_path());
19         announce(tarball.get_package()->get_name(), "TAR ", basename);
20         if(builder.get_verbose()>=2)
21                 IO::print("Create %s\n", basename);
22
23         if(!builder.get_dry_run())
24                 worker = new Worker(*this);
25 }
26
27
28 Tar::Worker::Worker(Tar &t):
29         tar(t)
30 {
31         launch();
32 }
33
34 void Tar::Worker::main()
35 {
36         const FS::Path &pkg_src = tar.tarball.get_package()->get_source();
37         FS::Path basedir = FS::basepart(FS::basename(tar.tarball.get_path()));
38
39         IO::File out(tar.tarball.get_path().str(), IO::M_WRITE);
40         const TargetList &deps = tar.tarball.get_depends();
41         for(TargetList::const_iterator i=deps.begin(); i!=deps.end(); ++i)
42         {
43                 FileTarget *ft = dynamic_cast<FileTarget *>(*i);
44                 if(!ft)
45                         continue;
46
47                 char buf[4096];
48                 memset(buf, 0, 512);
49
50                 string rel_path = (basedir/relative(ft->get_path(), pkg_src)).str();
51                 if(rel_path.size()>99)
52                 {
53                         IO::print("Can't store %s in tar archive - too long name\n", rel_path);
54                         error = true;
55                         break;
56                 }
57
58                 memcpy(buf, rel_path.data(), rel_path.size());
59
60                 FS::Stat st = FS::stat(ft->get_path());
61                 store_number(buf+100, 0666, 7);
62                 store_number(buf+108, 0, 7);
63                 store_number(buf+116, 0, 7);
64                 store_number(buf+124, st.get_size(), 11);
65                 store_number(buf+136, st.get_modify_time().to_unixtime(), 11);
66                 buf[156] = '0';
67
68                 memset(buf+148, ' ', 8);
69                 unsigned chk = 0;
70                 for(unsigned j=0; j<512; ++j)
71                         chk += static_cast<unsigned char>(buf[j]);
72                 store_number(buf+148, chk, 7);
73                 buf[155] = 0;
74
75                 out.write(buf, 512);
76                 IO::File in(ft->get_path().str());
77                 for(unsigned j=0; j<st.get_size(); j+=4096)
78                 {
79                         unsigned len = in.read(buf, 4096);
80                         len += ((~len)+1)&0777;
81                         out.write(buf, len);
82                 }
83         }
84
85         done = true;
86 }
87
88 void Tar::Worker::store_number(char *buf, unsigned value, unsigned length)
89 {
90         for(unsigned i=length; i--;)
91         {
92                 buf[i] = '0'+value%8;
93                 value /= 8;
94         }
95 }