]>
Commit | Line | Data |
---|---|---|
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); | |
31 | void CalculateHashes(FetchResult &Res); | |
32 | ||
33 | public: | |
34 | ||
35 | CopyMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {}; | |
36 | }; | |
37 | ||
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 | ||
53 | // CopyMethod::Fetch - Fetch a file /*{{{*/ | |
54 | // --------------------------------------------------------------------- | |
55 | /* */ | |
56 | bool CopyMethod::Fetch(FetchItem *Itm) | |
57 | { | |
58 | // this ensures that relative paths work in copy | |
59 | std::string File = Itm->Uri.substr(Itm->Uri.find(':')+1); | |
60 | ||
61 | // Stat the file and send a start message | |
62 | struct stat Buf; | |
63 | if (stat(File.c_str(),&Buf) != 0) | |
64 | return _error->Errno("stat",_("Failed to stat")); | |
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 | ||
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 | ||
82 | // See if the file exists | |
83 | FileFd From(File,FileFd::ReadOnly); | |
84 | FileFd To(Itm->DestFile,FileFd::WriteAtomic); | |
85 | To.EraseOnFailure(); | |
86 | if (_error->PendingError() == true) | |
87 | { | |
88 | To.OpFail(); | |
89 | return false; | |
90 | } | |
91 | ||
92 | // Copy the file | |
93 | if (CopyFile(From,To) == false) | |
94 | { | |
95 | To.OpFail(); | |
96 | return false; | |
97 | } | |
98 | ||
99 | From.Close(); | |
100 | To.Close(); | |
101 | ||
102 | // Transfer the modification times | |
103 | struct timeval times[2]; | |
104 | times[0].tv_sec = Buf.st_atime; | |
105 | times[1].tv_sec = Buf.st_mtime; | |
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")); | |
109 | ||
110 | CalculateHashes(Res); | |
111 | ||
112 | URIDone(Res); | |
113 | return true; | |
114 | } | |
115 | /*}}}*/ | |
116 | ||
117 | int main() | |
118 | { | |
119 | setlocale(LC_ALL, ""); | |
120 | ||
121 | CopyMethod Mth; | |
122 | ||
123 | Mth.DropPrivsOrDie(); | |
124 | return Mth.Run(); | |
125 | } |