]> git.tdb.fi Git - builder.git/blobdiff - source/tar.cpp
Support source generators that combine multiple files into one
[builder.git] / source / tar.cpp
index d43a5b0cbb24e0e49dc384591f9300201e6a58ff..6dee843df7ef4de8bee1baae45509a7fb2f34699 100644 (file)
@@ -1,14 +1,8 @@
-/* $Id$
-
-This file is part of builder
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <iostream>
 #include <cstring>
+#include <msp/fs/stat.h>
+#include <msp/fs/utils.h>
 #include <msp/io/file.h>
-#include <msp/path/utils.h>
+#include <msp/io/print.h>
 #include "builder.h"
 #include "sourcepackage.h"
 #include "tar.h"
@@ -17,81 +11,97 @@ Distributed under the LGPL
 using namespace std;
 using namespace Msp;
 
-Tar::Tar(Builder &b, const TarBall &t):
-       InternalAction(b),
-       tarball(t)
+Tar::Tar(Builder &b):
+       Tool(b, "TAR")
 {
-       string basename=tarball.get_name().substr(tarball.get_name().rfind('/')+1);
-       announce(tarball.get_package()->get_name(), "TAR ", basename);
-       if(builder.get_verbose()>=2)
-               cout<<"Create "<<basename<<'\n';
-
-       if(!builder.get_dry_run())
-               worker=new Worker(*this);
+       processing_unit = COMPONENT;
 }
 
+Target *Tar::create_target(const list<Target *> &sources, const string &arg)
+{
+       if(!sources.front()->get_package())
+               throw invalid_argument("Tar::create_target");
+
+       TarBall *tarball = new TarBall(builder, *sources.front()->get_package(), arg);
+       for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
+               tarball->add_dependency(**i);
+
+       tarball->set_tool(*this);
 
-Tar::Worker::Worker(Tar &t):
-       tar(t)
+       return tarball;
+}
+
+Task *Tar::run(const Target &target) const
 {
-       launch();
+       const TarBall &tarball = dynamic_cast<const TarBall &>(target);
+       Worker *worker = new Worker(tarball);
+       return new InternalTask(worker);
 }
 
+
+Tar::Worker::Worker(const TarBall &tb):
+       tarball(tb)
+{ }
+
 void Tar::Worker::main()
 {
-       const Path &pkg_src=tar.tarball.get_package()->get_source();
-       Path basedir=splitext(basename(tar.tarball.get_name())).base;
+       const FS::Path &pkg_src = tarball.get_package()->get_source_directory();
+       FS::Path basedir = FS::basepart(FS::basename(tarball.get_path()));
 
-       IO::File out(tar.tarball.get_name(), IO::M_WRITE);
-       const TargetList &deps=tar.tarball.get_depends();
-       for(TargetList::const_iterator i=deps.begin(); i!=deps.end(); ++i)
+       IO::File out(tarball.get_path().str(), IO::M_WRITE);
+       const Target::Dependencies &deps = tarball.get_dependencies();
+       for(Target::Dependencies::const_iterator i=deps.begin(); i!=deps.end(); ++i)
        {
+               FileTarget *ft = dynamic_cast<FileTarget *>(*i);
+               if(!ft)
+                       continue;
+
                char buf[4096];
                memset(buf, 0, 512);
 
-               string rel_path=(basedir/relative((*i)->get_name(), pkg_src)).str();
+               string rel_path = (basedir/relative(ft->get_path(), pkg_src)).str();
                if(rel_path.size()>99)
                {
-                       cout<<"Can't store "<<rel_path<<" in tar archive - too long name\n";
-                       error=true;
-                       break;
+                       IO::print("Can't store %s in tar archive - too long name\n", rel_path);
+                       status = Task::ERROR;
+                       return;
                }
 
                memcpy(buf, rel_path.data(), rel_path.size());
 
-               struct stat st=stat((*i)->get_name());
-               store_number(buf+100, st.st_mode, 7);
-               store_number(buf+108, st.st_uid, 7);
-               store_number(buf+116, st.st_gid, 7);
-               store_number(buf+124, st.st_size, 11);
-               store_number(buf+136, st.st_mtime, 11);
-               buf[156]='0';
+               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;
+               unsigned chk = 0;
                for(unsigned j=0; j<512; ++j)
-                       chk+=static_cast<unsigned char>(buf[j]);
+                       chk += static_cast<unsigned char>(buf[j]);
                store_number(buf+148, chk, 7);
-               buf[155]=0;
+               buf[155] = 0;
 
                out.write(buf, 512);
-               IO::File in((*i)->get_name());
-               for(int j=0; j<st.st_size; j+=4096)
+               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;
+                       unsigned len = in.read(buf, 4096);
+                       len += ((~len)+1)&0777;
                        out.write(buf, len);
                }
        }
 
-       done=true;
+       status = Task::SUCCESS;
 }
 
 void Tar::Worker::store_number(char *buf, unsigned value, unsigned length)
 {
        for(unsigned i=length; i--;)
        {
-               buf[i]='0'+value%8;
-               value/=8;
+               buf[i] = '0'+value%8;
+               value /= 8;
        }
 }