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>
17 processing_unit = COMPONENT;
18 set_run_internal(&_run);
21 Target *Tar::create_target(const vector<Target *> &sources, const string &arg)
23 if(sources.empty() || !sources.front()->get_package())
24 throw invalid_argument("Tar::create_target");
26 TarBall *tarball = new TarBall(builder, *sources.front()->get_package(), arg);
27 for(Target *s: sources)
28 tarball->add_dependency(*s);
30 tarball->set_tool(*this);
35 bool Tar::_run(const TarBall &tarball)
37 const FS::Path &pkg_src = tarball.get_package()->get_source_directory();
38 FS::Path basedir = FS::basepart(FS::basename(tarball.get_path()));
40 IO::File out(tarball.get_path().str(), IO::M_WRITE);
41 for(Target *d: tarball.get_dependencies())
43 FileTarget *ft = dynamic_cast<FileTarget *>(d);
50 string rel_path = (basedir/relative(ft->get_path(), pkg_src)).str();
51 if(rel_path.size()>99)
53 IO::print("Can't store %s in tar archive - too long name\n", rel_path);
57 memcpy(buf, rel_path.data(), rel_path.size());
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);
67 memset(buf+148, ' ', 8);
69 for(unsigned j=0; j<512; ++j)
70 chk += static_cast<unsigned char>(buf[j]);
71 store_number(buf+148, chk, 7);
75 IO::File in(ft->get_path().str());
76 for(unsigned j=0; j<st.get_size(); j+=4096)
78 unsigned len = in.read(buf, 4096);
79 len += ((~len)+1)&0777;
87 void Tar::store_number(char *buf, unsigned value, unsigned length)
89 for(unsigned i=length; i--;)