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)  

file.cc
Go to the documentation of this file.
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4 
5  File URI method for APT
6 
7  This simply checks that the file specified exists, if so the relevant
8  information is returned. If a .gz filename is specified then the file
9  name with .gz removed will also be checked and information about it
10  will be returned in Alt-*
11 
12  ##################################################################### */
13  /*}}}*/
14 // Include Files /*{{{*/
15 #include <config.h>
16 
17 #include "aptmethod.h"
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/hashes.h>
22 #include <apt-pkg/strutl.h>
23 
24 #include <string>
25 #include <sys/stat.h>
26 
27 #include <apti18n.h>
28  /*}}}*/
29 
30 class FileMethod : public aptMethod
31 {
32  virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
33 
34  public:
36  {
38  }
39 };
40 
41 // FileMethod::Fetch - Fetch a file /*{{{*/
42 // ---------------------------------------------------------------------
43 /* */
45 {
46  URI Get(Itm->Uri);
47  std::string const File = DecodeSendURI(Get.Path);
48  FetchResult Res;
49  if (Get.Host.empty() == false)
50  return _error->Error(_("Invalid URI, local URIS must not start with //"));
51 
52  struct stat Buf;
53  // deal with destination files which might linger around
54  if (lstat(Itm->DestFile.c_str(), &Buf) == 0)
55  {
56  if ((Buf.st_mode & S_IFREG) != 0)
57  {
58  if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
59  {
60  if (Itm->ExpectedHashes.VerifyFile(File))
61  {
62  Res.Filename = Itm->DestFile;
63  Res.IMSHit = true;
64  }
65  }
66  }
67  }
68  if (Res.IMSHit != true)
69  RemoveFile("file", Itm->DestFile);
70 
71  int olderrno = 0;
72  // See if the file exists
73  if (stat(File.c_str(),&Buf) == 0)
74  {
75  Res.Size = Buf.st_size;
76  Res.Filename = File;
77  Res.LastModified = Buf.st_mtime;
78  Res.IMSHit = false;
79  if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
80  {
81  unsigned long long const filesize = Itm->ExpectedHashes.FileSize();
82  if (filesize != 0 && filesize == Res.Size)
83  Res.IMSHit = true;
84  }
85 
86  CalculateHashes(Itm, Res);
87  }
88  else
89  olderrno = errno;
90  if (Res.IMSHit == false)
91  URIStart(Res);
92 
93  // See if the uncompressed file exists and reuse it
94  FetchResult AltRes;
95  AltRes.Filename.clear();
96  std::vector<std::string> extensions = APT::Configuration::getCompressorExtensions();
97  for (std::vector<std::string>::const_iterator ext = extensions.begin(); ext != extensions.end(); ++ext)
98  {
99  if (APT::String::Endswith(File, *ext) == true)
100  {
101  std::string const unfile = File.substr(0, File.length() - ext->length());
102  if (stat(unfile.c_str(),&Buf) == 0)
103  {
104  AltRes.Size = Buf.st_size;
105  AltRes.Filename = unfile;
106  AltRes.LastModified = Buf.st_mtime;
107  AltRes.IMSHit = false;
108  if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
109  AltRes.IMSHit = true;
110  break;
111  }
112  // no break here as we could have situations similar to '.gz' vs '.tar.gz' here
113  }
114  }
115 
116  if (AltRes.Filename.empty() == false)
117  URIDone(Res,&AltRes);
118  else if (Res.Filename.empty() == false)
119  URIDone(Res);
120  else
121  {
122  errno = olderrno;
123  return _error->Errno(File.c_str(), _("File not found"));
124  }
125 
126  return true;
127 }
128  /*}}}*/
129 
130 int main()
131 {
132  return FileMethod().Run();
133 }
FileMethod()
Definition: file.cc:35
virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE
Definition: file.cc:44
bool VerifyFile(std::string filename) const
Definition: hashes.cc:248
unsigned long long FileSize() const
Definition: hashes.cc:210
Definition: strutl.h:193
std::string Path
Definition: strutl.h:202
std::string Host
Definition: strutl.h:201
unsigned long SeccompFlags
Definition: aptmethod.h:50
bool CalculateHashes(FetchItem const *const Itm, FetchResult &Res) const APT_NONNULL(2)
Definition: aptmethod.h:377
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: file.cc:130
bool RemoveFile(char const *const Function, std::string const &FileName)
Definition: fileutl.cc:198
#define APT_OVERRIDE
Definition: macros.h:111
APT_PUBLIC std::vector< std::string > const getCompressorExtensions()
Return a vector of extensions supported for data.tar's.
bool Endswith(const std::string &s, const std::string &end)
Definition: strutl.cc:77
HashStringList ExpectedHashes
unsigned long long Size