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 "internaltask.h"
8 #include "sourcepackage.h"
18 processing_unit = COMPONENT;
19 set_run_internal(&_run);
22 Target *Tar::create_target(const vector<Target *> &sources, const string &arg)
24 if(sources.empty() || !sources.front()->get_package())
25 throw invalid_argument("Tar::create_target");
27 TarBall *tarball = new TarBall(builder, *sources.front()->get_package(), arg);
28 for(Target *s: sources)
29 tarball->add_dependency(*s);
31 tarball->set_tool(*this);
36 bool Tar::_run(const TarBall &tarball)
38 const FS::Path &pkg_src = tarball.get_package()->get_source_directory();
39 FS::Path basedir = FS::basepart(FS::basename(tarball.get_path()));
41 IO::File out(tarball.get_path().str(), IO::M_WRITE);
42 for(Target *d: tarball.get_dependencies())
44 FileTarget *ft = dynamic_cast<FileTarget *>(d);
51 string rel_path = (basedir/relative(ft->get_path(), pkg_src)).str();
52 if(rel_path.size()>99)
54 IO::print("Can't store %s in tar archive - too long name\n", rel_path);
58 memcpy(buf, rel_path.data(), rel_path.size());
60 FS::Stat st = FS::stat(ft->get_path());
61 store_number(buf+100, 0666, 7);
62 store_number(buf+108, 0, 7);
63 store_number(buf+116, 0, 7);
64 store_number(buf+124, st.get_size(), 11);
65 store_number(buf+136, st.get_modify_time().to_unixtime(), 11);
68 memset(buf+148, ' ', 8);
70 for(unsigned j=0; j<512; ++j)
71 chk += static_cast<unsigned char>(buf[j]);
72 store_number(buf+148, chk, 7);
76 IO::File in(ft->get_path().str());
77 for(unsigned j=0; j<st.get_size(); j+=4096)
79 unsigned len = in.read(buf, 4096);
80 len += ((~len)+1)&0777;
88 void Tar::store_number(char *buf, unsigned value, unsigned length)
90 for(unsigned i=length; i--;)