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