file.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: file.cc,v 1.6 1998/11/14 01:39:49 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(FetchItem *Itm);
  21. public:
  22. FileMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly) {};
  23. };
  24. // FileMethod::Fetch - Fetch a file /*{{{*/
  25. // ---------------------------------------------------------------------
  26. /* */
  27. bool FileMethod::Fetch(FetchItem *Itm)
  28. {
  29. URI Get = Itm->Uri;
  30. string File = Get.Path;
  31. FetchResult Res;
  32. // See if the file exists
  33. struct stat Buf;
  34. if (stat(File.c_str(),&Buf) == 0)
  35. {
  36. Res.Size = Buf.st_size;
  37. Res.Filename = File;
  38. Res.LastModified = Buf.st_mtime;
  39. Res.IMSHit = false;
  40. if (Itm->LastModified == Buf.st_mtime)
  41. Res.IMSHit = true;
  42. }
  43. // See if we can compute a file without a .gz exentsion
  44. string::size_type Pos = File.rfind(".gz");
  45. if (Pos + 3 == File.length())
  46. {
  47. File = string(File,0,Pos);
  48. if (stat(File.c_str(),&Buf) == 0)
  49. {
  50. FetchResult AltRes;
  51. AltRes.Size = Buf.st_size;
  52. AltRes.Filename = File;
  53. AltRes.LastModified = Buf.st_mtime;
  54. AltRes.IMSHit = false;
  55. if (Itm->LastModified == Buf.st_mtime)
  56. AltRes.IMSHit = true;
  57. URIDone(Res,&AltRes);
  58. return true;
  59. }
  60. }
  61. if (Res.Filename.empty() == true)
  62. return _error->Error("File not found");
  63. URIDone(Res);
  64. return true;
  65. }
  66. /*}}}*/
  67. int main()
  68. {
  69. FileMethod Mth;
  70. return Mth.Run();
  71. }