]> git.tdb.fi Git - builder.git/blob - plugins/base/tar.cpp
Convert builtin tools into a plugin
[builder.git] / plugins / base / tar.cpp
1 #include <cstring>
2 #include <msp/builder/builder.h>
3 #include <msp/builder/sourcepackage.h>
4 #include <msp/fs/stat.h>
5 #include <msp/fs/utils.h>
6 #include <msp/io/file.h>
7 #include <msp/io/print.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         processing_unit = COMPONENT;
18         set_run_internal(&_run);
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 bool Tar::_run(const TarBall &tarball)
36 {
37         const FS::Path &pkg_src = tarball.get_package()->get_source_directory();
38         FS::Path basedir = FS::basepart(FS::basename(tarball.get_path()));
39
40         IO::File out(tarball.get_path().str(), IO::M_WRITE);
41         for(Target *d: tarball.get_dependencies())
42         {
43                 FileTarget *ft = dynamic_cast<FileTarget *>(d);
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                         return false;
55                 }
56
57                 memcpy(buf, rel_path.data(), rel_path.size());
58
59                 FS::Stat st = FS::stat(ft->get_path());
60                 store_number(buf+100, 0666, 7);
61                 store_number(buf+108, 0, 7);
62                 store_number(buf+116, 0, 7);
63                 store_number(buf+124, st.get_size(), 11);
64                 store_number(buf+136, st.get_modify_time().to_unixtime(), 11);
65                 buf[156] = '0';
66
67                 memset(buf+148, ' ', 8);
68                 unsigned chk = 0;
69                 for(unsigned j=0; j<512; ++j)
70                         chk += static_cast<unsigned char>(buf[j]);
71                 store_number(buf+148, chk, 7);
72                 buf[155] = 0;
73
74                 out.write(buf, 512);
75                 IO::File in(ft->get_path().str());
76                 for(unsigned j=0; j<st.get_size(); j+=4096)
77                 {
78                         unsigned len = in.read(buf, 4096);
79                         len += ((~len)+1)&0777;
80                         out.write(buf, len);
81                 }
82         }
83
84         return true;
85 }
86
87 void Tar::store_number(char *buf, unsigned value, unsigned length)
88 {
89         for(unsigned i=length; i--;)
90         {
91                 buf[i] = '0'+value%8;
92                 value /= 8;
93         }
94 }