file.cc 3.6 KB

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