]> git.tdb.fi Git - builder.git/blob - source/copy.cpp
Add missing includes
[builder.git] / source / copy.cpp
1 #include <errno.h>
2 #include <fstream>
3 #include <iostream>
4 #include <msp/path/utils.h>
5 #include "builder.h"
6 #include "copy.h"
7 #include "package.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 Copy::Copy(Builder &b, const Package &pkg, const Path::Path &s, const Path::Path &d):
13         Action(b),
14         src(s),
15         dest(d),
16         worker(0)
17 {
18         announce(pkg.get_name(), "COPY", dest[-1]);
19         if(builder.get_verbose()>=2)
20                 cout<<s<<" -> "<<d<<'\n';
21         
22         if(!builder.get_dry_run())
23                 worker=new Worker(*this);
24 }
25
26 int Copy::check()
27 {
28         if(!worker)
29                 return 0;
30         
31         if(worker->get_done())
32         {
33                 signal_done.emit();
34                 worker->join();
35                 return worker->get_error()?1:0;
36         }
37         
38         return -1;
39 }
40
41 Copy::~Copy()
42 {
43         delete worker;
44 }
45
46 void Copy::Worker::main()
47 {
48         Path::mkpath(copy.dest.subpath(0, copy.dest.size()-1), 0755);
49         
50         if(unlink(copy.dest.str().c_str())<0 && errno!=ENOENT)
51         {
52                 int err=errno;
53                 cerr<<"Can't unlink "<<copy.dest<<": "<<strerror(err)<<'\n';
54                 done=error=true;
55                 return;
56         }
57
58         ifstream in(copy.src.str().c_str());
59         if(!in)
60         {
61                 cerr<<"Can't open "<<copy.src<<" for reading\n";
62                 done=error=true;
63                 return;
64         }
65
66         ofstream out(copy.dest.str().c_str());
67         if(!out)
68         {
69                 cerr<<"Can't open "<<copy.dest<<" for writing\n";
70                 done=error=true;
71                 return;
72         }
73
74         char buf[16384];
75         while(!in.eof())
76         {
77                 in.read(buf, sizeof(buf));
78                 out.write(buf, in.gcount());
79         }
80
81         struct stat st;
82         Path::stat(copy.src, st);
83         chmod(copy.dest.str().c_str(), st.st_mode&0777);
84
85         done=true;
86 }