]> git.saurik.com Git - apt.git/blame_incremental - methods/copy.cc
CMake: test/libapt: Use a prebuilt GTest library if available
[apt.git] / methods / copy.cc
... / ...
CommitLineData
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: copy.cc,v 1.7.2.1 2004/01/16 18:58:50 mdz Exp $
4/* ######################################################################
5
6 Copy URI - This method takes a uri like a file: uri and copies it
7 to the destination file.
8
9 ##################################################################### */
10 /*}}}*/
11// Include Files /*{{{*/
12#include <config.h>
13
14#include <apt-pkg/fileutl.h>
15#include <apt-pkg/strutl.h>
16#include <apt-pkg/error.h>
17#include <apt-pkg/hashes.h>
18#include <apt-pkg/configuration.h>
19#include "aptmethod.h"
20
21#include <string>
22#include <sys/stat.h>
23#include <sys/time.h>
24
25#include <apti18n.h>
26 /*}}}*/
27
28class CopyMethod : public aptMethod
29{
30 virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
31
32 public:
33
34 CopyMethod() : aptMethod("copy", "1.0",SingleInstance | SendConfig) {};
35};
36
37// CopyMethod::Fetch - Fetch a file /*{{{*/
38// ---------------------------------------------------------------------
39/* */
40bool CopyMethod::Fetch(FetchItem *Itm)
41{
42 // this ensures that relative paths work in copy
43 std::string const File = Itm->Uri.substr(Itm->Uri.find(':')+1);
44
45 // Stat the file and send a start message
46 struct stat Buf;
47 if (stat(File.c_str(),&Buf) != 0)
48 return _error->Errno("stat",_("Failed to stat"));
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;
55 Res.IMSHit = false;
56 URIStart(Res);
57
58 // just calc the hashes if the source and destination are identical
59 if (File == Itm->DestFile || Itm->DestFile == "/dev/null")
60 {
61 CalculateHashes(Itm, Res);
62 URIDone(Res);
63 return true;
64 }
65
66 // See if the file exists
67 FileFd From(File,FileFd::ReadOnly);
68 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
69 To.EraseOnFailure();
70
71 // Copy the file
72 if (CopyFile(From,To) == false)
73 {
74 To.OpFail();
75 return false;
76 }
77
78 From.Close();
79 To.Close();
80
81 if (TransferModificationTimes(File.c_str(), Res.Filename.c_str(), Res.LastModified) == false)
82 return false;
83
84 CalculateHashes(Itm, Res);
85 URIDone(Res);
86 return true;
87}
88 /*}}}*/
89
90int main()
91{
92 return CopyMethod().Run();
93}