file.cc 3.6 KB

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