]> git.tdb.fi Git - builder.git/blob - source/tar.cpp
Refactor InternalTask to take a functor
[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 "internaltask.h"
8 #include "sourcepackage.h"
9 #include "tar.h"
10 #include "tarball.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 Tar::Tar(Builder &b):
16         Tool(b, "TAR")
17 {
18         processing_unit = COMPONENT;
19 }
20
21 Target *Tar::create_target(const vector<Target *> &sources, const string &arg)
22 {
23         if(sources.empty() || !sources.front()->get_package())
24                 throw invalid_argument("Tar::create_target");
25
26         TarBall *tarball = new TarBall(builder, *sources.front()->get_package(), arg);
27         for(Target *s: sources)
28                 tarball->add_dependency(*s);
29
30         tarball->set_tool(*this);
31
32         return tarball;
33 }
34
35 Task *Tar::run(const Target &target) const
36 {
37         const TarBall &tarball = dynamic_cast<const TarBall &>(target);
38         return new InternalTask([&tarball]{ return _run(tarball); });
39 }
40
41 bool Tar::_run(const TarBall &tarball)
42 {
43         const FS::Path &pkg_src = tarball.get_package()->get_source_directory();
44         FS::Path basedir = FS::basepart(FS::basename(tarball.get_path()));
45
46         IO::File out(tarball.get_path().str(), IO::M_WRITE);
47         for(Target *d: tarball.get_dependencies())
48         {
49                 FileTarget *ft = dynamic_cast<FileTarget *>(d);
50                 if(!ft)
51                         continue;
52
53                 char buf[4096];
54                 memset(buf, 0, 512);
55
56                 string rel_path = (basedir/relative(ft->get_path(), pkg_src)).str();
57                 if(rel_path.size()>99)
58                 {
59                         IO::print("Can't store %s in tar archive - too long name\n", rel_path);
60                         return false;
61                 }
62
63                 memcpy(buf, rel_path.data(), rel_path.size());
64
65                 FS::Stat st = FS::stat(ft->get_path());
66                 store_number(buf+100, 0666, 7);
67                 store_number(buf+108, 0, 7);
68                 store_number(buf+116, 0, 7);
69                 store_number(buf+124, st.get_size(), 11);
70                 store_number(buf+136, st.get_modify_time().to_unixtime(), 11);
71                 buf[156] = '0';
72
73                 memset(buf+148, ' ', 8);
74                 unsigned chk = 0;
75                 for(unsigned j=0; j<512; ++j)
76                         chk += static_cast<unsigned char>(buf[j]);
77                 store_number(buf+148, chk, 7);
78                 buf[155] = 0;
79
80                 out.write(buf, 512);
81                 IO::File in(ft->get_path().str());
82                 for(unsigned j=0; j<st.get_size(); j+=4096)
83                 {
84                         unsigned len = in.read(buf, 4096);
85                         len += ((~len)+1)&0777;
86                         out.write(buf, len);
87                 }
88         }
89
90         return true;
91 }
92
93 void Tar::store_number(char *buf, unsigned value, unsigned length)
94 {
95         for(unsigned i=length; i--;)
96         {
97                 buf[i] = '0'+value%8;
98                 value /= 8;
99         }
100 }