]> git.saurik.com Git - apt.git/blame - methods/copy.cc
Merge remote-tracking branch 'debian/debian/experimental' into feature/acq-trans
[apt.git] / methods / copy.cc
CommitLineData
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>
5f6c6c6e 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
28class CopyMethod : public pkgAcqMethod
29{
be4401bf 30 virtual bool Fetch(FetchItem *Itm);
5f6c6c6e 31 void CalculateHashes(FetchResult &Res);
93bf083d
AL
32
33 public:
34
ca7fd76c 35 CopyMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
93bf083d
AL
36};
37
5f6c6c6e
MV
38void 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 56bool CopyMethod::Fetch(FetchItem *Itm)
561ab0db 57{
9da539c5
MV
58 // this ensures that relative paths work in copy
59 std::string File = Itm->Uri.substr(Itm->Uri.find(':')+1);
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);
5f6c6c6e
MV
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 }
91cb4c6b 81
ca7fd76c
MV
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
93bf083d
AL
90 // See if the file exists
91 FileFd From(File,FileFd::ReadOnly);
22041bd2 92 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
93bf083d
AL
93 To.EraseOnFailure();
94 if (_error->PendingError() == true)
91cb4c6b
AL
95 {
96 To.OpFail();
93bf083d 97 return false;
91cb4c6b 98 }
561ab0db 99
93bf083d
AL
100 // Copy the file
101 if (CopyFile(From,To) == false)
91cb4c6b
AL
102 {
103 To.OpFail();
93bf083d 104 return false;
91cb4c6b 105 }
561ab0db 106
93bf083d 107 From.Close();
246bbb61 108 To.Close();
9ce3cfc9 109
93bf083d 110 // Transfer the modification times
246bbb61 111 struct timeval times[2];
9ce3cfc9
DK
112 times[0].tv_sec = Buf.st_atime;
113 times[1].tv_sec = Buf.st_mtime;
246bbb61
DK
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"));
9ce3cfc9 117
5f6c6c6e 118 CalculateHashes(Res);
ac3dee0e 119
93bf083d
AL
120 URIDone(Res);
121 return true;
122}
123 /*}}}*/
124
125int main()
126{
b25423f6
MZ
127 setlocale(LC_ALL, "");
128
93bf083d
AL
129 CopyMethod Mth;
130 return Mth.Run();
561ab0db 131}