]> git.saurik.com Git - apt.git/blob - methods/copy.cc
do not ignore differently versioned self-provides
[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 #include <apt-pkg/configuration.h>
20
21 #include <string>
22 #include <sys/stat.h>
23 #include <sys/time.h>
24
25 #include <apti18n.h>
26 /*}}}*/
27
28 class CopyMethod : public pkgAcqMethod
29 {
30 virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
31 void CalculateHashes(FetchItem const * const Itm, FetchResult &Res);
32
33 public:
34
35 CopyMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
36 };
37
38 void CopyMethod::CalculateHashes(FetchItem const * const Itm, FetchResult &Res)
39 {
40 Hashes Hash(Itm->ExpectedHashes);
41 FileFd Fd(Res.Filename, FileFd::ReadOnly, FileFd::Extension);
42 Hash.AddFD(Fd);
43 Res.TakeHashes(Hash);
44 }
45
46 // CopyMethod::Fetch - Fetch a file /*{{{*/
47 // ---------------------------------------------------------------------
48 /* */
49 bool CopyMethod::Fetch(FetchItem *Itm)
50 {
51 // this ensures that relative paths work in copy
52 std::string const File = Itm->Uri.substr(Itm->Uri.find(':')+1);
53
54 // Stat the file and send a start message
55 struct stat Buf;
56 if (stat(File.c_str(),&Buf) != 0)
57 return _error->Errno("stat",_("Failed to stat"));
58
59 // Forumulate a result and send a start message
60 FetchResult Res;
61 Res.Size = Buf.st_size;
62 Res.Filename = Itm->DestFile;
63 Res.LastModified = Buf.st_mtime;
64 Res.IMSHit = false;
65 URIStart(Res);
66
67 // just calc the hashes if the source and destination are identical
68 if (File == Itm->DestFile)
69 {
70 CalculateHashes(Itm, Res);
71 URIDone(Res);
72 return true;
73 }
74
75 // See if the file exists
76 FileFd From(File,FileFd::ReadOnly);
77 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
78 To.EraseOnFailure();
79 if (_error->PendingError() == true)
80 {
81 To.OpFail();
82 return false;
83 }
84
85 // Copy the file
86 if (CopyFile(From,To) == false)
87 {
88 To.OpFail();
89 return false;
90 }
91
92 From.Close();
93 To.Close();
94
95 // Transfer the modification times
96 struct timeval times[2];
97 times[0].tv_sec = Buf.st_atime;
98 times[1].tv_sec = Buf.st_mtime;
99 times[0].tv_usec = times[1].tv_usec = 0;
100 if (utimes(Res.Filename.c_str(), times) != 0)
101 return _error->Errno("utimes",_("Failed to set modification time"));
102
103 CalculateHashes(Itm, Res);
104
105 URIDone(Res);
106 return true;
107 }
108 /*}}}*/
109
110 int main()
111 {
112 setlocale(LC_ALL, "");
113
114 CopyMethod Mth;
115
116 return Mth.Run();
117 }