]> git.saurik.com Git - apt.git/blame - methods/copy.cc
Deprecate SPtrArray<T> and convert everyone to unique_ptr<T[]>
[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{
3b302846 30 virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
9224ce3d 31 void CalculateHashes(FetchItem const * const Itm, FetchResult &Res);
93bf083d
AL
32
33 public:
34
ca7fd76c 35 CopyMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
93bf083d
AL
36};
37
9224ce3d 38void CopyMethod::CalculateHashes(FetchItem const * const Itm, FetchResult &Res)
5f6c6c6e 39{
9224ce3d 40 Hashes Hash(Itm->ExpectedHashes);
653ef26c 41 FileFd Fd(Res.Filename, FileFd::ReadOnly, FileFd::Extension);
5f6c6c6e
MV
42 Hash.AddFD(Fd);
43 Res.TakeHashes(Hash);
44}
45
93bf083d 46// CopyMethod::Fetch - Fetch a file /*{{{*/
561ab0db
AL
47// ---------------------------------------------------------------------
48/* */
be4401bf 49bool CopyMethod::Fetch(FetchItem *Itm)
561ab0db 50{
9da539c5 51 // this ensures that relative paths work in copy
653ef26c 52 std::string const File = Itm->Uri.substr(Itm->Uri.find(':')+1);
93bf083d 53
91cb4c6b
AL
54 // Stat the file and send a start message
55 struct stat Buf;
56 if (stat(File.c_str(),&Buf) != 0)
dc738e7a 57 return _error->Errno("stat",_("Failed to stat"));
91cb4c6b
AL
58
59 // Forumulate a result and send a start message
60 FetchResult Res;
61 Res.Size = Buf.st_size;
62 Res.Filename = Itm->DestFile;
63 Res.LastModified = Buf.st_mtime;
936d5613 64 Res.IMSHit = false;
91cb4c6b 65 URIStart(Res);
5f6c6c6e 66
ca7fd76c
MV
67 // just calc the hashes if the source and destination are identical
68 if (File == Itm->DestFile)
69 {
9224ce3d 70 CalculateHashes(Itm, Res);
ca7fd76c
MV
71 URIDone(Res);
72 return true;
73 }
74
93bf083d
AL
75 // See if the file exists
76 FileFd From(File,FileFd::ReadOnly);
22041bd2 77 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
93bf083d
AL
78 To.EraseOnFailure();
79 if (_error->PendingError() == true)
91cb4c6b
AL
80 {
81 To.OpFail();
93bf083d 82 return false;
91cb4c6b 83 }
561ab0db 84
93bf083d
AL
85 // Copy the file
86 if (CopyFile(From,To) == false)
91cb4c6b
AL
87 {
88 To.OpFail();
93bf083d 89 return false;
91cb4c6b 90 }
561ab0db 91
93bf083d 92 From.Close();
246bbb61 93 To.Close();
9ce3cfc9 94
93bf083d 95 // Transfer the modification times
246bbb61 96 struct timeval times[2];
9ce3cfc9
DK
97 times[0].tv_sec = Buf.st_atime;
98 times[1].tv_sec = Buf.st_mtime;
246bbb61
DK
99 times[0].tv_usec = times[1].tv_usec = 0;
100 if (utimes(Res.Filename.c_str(), times) != 0)
101 return _error->Errno("utimes",_("Failed to set modification time"));
9ce3cfc9 102
9224ce3d 103 CalculateHashes(Itm, Res);
ac3dee0e 104
93bf083d
AL
105 URIDone(Res);
106 return true;
107}
108 /*}}}*/
109
110int main()
111{
b25423f6
MZ
112 setlocale(LC_ALL, "");
113
93bf083d 114 CopyMethod Mth;
7b18d559 115
93bf083d 116 return Mth.Run();
561ab0db 117}