]> git.saurik.com Git - apt.git/blob - methods/copy.cc
744cc2b51a024b6f421af852d5396d4fd2ad6e37
[apt.git] / methods / copy.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: copy.cc,v 1.7.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
5
6 Copy URI - This method takes a uri like a file: uri and copies it
7 to the destination file.
8
9 ##################################################################### */
10 /*}}}*/
11 // Include Files /*{{{*/
12 #include <config.h>
13
14 #include <apt-pkg/fileutl.h>
15 #include <apt-pkg/strutl.h>
16 #include <apt-pkg/acquire-method.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/hashes.h>
19
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <apti18n.h>
23 /*}}}*/
24
25 class CopyMethod : public pkgAcqMethod
26 {
27 virtual bool Fetch(FetchItem *Itm);
28
29 public:
30
31 CopyMethod() : pkgAcqMethod("1.0",SingleInstance) {};
32 };
33
34 // CopyMethod::Fetch - Fetch a file /*{{{*/
35 // ---------------------------------------------------------------------
36 /* */
37 bool CopyMethod::Fetch(FetchItem *Itm)
38 {
39 URI Get = Itm->Uri;
40 std::string File = Get.Path;
41
42 // Stat the file and send a start message
43 struct stat Buf;
44 if (stat(File.c_str(),&Buf) != 0)
45 return _error->Errno("stat",_("Failed to stat"));
46
47 // Forumulate a result and send a start message
48 FetchResult Res;
49 Res.Size = Buf.st_size;
50 Res.Filename = Itm->DestFile;
51 Res.LastModified = Buf.st_mtime;
52 Res.IMSHit = false;
53 URIStart(Res);
54
55 // See if the file exists
56 FileFd From(File,FileFd::ReadOnly);
57 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
58 To.EraseOnFailure();
59 if (_error->PendingError() == true)
60 {
61 To.OpFail();
62 return false;
63 }
64
65 // Copy the file
66 if (CopyFile(From,To) == false)
67 {
68 To.OpFail();
69 return false;
70 }
71
72 From.Close();
73
74 // Transfer the modification times
75 struct timespec times[2];
76 times[0].tv_sec = Buf.st_atime;
77 times[1].tv_sec = Buf.st_mtime;
78 times[0].tv_nsec = times[1].tv_nsec = 0;
79 if (futimens(To.Fd(), times) != 0)
80 {
81 To.OpFail();
82 return _error->Errno("futimens",_("Failed to set modification time"));
83 }
84 To.Close();
85
86 Hashes Hash;
87 FileFd Fd(Res.Filename, FileFd::ReadOnly);
88 Hash.AddFD(Fd);
89 Res.TakeHashes(Hash);
90
91 URIDone(Res);
92 return true;
93 }
94 /*}}}*/
95
96 int main()
97 {
98 setlocale(LC_ALL, "");
99
100 CopyMethod Mth;
101 return Mth.Run();
102 }