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