]> git.tdb.fi Git - builder.git/blob - source/copy.cpp
Use mspio for all I/O operations
[builder.git] / source / copy.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <errno.h>
9 #include <msp/fs/dir.h>
10 #include <msp/fs/stat.h>
11 #include <msp/fs/utils.h>
12 #include <msp/io/file.h>
13 #include <msp/io/print.h>
14 #include "builder.h"
15 #include "copy.h"
16 #include "package.h"
17
18 using namespace std;
19 using namespace Msp;
20
21 Copy::Copy(Builder &b, const Package &pkg, const FS::Path &s, const FS::Path &d):
22         InternalAction(b),
23         src(s),
24         dest(d)
25 {
26         announce(pkg.get_name(), "COPY", dest[-1]);
27         if(builder.get_verbose()>=2)
28                 IO::print("%s -> %s\n", s, d);
29
30         if(!builder.get_dry_run())
31                 worker=new Worker(*this);
32 }
33
34
35 Copy::Worker::Worker(Copy &c):
36         copy(c)
37 {
38         launch();
39 }
40
41 void Copy::Worker::main()
42 {
43         FS::mkpath(FS::dirname(copy.dest), 0755);
44
45         try
46         {
47                 // Remove old file.  Not doing this would cause Bad Stuff when installing libraries.
48                 unlink(copy.dest);
49         }
50         catch(const SystemError &e)
51         {
52                 if(e.get_error_code()!=ENOENT)
53                 {
54                         IO::print(IO::cerr, "%s\n", e.what());
55                         done=error=true;
56                         return;
57                 }
58         }
59
60         try
61         {
62                 IO::File in(copy.src.str());
63                 IO::File out(copy.dest.str(), IO::M_WRITE);
64
65                 // Actual transfer loop
66                 char buf[16384];
67                 while(!in.eof())
68                 {
69                         unsigned len=in.read(buf, sizeof(buf));
70                         out.write(buf, len);
71                 }
72         }
73         catch(const Exception &e)
74         {
75                 IO::print(IO::cerr, "%s\n", e.what());
76                 done=error=true;
77                 return;
78         }
79
80         // Preserve file permissions
81         struct stat st=FS::stat(copy.src);
82         chmod(copy.dest.str().c_str(), st.st_mode&0777);
83
84         done=true;
85 }