-#include <cstring>
-#include <msp/fs/stat.h>
-#include <msp/fs/utils.h>
-#include <msp/io/file.h>
-#include <msp/io/print.h>
-#include "builder.h"
-#include "internaltask.h"
-#include "sourcepackage.h"
-#include "tar.h"
-#include "tarball.h"
-
-using namespace std;
-using namespace Msp;
-
-Tar::Tar(Builder &b):
- Tool(b, "TAR")
-{
- processing_unit = COMPONENT;
- set_run_internal(&_run);
-}
-
-Target *Tar::create_target(const vector<Target *> &sources, const string &arg)
-{
- if(sources.empty() || !sources.front()->get_package())
- throw invalid_argument("Tar::create_target");
-
- TarBall *tarball = new TarBall(builder, *sources.front()->get_package(), arg);
- for(Target *s: sources)
- tarball->add_dependency(*s);
-
- tarball->set_tool(*this);
-
- return tarball;
-}
-
-bool Tar::_run(const TarBall &tarball)
-{
- const FS::Path &pkg_src = tarball.get_package()->get_source_directory();
- FS::Path basedir = FS::basepart(FS::basename(tarball.get_path()));
-
- IO::File out(tarball.get_path().str(), IO::M_WRITE);
- for(Target *d: tarball.get_dependencies())
- {
- FileTarget *ft = dynamic_cast<FileTarget *>(d);
- if(!ft)
- continue;
-
- char buf[4096];
- memset(buf, 0, 512);
-
- string rel_path = (basedir/relative(ft->get_path(), pkg_src)).str();
- if(rel_path.size()>99)
- {
- IO::print("Can't store %s in tar archive - too long name\n", rel_path);
- return false;
- }
-
- memcpy(buf, rel_path.data(), rel_path.size());
-
- FS::Stat st = FS::stat(ft->get_path());
- store_number(buf+100, 0666, 7);
- store_number(buf+108, 0, 7);
- store_number(buf+116, 0, 7);
- store_number(buf+124, st.get_size(), 11);
- store_number(buf+136, st.get_modify_time().to_unixtime(), 11);
- buf[156] = '0';
-
- memset(buf+148, ' ', 8);
- unsigned chk = 0;
- for(unsigned j=0; j<512; ++j)
- chk += static_cast<unsigned char>(buf[j]);
- store_number(buf+148, chk, 7);
- buf[155] = 0;
-
- out.write(buf, 512);
- IO::File in(ft->get_path().str());
- for(unsigned j=0; j<st.get_size(); j+=4096)
- {
- unsigned len = in.read(buf, 4096);
- len += ((~len)+1)&0777;
- out.write(buf, len);
- }
- }
-
- return true;
-}
-
-void Tar::store_number(char *buf, unsigned value, unsigned length)
-{
- for(unsigned i=length; i--;)
- {
- buf[i] = '0'+value%8;
- value /= 8;
- }
-}