-#include <errno.h>
-#include <fstream>
-#include <iostream>
-#include <msp/path/utils.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <msp/fs/dir.h>
+#include <msp/fs/stat.h>
+#include <msp/fs/utils.h>
+#include <msp/io/file.h>
+#include <msp/io/print.h>
#include "builder.h"
#include "copy.h"
-#include "package.h"
+#include "installedfile.h"
using namespace std;
using namespace Msp;
-Copy::Copy(Builder &b, const Package &pkg, const Path::Path &s, const Path::Path &d):
- Action(b),
- src(s),
- dest(d),
- worker(0)
-{
- announce(pkg.get_name(), "COPY", dest[-1]);
- if(builder.get_verbose()>=2)
- cout<<s<<" -> "<<d<<'\n';
-
- if(!builder.get_dry_run())
- worker=new Worker(*this);
-}
+Copy::Copy(Builder &b):
+ Tool(b, "CP")
+{ }
-int Copy::check()
+Target *Copy::create_target(const list<Target *> &sources, const string &arg)
{
- if(!worker) // True for dry run
- {
- signal_done.emit();
- return 0;
- }
-
- if(worker->get_done())
- {
- signal_done.emit();
- worker->join();
- return worker->get_error()?1:0;
- }
-
- return -1;
+ FileTarget &file_tgt = dynamic_cast<FileTarget &>(*sources.front());
+ InstalledFile *inst = new InstalledFile(builder, *file_tgt.get_package(), file_tgt, arg);
+ inst->set_tool(*this);
+ return inst;
}
-Copy::~Copy()
+Task *Copy::run(const Target &target) const
{
- delete worker;
+ const InstalledFile &install = dynamic_cast<const InstalledFile &>(target);
+ Worker *worker = new Worker(install);
+ return new InternalTask(worker);
}
+
+Copy::Worker::Worker(const InstalledFile &t):
+ target(t)
+{ }
+
void Copy::Worker::main()
{
- Path::mkpath(copy.dest.subpath(0, copy.dest.size()-1), 0755);
-
+ const FileTarget &source = target.get_source();
+ const FS::Path &src_path = source.get_path();
+ const FS::Path &dst_path = target.get_path();
+ FS::mkpath(FS::dirname(dst_path), 0755);
+
// Remove old file. Not doing this would cause Bad Stuff when installing libraries.
- if(unlink(copy.dest.str().c_str())<0 && errno!=ENOENT)
+ if(FS::exists(dst_path))
{
- int err=errno;
- cerr<<"Can't unlink "<<copy.dest<<": "<<strerror(err)<<'\n';
- done=error=true;
- return;
+ try
+ {
+ unlink(dst_path);
+ }
+ catch(const exception &e)
+ {
+ IO::print(IO::cerr, "%s\n", e.what());
+ status = Task::ERROR;
+ return;
+ }
}
- ifstream in(copy.src.str().c_str());
- if(!in)
+ try
{
- cerr<<"Can't open "<<copy.src<<" for reading\n";
- done=error=true;
- return;
- }
+ IO::File in(src_path.str());
+ IO::File out(dst_path.str(), IO::M_WRITE);
- ofstream out(copy.dest.str().c_str());
- if(!out)
- {
- cerr<<"Can't open "<<copy.dest<<" for writing\n";
- done=error=true;
- return;
+ // Actual transfer loop
+ char buf[16384];
+ while(!in.eof())
+ {
+ unsigned len = in.read(buf, sizeof(buf));
+ out.write(buf, len);
+ }
}
-
- // Actual transfer loop
- char buf[16384];
- while(!in.eof())
+ catch(const exception &e)
{
- in.read(buf, sizeof(buf));
- out.write(buf, in.gcount());
+ IO::print(IO::cerr, "%s\n", e.what());
+ status = Task::ERROR;
+ return;
}
// Preserve file permissions
struct stat st;
- Path::stat(copy.src, st);
- chmod(copy.dest.str().c_str(), st.st_mode&0777);
+ if(stat(src_path.str().c_str(), &st)==0)
+ chmod(dst_path.str().c_str(), st.st_mode&0777);
+
+ const FS::Path &link = target.get_symlink();
+ if(!link.empty())
+ {
+ FS::Path relpath = FS::relative(dst_path, FS::dirname(link));
+ if(FS::exists(link))
+ FS::unlink(link);
+ symlink(relpath.str().c_str(), link.str().c_str());
+ }
- done=true;
+ status = Task::SUCCESS;
}