file.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: file.cc,v 1.4 1998/10/30 07:53:52 jgg Exp $
  4. /* ######################################################################
  5. File URI method for APT
  6. This simply checks that the file specified exists, if so the relevent
  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 <apt-pkg/acquire-method.h>
  14. #include <apt-pkg/error.h>
  15. #include <sys/stat.h>
  16. #include <unistd.h>
  17. /*}}}*/
  18. class FileMethod : public pkgAcqMethod
  19. {
  20. virtual bool Fetch(string Message,URI Get);
  21. public:
  22. FileMethod() : pkgAcqMethod("1.0",SingleInstance) {};
  23. };
  24. // FileMethod::Fetch - Fetch a file /*{{{*/
  25. // ---------------------------------------------------------------------
  26. /* */
  27. bool FileMethod::Fetch(string Message,URI Get)
  28. {
  29. string File = Get.Path;
  30. FetchResult Res;
  31. // See if the file exists
  32. struct stat Buf;
  33. if (stat(File.c_str(),&Buf) == 0)
  34. {
  35. Res.Size = Buf.st_size;
  36. Res.Filename = File;
  37. Res.LastModified = Buf.st_mtime;
  38. Res.IMSHit = false;
  39. if (LastModified == Buf.st_mtime)
  40. Res.IMSHit = true;
  41. }
  42. // See if we can compute a file without a .gz exentsion
  43. string::size_type Pos = File.rfind(".gz");
  44. if (Pos + 3 == File.length())
  45. {
  46. File = string(File,0,Pos);
  47. if (stat(File.c_str(),&Buf) == 0)
  48. {
  49. FetchResult AltRes;
  50. AltRes.Size = Buf.st_size;
  51. AltRes.Filename = File;
  52. AltRes.LastModified = Buf.st_mtime;
  53. AltRes.IMSHit = false;
  54. if (LastModified == Buf.st_mtime)
  55. AltRes.IMSHit = true;
  56. URIDone(Res,&AltRes);
  57. return true;
  58. }
  59. }
  60. if (Res.Filename.empty() == true)
  61. return _error->Error("File not found");
  62. URIDone(Res);
  63. return true;
  64. }
  65. /*}}}*/
  66. int main()
  67. {
  68. FileMethod Mth;
  69. return Mth.Run();
  70. }