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