]> git.saurik.com Git - apt.git/blame - methods/copy.cc
eipp: add Allow-Temporary-Remove-of-Essentials
[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>
561ab0db 16#include <apt-pkg/error.h>
95f45727 17#include <apt-pkg/hashes.h>
5f6c6c6e 18#include <apt-pkg/configuration.h>
30c8107e 19#include "aptmethod.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
30c8107e 28class CopyMethod : public aptMethod
93bf083d 29{
3b302846 30 virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
30c8107e 31
93bf083d 32 public:
93bf083d 33
30c8107e
DK
34 CopyMethod() : aptMethod("copy", "1.0",SingleInstance | SendConfig) {};
35};
5f6c6c6e 36
93bf083d 37// CopyMethod::Fetch - Fetch a file /*{{{*/
561ab0db
AL
38// ---------------------------------------------------------------------
39/* */
be4401bf 40bool CopyMethod::Fetch(FetchItem *Itm)
561ab0db 41{
9da539c5 42 // this ensures that relative paths work in copy
653ef26c 43 std::string const File = Itm->Uri.substr(Itm->Uri.find(':')+1);
93bf083d 44
91cb4c6b
AL
45 // Stat the file and send a start message
46 struct stat Buf;
47 if (stat(File.c_str(),&Buf) != 0)
dc738e7a 48 return _error->Errno("stat",_("Failed to stat"));
91cb4c6b
AL
49
50 // Forumulate a result and send a start message
51 FetchResult Res;
52 Res.Size = Buf.st_size;
53 Res.Filename = Itm->DestFile;
54 Res.LastModified = Buf.st_mtime;
936d5613 55 Res.IMSHit = false;
91cb4c6b 56 URIStart(Res);
5f6c6c6e 57
ca7fd76c 58 // just calc the hashes if the source and destination are identical
af9e40c9 59 if (File == Itm->DestFile || Itm->DestFile == "/dev/null")
ca7fd76c 60 {
9224ce3d 61 CalculateHashes(Itm, Res);
ca7fd76c
MV
62 URIDone(Res);
63 return true;
64 }
65
93bf083d
AL
66 // See if the file exists
67 FileFd From(File,FileFd::ReadOnly);
22041bd2 68 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
93bf083d 69 To.EraseOnFailure();
30c8107e 70
93bf083d
AL
71 // Copy the file
72 if (CopyFile(From,To) == false)
91cb4c6b
AL
73 {
74 To.OpFail();
93bf083d 75 return false;
91cb4c6b 76 }
561ab0db 77
93bf083d 78 From.Close();
246bbb61 79 To.Close();
9ce3cfc9 80
93bf083d 81 // Transfer the modification times
246bbb61 82 struct timeval times[2];
9ce3cfc9
DK
83 times[0].tv_sec = Buf.st_atime;
84 times[1].tv_sec = Buf.st_mtime;
246bbb61
DK
85 times[0].tv_usec = times[1].tv_usec = 0;
86 if (utimes(Res.Filename.c_str(), times) != 0)
87 return _error->Errno("utimes",_("Failed to set modification time"));
9ce3cfc9 88
9224ce3d 89 CalculateHashes(Itm, Res);
93bf083d
AL
90 URIDone(Res);
91 return true;
92}
93 /*}}}*/
94
95int main()
96{
8b79c94a 97 return CopyMethod().Run();
561ab0db 98}