]> git.saurik.com Git - apt.git/blob - methods/file.cc
* debian/rules:
[apt.git] / methods / file.cc
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
6 File URI method for APT
7
8 This simply checks that the file specified exists, if so the relevent
9 information is returned. If a .gz filename is specified then the file
10 name with .gz removed will also be checked and information about it
11 will be returned in Alt-*
12
13 ##################################################################### */
14 /*}}}*/
15 // Include Files /*{{{*/
16 #include <config.h>
17
18 #include <apt-pkg/acquire-method.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/hashes.h>
21 #include <apt-pkg/fileutl.h>
22 #include <apt-pkg/strutl.h>
23
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <apti18n.h>
27 /*}}}*/
28
29 class FileMethod : public pkgAcqMethod
30 {
31 virtual bool Fetch(FetchItem *Itm);
32
33 public:
34
35 FileMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly) {};
36 };
37
38 // FileMethod::Fetch - Fetch a file /*{{{*/
39 // ---------------------------------------------------------------------
40 /* */
41 bool FileMethod::Fetch(FetchItem *Itm)
42 {
43 URI Get = Itm->Uri;
44 std::string File = Get.Path;
45 FetchResult Res;
46 if (Get.Host.empty() == false)
47 return _error->Error(_("Invalid URI, local URIS must not start with //"));
48
49 // See if the file exists
50 struct stat Buf;
51 if (stat(File.c_str(),&Buf) == 0)
52 {
53 Res.Size = Buf.st_size;
54 Res.Filename = File;
55 Res.LastModified = Buf.st_mtime;
56 Res.IMSHit = false;
57 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
58 Res.IMSHit = true;
59 }
60
61 // See if we can compute a file without a .gz exentsion
62 std::string::size_type Pos = File.rfind(".gz");
63 if (Pos + 3 == File.length())
64 {
65 File = std::string(File,0,Pos);
66 if (stat(File.c_str(),&Buf) == 0)
67 {
68 FetchResult AltRes;
69 AltRes.Size = Buf.st_size;
70 AltRes.Filename = File;
71 AltRes.LastModified = Buf.st_mtime;
72 AltRes.IMSHit = false;
73 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
74 AltRes.IMSHit = true;
75
76 URIDone(Res,&AltRes);
77 return true;
78 }
79 }
80
81 if (Res.Filename.empty() == true)
82 return _error->Error(_("File not found"));
83
84 Hashes Hash;
85 FileFd Fd(Res.Filename, FileFd::ReadOnly);
86 Hash.AddFD(Fd);
87 Res.TakeHashes(Hash);
88 URIDone(Res);
89 return true;
90 }
91 /*}}}*/
92
93 int main()
94 {
95 setlocale(LC_ALL, "");
96
97 FileMethod Mth;
98 return Mth.Run();
99 }