]> git.saurik.com Git - apt.git/blob - methods/copy.cc
cleanup
[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);
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 // when the files are identical, just compute the hashes
75 if(File == Itm->DestFile)
76 {
77 CalculateHashes(Res);
78 URIDone(Res);
79 return true;
80 }
81
82 // just calc the hashes if the source and destination are identical
83 if (File == Itm->DestFile)
84 {
85 CalculateHashes(Res);
86 URIDone(Res);
87 return true;
88 }
89
90 // See if the file exists
91 FileFd From(File,FileFd::ReadOnly);
92 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
93 To.EraseOnFailure();
94 if (_error->PendingError() == true)
95 {
96 To.OpFail();
97 return false;
98 }
99
100 // Copy the file
101 if (CopyFile(From,To) == false)
102 {
103 To.OpFail();
104 return false;
105 }
106
107 From.Close();
108 To.Close();
109
110 // Transfer the modification times
111 struct timeval times[2];
112 times[0].tv_sec = Buf.st_atime;
113 times[1].tv_sec = Buf.st_mtime;
114 times[0].tv_usec = times[1].tv_usec = 0;
115 if (utimes(Res.Filename.c_str(), times) != 0)
116 return _error->Errno("utimes",_("Failed to set modification time"));
117
118 CalculateHashes(Res);
119
120 URIDone(Res);
121 return true;
122 }
123 /*}}}*/
124
125 int main()
126 {
127 setlocale(LC_ALL, "");
128
129 CopyMethod Mth;
130 return Mth.Run();
131 }