]> git.saurik.com Git - apt-legacy.git/blob - methods/file.cc
Added iPhone headers to APT and fixed 301 results.
[apt-legacy.git] / methods / file.cc
1 extern "C" {
2 #include <mach-o/nlist.h>
3 }
4
5 // -*- mode: cpp; mode: fold -*-
6 // Description /*{{{*/
7 // $Id: file.cc,v 1.9.2.1 2004/01/16 18:58:50 mdz Exp $
8 /* ######################################################################
9
10 File URI method for APT
11
12 This simply checks that the file specified exists, if so the relevent
13 information is returned. If a .gz filename is specified then the file
14 name with .gz removed will also be checked and information about it
15 will be returned in Alt-*
16
17 ##################################################################### */
18 /*}}}*/
19 // Include Files /*{{{*/
20 #include <apt-pkg/acquire-method.h>
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/hashes.h>
23 #include <apt-pkg/fileutl.h>
24
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <apti18n.h>
28 /*}}}*/
29
30 class FileMethod : public pkgAcqMethod
31 {
32 virtual bool Fetch(FetchItem *Itm);
33
34 public:
35
36 FileMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly) {};
37 };
38
39 // FileMethod::Fetch - Fetch a file /*{{{*/
40 // ---------------------------------------------------------------------
41 /* */
42 bool FileMethod::Fetch(FetchItem *Itm)
43 {
44 URI Get = Itm->Uri;
45 string File = Get.Path;
46 FetchResult Res;
47 if (Get.Host.empty() == false)
48 return _error->Error(_("Invalid URI, local URIS must not start with //"));
49
50 // See if the file exists
51 struct stat Buf;
52 if (stat(File.c_str(),&Buf) == 0)
53 {
54 Res.Size = Buf.st_size;
55 Res.Filename = File;
56 Res.LastModified = Buf.st_mtime;
57 Res.IMSHit = false;
58 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
59 Res.IMSHit = true;
60 }
61
62 // See if we can compute a file without a .gz exentsion
63 string::size_type Pos = File.rfind(".gz");
64 if (Pos + 3 == File.length())
65 {
66 File = string(File,0,Pos);
67 if (stat(File.c_str(),&Buf) == 0)
68 {
69 FetchResult AltRes;
70 AltRes.Size = Buf.st_size;
71 AltRes.Filename = File;
72 AltRes.LastModified = Buf.st_mtime;
73 AltRes.IMSHit = false;
74 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
75 AltRes.IMSHit = true;
76
77 URIDone(Res,&AltRes);
78 return true;
79 }
80 }
81
82 if (Res.Filename.empty() == true)
83 return _error->Error(_("File not found"));
84
85 Hashes Hash;
86 FileFd Fd(Res.Filename, FileFd::ReadOnly);
87 Hash.AddFD(Fd.Fd(), Fd.Size());
88 Res.TakeHashes(Hash);
89 URIDone(Res);
90 return true;
91 }
92 /*}}}*/
93
94 int main()
95 {
96 #if !defined(__ENVIRONMENT_ASPEN_VERSION_MIN_REQUIRED__) || __ENVIRONMENT_ASPEN_VERSION_MIN_REQUIRED__ < 10200
97 struct nlist nl[2];
98 memset(nl, 0, sizeof(nl));
99 nl[0].n_un.n_name = (char *) "_useMDNSResponder";
100 nlist("/usr/lib/libc.dylib", nl);
101 if (nl[0].n_type != N_UNDF)
102 *(int *) nl[0].n_value = 0;
103 #endif
104
105 setlocale(LC_ALL, "");
106
107 FileMethod Mth;
108 return Mth.Run();
109 }