2 #include <msp/fs/stat.h>
3 #include <msp/fs/utils.h>
4 #include <msp/io/file.h>
5 #include <msp/io/print.h>
7 #include "sourcepackage.h"
18 Target *Tar::create_target(const list<Target *> &sources, const string &arg) const
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());
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);
31 Task *Tar::run(const Target &target) const
33 const TarBall &tarball = dynamic_cast<const TarBall &>(target);
34 Worker *worker = new Worker(tarball);
35 return new InternalTask(worker);
39 Tar::Worker::Worker(const TarBall &tb):
43 void Tar::Worker::main()
45 const FS::Path &pkg_src = tarball.get_package()->get_source();
46 FS::Path basedir = FS::basepart(FS::basename(tarball.get_path()));
48 IO::File out(tarball.get_path().str(), IO::M_WRITE);
49 const Target::Dependencies &deps = tarball.get_depends();
50 for(Target::Dependencies::const_iterator i=deps.begin(); i!=deps.end(); ++i)
52 FileTarget *ft = dynamic_cast<FileTarget *>(*i);
59 string rel_path = (basedir/relative(ft->get_path(), pkg_src)).str();
60 if(rel_path.size()>99)
62 IO::print("Can't store %s in tar archive - too long name\n", rel_path);
67 memcpy(buf, rel_path.data(), rel_path.size());
69 FS::Stat st = FS::stat(ft->get_path());
70 store_number(buf+100, 0666, 7);
71 store_number(buf+108, 0, 7);
72 store_number(buf+116, 0, 7);
73 store_number(buf+124, st.get_size(), 11);
74 store_number(buf+136, st.get_modify_time().to_unixtime(), 11);
77 memset(buf+148, ' ', 8);
79 for(unsigned j=0; j<512; ++j)
80 chk += static_cast<unsigned char>(buf[j]);
81 store_number(buf+148, chk, 7);
85 IO::File in(ft->get_path().str());
86 for(unsigned j=0; j<st.get_size(); j+=4096)
88 unsigned len = in.read(buf, 4096);
89 len += ((~len)+1)&0777;
94 status = Task::SUCCESS;
97 void Tar::Worker::store_number(char *buf, unsigned value, unsigned length)
99 for(unsigned i=length; i--;)
101 buf[i] = '0'+value%8;