]> git.tdb.fi Git - builder.git/blob - source/tar.cpp
Make Target::add_depend take a reference since null is not allowed
[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):
15         Tool(b, "TAR")
16 { }
17
18 Target *Tar::create_target(const list<Target *> &sources, const string &arg) const
19 {
20         if(!sources.front()->get_package())
21                 throw invalid_argument("Tar::create_target");
22
23         TarBall *tarball = new TarBall(builder, *sources.front()->get_package(), arg);
24         for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
25                 tarball->add_depend(**i);
26
27         return tarball;
28 }
29
30 Task *Tar::run(const Target &target) const
31 {
32         const TarBall &tarball = dynamic_cast<const TarBall &>(target);
33         Worker *worker = new Worker(tarball);
34         return new InternalTask(worker);
35 }
36
37
38 Tar::Worker::Worker(const TarBall &tb):
39         tarball(tb)
40 { }
41
42 void Tar::Worker::main()
43 {
44         const FS::Path &pkg_src = tarball.get_package()->get_source();
45         FS::Path basedir = FS::basepart(FS::basename(tarball.get_path()));
46
47         IO::File out(tarball.get_path().str(), IO::M_WRITE);
48         const Target::Dependencies &deps = tarball.get_depends();
49         for(Target::Dependencies::const_iterator i=deps.begin(); i!=deps.end(); ++i)
50         {
51                 FileTarget *ft = dynamic_cast<FileTarget *>(*i);
52                 if(!ft)
53                         continue;
54
55                 char buf[4096];
56                 memset(buf, 0, 512);
57
58                 string rel_path = (basedir/relative(ft->get_path(), pkg_src)).str();
59                 if(rel_path.size()>99)
60                 {
61                         IO::print("Can't store %s in tar archive - too long name\n", rel_path);
62                         status = Task::ERROR;
63                         return;
64                 }
65
66                 memcpy(buf, rel_path.data(), rel_path.size());
67
68                 FS::Stat st = FS::stat(ft->get_path());
69                 store_number(buf+100, 0666, 7);
70                 store_number(buf+108, 0, 7);
71                 store_number(buf+116, 0, 7);
72                 store_number(buf+124, st.get_size(), 11);
73                 store_number(buf+136, st.get_modify_time().to_unixtime(), 11);
74                 buf[156] = '0';
75
76                 memset(buf+148, ' ', 8);
77                 unsigned chk = 0;
78                 for(unsigned j=0; j<512; ++j)
79                         chk += static_cast<unsigned char>(buf[j]);
80                 store_number(buf+148, chk, 7);
81                 buf[155] = 0;
82
83                 out.write(buf, 512);
84                 IO::File in(ft->get_path().str());
85                 for(unsigned j=0; j<st.get_size(); j+=4096)
86                 {
87                         unsigned len = in.read(buf, 4096);
88                         len += ((~len)+1)&0777;
89                         out.write(buf, len);
90                 }
91         }
92
93         status = Task::SUCCESS;
94 }
95
96 void Tar::Worker::store_number(char *buf, unsigned value, unsigned length)
97 {
98         for(unsigned i=length; i--;)
99         {
100                 buf[i] = '0'+value%8;
101                 value /= 8;
102         }
103 }