]>
Commit | Line | Data |
---|---|---|
561ab0db AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
b3d44315 | 3 | // $Id: copy.cc,v 1.7.2.1 2004/01/16 18:58:50 mdz Exp $ |
561ab0db AL |
4 | /* ###################################################################### |
5 | ||
6 | Copy URI - This method takes a uri like a file: uri and copies it | |
92173b19 | 7 | to the destination file. |
561ab0db AL |
8 | |
9 | ##################################################################### */ | |
10 | /*}}}*/ | |
11 | // Include Files /*{{{*/ | |
ea542140 DK |
12 | #include <config.h> |
13 | ||
561ab0db | 14 | #include <apt-pkg/fileutl.h> |
472ff00e | 15 | #include <apt-pkg/strutl.h> |
93bf083d | 16 | #include <apt-pkg/acquire-method.h> |
561ab0db | 17 | #include <apt-pkg/error.h> |
95f45727 | 18 | #include <apt-pkg/hashes.h> |
ca7fd76c | 19 | #include <apt-pkg/configuration.h> |
561ab0db | 20 | |
453b82a3 | 21 | #include <string> |
561ab0db | 22 | #include <sys/stat.h> |
246bbb61 | 23 | #include <sys/time.h> |
453b82a3 | 24 | |
d77559ac | 25 | #include <apti18n.h> |
561ab0db AL |
26 | /*}}}*/ |
27 | ||
93bf083d AL |
28 | class CopyMethod : public pkgAcqMethod |
29 | { | |
be4401bf | 30 | virtual bool Fetch(FetchItem *Itm); |
ca7fd76c | 31 | void CalculateHashes(FetchResult &Res); |
93bf083d AL |
32 | |
33 | public: | |
34 | ||
ca7fd76c | 35 | CopyMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {}; |
93bf083d AL |
36 | }; |
37 | ||
ca7fd76c MV |
38 | void CopyMethod::CalculateHashes(FetchResult &Res) |
39 | { | |
40 | // For gzip indexes we need to look inside the gzip for the hash | |
41 | // We can not use the extension here as its not used in partial | |
42 | // on a IMS hit | |
43 | FileFd::OpenMode OpenMode = FileFd::ReadOnly; | |
44 | if (_config->FindB("Acquire::GzipIndexes", false) == true) | |
45 | OpenMode = FileFd::ReadOnlyGzip; | |
46 | ||
47 | Hashes Hash; | |
48 | FileFd Fd(Res.Filename, OpenMode); | |
49 | Hash.AddFD(Fd); | |
50 | Res.TakeHashes(Hash); | |
51 | } | |
52 | ||
93bf083d | 53 | // CopyMethod::Fetch - Fetch a file /*{{{*/ |
561ab0db AL |
54 | // --------------------------------------------------------------------- |
55 | /* */ | |
be4401bf | 56 | bool CopyMethod::Fetch(FetchItem *Itm) |
561ab0db | 57 | { |
be4401bf | 58 | URI Get = Itm->Uri; |
8f3ba4e8 | 59 | std::string File = Get.Path; |
93bf083d | 60 | |
91cb4c6b AL |
61 | // Stat the file and send a start message |
62 | struct stat Buf; | |
63 | if (stat(File.c_str(),&Buf) != 0) | |
dc738e7a | 64 | return _error->Errno("stat",_("Failed to stat")); |
91cb4c6b AL |
65 | |
66 | // Forumulate a result and send a start message | |
67 | FetchResult Res; | |
68 | Res.Size = Buf.st_size; | |
69 | Res.Filename = Itm->DestFile; | |
70 | Res.LastModified = Buf.st_mtime; | |
71 | Res.IMSHit = false; | |
72 | URIStart(Res); | |
73 | ||
ca7fd76c MV |
74 | // just calc the hashes if the source and destination are identical |
75 | if (File == Itm->DestFile) | |
76 | { | |
77 | CalculateHashes(Res); | |
78 | URIDone(Res); | |
79 | return true; | |
80 | } | |
81 | ||
93bf083d AL |
82 | // See if the file exists |
83 | FileFd From(File,FileFd::ReadOnly); | |
22041bd2 | 84 | FileFd To(Itm->DestFile,FileFd::WriteAtomic); |
93bf083d AL |
85 | To.EraseOnFailure(); |
86 | if (_error->PendingError() == true) | |
91cb4c6b AL |
87 | { |
88 | To.OpFail(); | |
93bf083d | 89 | return false; |
91cb4c6b | 90 | } |
561ab0db | 91 | |
93bf083d AL |
92 | // Copy the file |
93 | if (CopyFile(From,To) == false) | |
91cb4c6b AL |
94 | { |
95 | To.OpFail(); | |
93bf083d | 96 | return false; |
91cb4c6b | 97 | } |
561ab0db | 98 | |
93bf083d | 99 | From.Close(); |
246bbb61 | 100 | To.Close(); |
9ce3cfc9 | 101 | |
93bf083d | 102 | // Transfer the modification times |
246bbb61 | 103 | struct timeval times[2]; |
9ce3cfc9 DK |
104 | times[0].tv_sec = Buf.st_atime; |
105 | times[1].tv_sec = Buf.st_mtime; | |
246bbb61 DK |
106 | times[0].tv_usec = times[1].tv_usec = 0; |
107 | if (utimes(Res.Filename.c_str(), times) != 0) | |
108 | return _error->Errno("utimes",_("Failed to set modification time")); | |
9ce3cfc9 | 109 | |
ca7fd76c | 110 | CalculateHashes(Res); |
ac3dee0e | 111 | |
93bf083d AL |
112 | URIDone(Res); |
113 | return true; | |
114 | } | |
115 | /*}}}*/ | |
116 | ||
117 | int main() | |
118 | { | |
b25423f6 MZ |
119 | setlocale(LC_ALL, ""); |
120 | ||
93bf083d AL |
121 | CopyMethod Mth; |
122 | return Mth.Run(); | |
561ab0db | 123 | } |