apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

copy.cc
Go to the documentation of this file.
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4 
5  Copy URI - This method takes a uri like a file: uri and copies it
6  to the destination file.
7 
8  ##################################################################### */
9  /*}}}*/
10 // Include Files /*{{{*/
11 #include <config.h>
12 
13 #include "aptmethod.h"
14 #include <apt-pkg/configuration.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/fileutl.h>
17 #include <apt-pkg/hashes.h>
18 #include <apt-pkg/strutl.h>
19 
20 #include <string>
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 
24 #include <apti18n.h>
25  /*}}}*/
26 
27 class CopyMethod : public aptMethod
28 {
29  virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
30 
31  public:
33  {
35  }
36 };
37 
38 // CopyMethod::Fetch - Fetch a file /*{{{*/
39 // ---------------------------------------------------------------------
40 /* */
42 {
43  // this ensures that relative paths work in copy
44  std::string const File = DecodeSendURI(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);
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  if (TransferModificationTimes(File.c_str(), Res.Filename.c_str(), Res.LastModified) == false)
83  return false;
84 
85  CalculateHashes(Itm, Res);
86  URIDone(Res);
87  return true;
88 }
89  /*}}}*/
90 
91 int main()
92 {
93  return CopyMethod().Run();
94 }
virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE
Definition: copy.cc:41
CopyMethod()
Definition: copy.cc:32
Definition: fileutl.h:39
@ WriteAtomic
Definition: fileutl.h:74
@ ReadOnly
Definition: fileutl.h:59
void OpFail()
Definition: fileutl.h:153
void EraseOnFailure()
Definition: fileutl.h:152
bool Close()
Definition: fileutl.cc:2977
unsigned long SeccompFlags
Definition: aptmethod.h:50
bool CalculateHashes(FetchItem const *const Itm, FetchResult &Res) const APT_NONNULL(2)
Definition: aptmethod.h:377
bool TransferModificationTimes(char const *const From, char const *const To, time_t &LastModified) APT_NONNULL(2
static std::string DecodeSendURI(std::string const &part)
Definition: aptmethod.h:493
struct stat Buf
Definition: aptmethod.h:464
virtual void URIStart(FetchResult &Res)
int Run(bool Single=false)
virtual void URIDone(FetchResult &Res, FetchResult *Alt=0)
int main()
Definition: copy.cc:91
bool CopyFile(FileFd &From, FileFd &To)
Definition: fileutl.cc:164
#define APT_OVERRIDE
Definition: macros.h:111
unsigned long long Size