]> git.saurik.com Git - apt.git/blame_incremental - methods/copy.cc
test framework: Unset http proxy variables in setupenvironment
[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/acquire-method.h>
17#include <apt-pkg/error.h>
18#include <apt-pkg/hashes.h>
19#include <apt-pkg/configuration.h>
20#include "aptmethod.h"
21
22#include <string>
23#include <sys/stat.h>
24#include <sys/time.h>
25
26#include <apti18n.h>
27 /*}}}*/
28
29class CopyMethod : public aptMethod
30{
31 virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
32
33 public:
34
35 CopyMethod() : aptMethod("copy", "1.0",SingleInstance | SendConfig) {};
36};
37
38// CopyMethod::Fetch - Fetch a file /*{{{*/
39// ---------------------------------------------------------------------
40/* */
41bool CopyMethod::Fetch(FetchItem *Itm)
42{
43 // this ensures that relative paths work in copy
44 std::string const File = Itm->Uri.substr(Itm->Uri.find(':')+1);
45
46 // Stat the file and send a start message
47 struct stat Buf;
48 if (stat(File.c_str(),&Buf) != 0)
49 return _error->Errno("stat",_("Failed to stat"));
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;
56 Res.IMSHit = false;
57 URIStart(Res);
58
59 // just calc the hashes if the source and destination are identical
60 if (File == Itm->DestFile || Itm->DestFile == "/dev/null")
61 {
62 CalculateHashes(Itm, Res);
63 URIDone(Res);
64 return true;
65 }
66
67 // See if the file exists
68 FileFd From(File,FileFd::ReadOnly);
69 FileFd To(Itm->DestFile,FileFd::WriteAtomic);
70 To.EraseOnFailure();
71
72 // Copy the file
73 if (CopyFile(From,To) == false)
74 {
75 To.OpFail();
76 return false;
77 }
78
79 From.Close();
80 To.Close();
81
82 // Transfer the modification times
83 struct timeval times[2];
84 times[0].tv_sec = Buf.st_atime;
85 times[1].tv_sec = Buf.st_mtime;
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"));
89
90 CalculateHashes(Itm, Res);
91 URIDone(Res);
92 return true;
93}
94 /*}}}*/
95
96int main()
97{
98 setlocale(LC_ALL, "");
99
100 CopyMethod Mth;
101
102 return Mth.Run();
103}