]> git.saurik.com Git - apt.git/blame - methods/copy.cc
Merge branch 'debian/sid' into debian/experimental
[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{
b0f4b486
MV
40 Hashes Hash;
41 FileFd::CompressMode CompressMode = FileFd::None;
5f6c6c6e 42 if (_config->FindB("Acquire::GzipIndexes", false) == true)
b0f4b486 43 CompressMode = FileFd::Extension;
5f6c6c6e 44
b0f4b486 45 FileFd Fd(Res.Filename, FileFd::ReadOnly, CompressMode);
5f6c6c6e
MV
46 Hash.AddFD(Fd);
47 Res.TakeHashes(Hash);
48}
49
93bf083d 50// CopyMethod::Fetch - Fetch a file /*{{{*/
561ab0db
AL
51// ---------------------------------------------------------------------
52/* */
be4401bf 53bool CopyMethod::Fetch(FetchItem *Itm)
561ab0db 54{
9da539c5
MV
55 // this ensures that relative paths work in copy
56 std::string File = Itm->Uri.substr(Itm->Uri.find(':')+1);
93bf083d 57
91cb4c6b
AL
58 // Stat the file and send a start message
59 struct stat Buf;
60 if (stat(File.c_str(),&Buf) != 0)
dc738e7a 61 return _error->Errno("stat",_("Failed to stat"));
91cb4c6b
AL
62
63 // Forumulate a result and send a start message
64 FetchResult Res;
65 Res.Size = Buf.st_size;
66 Res.Filename = Itm->DestFile;
67 Res.LastModified = Buf.st_mtime;
68 Res.IMSHit = false;
69 URIStart(Res);
5f6c6c6e
MV
70
71 // when the files are identical, just compute the hashes
72 if(File == Itm->DestFile)
73 {
74 CalculateHashes(Res);
75 URIDone(Res);
76 return true;
77 }
91cb4c6b 78
ca7fd76c
MV
79 // just calc the hashes if the source and destination are identical
80 if (File == Itm->DestFile)
81 {
82 CalculateHashes(Res);
83 URIDone(Res);
84 return true;
85 }
86
93bf083d
AL
87 // See if the file exists
88 FileFd From(File,FileFd::ReadOnly);
22041bd2 89 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
93bf083d
AL
90 To.EraseOnFailure();
91 if (_error->PendingError() == true)
91cb4c6b
AL
92 {
93 To.OpFail();
93bf083d 94 return false;
91cb4c6b 95 }
561ab0db 96
93bf083d
AL
97 // Copy the file
98 if (CopyFile(From,To) == false)
91cb4c6b
AL
99 {
100 To.OpFail();
93bf083d 101 return false;
91cb4c6b 102 }
561ab0db 103
93bf083d 104 From.Close();
246bbb61 105 To.Close();
9ce3cfc9 106
93bf083d 107 // Transfer the modification times
246bbb61 108 struct timeval times[2];
9ce3cfc9
DK
109 times[0].tv_sec = Buf.st_atime;
110 times[1].tv_sec = Buf.st_mtime;
246bbb61
DK
111 times[0].tv_usec = times[1].tv_usec = 0;
112 if (utimes(Res.Filename.c_str(), times) != 0)
113 return _error->Errno("utimes",_("Failed to set modification time"));
9ce3cfc9 114
5f6c6c6e 115 CalculateHashes(Res);
ac3dee0e 116
93bf083d
AL
117 URIDone(Res);
118 return true;
119}
120 /*}}}*/
121
122int main()
123{
b25423f6
MZ
124 setlocale(LC_ALL, "");
125
93bf083d 126 CopyMethod Mth;
7b18d559 127
93bf083d 128 return Mth.Run();
561ab0db 129}