]> git.tdb.fi Git - builder.git/blob - source/copy.cpp
cdbbd48f1eead0b7c399a4b7ec7ad534252be2c4
[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(*this)
16 {
17         announce(pkg.get_name(), "INST", dest[-1]);
18         if(builder.get_verbose()>=2)
19                 cout<<s<<" -> "<<d<<'\n';
20 }
21
22 int Copy::check()
23 {
24         if(worker.get_done())
25         {
26                 signal_done.emit();
27                 worker.join();
28                 return worker.get_error()?1:0;
29         }
30         return -1;
31 }
32
33 void Copy::Worker::main()
34 {
35         Path::mkpath(copy.dest.subpath(0, copy.dest.size()-1), 0755);
36         
37         if(unlink(copy.dest.str().c_str())<0 && errno!=ENOENT)
38         {
39                 int err=errno;
40                 cerr<<"Can't unlink "<<copy.dest<<": "<<strerror(err)<<'\n';
41                 done=error=true;
42                 return;
43         }
44
45         ifstream in(copy.src.str().c_str());
46         if(!in)
47         {
48                 cerr<<"Can't open "<<copy.src<<" for reading\n";
49                 done=error=true;
50                 return;
51         }
52
53         ofstream out(copy.dest.str().c_str());
54         if(!out)
55         {
56                 cerr<<"Can't open "<<copy.dest<<" for writing\n";
57                 done=error=true;
58                 return;
59         }
60
61         char buf[16384];
62         while(!in.eof())
63         {
64                 in.read(buf, sizeof(buf));
65                 out.write(buf, in.gcount());
66         }
67
68         struct stat st;
69         Path::stat(copy.src, st);
70         chmod(copy.dest.str().c_str(), st.st_mode&0777);
71
72         done=true;
73 }