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